wp/wp-includes/class-walker-page.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    28 
    28 
    29 	/**
    29 	/**
    30 	 * Database fields to use.
    30 	 * Database fields to use.
    31 	 *
    31 	 *
    32 	 * @since 2.1.0
    32 	 * @since 2.1.0
    33 	 * @var array
    33 	 * @var string[]
    34 	 *
    34 	 *
    35 	 * @see Walker::$db_fields
    35 	 * @see Walker::$db_fields
    36 	 * @todo Decouple this.
    36 	 * @todo Decouple this.
    37 	 */
    37 	 */
    38 	public $db_fields = array(
    38 	public $db_fields = array(
    91 	/**
    91 	/**
    92 	 * Outputs the beginning of the current element in the tree.
    92 	 * Outputs the beginning of the current element in the tree.
    93 	 *
    93 	 *
    94 	 * @see Walker::start_el()
    94 	 * @see Walker::start_el()
    95 	 * @since 2.1.0
    95 	 * @since 2.1.0
    96 	 *
    96 	 * @since 5.9.0 Renamed `$page` to `$data_object` and `$current_page` to `$current_object_id`
    97 	 * @param string  $output       Used to append additional content. Passed by reference.
    97 	 *              to match parent class for PHP 8 named parameter support.
    98 	 * @param WP_Post $page         Page data object.
    98 	 *
    99 	 * @param int     $depth        Optional. Depth of page. Used for padding. Default 0.
    99 	 * @param string  $output            Used to append additional content. Passed by reference.
   100 	 * @param array   $args         Optional. Array of arguments. Default empty array.
   100 	 * @param WP_Post $data_object       Page data object.
   101 	 * @param int     $current_page Optional. Page ID. Default 0.
   101 	 * @param int     $depth             Optional. Depth of page. Used for padding. Default 0.
   102 	 */
   102 	 * @param array   $args              Optional. Array of arguments. Default empty array.
   103 	public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
   103 	 * @param int     $current_object_id Optional. ID of the current page. Default 0.
       
   104 	 */
       
   105 	public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
       
   106 		// Restores the more descriptive, specific name for use within this method.
       
   107 		$page            = $data_object;
       
   108 		$current_page_id = $current_object_id;
       
   109 
   104 		if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
   110 		if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
   105 			$t = "\t";
   111 			$t = "\t";
   106 			$n = "\n";
   112 			$n = "\n";
   107 		} else {
   113 		} else {
   108 			$t = '';
   114 			$t = '';
   118 
   124 
   119 		if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
   125 		if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
   120 			$css_class[] = 'page_item_has_children';
   126 			$css_class[] = 'page_item_has_children';
   121 		}
   127 		}
   122 
   128 
   123 		if ( ! empty( $current_page ) ) {
   129 		if ( ! empty( $current_page_id ) ) {
   124 			$_current_page = get_post( $current_page );
   130 			$_current_page = get_post( $current_page_id );
   125 
   131 
   126 			if ( $_current_page && in_array( $page->ID, $_current_page->ancestors, true ) ) {
   132 			if ( $_current_page && in_array( $page->ID, $_current_page->ancestors, true ) ) {
   127 				$css_class[] = 'current_page_ancestor';
   133 				$css_class[] = 'current_page_ancestor';
   128 			}
   134 			}
   129 
   135 
   130 			if ( $page->ID == $current_page ) {
   136 			if ( $page->ID == $current_page_id ) {
   131 				$css_class[] = 'current_page_item';
   137 				$css_class[] = 'current_page_item';
   132 			} elseif ( $_current_page && $page->ID === $_current_page->post_parent ) {
   138 			} elseif ( $_current_page && $page->ID === $_current_page->post_parent ) {
   133 				$css_class[] = 'current_page_parent';
   139 				$css_class[] = 'current_page_parent';
   134 			}
   140 			}
   135 		} elseif ( get_option( 'page_for_posts' ) == $page->ID ) {
   141 		} elseif ( get_option( 'page_for_posts' ) == $page->ID ) {
   141 		 *
   147 		 *
   142 		 * @since 2.8.0
   148 		 * @since 2.8.0
   143 		 *
   149 		 *
   144 		 * @see wp_list_pages()
   150 		 * @see wp_list_pages()
   145 		 *
   151 		 *
   146 		 * @param string[] $css_class    An array of CSS classes to be applied to each list item.
   152 		 * @param string[] $css_class       An array of CSS classes to be applied to each list item.
   147 		 * @param WP_Post  $page         Page data object.
   153 		 * @param WP_Post  $page            Page data object.
   148 		 * @param int      $depth        Depth of page, used for padding.
   154 		 * @param int      $depth           Depth of page, used for padding.
   149 		 * @param array    $args         An array of arguments.
   155 		 * @param array    $args            An array of arguments.
   150 		 * @param int      $current_page ID of the current page.
   156 		 * @param int      $current_page_id ID of the current page.
   151 		 */
   157 		 */
   152 		$css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
   158 		$css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page_id ) );
   153 		$css_classes = $css_classes ? ' class="' . esc_attr( $css_classes ) . '"' : '';
   159 		$css_classes = $css_classes ? ' class="' . esc_attr( $css_classes ) . '"' : '';
   154 
   160 
   155 		if ( '' === $page->post_title ) {
   161 		if ( '' === $page->post_title ) {
   156 			/* translators: %d: ID of a post. */
   162 			/* translators: %d: ID of a post. */
   157 			$page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
   163 			$page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
   160 		$args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
   166 		$args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
   161 		$args['link_after']  = empty( $args['link_after'] ) ? '' : $args['link_after'];
   167 		$args['link_after']  = empty( $args['link_after'] ) ? '' : $args['link_after'];
   162 
   168 
   163 		$atts                 = array();
   169 		$atts                 = array();
   164 		$atts['href']         = get_permalink( $page->ID );
   170 		$atts['href']         = get_permalink( $page->ID );
   165 		$atts['aria-current'] = ( $page->ID == $current_page ) ? 'page' : '';
   171 		$atts['aria-current'] = ( $page->ID == $current_page_id ) ? 'page' : '';
   166 
   172 
   167 		/**
   173 		/**
   168 		 * Filters the HTML attributes applied to a page menu item's anchor element.
   174 		 * Filters the HTML attributes applied to a page menu item's anchor element.
   169 		 *
   175 		 *
   170 		 * @since 4.8.0
   176 		 * @since 4.8.0
   173 		 *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
   179 		 *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
   174 		 *
   180 		 *
   175 		 *     @type string $href         The href attribute.
   181 		 *     @type string $href         The href attribute.
   176 		 *     @type string $aria-current The aria-current attribute.
   182 		 *     @type string $aria-current The aria-current attribute.
   177 		 * }
   183 		 * }
   178 		 * @param WP_Post $page         Page data object.
   184 		 * @param WP_Post $page            Page data object.
   179 		 * @param int     $depth        Depth of page, used for padding.
   185 		 * @param int     $depth           Depth of page, used for padding.
   180 		 * @param array   $args         An array of arguments.
   186 		 * @param array   $args            An array of arguments.
   181 		 * @param int     $current_page ID of the current page.
   187 		 * @param int     $current_page_id ID of the current page.
   182 		 */
   188 		 */
   183 		$atts = apply_filters( 'page_menu_link_attributes', $atts, $page, $depth, $args, $current_page );
   189 		$atts = apply_filters( 'page_menu_link_attributes', $atts, $page, $depth, $args, $current_page_id );
   184 
   190 
   185 		$attributes = '';
   191 		$attributes = '';
   186 		foreach ( $atts as $attr => $value ) {
   192 		foreach ( $atts as $attr => $value ) {
   187 			if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
   193 			if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
   188 				$value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
   194 				$value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
   214 
   220 
   215 	/**
   221 	/**
   216 	 * Outputs the end of the current element in the tree.
   222 	 * Outputs the end of the current element in the tree.
   217 	 *
   223 	 *
   218 	 * @since 2.1.0
   224 	 * @since 2.1.0
       
   225 	 * @since 5.9.0 Renamed `$page` to `$data_object` to match parent class for PHP 8 named parameter support.
   219 	 *
   226 	 *
   220 	 * @see Walker::end_el()
   227 	 * @see Walker::end_el()
   221 	 *
   228 	 *
   222 	 * @param string  $output Used to append additional content. Passed by reference.
   229 	 * @param string  $output      Used to append additional content. Passed by reference.
   223 	 * @param WP_Post $page   Page data object. Not used.
   230 	 * @param WP_Post $data_object Page data object. Not used.
   224 	 * @param int     $depth  Optional. Depth of page. Default 0 (unused).
   231 	 * @param int     $depth       Optional. Depth of page. Default 0 (unused).
   225 	 * @param array   $args   Optional. Array of arguments. Default empty array.
   232 	 * @param array   $args        Optional. Array of arguments. Default empty array.
   226 	 */
   233 	 */
   227 	public function end_el( &$output, $page, $depth = 0, $args = array() ) {
   234 	public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {
   228 		if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
   235 		if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
   229 			$t = "\t";
   236 			$t = "\t";
   230 			$n = "\n";
   237 			$n = "\n";
   231 		} else {
   238 		} else {
   232 			$t = '';
   239 			$t = '';