[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/classes/ -> class-bp-core-bp-nav-backcompat.php (source)

   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      }
  54  
  55      /**
  56       * Assign a value to the nav array at the specified offset.
  57       *
  58       * @since 2.6.0
  59       *
  60       * @param mixed $offset Array offset.
  61       * @param array $value  Nav item.
  62       */
  63  	public function offsetSet( $offset, $value ) {
  64          _doing_it_wrong(
  65              'bp_nav',
  66              __( 'The bp_nav and bp_options_nav globals should not be used directly and are deprecated. Please use the BuddyPress nav functions instead.', 'buddypress' ),
  67              '2.6.0'
  68          );
  69  
  70          $bp = buddypress();
  71  
  72          if ( is_array( $value ) ) {
  73              $value = new self( $value );
  74          }
  75  
  76          if ( $offset !== null ) {
  77              // Temporarily set the backcompat_nav.
  78              $this->backcompat_nav[ $offset ] = $value;
  79  
  80              $args = $this->to_array();
  81              if ( isset( $args['parent_slug'] ) ) {
  82                  $this->get_component_nav( $args['parent_slug'] )->edit_nav( $args, $args['slug'], $args['parent_slug'] );
  83              } elseif ( isset( $args['slug'] ) ) {
  84                  $bp->members->nav->edit_nav( $args, $args['slug'] );
  85              }
  86          }
  87      }
  88  
  89      /**
  90       * Get a value of the nav array at the specified offset.
  91       *
  92       * @since 2.6.0
  93       *
  94       * @param mixed $offset Array offset.
  95       * @return BP_Core_BP_Nav_BackCompat
  96       */
  97  	public function offsetGet( $offset ) {
  98          _doing_it_wrong(
  99              'bp_nav',
 100              __( 'The bp_nav and bp_options_nav globals should not be used directly and are deprecated. Please use the BuddyPress nav functions instead.', 'buddypress' ),
 101              '2.6.0'
 102          );
 103  
 104  //        if ( ! isset( $this->backcompat_nav[ $offset ] ) ) {
 105              $nav = $this->get_nav( $offset );
 106              if ( $nav && isset( $nav[ $offset ] ) ) {
 107                  $this->backcompat_nav[ $offset ] = new self( $nav[ $offset ] );
 108              }
 109  //        }
 110  
 111          return $this->backcompat_nav[ $offset ];
 112      }
 113  
 114      /**
 115       * Check whether nav array has a value at the specified offset.
 116       *
 117       * @since 2.6.0
 118       *
 119       * @param mixed $offset Array offset.
 120       * @return bool
 121       */
 122  	public function offsetExists( $offset ) {
 123          _doing_it_wrong(
 124              'bp_nav',
 125              __( 'The bp_nav and bp_options_nav globals should not be used directly and are deprecated. Please use the BuddyPress nav functions instead.', 'buddypress' ),
 126              '2.6.0'
 127          );
 128  
 129          if ( isset( $this->backcompat_nav[ $offset ] ) ) {
 130              return true;
 131          }
 132  
 133          $nav = $this->get_nav( $offset );
 134          if ( $nav && isset( $nav[ $offset ] ) ) {
 135              return true;
 136          }
 137  
 138          return false;
 139      }
 140  
 141      /**
 142       * Unset a nav array value at the specified offset.
 143       *
 144       * @since 2.6.0
 145       *
 146       * @param mixed $offset Array offset.
 147       */
 148  	public function offsetUnset( $offset ) {
 149          _doing_it_wrong(
 150              'bp_nav',
 151              __( 'The bp_nav and bp_options_nav globals should not be used directly and are deprecated. Please use the BuddyPress nav functions instead.', 'buddypress' ),
 152              '2.6.0'
 153          );
 154  
 155          // For top-level nav items, the backcompat nav hasn't yet been initialized.
 156          if ( ! isset( $this->backcompat_nav[ $offset ] ) ) {
 157              buddypress()->members->nav->delete_nav( $offset );
 158              unset( $this->backcompat_nav[ $offset ] );
 159          }
 160      }
 161  
 162      /**
 163       * Set the component to which the nav belongs.
 164       *
 165       * @since 2.6.0
 166       *
 167       * @param string $component
 168       */
 169  	public function set_component( $component ) {
 170          $this->component = $component;
 171      }
 172  
 173      /**
 174       * Get the component to which the a nav item belongs.
 175       *
 176       * We use the following heuristic to guess, based on an offset, which component the item belongs to:
 177       *   - If this is a group, and the offset is the same as the current group's slug, it's a group nav item.
 178       *   - Otherwise, it's a member nav item.
 179       *
 180       * @since 2.6.0
 181       *
 182       * @param mixed $offset Array offset.
 183       * @return string|array
 184       */
 185  	public function get_component( $offset = '' ) {
 186          if ( ! isset( $this->component ) ) {
 187              if ( bp_is_active( 'groups' ) && $offset === bp_get_current_group_slug() ) {
 188                  $this->component = 'groups';
 189              } else {
 190                  $this->component = 'members';
 191              }
 192          }
 193  
 194          return $this->component;
 195      }
 196  
 197      /**
 198       * Reset the cached nav items.
 199       *
 200       * Called when the nav API removes items from the nav array.
 201       *
 202       * @since 2.6.0
 203       */
 204  	public function reset() {
 205          $this->backcompat_nav = array();
 206      }
 207  
 208      /**
 209       * Get the nav object corresponding to the specified offset.
 210       *
 211       * @since 2.6.0
 212       *
 213       * @param mixed $offset Array offset.
 214       * @return bool|array
 215       */
 216  	protected function get_nav( $offset ) {
 217          $bp = buddypress();
 218  
 219          $component_nav = $this->get_component_nav( $offset );
 220          $primary_nav   = $component_nav->get_primary( array( 'slug' => $offset ), false );
 221  
 222          $nav = array();
 223  
 224          if ( empty( $primary_nav ) ) {
 225              return $nav;
 226          }
 227  
 228          foreach ( $primary_nav as $item ) {
 229              $nav[ $item->slug ] = (array) $item;
 230          }
 231  
 232          return $nav;
 233      }
 234  
 235      /**
 236       * Get the BP_Core_Nav object corresponding to the component, based on a nav item name.
 237       *
 238       * The way bp_nav was previously organized makes it impossible to know for sure which component's nav is
 239       * being referenced by a given nav item name. We guess in the following manner:
 240       *   - If we're looking at a group, and the nav item name (`$offset`) is the same as the slug of the current
 241       *     group, we assume that the proper component nav is 'groups'.
 242       *   - Otherwise, fall back on 'members'.
 243       *
 244       * @since 2.6.0
 245       *
 246       * @param string $offset Nav item name.
 247       * @return BP_Core_Nav
 248       */
 249  	protected function get_component_nav( $offset = '' ) {
 250          $component = $this->get_component( $offset );
 251  
 252          $bp = buddypress();
 253          if ( ! isset( $bp->{$component}->nav ) ) {
 254              return false;
 255          }
 256  
 257          return $bp->{$component}->nav;
 258      }
 259  
 260      /**
 261       * Get the nav data, formatted as a flat array.
 262       *
 263       * @since 2.6.0
 264       *
 265       * @return array
 266       */
 267  	protected function to_array() {
 268          return $this->backcompat_nav;
 269      }
 270  }


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1