]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-walker-page.php
WordPress 4.7-scripts
[autoinstalls/wordpress.git] / wp-includes / class-walker-page.php
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  */
17 class Walker_Page extends Walker {
18
19         /**
20          * What the class handles.
21          *
22          * @since 2.1.0
23          * @access public
24          * @var string
25          *
26          * @see Walker::$tree_type
27          */
28         public $tree_type = 'page';
29
30         /**
31          * Database fields to use.
32          *
33          * @since 2.1.0
34          * @access private
35          * @var array
36          *
37          * @see Walker::$db_fields
38          * @todo Decouple this.
39          */
40         public $db_fields = array( 'parent' => 'post_parent', 'id' => 'ID' );
41
42         /**
43          * Outputs the beginning of the current level in the tree before elements are output.
44          *
45          * @since 2.1.0
46          * @access public
47          *
48          * @see Walker::start_lvl()
49          *
50          * @param string $output Passed by reference. Used to append additional content.
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 ( '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          * @access public
72          *
73          * @see Walker::end_lvl()
74          *
75          * @param string $output Passed by reference. Used to append additional content.
76          * @param int    $depth  Optional. Depth of page. Used for padding. Default 0.
77          * @param array  $args   Optional. Arguments for outputting the end of the current level.
78          *                       Default empty array.
79          */
80         public function end_lvl( &$output, $depth = 0, $args = array() ) {
81                 if ( 'preserve' === $args['item_spacing'] ) {
82                         $t = "\t";
83                         $n = "\n";
84                 } else {
85                         $t = '';
86                         $n = '';
87                 }
88                 $indent = str_repeat( $t, $depth );
89                 $output .= "{$indent}</ul>{$n}";
90         }
91
92         /**
93          * Outputs the beginning of the current element in the tree.
94          *
95          * @see Walker::start_el()
96          * @since 2.1.0
97          * @access public
98          *
99          * @param string  $output       Used to append additional content. Passed by reference.
100          * @param WP_Post $page         Page data object.
101          * @param int     $depth        Optional. Depth of page. Used for padding. Default 0.
102          * @param array   $args         Optional. Array of arguments. Default empty array.
103          * @param int     $current_page Optional. Page ID. Default 0.
104          */
105         public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
106                 if ( 'preserve' === $args['item_spacing'] ) {
107                         $t = "\t";
108                         $n = "\n";
109                 } else {
110                         $t = '';
111                         $n = '';
112                 }
113                 if ( $depth ) {
114                         $indent = str_repeat( $t, $depth );
115                 } else {
116                         $indent = '';
117                 }
118
119                 $css_class = array( 'page_item', 'page-item-' . $page->ID );
120
121                 if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
122                         $css_class[] = 'page_item_has_children';
123                 }
124
125                 if ( ! empty( $current_page ) ) {
126                         $_current_page = get_post( $current_page );
127                         if ( $_current_page && in_array( $page->ID, $_current_page->ancestors ) ) {
128                                 $css_class[] = 'current_page_ancestor';
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 ( $page->ID == get_option('page_for_posts') ) {
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 array   $css_class    An array of CSS classes to be applied
147                  *                              to each list item.
148                  * @param WP_Post $page         Page data object.
149                  * @param int     $depth        Depth of page, used for padding.
150                  * @param array   $args         An array of arguments.
151                  * @param int     $current_page ID of the current page.
152                  */
153                 $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
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                 $output .= $indent . sprintf(
164                         '<li class="%s"><a href="%s">%s%s%s</a>',
165                         $css_classes,
166                         get_permalink( $page->ID ),
167                         $args['link_before'],
168                         /** This filter is documented in wp-includes/post-template.php */
169                         apply_filters( 'the_title', $page->post_title, $page->ID ),
170                         $args['link_after']
171                 );
172
173                 if ( ! empty( $args['show_date'] ) ) {
174                         if ( 'modified' == $args['show_date'] ) {
175                                 $time = $page->post_modified;
176                         } else {
177                                 $time = $page->post_date;
178                         }
179
180                         $date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
181                         $output .= " " . mysql2date( $date_format, $time );
182                 }
183         }
184
185         /**
186          * Outputs the end of the current element in the tree.
187          *
188          * @since 2.1.0
189          * @access public
190          *
191          * @see Walker::end_el()
192          *
193          * @param string  $output Used to append additional content. Passed by reference.
194          * @param WP_Post $page   Page data object. Not used.
195          * @param int     $depth  Optional. Depth of page. Default 0 (unused).
196          * @param array   $args   Optional. Array of arguments. Default empty array.
197          */
198         public function end_el( &$output, $page, $depth = 0, $args = array() ) {
199                 if ( 'preserve' === $args['item_spacing'] ) {
200                         $t = "\t";
201                         $n = "\n";
202                 } else {
203                         $t = '';
204                         $n = '';
205                 }
206                 $output .= "</li>{$n}";
207         }
208
209 }