1<?php
2/**
3 * Post API: Walker_PageDropdown class
4 *
5 * @package WordPress
6 * @subpackage Post
7 * @since 4.4.0
8 */
9
10/**
11 * Core class used to create an HTML drop-down list of pages.
12 *
13 * @since 2.1.0
14 *
15 * @see Walker
16 */
17class Walker_PageDropdown 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 string[]
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 * Starts the element output.
45 *
46 * @since 2.1.0
47 * @since 5.9.0 Renamed `$page` to `$data_object` and `$id` to `$current_object_id`
48 * to match parent class for PHP 8 named parameter support.
49 *
50 * @see Walker::start_el()
51 *
52 * @param string $output Used to append additional content. Passed by reference.
53 * @param WP_Post $data_object Page data object.
54 * @param int $depth Optional. Depth of page in reference to parent pages.
55 * Used for padding. Default 0.
56 * @param array $args Optional. Uses 'selected' argument for selected page to
57 * set selected HTML attribute for option element. Uses
58 * 'value_field' argument to fill "value" attribute.
59 * See wp_dropdown_pages(). Default empty array.
60 * @param int $current_object_id Optional. ID of the current page. Default 0.
61 */
62 public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
63 // Restores the more descriptive, specific name for use within this method.
64 $page = $data_object;
65
66 $pad = str_repeat( ' ', $depth * 3 );
67
68 if ( ! isset( $args['value_field'] ) || ! isset( $page->{$args['value_field']} ) ) {
69 $args['value_field'] = 'ID';
70 }
71
72 $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $page->{$args['value_field']} ) . '"';
73 if ( $page->ID === (int) $args['selected'] ) {
74 $output .= ' selected="selected"';
75 }
76 $output .= '>';
77
78 $title = $page->post_title;
79 if ( '' === $title ) {
80 /* translators: %d: ID of a post. */
81 $title = sprintf( __( '#%d (no title)' ), $page->ID );
82 }
83
84 /**
85 * Filters the page title when creating an HTML drop-down list of pages.
86 *
87 * @since 3.1.0
88 *
89 * @param string $title Page title.
90 * @param WP_Post $page Page data object.
91 */
92 $title = apply_filters( 'list_pages', $title, $page );
93
94 $output .= $pad . esc_html( $title );
95 $output .= "</option>\n";
96 }
97}
98