1<?php
2/**
3 * Post API: Walker_Page class
4 *
5 * @package WordPress
6 * @subpackage Template
7 * @since 4.4.0
8 */
9
10/**
11 * Core walker class used to create an HTML list of pages.
12 *
13 * @since 2.1.0
14 *
15 * @see Walker
16 */
17class Walker_Page extends Walker {
18
19	/**
20	 * What the class handles.
21	 *
22	 * @since 2.1.0
23	 * @var string
24	 *
25	 * @see Walker::$tree_type
26	 */
27	public $tree_type = 'page';
28
29	/**
30	 * Database fields to use.
31	 *
32	 * @since 2.1.0
33	 * @var array
34	 *
35	 * @see Walker::$db_fields
36	 * @todo Decouple this.
37	 */
38	public $db_fields = array(
39		'parent' => 'post_parent',
40		'id'     => 'ID',
41	);
42
43	/**
44	 * Outputs the beginning of the current level in the tree before elements are output.
45	 *
46	 * @since 2.1.0
47	 *
48	 * @see Walker::start_lvl()
49	 *
50	 * @param string $output Used to append additional content (passed by reference).
51	 * @param int    $depth  Optional. Depth of page. Used for padding. Default 0.
52	 * @param array  $args   Optional. Arguments for outputting the next level.
53	 *                       Default empty array.
54	 */
55	public function start_lvl( &$output, $depth = 0, $args = array() ) {
56		if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
57			$t = "\t";
58			$n = "\n";
59		} else {
60			$t = '';
61			$n = '';
62		}
63		$indent  = str_repeat( $t, $depth );
64		$output .= "{$n}{$indent}<ul class='children'>{$n}";
65	}
66
67	/**
68	 * Outputs the end of the current level in the tree after elements are output.
69	 *
70	 * @since 2.1.0
71	 *
72	 * @see Walker::end_lvl()
73	 *
74	 * @param string $output Used to append additional content (passed by reference).
75	 * @param int    $depth  Optional. Depth of page. Used for padding. Default 0.
76	 * @param array  $args   Optional. Arguments for outputting the end of the current level.
77	 *                       Default empty array.
78	 */
79	public function end_lvl( &$output, $depth = 0, $args = array() ) {
80		if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
81			$t = "\t";
82			$n = "\n";
83		} else {
84			$t = '';
85			$n = '';
86		}
87		$indent  = str_repeat( $t, $depth );
88		$output .= "{$indent}</ul>{$n}";
89	}
90
91	/**
92	 * Outputs the beginning of the current element in the tree.
93	 *
94	 * @see Walker::start_el()
95	 * @since 2.1.0
96	 *
97	 * @param string  $output       Used to append additional content. Passed by reference.
98	 * @param WP_Post $page         Page data object.
99	 * @param int     $depth        Optional. Depth of page. Used for padding. Default 0.
100	 * @param array   $args         Optional. Array of arguments. Default empty array.
101	 * @param int     $current_page Optional. Page ID. Default 0.
102	 */
103	public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
104		if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
105			$t = "\t";
106			$n = "\n";
107		} else {
108			$t = '';
109			$n = '';
110		}
111		if ( $depth ) {
112			$indent = str_repeat( $t, $depth );
113		} else {
114			$indent = '';
115		}
116
117		$css_class = array( 'page_item', 'page-item-' . $page->ID );
118
119		if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
120			$css_class[] = 'page_item_has_children';
121		}
122
123		if ( ! empty( $current_page ) ) {
124			$_current_page = get_post( $current_page );
125
126			if ( $_current_page && in_array( $page->ID, $_current_page->ancestors, true ) ) {
127				$css_class[] = 'current_page_ancestor';
128			}
129
130			if ( $page->ID == $current_page ) {
131				$css_class[] = 'current_page_item';
132			} elseif ( $_current_page && $page->ID === $_current_page->post_parent ) {
133				$css_class[] = 'current_page_parent';
134			}
135		} elseif ( get_option( 'page_for_posts' ) == $page->ID ) {
136			$css_class[] = 'current_page_parent';
137		}
138
139		/**
140		 * Filters the list of CSS classes to include with each page item in the list.
141		 *
142		 * @since 2.8.0
143		 *
144		 * @see wp_list_pages()
145		 *
146		 * @param string[] $css_class    An array of CSS classes to be applied to each list item.
147		 * @param WP_Post  $page         Page data object.
148		 * @param int      $depth        Depth of page, used for padding.
149		 * @param array    $args         An array of arguments.
150		 * @param int      $current_page ID of the current page.
151		 */
152		$css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
153		$css_classes = $css_classes ? ' class="' . esc_attr( $css_classes ) . '"' : '';
154
155		if ( '' === $page->post_title ) {
156			/* translators: %d: ID of a post. */
157			$page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
158		}
159
160		$args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
161		$args['link_after']  = empty( $args['link_after'] ) ? '' : $args['link_after'];
162
163		$atts                 = array();
164		$atts['href']         = get_permalink( $page->ID );
165		$atts['aria-current'] = ( $page->ID == $current_page ) ? 'page' : '';
166
167		/**
168		 * Filters the HTML attributes applied to a page menu item's anchor element.
169		 *
170		 * @since 4.8.0
171		 *
172		 * @param array $atts {
173		 *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
174		 *
175		 *     @type string $href         The href attribute.
176		 *     @type string $aria-current The aria-current attribute.
177		 * }
178		 * @param WP_Post $page         Page data object.
179		 * @param int     $depth        Depth of page, used for padding.
180		 * @param array   $args         An array of arguments.
181		 * @param int     $current_page ID of the current page.
182		 */
183		$atts = apply_filters( 'page_menu_link_attributes', $atts, $page, $depth, $args, $current_page );
184
185		$attributes = '';
186		foreach ( $atts as $attr => $value ) {
187			if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
188				$value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
189				$attributes .= ' ' . $attr . '="' . $value . '"';
190			}
191		}
192
193		$output .= $indent . sprintf(
194			'<li%s><a%s>%s%s%s</a>',
195			$css_classes,
196			$attributes,
197			$args['link_before'],
198			/** This filter is documented in wp-includes/post-template.php */
199			apply_filters( 'the_title', $page->post_title, $page->ID ),
200			$args['link_after']
201		);
202
203		if ( ! empty( $args['show_date'] ) ) {
204			if ( 'modified' === $args['show_date'] ) {
205				$time = $page->post_modified;
206			} else {
207				$time = $page->post_date;
208			}
209
210			$date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
211			$output     .= ' ' . mysql2date( $date_format, $time );
212		}
213	}
214
215	/**
216	 * Outputs the end of the current element in the tree.
217	 *
218	 * @since 2.1.0
219	 *
220	 * @see Walker::end_el()
221	 *
222	 * @param string  $output Used to append additional content. Passed by reference.
223	 * @param WP_Post $page   Page data object. Not used.
224	 * @param int     $depth  Optional. Depth of page. Default 0 (unused).
225	 * @param array   $args   Optional. Array of arguments. Default empty array.
226	 */
227	public function end_el( &$output, $page, $depth = 0, $args = array() ) {
228		if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
229			$t = "\t";
230			$n = "\n";
231		} else {
232			$t = '';
233			$n = '';
234		}
235		$output .= "</li>{$n}";
236	}
237
238}
239