/bp-core/classes/class-bp-core-bp-nav-backcompat.php

  1. <?php 
  2. /** 
  3. * Backward compatibility for the $bp->bp_nav global. 
  4. * 
  5. * @since 2.6.0 
  6. */ 
  7.  
  8. // Exit if accessed directly. 
  9. defined( 'ABSPATH' ) || exit; 
  10.  
  11. /** 
  12. * bp_nav backward compatibility class. 
  13. * 
  14. * This class is used to provide backward compatibility for extensions that access and modify 
  15. * the $bp->bp_nav global. 
  16. * 
  17. * @since 2.6.0 
  18. */ 
  19. class BP_Core_BP_Nav_BackCompat implements ArrayAccess { 
  20. /** 
  21. * Nav items. 
  22. * 
  23. * @since 2.6.0 
  24. * @access public 
  25. * @var array 
  26. */ 
  27. public $backcompat_nav = array(); 
  28.  
  29. /** 
  30. * Component to which nav items belong. 
  31. * 
  32. * @since 2.6.0 
  33. * @access public 
  34. * @var array 
  35. */ 
  36. public $component; 
  37.  
  38. /** 
  39. * Constructor. 
  40. * 
  41. * @since 2.6.0 
  42. * 
  43. * @param array $backcompat_nav Optional. Array of nav items. 
  44. */ 
  45. public function __construct( $backcompat_nav = array() ) { 
  46. foreach ( $backcompat_nav as $key => $value ) { 
  47. if ( is_array( $value ) ) { 
  48. $this->backcompat_nav[ $key ] = new self( $value ); 
  49. } else { 
  50. $this->backcompat_nav[ $key ] = $value; 
  51.  
  52. /** 
  53. * Assign a value to the nav array at the specified offset. 
  54. * 
  55. * @since 2.6.0 
  56. * 
  57. * @param mixed $offset Array offset. 
  58. * @param array $value Nav item. 
  59. */ 
  60. public function offsetSet( $offset, $value ) { 
  61. _doing_it_wrong( 
  62. 'bp_nav',  
  63. __( 'The bp_nav and bp_options_nav globals should not be used directly and are deprecated. Please use the BuddyPress nav functions instead.', 'buddypress' ),  
  64. '2.6.0' 
  65. ); 
  66.  
  67. $bp = buddypress(); 
  68.  
  69. if ( is_array( $value ) ) { 
  70. $value = new self( $value ); 
  71.  
  72. if ( $offset !== null ) { 
  73. // Temporarily set the backcompat_nav. 
  74. $this->backcompat_nav[ $offset ] = $value; 
  75.  
  76. $args = $this->to_array(); 
  77. if ( isset( $args['parent_slug'] ) ) { 
  78. $this->get_component_nav( $args['parent_slug'] )->edit_nav( $args, $args['slug'], $args['parent_slug'] ); 
  79. } elseif ( isset( $args['slug'] ) ) { 
  80. $bp->members->nav->edit_nav( $args, $args['slug'] ); 
  81.  
  82. /** 
  83. * Get a value of the nav array at the specified offset. 
  84. * 
  85. * @since 2.6.0 
  86. * 
  87. * @param mixed $offset Array offset. 
  88. * @return BP_Core_BP_Nav_BackCompat 
  89. */ 
  90. public function offsetGet( $offset ) { 
  91. _doing_it_wrong( 
  92. 'bp_nav',  
  93. __( 'The bp_nav and bp_options_nav globals should not be used directly and are deprecated. Please use the BuddyPress nav functions instead.', 'buddypress' ),  
  94. '2.6.0' 
  95. ); 
  96.  
  97. // if ( ! isset( $this->backcompat_nav[ $offset ] ) ) { 
  98. $nav = $this->get_nav( $offset ); 
  99. if ( $nav && isset( $nav[ $offset ] ) ) { 
  100. $this->backcompat_nav[ $offset ] = new self( $nav[ $offset ] ); 
  101. // } 
  102.  
  103. return $this->backcompat_nav[ $offset ]; 
  104.  
  105. /** 
  106. * Check whether nav array has a value at the specified offset. 
  107. * 
  108. * @since 2.6.0 
  109. * 
  110. * @param mixed $offset Array offset. 
  111. * @return bool 
  112. */ 
  113. public function offsetExists( $offset ) { 
  114. _doing_it_wrong( 
  115. 'bp_nav',  
  116. __( 'The bp_nav and bp_options_nav globals should not be used directly and are deprecated. Please use the BuddyPress nav functions instead.', 'buddypress' ),  
  117. '2.6.0' 
  118. ); 
  119.  
  120. if ( isset( $this->backcompat_nav[ $offset ] ) ) { 
  121. return true; 
  122.  
  123. $nav = $this->get_nav( $offset ); 
  124. if ( $nav && isset( $nav[ $offset ] ) ) { 
  125. return true; 
  126.  
  127. return false; 
  128.  
  129. /** 
  130. * Unset a nav array value at the specified offset. 
  131. * 
  132. * @since 2.6.0 
  133. * 
  134. * @param mixed $offset Array offset. 
  135. */ 
  136. public function offsetUnset( $offset ) { 
  137. _doing_it_wrong( 
  138. 'bp_nav',  
  139. __( 'The bp_nav and bp_options_nav globals should not be used directly and are deprecated. Please use the BuddyPress nav functions instead.', 'buddypress' ),  
  140. '2.6.0' 
  141. ); 
  142.  
  143. // For top-level nav items, the backcompat nav hasn't yet been initialized. 
  144. if ( ! isset( $this->backcompat_nav[ $offset ] ) ) { 
  145. buddypress()->members->nav->delete_nav( $offset ); 
  146. unset( $this->backcompat_nav[ $offset ] ); 
  147.  
  148. /** 
  149. * Set the component to which the nav belongs. 
  150. * 
  151. * @since 2.6.0 
  152. * 
  153. * @param string $component 
  154. */ 
  155. public function set_component( $component ) { 
  156. $this->component = $component; 
  157.  
  158. /** 
  159. * Get the component to which the a nav item belongs. 
  160. * 
  161. * We use the following heuristic to guess, based on an offset, which component the item belongs to: 
  162. * - If this is a group, and the offset is the same as the current group's slug, it's a group nav item. 
  163. * - Otherwise, it's a member nav item. 
  164. * 
  165. * @since 2.6.0 
  166. * 
  167. * @param mixed $offset Array offset. 
  168. * @return string 
  169. */ 
  170. public function get_component( $offset = '' ) { 
  171. if ( ! isset( $this->component ) ) { 
  172. if ( bp_is_active( 'groups' ) && $offset === bp_get_current_group_slug() ) { 
  173. $this->component = 'groups'; 
  174. } else { 
  175. $this->component = 'members'; 
  176.  
  177. return $this->component; 
  178.  
  179. /** 
  180. * Reset the cached nav items. 
  181. * 
  182. * Called when the nav API removes items from the nav array. 
  183. * 
  184. * @since 2.6.0 
  185. */ 
  186. public function reset() { 
  187. $this->backcompat_nav = array(); 
  188.  
  189. /** 
  190. * Get the nav object corresponding to the specified offset. 
  191. * 
  192. * @since 2.6.0 
  193. * 
  194. * @param mixed $offset Array offset. 
  195. * @return bool|array 
  196. */ 
  197. protected function get_nav( $offset ) { 
  198. $bp = buddypress(); 
  199.  
  200. $component_nav = $this->get_component_nav( $offset ); 
  201. $primary_nav = $component_nav->get_primary( array( 'slug' => $offset ), false ); 
  202.  
  203. $nav = array(); 
  204.  
  205. if ( empty( $primary_nav ) ) { 
  206. return $nav; 
  207.  
  208. foreach ( $primary_nav as $item ) { 
  209. $nav[ $item->slug ] = (array) $item; 
  210.  
  211. return $nav; 
  212.  
  213. /** 
  214. * Get the BP_Core_Nav object corresponding to the component, based on a nav item name. 
  215. * 
  216. * The way bp_nav was previously organized makes it impossible to know for sure which component's nav is 
  217. * being referenced by a given nav item name. We guess in the following manner: 
  218. * - If we're looking at a group, and the nav item name (`$offset`) is the same as the slug of the current 
  219. * group, we assume that the proper component nav is 'groups'. 
  220. * - Otherwise, fall back on 'members'. 
  221. * 
  222. * @since 2.6.0 
  223. * 
  224. * @param string $offset Nav item name. 
  225. * @return BP_Core_Nav 
  226. */ 
  227. protected function get_component_nav( $offset = '' ) { 
  228. $component = $this->get_component( $offset ); 
  229.  
  230. $bp = buddypress(); 
  231. if ( ! isset( $bp->{$component}->nav ) ) { 
  232. return false; 
  233.  
  234. return $bp->{$component}->nav; 
  235.  
  236. /** 
  237. * Get the nav data, formatted as a flat array. 
  238. * 
  239. * @since 2.6.0 
  240. * 
  241. * @return array 
  242. */ 
  243. protected function to_array() { 
  244. return $this->backcompat_nav; 
.