BP_Core_BP_Nav_BackCompat

Bp_nav backward compatibility class.

Defined (1)

The class is defined in the following location(s).

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