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


Generated: Tue Mar 19 01:01:09 2024 Cross-referenced by PHPXref 0.7.1