[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Core component classes.
   4   *
   5   * @package BuddyPress
   6   * @subpackage Core
   7   * @since 1.7.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Compatibility Class to make BP_Walker_Nav_Menu::walk() compatible
  15   * from PHP 5.3 to 5.6 and up.
  16   *
  17   * @since 5.1.0
  18   */
  19  class BP_Walker_Nav_Menu_Compat extends Walker_Nav_Menu {
  20      /**
  21       * Description of fields indexes for building markup.
  22       *
  23       * @since 1.7.0
  24       * @var array
  25       */
  26      var $db_fields = array( 'id' => 'css_id', 'parent' => 'parent' );
  27  
  28      /**
  29       * Tree type.
  30       *
  31       * @since 1.7.0
  32       * @var string
  33       */
  34      var $tree_type = array();
  35  
  36      /**
  37       * Display array of elements hierarchically.
  38       *
  39       * This method is almost identical to the version in {@link Walker::walk()}.
  40       * The only change is on one line which has been commented. An IF was
  41       * comparing 0 to a non-empty string which was preventing child elements
  42       * being grouped under their parent menu element.
  43       *
  44       * This caused a problem for BuddyPress because our primary/secondary
  45       * navigations don't have a unique numerical ID that describes a
  46       * hierarchy (we use a slug). Obviously, WordPress Menus use Posts, and
  47       * those have ID/post_parent.
  48       *
  49       * @since 1.7.0
  50       * @since 5.1.0 Method was renamed from `walk` to `do_walk` to ensure PHP 5.3 compatibility
  51       *
  52       * @see Walker::walk()
  53       *
  54       * @param array $elements  See {@link Walker::walk()}.
  55       * @param int   $max_depth See {@link Walker::walk()}.
  56       * @param array $args      Optional additional arguments.
  57       * @return string See {@link Walker::walk()}.
  58       */
  59  	public function do_walk( $elements, $max_depth, $args = array() ) {
  60          $output = '';
  61  
  62          if ( $max_depth < -1 ) // Invalid parameter.
  63              return $output;
  64  
  65          if ( empty( $elements ) ) // Nothing to walk.
  66              return $output;
  67  
  68          $parent_field = $this->db_fields['parent'];
  69  
  70          // Flat display.
  71          if ( -1 == $max_depth ) {
  72  
  73              $empty_array = array();
  74              foreach ( $elements as $e )
  75                  $this->display_element( $e, $empty_array, 1, 0, $args, $output );
  76  
  77              return $output;
  78          }
  79  
  80          /*
  81           * Need to display in hierarchical order
  82           * separate elements into two buckets: top level and children elements
  83           * children_elements is two dimensional array, eg.
  84           * children_elements[10][] contains all sub-elements whose parent is 10.
  85           */
  86          $top_level_elements = array();
  87          $children_elements  = array();
  88  
  89          foreach ( $elements as $e ) {
  90              // BuddyPress: changed '==' to '==='. This is the only change from version in Walker::walk().
  91              if ( 0 === $e->$parent_field )
  92                  $top_level_elements[] = $e;
  93              else
  94                  $children_elements[$e->$parent_field][] = $e;
  95          }
  96  
  97          /*
  98           * When none of the elements is top level
  99           * assume the first one must be root of the sub elements.
 100           */
 101          if ( empty( $top_level_elements ) ) {
 102  
 103              $first              = array_slice( $elements, 0, 1 );
 104              $root               = $first[0];
 105              $top_level_elements = array();
 106              $children_elements  = array();
 107  
 108              foreach ( $elements as $e ) {
 109                  if ( $root->$parent_field == $e->$parent_field )
 110                      $top_level_elements[] = $e;
 111                  else
 112                      $children_elements[$e->$parent_field][] = $e;
 113              }
 114          }
 115  
 116          foreach ( $top_level_elements as $e )
 117              $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
 118  
 119          /*
 120           * If we are displaying all levels, and remaining children_elements is not empty,
 121           * then we got orphans, which should be displayed regardless.
 122           */
 123          if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
 124              $empty_array = array();
 125  
 126              foreach ( $children_elements as $orphans )
 127                  foreach ( $orphans as $op )
 128                      $this->display_element( $op, $empty_array, 1, 0, $args, $output );
 129           }
 130  
 131           return $output;
 132      }
 133  
 134      /**
 135       * Display the current <li> that we are on.
 136       *
 137       * @see Walker::start_el() for complete description of parameters.
 138       *
 139       * @since 1.7.0
 140       *
 141       * @param string $output Passed by reference. Used to append
 142       *                       additional content.
 143       * @param object $item   Menu item data object.
 144       * @param int    $depth  Depth of menu item. Used for padding. Optional,
 145       *                       defaults to 0.
 146       * @param array  $args   Optional. See {@link Walker::start_el()}.
 147       * @param int    $id     Menu item ID. Optional.
 148       */
 149  	public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
 150          // If we're someway down the tree, indent the HTML with the appropriate number of tabs.
 151          $indent = $depth ? str_repeat( "\t", $depth ) : '';
 152  
 153          /**
 154           * Filters the classes to be added to the nav menu markup.
 155           *
 156           * @since 1.7.0
 157           *
 158           * @param array  $value Array of classes to be added.
 159           * @param object $item  Menu item data object.
 160           * @param array  $args  Array of arguments for the item.
 161           */
 162          $class_names = join( ' ', apply_filters( 'bp_nav_menu_css_class', array_filter( $item->class ), $item, $args ) );
 163          $class_names = ! empty( $class_names ) ? ' class="' . esc_attr( $class_names ) . '"' : '';
 164  
 165          // Add HTML ID
 166          $id = sanitize_html_class( $item->css_id . '-personal-li' );  // Backpat with BP pre-1.7.
 167  
 168          /**
 169           * Filters the value to be used for the nav menu ID attribute.
 170           *
 171           * @since 1.7.0
 172           *
 173           * @param string $id   ID attribute to be added to the menu item.
 174           * @param object $item Menu item data object.
 175           * @param array  $args Array of arguments for the item.
 176           */
 177          $id = apply_filters( 'bp_nav_menu_item_id', $id, $item, $args );
 178          $id = ! empty( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
 179  
 180          // Opening tag; closing tag is handled in Walker_Nav_Menu::end_el().
 181          $output .= $indent . '<li' . $id . $class_names . '>';
 182  
 183          // Add href attribute.
 184          $attributes = ! empty( $item->link ) ? ' href="' . esc_url( $item->link ) . '"' : '';
 185  
 186          // Construct the link.
 187          $item_output = $args->before;
 188          $item_output .= '<a' . $attributes . '>';
 189  
 190          /**
 191           * Filters the link text to be added to the item output.
 192           *
 193           * @since 1.7.0
 194           *
 195           * @param string $name  Item text to be applied.
 196           * @param int    $value Post ID the title is for.
 197           */
 198          $item_output .= $args->link_before . apply_filters( 'the_title', $item->name, 0 ) . $args->link_after;
 199          $item_output .= '</a>';
 200          $item_output .= $args->after;
 201  
 202          /**
 203           * Filters the final result for the menu item.
 204           *
 205           * @since 1.7.0
 206           *
 207           * @param string $item_output Constructed output for the menu item to append to output.
 208           * @param object $item        Menu item data object.
 209           * @param int    $depth       Depth of menu item. Used for padding.
 210           * @param array  $args        Array of arguments for the item.
 211           */
 212          $output .= apply_filters( 'bp_walker_nav_menu_start_el', $item_output, $item, $depth, $args );
 213      }
 214  }
 215  
 216  if ( PHP_VERSION_ID >= 50600 ) {
 217      require_once dirname( __DIR__ ) . '/compat/php56/class-bp-compat-walker-nav-menu.php';
 218  } else {
 219      require_once dirname( __DIR__ ) . '/compat/php53/class-bp-compat-walker-nav-menu.php';
 220  }


Generated: Sun Dec 8 01:01:37 2019 Cross-referenced by PHPXref 0.7.1