wp/wp-includes/class-walker-nav-menu.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    71 		 * @param string[] $classes Array of the CSS classes that are applied to the menu `<ul>` element.
    71 		 * @param string[] $classes Array of the CSS classes that are applied to the menu `<ul>` element.
    72 		 * @param stdClass $args    An object of `wp_nav_menu()` arguments.
    72 		 * @param stdClass $args    An object of `wp_nav_menu()` arguments.
    73 		 * @param int      $depth   Depth of menu item. Used for padding.
    73 		 * @param int      $depth   Depth of menu item. Used for padding.
    74 		 */
    74 		 */
    75 		$class_names = implode( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
    75 		$class_names = implode( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
    76 		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
    76 
    77 
    77 		$atts          = array();
    78 		$output .= "{$n}{$indent}<ul$class_names>{$n}";
    78 		$atts['class'] = ! empty( $class_names ) ? $class_names : '';
       
    79 
       
    80 		/**
       
    81 		 * Filters the HTML attributes applied to a menu list element.
       
    82 		 *
       
    83 		 * @since 6.3.0
       
    84 		 *
       
    85 		 * @param array $atts {
       
    86 		 *     The HTML attributes applied to the `<ul>` element, empty strings are ignored.
       
    87 		 *
       
    88 		 *     @type string $class    HTML CSS class attribute.
       
    89 		 * }
       
    90 		 * @param stdClass $args      An object of `wp_nav_menu()` arguments.
       
    91 		 * @param int      $depth     Depth of menu item. Used for padding.
       
    92 		 */
       
    93 		$atts       = apply_filters( 'nav_menu_submenu_attributes', $atts, $args, $depth );
       
    94 		$attributes = $this->build_atts( $atts );
       
    95 
       
    96 		$output .= "{$n}{$indent}<ul{$attributes}>{$n}";
    79 	}
    97 	}
    80 
    98 
    81 	/**
    99 	/**
    82 	 * Ends the list of after the elements are added.
   100 	 * Ends the list of after the elements are added.
    83 	 *
   101 	 *
   154 		 * @param WP_Post  $menu_item The current menu item object.
   172 		 * @param WP_Post  $menu_item The current menu item object.
   155 		 * @param stdClass $args      An object of wp_nav_menu() arguments.
   173 		 * @param stdClass $args      An object of wp_nav_menu() arguments.
   156 		 * @param int      $depth     Depth of menu item. Used for padding.
   174 		 * @param int      $depth     Depth of menu item. Used for padding.
   157 		 */
   175 		 */
   158 		$class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $menu_item, $args, $depth ) );
   176 		$class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $menu_item, $args, $depth ) );
   159 		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
   177 
   160 
   178 		/**
   161 		/**
   179 		 * Filters the ID attribute applied to a menu item's list item element.
   162 		 * Filters the ID applied to a menu item's list item element.
       
   163 		 *
   180 		 *
   164 		 * @since 3.0.1
   181 		 * @since 3.0.1
   165 		 * @since 4.1.0 The `$depth` parameter was added.
   182 		 * @since 4.1.0 The `$depth` parameter was added.
   166 		 *
   183 		 *
   167 		 * @param string   $menu_id   The ID that is applied to the menu item's `<li>` element.
   184 		 * @param string   $menu_item_id The ID attribute applied to the menu item's `<li>` element.
   168 		 * @param WP_Post  $menu_item The current menu item.
   185 		 * @param WP_Post  $menu_item    The current menu item.
   169 		 * @param stdClass $args      An object of wp_nav_menu() arguments.
   186 		 * @param stdClass $args         An object of wp_nav_menu() arguments.
   170 		 * @param int      $depth     Depth of menu item. Used for padding.
   187 		 * @param int      $depth        Depth of menu item. Used for padding.
   171 		 */
   188 		 */
   172 		$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $menu_item->ID, $menu_item, $args, $depth );
   189 		$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $menu_item->ID, $menu_item, $args, $depth );
   173 		$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
   190 
   174 
   191 		$li_atts          = array();
   175 		$output .= $indent . '<li' . $id . $class_names . '>';
   192 		$li_atts['id']    = ! empty( $id ) ? $id : '';
       
   193 		$li_atts['class'] = ! empty( $class_names ) ? $class_names : '';
       
   194 
       
   195 		/**
       
   196 		 * Filters the HTML attributes applied to a menu's list item element.
       
   197 		 *
       
   198 		 * @since 6.3.0
       
   199 		 *
       
   200 		 * @param array $li_atts {
       
   201 		 *     The HTML attributes applied to the menu item's `<li>` element, empty strings are ignored.
       
   202 		 *
       
   203 		 *     @type string $class        HTML CSS class attribute.
       
   204 		 *     @type string $id           HTML id attribute.
       
   205 		 * }
       
   206 		 * @param WP_Post  $menu_item The current menu item object.
       
   207 		 * @param stdClass $args      An object of wp_nav_menu() arguments.
       
   208 		 * @param int      $depth     Depth of menu item. Used for padding.
       
   209 		 */
       
   210 		$li_atts       = apply_filters( 'nav_menu_item_attributes', $li_atts, $menu_item, $args, $depth );
       
   211 		$li_attributes = $this->build_atts( $li_atts );
       
   212 
       
   213 		$output .= $indent . '<li' . $li_attributes . '>';
   176 
   214 
   177 		$atts           = array();
   215 		$atts           = array();
   178 		$atts['title']  = ! empty( $menu_item->attr_title ) ? $menu_item->attr_title : '';
   216 		$atts['title']  = ! empty( $menu_item->attr_title ) ? $menu_item->attr_title : '';
   179 		$atts['target'] = ! empty( $menu_item->target ) ? $menu_item->target : '';
   217 		$atts['target'] = ! empty( $menu_item->target ) ? $menu_item->target : '';
   180 		if ( '_blank' === $menu_item->target && empty( $menu_item->xfn ) ) {
   218 		if ( '_blank' === $menu_item->target && empty( $menu_item->xfn ) ) {
   181 			$atts['rel'] = 'noopener';
   219 			$atts['rel'] = 'noopener';
   182 		} else {
   220 		} else {
   183 			$atts['rel'] = $menu_item->xfn;
   221 			$atts['rel'] = $menu_item->xfn;
   184 		}
   222 		}
   185 		$atts['href']         = ! empty( $menu_item->url ) ? $menu_item->url : '';
   223 
       
   224 		if ( ! empty( $menu_item->url ) ) {
       
   225 			if ( get_privacy_policy_url() === $menu_item->url ) {
       
   226 				$atts['rel'] = empty( $atts['rel'] ) ? 'privacy-policy' : $atts['rel'] . ' privacy-policy';
       
   227 			}
       
   228 
       
   229 			$atts['href'] = $menu_item->url;
       
   230 		} else {
       
   231 			$atts['href'] = '';
       
   232 		}
       
   233 
   186 		$atts['aria-current'] = $menu_item->current ? 'page' : '';
   234 		$atts['aria-current'] = $menu_item->current ? 'page' : '';
   187 
   235 
   188 		/**
   236 		/**
   189 		 * Filters the HTML attributes applied to a menu item's anchor element.
   237 		 * Filters the HTML attributes applied to a menu item's anchor element.
   190 		 *
   238 		 *
   202 		 * }
   250 		 * }
   203 		 * @param WP_Post  $menu_item The current menu item object.
   251 		 * @param WP_Post  $menu_item The current menu item object.
   204 		 * @param stdClass $args      An object of wp_nav_menu() arguments.
   252 		 * @param stdClass $args      An object of wp_nav_menu() arguments.
   205 		 * @param int      $depth     Depth of menu item. Used for padding.
   253 		 * @param int      $depth     Depth of menu item. Used for padding.
   206 		 */
   254 		 */
   207 		$atts = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth );
   255 		$atts       = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth );
   208 
   256 		$attributes = $this->build_atts( $atts );
   209 		$attributes = '';
       
   210 		foreach ( $atts as $attr => $value ) {
       
   211 			if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
       
   212 				$value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
       
   213 				$attributes .= ' ' . $attr . '="' . $value . '"';
       
   214 			}
       
   215 		}
       
   216 
   257 
   217 		/** This filter is documented in wp-includes/post-template.php */
   258 		/** This filter is documented in wp-includes/post-template.php */
   218 		$title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
   259 		$title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
   219 
   260 
   220 		/**
   261 		/**
   274 			$n = "\n";
   315 			$n = "\n";
   275 		}
   316 		}
   276 		$output .= "</li>{$n}";
   317 		$output .= "</li>{$n}";
   277 	}
   318 	}
   278 
   319 
       
   320 	/**
       
   321 	 * Builds a string of HTML attributes from an array of key/value pairs.
       
   322 	 * Empty values are ignored.
       
   323 	 *
       
   324 	 * @since 6.3.0
       
   325 	 *
       
   326 	 * @param  array $atts Optional. An array of HTML attribute key/value pairs. Default empty array.
       
   327 	 * @return string A string of HTML attributes.
       
   328 	 */
       
   329 	protected function build_atts( $atts = array() ) {
       
   330 		$attribute_string = '';
       
   331 		foreach ( $atts as $attr => $value ) {
       
   332 			if ( false !== $value && '' !== $value && is_scalar( $value ) ) {
       
   333 				$value             = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
       
   334 				$attribute_string .= ' ' . $attr . '="' . $value . '"';
       
   335 			}
       
   336 		}
       
   337 		return $attribute_string;
       
   338 	}
   279 }
   339 }