wp/wp-includes/class-walker-nav-menu.php
changeset 7 cf61fcea0001
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
       
     1 <?php
       
     2 /**
       
     3  * Nav Menu API: Walker_Nav_Menu class
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Nav_Menus
       
     7  * @since 4.6.0
       
     8  */
       
     9 
       
    10 /**
       
    11  * Core class used to implement an HTML list of nav menu items.
       
    12  *
       
    13  * @since 3.0.0
       
    14  *
       
    15  * @see Walker
       
    16  */
       
    17 class Walker_Nav_Menu extends Walker {
       
    18 	/**
       
    19 	 * What the class handles.
       
    20 	 *
       
    21 	 * @since 3.0.0
       
    22 	 * @var string
       
    23 	 *
       
    24 	 * @see Walker::$tree_type
       
    25 	 */
       
    26 	public $tree_type = array( 'post_type', 'taxonomy', 'custom' );
       
    27 
       
    28 	/**
       
    29 	 * Database fields to use.
       
    30 	 *
       
    31 	 * @since 3.0.0
       
    32 	 * @todo Decouple this.
       
    33 	 * @var array
       
    34 	 *
       
    35 	 * @see Walker::$db_fields
       
    36 	 */
       
    37 	public $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
       
    38 
       
    39 	/**
       
    40 	 * Starts the list before the elements are added.
       
    41 	 *
       
    42 	 * @since 3.0.0
       
    43 	 *
       
    44 	 * @see Walker::start_lvl()
       
    45 	 *
       
    46 	 * @param string   $output Used to append additional content (passed by reference).
       
    47 	 * @param int      $depth  Depth of menu item. Used for padding.
       
    48 	 * @param stdClass $args   An object of wp_nav_menu() arguments.
       
    49 	 */
       
    50 	public function start_lvl( &$output, $depth = 0, $args = array() ) {
       
    51 		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
       
    52 			$t = '';
       
    53 			$n = '';
       
    54 		} else {
       
    55 			$t = "\t";
       
    56 			$n = "\n";
       
    57 		}
       
    58 		$indent = str_repeat( $t, $depth );
       
    59 
       
    60 		// Default class.
       
    61 		$classes = array( 'sub-menu' );
       
    62 
       
    63 		/**
       
    64 		 * Filters the CSS class(es) applied to a menu list element.
       
    65 		 *
       
    66 		 * @since 4.8.0
       
    67 		 *
       
    68 		 * @param array    $classes The CSS classes that are applied to the menu `<ul>` element.
       
    69 		 * @param stdClass $args    An object of `wp_nav_menu()` arguments.
       
    70 		 * @param int      $depth   Depth of menu item. Used for padding.
       
    71 		 */
       
    72 		$class_names = join( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
       
    73 		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
       
    74 
       
    75 		$output .= "{$n}{$indent}<ul$class_names>{$n}";
       
    76 	}
       
    77 
       
    78 	/**
       
    79 	 * Ends the list of after the elements are added.
       
    80 	 *
       
    81 	 * @since 3.0.0
       
    82 	 *
       
    83 	 * @see Walker::end_lvl()
       
    84 	 *
       
    85 	 * @param string   $output Used to append additional content (passed by reference).
       
    86 	 * @param int      $depth  Depth of menu item. Used for padding.
       
    87 	 * @param stdClass $args   An object of wp_nav_menu() arguments.
       
    88 	 */
       
    89 	public function end_lvl( &$output, $depth = 0, $args = array() ) {
       
    90 		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
       
    91 			$t = '';
       
    92 			$n = '';
       
    93 		} else {
       
    94 			$t = "\t";
       
    95 			$n = "\n";
       
    96 		}
       
    97 		$indent = str_repeat( $t, $depth );
       
    98 		$output .= "$indent</ul>{$n}";
       
    99 	}
       
   100 
       
   101 	/**
       
   102 	 * Starts the element output.
       
   103 	 *
       
   104 	 * @since 3.0.0
       
   105 	 * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added.
       
   106 	 *
       
   107 	 * @see Walker::start_el()
       
   108 	 *
       
   109 	 * @param string   $output Used to append additional content (passed by reference).
       
   110 	 * @param WP_Post  $item   Menu item data object.
       
   111 	 * @param int      $depth  Depth of menu item. Used for padding.
       
   112 	 * @param stdClass $args   An object of wp_nav_menu() arguments.
       
   113 	 * @param int      $id     Current item ID.
       
   114 	 */
       
   115 	public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
       
   116 		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
       
   117 			$t = '';
       
   118 			$n = '';
       
   119 		} else {
       
   120 			$t = "\t";
       
   121 			$n = "\n";
       
   122 		}
       
   123 		$indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
       
   124 
       
   125 		$classes = empty( $item->classes ) ? array() : (array) $item->classes;
       
   126 		$classes[] = 'menu-item-' . $item->ID;
       
   127 
       
   128 		/**
       
   129 		 * Filters the arguments for a single nav menu item.
       
   130 		 *
       
   131 		 * @since 4.4.0
       
   132 		 *
       
   133 		 * @param stdClass $args  An object of wp_nav_menu() arguments.
       
   134 		 * @param WP_Post  $item  Menu item data object.
       
   135 		 * @param int      $depth Depth of menu item. Used for padding.
       
   136 		 */
       
   137 		$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
       
   138 
       
   139 		/**
       
   140 		 * Filters the CSS class(es) applied to a menu item's list item element.
       
   141 		 *
       
   142 		 * @since 3.0.0
       
   143 		 * @since 4.1.0 The `$depth` parameter was added.
       
   144 		 *
       
   145 		 * @param array    $classes The CSS classes that are applied to the menu item's `<li>` element.
       
   146 		 * @param WP_Post  $item    The current menu item.
       
   147 		 * @param stdClass $args    An object of wp_nav_menu() arguments.
       
   148 		 * @param int      $depth   Depth of menu item. Used for padding.
       
   149 		 */
       
   150 		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
       
   151 		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
       
   152 
       
   153 		/**
       
   154 		 * Filters the ID applied to a menu item's list item element.
       
   155 		 *
       
   156 		 * @since 3.0.1
       
   157 		 * @since 4.1.0 The `$depth` parameter was added.
       
   158 		 *
       
   159 		 * @param string   $menu_id The ID that is applied to the menu item's `<li>` element.
       
   160 		 * @param WP_Post  $item    The current menu item.
       
   161 		 * @param stdClass $args    An object of wp_nav_menu() arguments.
       
   162 		 * @param int      $depth   Depth of menu item. Used for padding.
       
   163 		 */
       
   164 		$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
       
   165 		$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
       
   166 
       
   167 		$output .= $indent . '<li' . $id . $class_names .'>';
       
   168 
       
   169 		$atts = array();
       
   170 		$atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
       
   171 		$atts['target'] = ! empty( $item->target )     ? $item->target     : '';
       
   172 		$atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
       
   173 		$atts['href']   = ! empty( $item->url )        ? $item->url        : '';
       
   174 
       
   175 		/**
       
   176 		 * Filters the HTML attributes applied to a menu item's anchor element.
       
   177 		 *
       
   178 		 * @since 3.6.0
       
   179 		 * @since 4.1.0 The `$depth` parameter was added.
       
   180 		 *
       
   181 		 * @param array $atts {
       
   182 		 *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
       
   183 		 *
       
   184 		 *     @type string $title  Title attribute.
       
   185 		 *     @type string $target Target attribute.
       
   186 		 *     @type string $rel    The rel attribute.
       
   187 		 *     @type string $href   The href attribute.
       
   188 		 * }
       
   189 		 * @param WP_Post  $item  The current menu item.
       
   190 		 * @param stdClass $args  An object of wp_nav_menu() arguments.
       
   191 		 * @param int      $depth Depth of menu item. Used for padding.
       
   192 		 */
       
   193 		$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
       
   194 
       
   195 		$attributes = '';
       
   196 		foreach ( $atts as $attr => $value ) {
       
   197 			if ( ! empty( $value ) ) {
       
   198 				$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
       
   199 				$attributes .= ' ' . $attr . '="' . $value . '"';
       
   200 			}
       
   201 		}
       
   202 
       
   203 		/** This filter is documented in wp-includes/post-template.php */
       
   204 		$title = apply_filters( 'the_title', $item->title, $item->ID );
       
   205 
       
   206 		/**
       
   207 		 * Filters a menu item's title.
       
   208 		 *
       
   209 		 * @since 4.4.0
       
   210 		 *
       
   211 		 * @param string   $title The menu item's title.
       
   212 		 * @param WP_Post  $item  The current menu item.
       
   213 		 * @param stdClass $args  An object of wp_nav_menu() arguments.
       
   214 		 * @param int      $depth Depth of menu item. Used for padding.
       
   215 		 */
       
   216 		$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
       
   217 
       
   218 		$item_output = $args->before;
       
   219 		$item_output .= '<a'. $attributes .'>';
       
   220 		$item_output .= $args->link_before . $title . $args->link_after;
       
   221 		$item_output .= '</a>';
       
   222 		$item_output .= $args->after;
       
   223 
       
   224 		/**
       
   225 		 * Filters a menu item's starting output.
       
   226 		 *
       
   227 		 * The menu item's starting output only includes `$args->before`, the opening `<a>`,
       
   228 		 * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
       
   229 		 * no filter for modifying the opening and closing `<li>` for a menu item.
       
   230 		 *
       
   231 		 * @since 3.0.0
       
   232 		 *
       
   233 		 * @param string   $item_output The menu item's starting HTML output.
       
   234 		 * @param WP_Post  $item        Menu item data object.
       
   235 		 * @param int      $depth       Depth of menu item. Used for padding.
       
   236 		 * @param stdClass $args        An object of wp_nav_menu() arguments.
       
   237 		 */
       
   238 		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
       
   239 	}
       
   240 
       
   241 	/**
       
   242 	 * Ends the element output, if needed.
       
   243 	 *
       
   244 	 * @since 3.0.0
       
   245 	 *
       
   246 	 * @see Walker::end_el()
       
   247 	 *
       
   248 	 * @param string   $output Used to append additional content (passed by reference).
       
   249 	 * @param WP_Post  $item   Page data object. Not used.
       
   250 	 * @param int      $depth  Depth of page. Not Used.
       
   251 	 * @param stdClass $args   An object of wp_nav_menu() arguments.
       
   252 	 */
       
   253 	public function end_el( &$output, $item, $depth = 0, $args = array() ) {
       
   254 		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
       
   255 			$t = '';
       
   256 			$n = '';
       
   257 		} else {
       
   258 			$t = "\t";
       
   259 			$n = "\n";
       
   260 		}
       
   261 		$output .= "</li>{$n}";
       
   262 	}
       
   263 
       
   264 } // Walker_Nav_Menu