wp/wp-includes/class-walker-nav-menu.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    28 	/**
    28 	/**
    29 	 * Database fields to use.
    29 	 * Database fields to use.
    30 	 *
    30 	 *
    31 	 * @since 3.0.0
    31 	 * @since 3.0.0
    32 	 * @todo Decouple this.
    32 	 * @todo Decouple this.
    33 	 * @var array
    33 	 * @var string[]
    34 	 *
    34 	 *
    35 	 * @see Walker::$db_fields
    35 	 * @see Walker::$db_fields
    36 	 */
    36 	 */
    37 	public $db_fields = array(
    37 	public $db_fields = array(
    38 		'parent' => 'menu_item_parent',
    38 		'parent' => 'menu_item_parent',
   104 	/**
   104 	/**
   105 	 * Starts the element output.
   105 	 * Starts the element output.
   106 	 *
   106 	 *
   107 	 * @since 3.0.0
   107 	 * @since 3.0.0
   108 	 * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added.
   108 	 * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added.
       
   109 	 * @since 5.9.0 Renamed `$item` to `$data_object` and `$id` to `$current_object_id`
       
   110 	 *              to match parent class for PHP 8 named parameter support.
   109 	 *
   111 	 *
   110 	 * @see Walker::start_el()
   112 	 * @see Walker::start_el()
   111 	 *
   113 	 *
   112 	 * @param string   $output Used to append additional content (passed by reference).
   114 	 * @param string   $output            Used to append additional content (passed by reference).
   113 	 * @param WP_Post  $item   Menu item data object.
   115 	 * @param WP_Post  $data_object       Menu item data object.
   114 	 * @param int      $depth  Depth of menu item. Used for padding.
   116 	 * @param int      $depth             Depth of menu item. Used for padding.
   115 	 * @param stdClass $args   An object of wp_nav_menu() arguments.
   117 	 * @param stdClass $args              An object of wp_nav_menu() arguments.
   116 	 * @param int      $id     Current item ID.
   118 	 * @param int      $current_object_id Optional. ID of the current menu item. Default 0.
   117 	 */
   119 	 */
   118 	public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
   120 	public function start_el( &$output, $data_object, $depth = 0, $args = null, $current_object_id = 0 ) {
       
   121 		// Restores the more descriptive, specific name for use within this method.
       
   122 		$menu_item = $data_object;
       
   123 
   119 		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
   124 		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
   120 			$t = '';
   125 			$t = '';
   121 			$n = '';
   126 			$n = '';
   122 		} else {
   127 		} else {
   123 			$t = "\t";
   128 			$t = "\t";
   124 			$n = "\n";
   129 			$n = "\n";
   125 		}
   130 		}
   126 		$indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
   131 		$indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
   127 
   132 
   128 		$classes   = empty( $item->classes ) ? array() : (array) $item->classes;
   133 		$classes   = empty( $menu_item->classes ) ? array() : (array) $menu_item->classes;
   129 		$classes[] = 'menu-item-' . $item->ID;
   134 		$classes[] = 'menu-item-' . $menu_item->ID;
   130 
   135 
   131 		/**
   136 		/**
   132 		 * Filters the arguments for a single nav menu item.
   137 		 * Filters the arguments for a single nav menu item.
   133 		 *
   138 		 *
   134 		 * @since 4.4.0
   139 		 * @since 4.4.0
   135 		 *
   140 		 *
   136 		 * @param stdClass $args  An object of wp_nav_menu() arguments.
   141 		 * @param stdClass $args      An object of wp_nav_menu() arguments.
   137 		 * @param WP_Post  $item  Menu item data object.
   142 		 * @param WP_Post  $menu_item Menu item data object.
   138 		 * @param int      $depth Depth of menu item. Used for padding.
   143 		 * @param int      $depth     Depth of menu item. Used for padding.
   139 		 */
   144 		 */
   140 		$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
   145 		$args = apply_filters( 'nav_menu_item_args', $args, $menu_item, $depth );
   141 
   146 
   142 		/**
   147 		/**
   143 		 * Filters the CSS classes applied to a menu item's list item element.
   148 		 * Filters the CSS classes applied to a menu item's list item element.
   144 		 *
   149 		 *
   145 		 * @since 3.0.0
   150 		 * @since 3.0.0
   146 		 * @since 4.1.0 The `$depth` parameter was added.
   151 		 * @since 4.1.0 The `$depth` parameter was added.
   147 		 *
   152 		 *
   148 		 * @param string[] $classes Array of the CSS classes that are applied to the menu item's `<li>` element.
   153 		 * @param string[] $classes   Array of the CSS classes that are applied to the menu item's `<li>` element.
   149 		 * @param WP_Post  $item    The current menu item.
   154 		 * @param WP_Post  $menu_item The current menu item object.
   150 		 * @param stdClass $args    An object of wp_nav_menu() arguments.
   155 		 * @param stdClass $args      An object of wp_nav_menu() arguments.
   151 		 * @param int      $depth   Depth of menu item. Used for padding.
   156 		 * @param int      $depth     Depth of menu item. Used for padding.
   152 		 */
   157 		 */
   153 		$class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
   158 		$class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $menu_item, $args, $depth ) );
   154 		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
   159 		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
   155 
   160 
   156 		/**
   161 		/**
   157 		 * Filters the ID applied to a menu item's list item element.
   162 		 * Filters the ID applied to a menu item's list item element.
   158 		 *
   163 		 *
   159 		 * @since 3.0.1
   164 		 * @since 3.0.1
   160 		 * @since 4.1.0 The `$depth` parameter was added.
   165 		 * @since 4.1.0 The `$depth` parameter was added.
   161 		 *
   166 		 *
   162 		 * @param string   $menu_id The ID that is applied to the menu item's `<li>` element.
   167 		 * @param string   $menu_id   The ID that is applied to the menu item's `<li>` element.
   163 		 * @param WP_Post  $item    The current menu item.
   168 		 * @param WP_Post  $menu_item The current menu item.
   164 		 * @param stdClass $args    An object of wp_nav_menu() arguments.
   169 		 * @param stdClass $args      An object of wp_nav_menu() arguments.
   165 		 * @param int      $depth   Depth of menu item. Used for padding.
   170 		 * @param int      $depth     Depth of menu item. Used for padding.
   166 		 */
   171 		 */
   167 		$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth );
   172 		$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $menu_item->ID, $menu_item, $args, $depth );
   168 		$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
   173 		$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
   169 
   174 
   170 		$output .= $indent . '<li' . $id . $class_names . '>';
   175 		$output .= $indent . '<li' . $id . $class_names . '>';
   171 
   176 
   172 		$atts           = array();
   177 		$atts           = array();
   173 		$atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
   178 		$atts['title']  = ! empty( $menu_item->attr_title ) ? $menu_item->attr_title : '';
   174 		$atts['target'] = ! empty( $item->target ) ? $item->target : '';
   179 		$atts['target'] = ! empty( $menu_item->target ) ? $menu_item->target : '';
   175 		if ( '_blank' === $item->target && empty( $item->xfn ) ) {
   180 		if ( '_blank' === $menu_item->target && empty( $menu_item->xfn ) ) {
   176 			$atts['rel'] = 'noopener';
   181 			$atts['rel'] = 'noopener';
   177 		} else {
   182 		} else {
   178 			$atts['rel'] = $item->xfn;
   183 			$atts['rel'] = $menu_item->xfn;
   179 		}
   184 		}
   180 		$atts['href']         = ! empty( $item->url ) ? $item->url : '';
   185 		$atts['href']         = ! empty( $menu_item->url ) ? $menu_item->url : '';
   181 		$atts['aria-current'] = $item->current ? 'page' : '';
   186 		$atts['aria-current'] = $menu_item->current ? 'page' : '';
   182 
   187 
   183 		/**
   188 		/**
   184 		 * Filters the HTML attributes applied to a menu item's anchor element.
   189 		 * Filters the HTML attributes applied to a menu item's anchor element.
   185 		 *
   190 		 *
   186 		 * @since 3.6.0
   191 		 * @since 3.6.0
   193 		 *     @type string $target       Target attribute.
   198 		 *     @type string $target       Target attribute.
   194 		 *     @type string $rel          The rel attribute.
   199 		 *     @type string $rel          The rel attribute.
   195 		 *     @type string $href         The href attribute.
   200 		 *     @type string $href         The href attribute.
   196 		 *     @type string $aria-current The aria-current attribute.
   201 		 *     @type string $aria-current The aria-current attribute.
   197 		 * }
   202 		 * }
   198 		 * @param WP_Post  $item  The current menu item.
   203 		 * @param WP_Post  $menu_item The current menu item object.
   199 		 * @param stdClass $args  An object of wp_nav_menu() arguments.
   204 		 * @param stdClass $args      An object of wp_nav_menu() arguments.
   200 		 * @param int      $depth Depth of menu item. Used for padding.
   205 		 * @param int      $depth     Depth of menu item. Used for padding.
   201 		 */
   206 		 */
   202 		$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
   207 		$atts = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth );
   203 
   208 
   204 		$attributes = '';
   209 		$attributes = '';
   205 		foreach ( $atts as $attr => $value ) {
   210 		foreach ( $atts as $attr => $value ) {
   206 			if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
   211 			if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
   207 				$value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
   212 				$value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
   208 				$attributes .= ' ' . $attr . '="' . $value . '"';
   213 				$attributes .= ' ' . $attr . '="' . $value . '"';
   209 			}
   214 			}
   210 		}
   215 		}
   211 
   216 
   212 		/** This filter is documented in wp-includes/post-template.php */
   217 		/** This filter is documented in wp-includes/post-template.php */
   213 		$title = apply_filters( 'the_title', $item->title, $item->ID );
   218 		$title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
   214 
   219 
   215 		/**
   220 		/**
   216 		 * Filters a menu item's title.
   221 		 * Filters a menu item's title.
   217 		 *
   222 		 *
   218 		 * @since 4.4.0
   223 		 * @since 4.4.0
   219 		 *
   224 		 *
   220 		 * @param string   $title The menu item's title.
   225 		 * @param string   $title     The menu item's title.
   221 		 * @param WP_Post  $item  The current menu item.
   226 		 * @param WP_Post  $menu_item The current menu item object.
   222 		 * @param stdClass $args  An object of wp_nav_menu() arguments.
   227 		 * @param stdClass $args      An object of wp_nav_menu() arguments.
   223 		 * @param int      $depth Depth of menu item. Used for padding.
   228 		 * @param int      $depth     Depth of menu item. Used for padding.
   224 		 */
   229 		 */
   225 		$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
   230 		$title = apply_filters( 'nav_menu_item_title', $title, $menu_item, $args, $depth );
   226 
   231 
   227 		$item_output  = $args->before;
   232 		$item_output  = $args->before;
   228 		$item_output .= '<a' . $attributes . '>';
   233 		$item_output .= '<a' . $attributes . '>';
   229 		$item_output .= $args->link_before . $title . $args->link_after;
   234 		$item_output .= $args->link_before . $title . $args->link_after;
   230 		$item_output .= '</a>';
   235 		$item_output .= '</a>';
   238 		 * no filter for modifying the opening and closing `<li>` for a menu item.
   243 		 * no filter for modifying the opening and closing `<li>` for a menu item.
   239 		 *
   244 		 *
   240 		 * @since 3.0.0
   245 		 * @since 3.0.0
   241 		 *
   246 		 *
   242 		 * @param string   $item_output The menu item's starting HTML output.
   247 		 * @param string   $item_output The menu item's starting HTML output.
   243 		 * @param WP_Post  $item        Menu item data object.
   248 		 * @param WP_Post  $menu_item   Menu item data object.
   244 		 * @param int      $depth       Depth of menu item. Used for padding.
   249 		 * @param int      $depth       Depth of menu item. Used for padding.
   245 		 * @param stdClass $args        An object of wp_nav_menu() arguments.
   250 		 * @param stdClass $args        An object of wp_nav_menu() arguments.
   246 		 */
   251 		 */
   247 		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
   252 		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $menu_item, $depth, $args );
   248 	}
   253 	}
   249 
   254 
   250 	/**
   255 	/**
   251 	 * Ends the element output, if needed.
   256 	 * Ends the element output, if needed.
   252 	 *
   257 	 *
   253 	 * @since 3.0.0
   258 	 * @since 3.0.0
       
   259 	 * @since 5.9.0 Renamed `$item` to `$data_object` to match parent class for PHP 8 named parameter support.
   254 	 *
   260 	 *
   255 	 * @see Walker::end_el()
   261 	 * @see Walker::end_el()
   256 	 *
   262 	 *
   257 	 * @param string   $output Used to append additional content (passed by reference).
   263 	 * @param string   $output      Used to append additional content (passed by reference).
   258 	 * @param WP_Post  $item   Page data object. Not used.
   264 	 * @param WP_Post  $data_object Menu item data object. Not used.
   259 	 * @param int      $depth  Depth of page. Not Used.
   265 	 * @param int      $depth       Depth of page. Not Used.
   260 	 * @param stdClass $args   An object of wp_nav_menu() arguments.
   266 	 * @param stdClass $args        An object of wp_nav_menu() arguments.
   261 	 */
   267 	 */
   262 	public function end_el( &$output, $item, $depth = 0, $args = null ) {
   268 	public function end_el( &$output, $data_object, $depth = 0, $args = null ) {
   263 		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
   269 		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
   264 			$t = '';
   270 			$t = '';
   265 			$n = '';
   271 			$n = '';
   266 		} else {
   272 		} else {
   267 			$t = "\t";
   273 			$t = "\t";