]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/post-template.php
WordPress 4.5.1
[autoinstalls/wordpress.git] / wp-includes / post-template.php
1 <?php
2 /**
3  * WordPress Post Template Functions.
4  *
5  * Gets content for the current post in the loop.
6  *
7  * @package WordPress
8  * @subpackage Template
9  */
10
11 /**
12  * Display the ID of the current item in the WordPress Loop.
13  *
14  * @since 0.71
15  */
16 function the_ID() {
17         echo get_the_ID();
18 }
19
20 /**
21  * Retrieve the ID of the current item in the WordPress Loop.
22  *
23  * @since 2.1.0
24  *
25  * @return int|false The ID of the current item in the WordPress Loop. False if $post is not set.
26  */
27 function get_the_ID() {
28         $post = get_post();
29         return ! empty( $post ) ? $post->ID : false;
30 }
31
32 /**
33  * Display or retrieve the current post title with optional content.
34  *
35  * @since 0.71
36  *
37  * @param string $before Optional. Content to prepend to the title.
38  * @param string $after  Optional. Content to append to the title.
39  * @param bool   $echo   Optional, default to true.Whether to display or return.
40  * @return string|void String if $echo parameter is false.
41  */
42 function the_title( $before = '', $after = '', $echo = true ) {
43         $title = get_the_title();
44
45         if ( strlen($title) == 0 )
46                 return;
47
48         $title = $before . $title . $after;
49
50         if ( $echo )
51                 echo $title;
52         else
53                 return $title;
54 }
55
56 /**
57  * Sanitize the current title when retrieving or displaying.
58  *
59  * Works like {@link the_title()}, except the parameters can be in a string or
60  * an array. See the function for what can be override in the $args parameter.
61  *
62  * The title before it is displayed will have the tags stripped and {@link
63  * esc_attr()} before it is passed to the user or displayed. The default
64  * as with {@link the_title()}, is to display the title.
65  *
66  * @since 2.3.0
67  *
68  * @param string|array $args {
69  *     Title attribute arguments. Optional.
70  *
71  *     @type string  $before Markup to prepend to the title. Default empty.
72  *     @type string  $after  Markup to append to the title. Default empty.
73  *     @type bool    $echo   Whether to echo or return the title. Default true for echo.
74  *     @type WP_Post $post   Current post object to retrieve the title for.
75  * }
76  * @return string|void String when echo is false.
77  */
78 function the_title_attribute( $args = '' ) {
79         $defaults = array( 'before' => '', 'after' =>  '', 'echo' => true, 'post' => get_post() );
80         $r = wp_parse_args( $args, $defaults );
81
82         $title = get_the_title( $r['post'] );
83
84         if ( strlen( $title ) == 0 ) {
85                 return;
86         }
87
88         $title = $r['before'] . $title . $r['after'];
89         $title = esc_attr( strip_tags( $title ) );
90
91         if ( $r['echo'] ) {
92                 echo $title;
93         } else {
94                 return $title;
95         }
96 }
97
98 /**
99  * Retrieve post title.
100  *
101  * If the post is protected and the visitor is not an admin, then "Protected"
102  * will be displayed before the post title. If the post is private, then
103  * "Private" will be located before the post title.
104  *
105  * @since 0.71
106  *
107  * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
108  * @return string
109  */
110 function get_the_title( $post = 0 ) {
111         $post = get_post( $post );
112
113         $title = isset( $post->post_title ) ? $post->post_title : '';
114         $id = isset( $post->ID ) ? $post->ID : 0;
115
116         if ( ! is_admin() ) {
117                 if ( ! empty( $post->post_password ) ) {
118
119                         /**
120                          * Filter the text prepended to the post title for protected posts.
121                          *
122                          * The filter is only applied on the front end.
123                          *
124                          * @since 2.8.0
125                          *
126                          * @param string  $prepend Text displayed before the post title.
127                          *                         Default 'Protected: %s'.
128                          * @param WP_Post $post    Current post object.
129                          */
130                         $protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ), $post );
131                         $title = sprintf( $protected_title_format, $title );
132                 } elseif ( isset( $post->post_status ) && 'private' == $post->post_status ) {
133
134                         /**
135                          * Filter the text prepended to the post title of private posts.
136                          *
137                          * The filter is only applied on the front end.
138                          *
139                          * @since 2.8.0
140                          *
141                          * @param string  $prepend Text displayed before the post title.
142                          *                         Default 'Private: %s'.
143                          * @param WP_Post $post    Current post object.
144                          */
145                         $private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ), $post );
146                         $title = sprintf( $private_title_format, $title );
147                 }
148         }
149
150         /**
151          * Filter the post title.
152          *
153          * @since 0.71
154          *
155          * @param string $title The post title.
156          * @param int    $id    The post ID.
157          */
158         return apply_filters( 'the_title', $title, $id );
159 }
160
161 /**
162  * Display the Post Global Unique Identifier (guid).
163  *
164  * The guid will appear to be a link, but should not be used as a link to the
165  * post. The reason you should not use it as a link, is because of moving the
166  * blog across domains.
167  *
168  * URL is escaped to make it XML-safe.
169  *
170  * @since 1.5.0
171  *
172  * @param int|WP_Post $post Optional. Post ID or post object. Default is global $post.
173  */
174 function the_guid( $post = 0 ) {
175         $post = get_post( $post );
176
177         $guid = isset( $post->guid ) ? get_the_guid( $post ) : '';
178         $id   = isset( $post->ID ) ? $post->ID : 0;
179
180         /**
181          * Filter the escaped Global Unique Identifier (guid) of the post.
182          *
183          * @since 4.2.0
184          *
185          * @see get_the_guid()
186          *
187          * @param string $guid Escaped Global Unique Identifier (guid) of the post.
188          * @param int    $id   The post ID.
189          */
190         echo apply_filters( 'the_guid', $guid, $id );
191 }
192
193 /**
194  * Retrieve the Post Global Unique Identifier (guid).
195  *
196  * The guid will appear to be a link, but should not be used as an link to the
197  * post. The reason you should not use it as a link, is because of moving the
198  * blog across domains.
199  *
200  * @since 1.5.0
201  *
202  * @param int|WP_Post $post Optional. Post ID or post object. Default is global $post.
203  * @return string
204  */
205 function get_the_guid( $post = 0 ) {
206         $post = get_post( $post );
207
208         $guid = isset( $post->guid ) ? $post->guid : '';
209         $id   = isset( $post->ID ) ? $post->ID : 0;
210
211         /**
212          * Filter the Global Unique Identifier (guid) of the post.
213          *
214          * @since 1.5.0
215          *
216          * @param string $guid Global Unique Identifier (guid) of the post.
217          * @param int    $id   The post ID.
218          */
219         return apply_filters( 'get_the_guid', $guid, $id );
220 }
221
222 /**
223  * Display the post content.
224  *
225  * @since 0.71
226  *
227  * @param string $more_link_text Optional. Content for when there is more text.
228  * @param bool   $strip_teaser   Optional. Strip teaser content before the more text. Default is false.
229  */
230 function the_content( $more_link_text = null, $strip_teaser = false) {
231         $content = get_the_content( $more_link_text, $strip_teaser );
232
233         /**
234          * Filter the post content.
235          *
236          * @since 0.71
237          *
238          * @param string $content Content of the current post.
239          */
240         $content = apply_filters( 'the_content', $content );
241         $content = str_replace( ']]>', ']]&gt;', $content );
242         echo $content;
243 }
244
245 /**
246  * Retrieve the post content.
247  *
248  * @since 0.71
249  *
250  * @global int   $page
251  * @global int   $more
252  * @global bool  $preview
253  * @global array $pages
254  * @global int   $multipage
255  *
256  * @param string $more_link_text Optional. Content for when there is more text.
257  * @param bool   $strip_teaser   Optional. Strip teaser content before the more text. Default is false.
258  * @return string
259  */
260 function get_the_content( $more_link_text = null, $strip_teaser = false ) {
261         global $page, $more, $preview, $pages, $multipage;
262
263         $post = get_post();
264
265         if ( null === $more_link_text )
266                 $more_link_text = __( '(more&hellip;)' );
267
268         $output = '';
269         $has_teaser = false;
270
271         // If post password required and it doesn't match the cookie.
272         if ( post_password_required( $post ) )
273                 return get_the_password_form( $post );
274
275         if ( $page > count( $pages ) ) // if the requested page doesn't exist
276                 $page = count( $pages ); // give them the highest numbered page that DOES exist
277
278         $content = $pages[$page - 1];
279         if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
280                 $content = explode( $matches[0], $content, 2 );
281                 if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) )
282                         $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
283
284                 $has_teaser = true;
285         } else {
286                 $content = array( $content );
287         }
288
289         if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page == 1 ) )
290                 $strip_teaser = true;
291
292         $teaser = $content[0];
293
294         if ( $more && $strip_teaser && $has_teaser )
295                 $teaser = '';
296
297         $output .= $teaser;
298
299         if ( count( $content ) > 1 ) {
300                 if ( $more ) {
301                         $output .= '<span id="more-' . $post->ID . '"></span>' . $content[1];
302                 } else {
303                         if ( ! empty( $more_link_text ) )
304
305                                 /**
306                                  * Filter the Read More link text.
307                                  *
308                                  * @since 2.8.0
309                                  *
310                                  * @param string $more_link_element Read More link element.
311                                  * @param string $more_link_text    Read More text.
312                                  */
313                                 $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );
314                         $output = force_balance_tags( $output );
315                 }
316         }
317
318         if ( $preview ) // Preview fix for JavaScript bug with foreign languages.
319                 $output =       preg_replace_callback( '/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output );
320
321         return $output;
322 }
323
324 /**
325  * Preview fix for JavaScript bug with foreign languages.
326  *
327  * @since 3.1.0
328  * @access private
329  *
330  * @param array $match Match array from preg_replace_callback.
331  * @return string
332  */
333 function _convert_urlencoded_to_entities( $match ) {
334         return '&#' . base_convert( $match[1], 16, 10 ) . ';';
335 }
336
337 /**
338  * Display the post excerpt.
339  *
340  * @since 0.71
341  */
342 function the_excerpt() {
343
344         /**
345          * Filter the displayed post excerpt.
346          *
347          * @since 0.71
348          *
349          * @see get_the_excerpt()
350          *
351          * @param string $post_excerpt The post excerpt.
352          */
353         echo apply_filters( 'the_excerpt', get_the_excerpt() );
354 }
355
356 /**
357  * Retrieves the post excerpt.
358  *
359  * @since 0.71
360  * @since 4.5.0 Introduced the `$post` parameter.
361  *
362  * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
363  * @return string Post excerpt.
364  */
365 function get_the_excerpt( $post = null ) {
366         if ( is_bool( $post ) ) {
367                 _deprecated_argument( __FUNCTION__, '2.3' );
368         }
369
370         $post = get_post( $post );
371         if ( empty( $post ) ) {
372                 return '';
373         }
374
375         if ( post_password_required( $post ) ) {
376                 return __( 'There is no excerpt because this is a protected post.' );
377         }
378
379         /**
380          * Filter the retrieved post excerpt.
381          *
382          * @since 1.2.0
383          * @since 4.5.0 Introduced the `$post` parameter.
384          *
385          * @param string $post_excerpt The post excerpt.
386          * @param WP_Post $post Post object.
387          */
388         return apply_filters( 'get_the_excerpt', $post->post_excerpt, $post );
389 }
390
391 /**
392  * Whether post has excerpt.
393  *
394  * @since 2.3.0
395  *
396  * @param int|WP_Post $id Optional. Post ID or post object.
397  * @return bool
398  */
399 function has_excerpt( $id = 0 ) {
400         $post = get_post( $id );
401         return ( !empty( $post->post_excerpt ) );
402 }
403
404 /**
405  * Display the classes for the post div.
406  *
407  * @since 2.7.0
408  *
409  * @param string|array $class   One or more classes to add to the class list.
410  * @param int|WP_Post  $post_id Optional. Post ID or post object. Defaults to the global `$post`.
411  */
412 function post_class( $class = '', $post_id = null ) {
413         // Separates classes with a single space, collates classes for post DIV
414         echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';
415 }
416
417 /**
418  * Retrieve the classes for the post div as an array.
419  *
420  * The class names are many. If the post is a sticky, then the 'sticky'
421  * class name. The class 'hentry' is always added to each post. If the post has a
422  * post thumbnail, 'has-post-thumbnail' is added as a class. For each taxonomy that
423  * the post belongs to, a class will be added of the format '{$taxonomy}-{$slug}' -
424  * eg 'category-foo' or 'my_custom_taxonomy-bar'. The 'post_tag' taxonomy is a special
425  * case; the class has the 'tag-' prefix instead of 'post_tag-'. All classes are
426  * passed through the filter, 'post_class' with the list of classes, followed by
427  * $class parameter value, with the post ID as the last parameter.
428  *
429  * @since 2.7.0
430  * @since 4.2.0 Custom taxonomy classes were added.
431  *
432  * @param string|array $class   One or more classes to add to the class list.
433  * @param int|WP_Post  $post_id Optional. Post ID or post object.
434  * @return array Array of classes.
435  */
436 function get_post_class( $class = '', $post_id = null ) {
437         $post = get_post( $post_id );
438
439         $classes = array();
440
441         if ( $class ) {
442                 if ( ! is_array( $class ) ) {
443                         $class = preg_split( '#\s+#', $class );
444                 }
445                 $classes = array_map( 'esc_attr', $class );
446         } else {
447                 // Ensure that we always coerce class to being an array.
448                 $class = array();
449         }
450
451         if ( ! $post ) {
452                 return $classes;
453         }
454
455         $classes[] = 'post-' . $post->ID;
456         if ( ! is_admin() )
457                 $classes[] = $post->post_type;
458         $classes[] = 'type-' . $post->post_type;
459         $classes[] = 'status-' . $post->post_status;
460
461         // Post Format
462         if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
463                 $post_format = get_post_format( $post->ID );
464
465                 if ( $post_format && !is_wp_error($post_format) )
466                         $classes[] = 'format-' . sanitize_html_class( $post_format );
467                 else
468                         $classes[] = 'format-standard';
469         }
470
471         $post_password_required = post_password_required( $post->ID );
472
473         // Post requires password.
474         if ( $post_password_required ) {
475                 $classes[] = 'post-password-required';
476         } elseif ( ! empty( $post->post_password ) ) {
477                 $classes[] = 'post-password-protected';
478         }
479
480         // Post thumbnails.
481         if ( current_theme_supports( 'post-thumbnails' ) && has_post_thumbnail( $post->ID ) && ! is_attachment( $post ) && ! $post_password_required ) {
482                 $classes[] = 'has-post-thumbnail';
483         }
484
485         // sticky for Sticky Posts
486         if ( is_sticky( $post->ID ) ) {
487                 if ( is_home() && ! is_paged() ) {
488                         $classes[] = 'sticky';
489                 } elseif ( is_admin() ) {
490                         $classes[] = 'status-sticky';
491                 }
492         }
493
494         // hentry for hAtom compliance
495         $classes[] = 'hentry';
496
497         // All public taxonomies
498         $taxonomies = get_taxonomies( array( 'public' => true ) );
499         foreach ( (array) $taxonomies as $taxonomy ) {
500                 if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
501                         foreach ( (array) get_the_terms( $post->ID, $taxonomy ) as $term ) {
502                                 if ( empty( $term->slug ) ) {
503                                         continue;
504                                 }
505
506                                 $term_class = sanitize_html_class( $term->slug, $term->term_id );
507                                 if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
508                                         $term_class = $term->term_id;
509                                 }
510
511                                 // 'post_tag' uses the 'tag' prefix for backward compatibility.
512                                 if ( 'post_tag' == $taxonomy ) {
513                                         $classes[] = 'tag-' . $term_class;
514                                 } else {
515                                         $classes[] = sanitize_html_class( $taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id );
516                                 }
517                         }
518                 }
519         }
520
521         $classes = array_map( 'esc_attr', $classes );
522
523         /**
524          * Filter the list of CSS classes for the current post.
525          *
526          * @since 2.7.0
527          *
528          * @param array $classes An array of post classes.
529          * @param array $class   An array of additional classes added to the post.
530          * @param int   $post_id The post ID.
531          */
532         $classes = apply_filters( 'post_class', $classes, $class, $post->ID );
533
534         return array_unique( $classes );
535 }
536
537 /**
538  * Display the classes for the body element.
539  *
540  * @since 2.8.0
541  *
542  * @param string|array $class One or more classes to add to the class list.
543  */
544 function body_class( $class = '' ) {
545         // Separates classes with a single space, collates classes for body element
546         echo 'class="' . join( ' ', get_body_class( $class ) ) . '"';
547 }
548
549 /**
550  * Retrieve the classes for the body element as an array.
551  *
552  * @since 2.8.0
553  *
554  * @global WP_Query $wp_query
555  *
556  * @param string|array $class One or more classes to add to the class list.
557  * @return array Array of classes.
558  */
559 function get_body_class( $class = '' ) {
560         global $wp_query;
561
562         $classes = array();
563
564         if ( is_rtl() )
565                 $classes[] = 'rtl';
566
567         if ( is_front_page() )
568                 $classes[] = 'home';
569         if ( is_home() )
570                 $classes[] = 'blog';
571         if ( is_archive() )
572                 $classes[] = 'archive';
573         if ( is_date() )
574                 $classes[] = 'date';
575         if ( is_search() ) {
576                 $classes[] = 'search';
577                 $classes[] = $wp_query->posts ? 'search-results' : 'search-no-results';
578         }
579         if ( is_paged() )
580                 $classes[] = 'paged';
581         if ( is_attachment() )
582                 $classes[] = 'attachment';
583         if ( is_404() )
584                 $classes[] = 'error404';
585
586         if ( is_single() ) {
587                 $post_id = $wp_query->get_queried_object_id();
588                 $post = $wp_query->get_queried_object();
589
590                 $classes[] = 'single';
591                 if ( isset( $post->post_type ) ) {
592                         $classes[] = 'single-' . sanitize_html_class($post->post_type, $post_id);
593                         $classes[] = 'postid-' . $post_id;
594
595                         // Post Format
596                         if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
597                                 $post_format = get_post_format( $post->ID );
598
599                                 if ( $post_format && !is_wp_error($post_format) )
600                                         $classes[] = 'single-format-' . sanitize_html_class( $post_format );
601                                 else
602                                         $classes[] = 'single-format-standard';
603                         }
604                 }
605
606                 if ( is_attachment() ) {
607                         $mime_type = get_post_mime_type($post_id);
608                         $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
609                         $classes[] = 'attachmentid-' . $post_id;
610                         $classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type );
611                 }
612         } elseif ( is_archive() ) {
613                 if ( is_post_type_archive() ) {
614                         $classes[] = 'post-type-archive';
615                         $post_type = get_query_var( 'post_type' );
616                         if ( is_array( $post_type ) )
617                                 $post_type = reset( $post_type );
618                         $classes[] = 'post-type-archive-' . sanitize_html_class( $post_type );
619                 } elseif ( is_author() ) {
620                         $author = $wp_query->get_queried_object();
621                         $classes[] = 'author';
622                         if ( isset( $author->user_nicename ) ) {
623                                 $classes[] = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID );
624                                 $classes[] = 'author-' . $author->ID;
625                         }
626                 } elseif ( is_category() ) {
627                         $cat = $wp_query->get_queried_object();
628                         $classes[] = 'category';
629                         if ( isset( $cat->term_id ) ) {
630                                 $cat_class = sanitize_html_class( $cat->slug, $cat->term_id );
631                                 if ( is_numeric( $cat_class ) || ! trim( $cat_class, '-' ) ) {
632                                         $cat_class = $cat->term_id;
633                                 }
634
635                                 $classes[] = 'category-' . $cat_class;
636                                 $classes[] = 'category-' . $cat->term_id;
637                         }
638                 } elseif ( is_tag() ) {
639                         $tag = $wp_query->get_queried_object();
640                         $classes[] = 'tag';
641                         if ( isset( $tag->term_id ) ) {
642                                 $tag_class = sanitize_html_class( $tag->slug, $tag->term_id );
643                                 if ( is_numeric( $tag_class ) || ! trim( $tag_class, '-' ) ) {
644                                         $tag_class = $tag->term_id;
645                                 }
646
647                                 $classes[] = 'tag-' . $tag_class;
648                                 $classes[] = 'tag-' . $tag->term_id;
649                         }
650                 } elseif ( is_tax() ) {
651                         $term = $wp_query->get_queried_object();
652                         if ( isset( $term->term_id ) ) {
653                                 $term_class = sanitize_html_class( $term->slug, $term->term_id );
654                                 if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
655                                         $term_class = $term->term_id;
656                                 }
657
658                                 $classes[] = 'tax-' . sanitize_html_class( $term->taxonomy );
659                                 $classes[] = 'term-' . $term_class;
660                                 $classes[] = 'term-' . $term->term_id;
661                         }
662                 }
663         } elseif ( is_page() ) {
664                 $classes[] = 'page';
665
666                 $page_id = $wp_query->get_queried_object_id();
667
668                 $post = get_post($page_id);
669
670                 $classes[] = 'page-id-' . $page_id;
671
672                 if ( get_pages( array( 'parent' => $page_id, 'number' => 1 ) ) ) {
673                         $classes[] = 'page-parent';
674                 }
675
676                 if ( $post->post_parent ) {
677                         $classes[] = 'page-child';
678                         $classes[] = 'parent-pageid-' . $post->post_parent;
679                 }
680                 if ( is_page_template() ) {
681                         $classes[] = 'page-template';
682
683                         $template_slug  = get_page_template_slug( $page_id );
684                         $template_parts = explode( '/', $template_slug );
685
686                         foreach ( $template_parts as $part ) {
687                                 $classes[] = 'page-template-' . sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) );
688                         }
689                         $classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', $template_slug ) );
690                 } else {
691                         $classes[] = 'page-template-default';
692                 }
693         }
694
695         if ( is_user_logged_in() )
696                 $classes[] = 'logged-in';
697
698         if ( is_admin_bar_showing() ) {
699                 $classes[] = 'admin-bar';
700                 $classes[] = 'no-customize-support';
701         }
702
703         if ( get_background_color() !== get_theme_support( 'custom-background', 'default-color' ) || get_background_image() )
704                 $classes[] = 'custom-background';
705
706         if ( has_custom_logo() ) {
707                 $classes[] = 'wp-custom-logo';
708         }
709
710         $page = $wp_query->get( 'page' );
711
712         if ( ! $page || $page < 2 )
713                 $page = $wp_query->get( 'paged' );
714
715         if ( $page && $page > 1 && ! is_404() ) {
716                 $classes[] = 'paged-' . $page;
717
718                 if ( is_single() )
719                         $classes[] = 'single-paged-' . $page;
720                 elseif ( is_page() )
721                         $classes[] = 'page-paged-' . $page;
722                 elseif ( is_category() )
723                         $classes[] = 'category-paged-' . $page;
724                 elseif ( is_tag() )
725                         $classes[] = 'tag-paged-' . $page;
726                 elseif ( is_date() )
727                         $classes[] = 'date-paged-' . $page;
728                 elseif ( is_author() )
729                         $classes[] = 'author-paged-' . $page;
730                 elseif ( is_search() )
731                         $classes[] = 'search-paged-' . $page;
732                 elseif ( is_post_type_archive() )
733                         $classes[] = 'post-type-paged-' . $page;
734         }
735
736         if ( ! empty( $class ) ) {
737                 if ( !is_array( $class ) )
738                         $class = preg_split( '#\s+#', $class );
739                 $classes = array_merge( $classes, $class );
740         } else {
741                 // Ensure that we always coerce class to being an array.
742                 $class = array();
743         }
744
745         $classes = array_map( 'esc_attr', $classes );
746
747         /**
748          * Filter the list of CSS body classes for the current post or page.
749          *
750          * @since 2.8.0
751          *
752          * @param array $classes An array of body classes.
753          * @param array $class   An array of additional classes added to the body.
754          */
755         $classes = apply_filters( 'body_class', $classes, $class );
756
757         return array_unique( $classes );
758 }
759
760 /**
761  * Whether post requires password and correct password has been provided.
762  *
763  * @since 2.7.0
764  *
765  * @param int|WP_Post|null $post An optional post. Global $post used if not provided.
766  * @return bool false if a password is not required or the correct password cookie is present, true otherwise.
767  */
768 function post_password_required( $post = null ) {
769         $post = get_post($post);
770
771         if ( empty( $post->post_password ) )
772                 return false;
773
774         if ( ! isset( $_COOKIE['wp-postpass_' . COOKIEHASH] ) )
775                 return true;
776
777         require_once ABSPATH . WPINC . '/class-phpass.php';
778         $hasher = new PasswordHash( 8, true );
779
780         $hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
781         if ( 0 !== strpos( $hash, '$P$B' ) )
782                 return true;
783
784         return ! $hasher->CheckPassword( $post->post_password, $hash );
785 }
786
787 //
788 // Page Template Functions for usage in Themes
789 //
790
791 /**
792  * The formatted output of a list of pages.
793  *
794  * Displays page links for paginated posts (i.e. includes the <!--nextpage-->.
795  * Quicktag one or more times). This tag must be within The Loop.
796  *
797  * @since 1.2.0
798  *
799  * @global int $page
800  * @global int $numpages
801  * @global int $multipage
802  * @global int $more
803  *
804  * @param string|array $args {
805  *     Optional. Array or string of default arguments.
806  *
807  *     @type string       $before           HTML or text to prepend to each link. Default is `<p> Pages:`.
808  *     @type string       $after            HTML or text to append to each link. Default is `</p>`.
809  *     @type string       $link_before      HTML or text to prepend to each link, inside the `<a>` tag.
810  *                                          Also prepended to the current item, which is not linked. Default empty.
811  *     @type string       $link_after       HTML or text to append to each Pages link inside the `<a>` tag.
812  *                                          Also appended to the current item, which is not linked. Default empty.
813  *     @type string       $next_or_number   Indicates whether page numbers should be used. Valid values are number
814  *                                          and next. Default is 'number'.
815  *     @type string       $separator        Text between pagination links. Default is ' '.
816  *     @type string       $nextpagelink     Link text for the next page link, if available. Default is 'Next Page'.
817  *     @type string       $previouspagelink Link text for the previous page link, if available. Default is 'Previous Page'.
818  *     @type string       $pagelink         Format string for page numbers. The % in the parameter string will be
819  *                                          replaced with the page number, so 'Page %' generates "Page 1", "Page 2", etc.
820  *                                          Defaults to '%', just the page number.
821  *     @type int|bool     $echo             Whether to echo or not. Accepts 1|true or 0|false. Default 1|true.
822  * }
823  * @return string Formatted output in HTML.
824  */
825 function wp_link_pages( $args = '' ) {
826         global $page, $numpages, $multipage, $more;
827
828         $defaults = array(
829                 'before'           => '<p>' . __( 'Pages:' ),
830                 'after'            => '</p>',
831                 'link_before'      => '',
832                 'link_after'       => '',
833                 'next_or_number'   => 'number',
834                 'separator'        => ' ',
835                 'nextpagelink'     => __( 'Next page' ),
836                 'previouspagelink' => __( 'Previous page' ),
837                 'pagelink'         => '%',
838                 'echo'             => 1
839         );
840
841         $params = wp_parse_args( $args, $defaults );
842
843         /**
844          * Filter the arguments used in retrieving page links for paginated posts.
845          *
846          * @since 3.0.0
847          *
848          * @param array $params An array of arguments for page links for paginated posts.
849          */
850         $r = apply_filters( 'wp_link_pages_args', $params );
851
852         $output = '';
853         if ( $multipage ) {
854                 if ( 'number' == $r['next_or_number'] ) {
855                         $output .= $r['before'];
856                         for ( $i = 1; $i <= $numpages; $i++ ) {
857                                 $link = $r['link_before'] . str_replace( '%', $i, $r['pagelink'] ) . $r['link_after'];
858                                 if ( $i != $page || ! $more && 1 == $page ) {
859                                         $link = _wp_link_page( $i ) . $link . '</a>';
860                                 }
861                                 /**
862                                  * Filter the HTML output of individual page number links.
863                                  *
864                                  * @since 3.6.0
865                                  *
866                                  * @param string $link The page number HTML output.
867                                  * @param int    $i    Page number for paginated posts' page links.
868                                  */
869                                 $link = apply_filters( 'wp_link_pages_link', $link, $i );
870
871                                 // Use the custom links separator beginning with the second link.
872                                 $output .= ( 1 === $i ) ? ' ' : $r['separator'];
873                                 $output .= $link;
874                         }
875                         $output .= $r['after'];
876                 } elseif ( $more ) {
877                         $output .= $r['before'];
878                         $prev = $page - 1;
879                         if ( $prev > 0 ) {
880                                 $link = _wp_link_page( $prev ) . $r['link_before'] . $r['previouspagelink'] . $r['link_after'] . '</a>';
881
882                                 /** This filter is documented in wp-includes/post-template.php */
883                                 $output .= apply_filters( 'wp_link_pages_link', $link, $prev );
884                         }
885                         $next = $page + 1;
886                         if ( $next <= $numpages ) {
887                                 if ( $prev ) {
888                                         $output .= $r['separator'];
889                                 }
890                                 $link = _wp_link_page( $next ) . $r['link_before'] . $r['nextpagelink'] . $r['link_after'] . '</a>';
891
892                                 /** This filter is documented in wp-includes/post-template.php */
893                                 $output .= apply_filters( 'wp_link_pages_link', $link, $next );
894                         }
895                         $output .= $r['after'];
896                 }
897         }
898
899         /**
900          * Filter the HTML output of page links for paginated posts.
901          *
902          * @since 3.6.0
903          *
904          * @param string $output HTML output of paginated posts' page links.
905          * @param array  $args   An array of arguments.
906          */
907         $html = apply_filters( 'wp_link_pages', $output, $args );
908
909         if ( $r['echo'] ) {
910                 echo $html;
911         }
912         return $html;
913 }
914
915 /**
916  * Helper function for wp_link_pages().
917  *
918  * @since 3.1.0
919  * @access private
920  *
921  * @global WP_Rewrite $wp_rewrite
922  *
923  * @param int $i Page number.
924  * @return string Link.
925  */
926 function _wp_link_page( $i ) {
927         global $wp_rewrite;
928         $post = get_post();
929         $query_args = array();
930
931         if ( 1 == $i ) {
932                 $url = get_permalink();
933         } else {
934                 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
935                         $url = add_query_arg( 'page', $i, get_permalink() );
936                 elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') == $post->ID )
937                         $url = trailingslashit(get_permalink()) . user_trailingslashit("$wp_rewrite->pagination_base/" . $i, 'single_paged');
938                 else
939                         $url = trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged');
940         }
941
942         if ( is_preview() ) {
943
944                 if ( ( 'draft' !== $post->post_status ) && isset( $_GET['preview_id'], $_GET['preview_nonce'] ) ) {
945                         $query_args['preview_id'] = wp_unslash( $_GET['preview_id'] );
946                         $query_args['preview_nonce'] = wp_unslash( $_GET['preview_nonce'] );
947                 }
948
949                 $url = get_preview_post_link( $post, $query_args, $url );
950         }
951
952         return '<a href="' . esc_url( $url ) . '">';
953 }
954
955 //
956 // Post-meta: Custom per-post fields.
957 //
958
959 /**
960  * Retrieve post custom meta data field.
961  *
962  * @since 1.5.0
963  *
964  * @param string $key Meta data key name.
965  * @return false|string|array Array of values or single value, if only one element exists. False will be returned if key does not exist.
966  */
967 function post_custom( $key = '' ) {
968         $custom = get_post_custom();
969
970         if ( !isset( $custom[$key] ) )
971                 return false;
972         elseif ( 1 == count($custom[$key]) )
973                 return $custom[$key][0];
974         else
975                 return $custom[$key];
976 }
977
978 /**
979  * Display list of post custom fields.
980  *
981  * @since 1.2.0
982  *
983  * @internal This will probably change at some point...
984  *
985  */
986 function the_meta() {
987         if ( $keys = get_post_custom_keys() ) {
988                 echo "<ul class='post-meta'>\n";
989                 foreach ( (array) $keys as $key ) {
990                         $keyt = trim($key);
991                         if ( is_protected_meta( $keyt, 'post' ) )
992                                 continue;
993                         $values = array_map('trim', get_post_custom_values($key));
994                         $value = implode($values,', ');
995
996                         /**
997                          * Filter the HTML output of the li element in the post custom fields list.
998                          *
999                          * @since 2.2.0
1000                          *
1001                          * @param string $html  The HTML output for the li element.
1002                          * @param string $key   Meta key.
1003                          * @param string $value Meta value.
1004                          */
1005                         echo apply_filters( 'the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value );
1006                 }
1007                 echo "</ul>\n";
1008         }
1009 }
1010
1011 //
1012 // Pages
1013 //
1014
1015 /**
1016  * Retrieve or display list of pages as a dropdown (select list).
1017  *
1018  * @since 2.1.0
1019  * @since 4.2.0 The `$value_field` argument was added.
1020  * @since 4.3.0 The `$class` argument was added.
1021  *
1022  * @param array|string $args {
1023  *     Optional. Array or string of arguments to generate a pages drop-down element.
1024  *
1025  *     @type int          $depth                 Maximum depth. Default 0.
1026  *     @type int          $child_of              Page ID to retrieve child pages of. Default 0.
1027  *     @type int|string   $selected              Value of the option that should be selected. Default 0.
1028  *     @type bool|int     $echo                  Whether to echo or return the generated markup. Accepts 0, 1,
1029  *                                               or their bool equivalents. Default 1.
1030  *     @type string       $name                  Value for the 'name' attribute of the select element.
1031  *                                               Default 'page_id'.
1032  *     @type string       $id                    Value for the 'id' attribute of the select element.
1033  *     @type string       $class                 Value for the 'class' attribute of the select element. Default: none.
1034  *                                               Defaults to the value of `$name`.
1035  *     @type string       $show_option_none      Text to display for showing no pages. Default empty (does not display).
1036  *     @type string       $show_option_no_change Text to display for "no change" option. Default empty (does not display).
1037  *     @type string       $option_none_value     Value to use when no page is selected. Default empty.
1038  *     @type string       $value_field           Post field used to populate the 'value' attribute of the option
1039  *                                               elements. Accepts any valid post field. Default 'ID'.
1040  * }
1041  * @return string HTML content, if not displaying.
1042  */
1043 function wp_dropdown_pages( $args = '' ) {
1044         $defaults = array(
1045                 'depth' => 0, 'child_of' => 0,
1046                 'selected' => 0, 'echo' => 1,
1047                 'name' => 'page_id', 'id' => '',
1048                 'class' => '',
1049                 'show_option_none' => '', 'show_option_no_change' => '',
1050                 'option_none_value' => '',
1051                 'value_field' => 'ID',
1052         );
1053
1054         $r = wp_parse_args( $args, $defaults );
1055
1056         $pages = get_pages( $r );
1057         $output = '';
1058         // Back-compat with old system where both id and name were based on $name argument
1059         if ( empty( $r['id'] ) ) {
1060                 $r['id'] = $r['name'];
1061         }
1062
1063         if ( ! empty( $pages ) ) {
1064                 $class = '';
1065                 if ( ! empty( $r['class'] ) ) {
1066                         $class = " class='" . esc_attr( $r['class'] ) . "'";
1067                 }
1068
1069                 $output = "<select name='" . esc_attr( $r['name'] ) . "'" . $class . " id='" . esc_attr( $r['id'] ) . "'>\n";
1070                 if ( $r['show_option_no_change'] ) {
1071                         $output .= "\t<option value=\"-1\">" . $r['show_option_no_change'] . "</option>\n";
1072                 }
1073                 if ( $r['show_option_none'] ) {
1074                         $output .= "\t<option value=\"" . esc_attr( $r['option_none_value'] ) . '">' . $r['show_option_none'] . "</option>\n";
1075                 }
1076                 $output .= walk_page_dropdown_tree( $pages, $r['depth'], $r );
1077                 $output .= "</select>\n";
1078         }
1079
1080         /**
1081          * Filter the HTML output of a list of pages as a drop down.
1082          *
1083          * @since 2.1.0
1084          * @since 4.4.0 `$r` and `$pages` added as arguments.
1085          *
1086          * @param string $output HTML output for drop down list of pages.
1087          * @param array  $r      The parsed arguments array.
1088          * @param array  $pages  List of WP_Post objects returned by `get_pages()`
1089          */
1090         $html = apply_filters( 'wp_dropdown_pages', $output, $r, $pages );
1091
1092         if ( $r['echo'] ) {
1093                 echo $html;
1094         }
1095         return $html;
1096 }
1097
1098 /**
1099  * Retrieve or display list of pages in list (li) format.
1100  *
1101  * @since 1.5.0
1102  *
1103  * @see get_pages()
1104  *
1105  * @global WP_Query $wp_query
1106  *
1107  * @param array|string $args {
1108  *     Array or string of arguments. Optional.
1109  *
1110  *     @type int    $child_of     Display only the sub-pages of a single page by ID. Default 0 (all pages).
1111  *     @type string $authors      Comma-separated list of author IDs. Default empty (all authors).
1112  *     @type string $date_format  PHP date format to use for the listed pages. Relies on the 'show_date' parameter.
1113  *                                Default is the value of 'date_format' option.
1114  *     @type int    $depth        Number of levels in the hierarchy of pages to include in the generated list.
1115  *                                Accepts -1 (any depth), 0 (all pages), 1 (top-level pages only), and n (pages to
1116  *                                the given n depth). Default 0.
1117  *     @type bool   $echo         Whether or not to echo the list of pages. Default true.
1118  *     @type string $exclude      Comma-separated list of page IDs to exclude. Default empty.
1119  *     @type array  $include      Comma-separated list of page IDs to include. Default empty.
1120  *     @type string $link_after   Text or HTML to follow the page link label. Default null.
1121  *     @type string $link_before  Text or HTML to precede the page link label. Default null.
1122  *     @type string $post_type    Post type to query for. Default 'page'.
1123  *     @type string $post_status  Comma-separated list of post statuses to include. Default 'publish'.
1124  *     @type string $show_date    Whether to display the page publish or modified date for each page. Accepts
1125  *                                'modified' or any other value. An empty value hides the date. Default empty.
1126  *     @type string $sort_column  Comma-separated list of column names to sort the pages by. Accepts 'post_author',
1127  *                                'post_date', 'post_title', 'post_name', 'post_modified', 'post_modified_gmt',
1128  *                                'menu_order', 'post_parent', 'ID', 'rand', or 'comment_count'. Default 'post_title'.
1129  *     @type string $title_li     List heading. Passing a null or empty value will result in no heading, and the list
1130  *                                will not be wrapped with unordered list `<ul>` tags. Default 'Pages'.
1131  *     @type Walker $walker       Walker instance to use for listing pages. Default empty (Walker_Page).
1132  * }
1133  * @return string|void HTML list of pages.
1134  */
1135 function wp_list_pages( $args = '' ) {
1136         $defaults = array(
1137                 'depth' => 0, 'show_date' => '',
1138                 'date_format' => get_option( 'date_format' ),
1139                 'child_of' => 0, 'exclude' => '',
1140                 'title_li' => __( 'Pages' ), 'echo' => 1,
1141                 'authors' => '', 'sort_column' => 'menu_order, post_title',
1142                 'link_before' => '', 'link_after' => '', 'walker' => '',
1143         );
1144
1145         $r = wp_parse_args( $args, $defaults );
1146
1147         $output = '';
1148         $current_page = 0;
1149
1150         // sanitize, mostly to keep spaces out
1151         $r['exclude'] = preg_replace( '/[^0-9,]/', '', $r['exclude'] );
1152
1153         // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array)
1154         $exclude_array = ( $r['exclude'] ) ? explode( ',', $r['exclude'] ) : array();
1155
1156         /**
1157          * Filter the array of pages to exclude from the pages list.
1158          *
1159          * @since 2.1.0
1160          *
1161          * @param array $exclude_array An array of page IDs to exclude.
1162          */
1163         $r['exclude'] = implode( ',', apply_filters( 'wp_list_pages_excludes', $exclude_array ) );
1164
1165         // Query pages.
1166         $r['hierarchical'] = 0;
1167         $pages = get_pages( $r );
1168
1169         if ( ! empty( $pages ) ) {
1170                 if ( $r['title_li'] ) {
1171                         $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
1172                 }
1173                 global $wp_query;
1174                 if ( is_page() || is_attachment() || $wp_query->is_posts_page ) {
1175                         $current_page = get_queried_object_id();
1176                 } elseif ( is_singular() ) {
1177                         $queried_object = get_queried_object();
1178                         if ( is_post_type_hierarchical( $queried_object->post_type ) ) {
1179                                 $current_page = $queried_object->ID;
1180                         }
1181                 }
1182
1183                 $output .= walk_page_tree( $pages, $r['depth'], $current_page, $r );
1184
1185                 if ( $r['title_li'] ) {
1186                         $output .= '</ul></li>';
1187                 }
1188         }
1189
1190         /**
1191          * Filter the HTML output of the pages to list.
1192          *
1193          * @since 1.5.1
1194          * @since 4.4.0 `$pages` added as arguments.
1195          *
1196          * @see wp_list_pages()
1197          *
1198          * @param string $output HTML output of the pages list.
1199          * @param array  $r      An array of page-listing arguments.
1200          * @param array  $pages  List of WP_Post objects returned by `get_pages()`
1201          */
1202         $html = apply_filters( 'wp_list_pages', $output, $r, $pages );
1203
1204         if ( $r['echo'] ) {
1205                 echo $html;
1206         } else {
1207                 return $html;
1208         }
1209 }
1210
1211 /**
1212  * Display or retrieve list of pages with optional home link.
1213  *
1214  * The arguments are listed below and part of the arguments are for {@link
1215  * wp_list_pages()} function. Check that function for more info on those
1216  * arguments.
1217  *
1218  * @since 2.7.0
1219  * @since 4.4.0 Added `menu_id`, `container`, `before`, `after`, and `walker` arguments.
1220  *
1221  * @param array|string $args {
1222  *     Optional. Arguments to generate a page menu. See wp_list_pages() for additional arguments.
1223  *
1224  *     @type string          $sort_column How to short the list of pages. Accepts post column names.
1225  *                                        Default 'menu_order, post_title'.
1226  *     @type string          $menu_id     ID for the div containing the page list. Default is empty string.
1227  *     @type string          $menu_class  Class to use for the element containing the page list. Default 'menu'.
1228  *     @type string          $container   Element to use for the element containing the page list. Default 'div'.
1229  *     @type bool            $echo        Whether to echo the list or return it. Accepts true (echo) or false (return).
1230  *                                        Default true.
1231  *     @type int|bool|string $show_home   Whether to display the link to the home page. Can just enter the text
1232  *                                        you'd like shown for the home link. 1|true defaults to 'Home'.
1233  *     @type string          $link_before The HTML or text to prepend to $show_home text. Default empty.
1234  *     @type string          $link_after  The HTML or text to append to $show_home text. Default empty.
1235  *     @type string          $before      The HTML or text to prepend to the menu. Default is '<ul>'.
1236  *     @type string          $after       The HTML or text to append to the menu. Default is '</ul>'.
1237  *     @type Walker          $walker      Walker instance to use for listing pages. Default empty (Walker_Page).
1238  * }
1239  * @return string|void HTML menu
1240  */
1241 function wp_page_menu( $args = array() ) {
1242         $defaults = array(
1243                 'sort_column' => 'menu_order, post_title',
1244                 'menu_id'     => '',
1245                 'menu_class'  => 'menu',
1246                 'container'   => 'div',
1247                 'echo'        => true,
1248                 'link_before' => '',
1249                 'link_after'  => '',
1250                 'before'      => '<ul>',
1251                 'after'       => '</ul>',
1252                 'walker'      => '',
1253         );
1254         $args = wp_parse_args( $args, $defaults );
1255
1256         /**
1257          * Filter the arguments used to generate a page-based menu.
1258          *
1259          * @since 2.7.0
1260          *
1261          * @see wp_page_menu()
1262          *
1263          * @param array $args An array of page menu arguments.
1264          */
1265         $args = apply_filters( 'wp_page_menu_args', $args );
1266
1267         $menu = '';
1268
1269         $list_args = $args;
1270
1271         // Show Home in the menu
1272         if ( ! empty($args['show_home']) ) {
1273                 if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] )
1274                         $text = __('Home');
1275                 else
1276                         $text = $args['show_home'];
1277                 $class = '';
1278                 if ( is_front_page() && !is_paged() )
1279                         $class = 'class="current_page_item"';
1280                 $menu .= '<li ' . $class . '><a href="' . home_url( '/' ) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>';
1281                 // If the front page is a page, add it to the exclude list
1282                 if (get_option('show_on_front') == 'page') {
1283                         if ( !empty( $list_args['exclude'] ) ) {
1284                                 $list_args['exclude'] .= ',';
1285                         } else {
1286                                 $list_args['exclude'] = '';
1287                         }
1288                         $list_args['exclude'] .= get_option('page_on_front');
1289                 }
1290         }
1291
1292         $list_args['echo'] = false;
1293         $list_args['title_li'] = '';
1294         $menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages($list_args) );
1295
1296         $container = sanitize_text_field( $args['container'] );
1297
1298         // Fallback in case `wp_nav_menu()` was called without a container.
1299         if ( empty( $container ) ) {
1300                 $container = 'div';
1301         }
1302
1303         if ( $menu ) {
1304
1305                 // wp_nav_menu doesn't set before and after
1306                 if ( isset( $args['fallback_cb'] ) &&
1307                         'wp_page_menu' === $args['fallback_cb'] &&
1308                         'ul' !== $container ) {
1309                         $args['before'] = '<ul>';
1310                         $args['after'] = '</ul>';
1311                 }
1312
1313                 $menu = $args['before'] . $menu . $args['after'];
1314         }
1315
1316         $attrs = '';
1317         if ( ! empty( $args['menu_id'] ) ) {
1318                 $attrs .= ' id="' . esc_attr( $args['menu_id'] ) . '"';
1319         }
1320
1321         if ( ! empty( $args['menu_class'] ) ) {
1322                 $attrs .= ' class="' . esc_attr( $args['menu_class'] ) . '"';
1323         }
1324
1325         $menu = "<{$container}{$attrs}>" . $menu . "</{$container}>\n";
1326
1327         /**
1328          * Filter the HTML output of a page-based menu.
1329          *
1330          * @since 2.7.0
1331          *
1332          * @see wp_page_menu()
1333          *
1334          * @param string $menu The HTML output.
1335          * @param array  $args An array of arguments.
1336          */
1337         $menu = apply_filters( 'wp_page_menu', $menu, $args );
1338         if ( $args['echo'] )
1339                 echo $menu;
1340         else
1341                 return $menu;
1342 }
1343
1344 //
1345 // Page helpers
1346 //
1347
1348 /**
1349  * Retrieve HTML list content for page list.
1350  *
1351  * @uses Walker_Page to create HTML list content.
1352  * @since 2.1.0
1353  *
1354  * @param array $pages
1355  * @param int   $depth
1356  * @param int   $current_page
1357  * @param array $r
1358  * @return string
1359  */
1360 function walk_page_tree( $pages, $depth, $current_page, $r ) {
1361         if ( empty($r['walker']) )
1362                 $walker = new Walker_Page;
1363         else
1364                 $walker = $r['walker'];
1365
1366         foreach ( (array) $pages as $page ) {
1367                 if ( $page->post_parent )
1368                         $r['pages_with_children'][ $page->post_parent ] = true;
1369         }
1370
1371         $args = array($pages, $depth, $r, $current_page);
1372         return call_user_func_array(array($walker, 'walk'), $args);
1373 }
1374
1375 /**
1376  * Retrieve HTML dropdown (select) content for page list.
1377  *
1378  * @uses Walker_PageDropdown to create HTML dropdown content.
1379  * @since 2.1.0
1380  * @see Walker_PageDropdown::walk() for parameters and return description.
1381  *
1382  * @return string
1383  */
1384 function walk_page_dropdown_tree() {
1385         $args = func_get_args();
1386         if ( empty($args[2]['walker']) ) // the user's options are the third parameter
1387                 $walker = new Walker_PageDropdown;
1388         else
1389                 $walker = $args[2]['walker'];
1390
1391         return call_user_func_array(array($walker, 'walk'), $args);
1392 }
1393
1394 //
1395 // Attachments
1396 //
1397
1398 /**
1399  * Display an attachment page link using an image or icon.
1400  *
1401  * @since 2.0.0
1402  *
1403  * @param int|WP_Post $id Optional. Post ID or post object.
1404  * @param bool        $fullsize     Optional, default is false. Whether to use full size.
1405  * @param bool        $deprecated   Deprecated. Not used.
1406  * @param bool        $permalink    Optional, default is false. Whether to include permalink.
1407  */
1408 function the_attachment_link( $id = 0, $fullsize = false, $deprecated = false, $permalink = false ) {
1409         if ( !empty( $deprecated ) )
1410                 _deprecated_argument( __FUNCTION__, '2.5' );
1411
1412         if ( $fullsize )
1413                 echo wp_get_attachment_link($id, 'full', $permalink);
1414         else
1415                 echo wp_get_attachment_link($id, 'thumbnail', $permalink);
1416 }
1417
1418 /**
1419  * Retrieve an attachment page link using an image or icon, if possible.
1420  *
1421  * @since 2.5.0
1422  * @since 4.4.0 The `$id` parameter can now accept either a post ID or `WP_Post` object.
1423  *
1424  * @param int|WP_Post  $id        Optional. Post ID or post object.
1425  * @param string|array $size      Optional. Image size. Accepts any valid image size, or an array
1426  *                                of width and height values in pixels (in that order).
1427  *                                Default 'thumbnail'.
1428  * @param bool         $permalink Optional, Whether to add permalink to image. Default false.
1429  * @param bool         $icon      Optional. Whether the attachment is an icon. Default false.
1430  * @param string|false $text      Optional. Link text to use. Activated by passing a string, false otherwise.
1431  *                                Default false.
1432  * @param array|string $attr      Optional. Array or string of attributes. Default empty.
1433  * @return string HTML content.
1434  */
1435 function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) {
1436         $_post = get_post( $id );
1437
1438         if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )
1439                 return __( 'Missing Attachment' );
1440
1441         if ( $permalink )
1442                 $url = get_attachment_link( $_post->ID );
1443
1444         if ( $text ) {
1445                 $link_text = $text;
1446         } elseif ( $size && 'none' != $size ) {
1447                 $link_text = wp_get_attachment_image( $_post->ID, $size, $icon, $attr );
1448         } else {
1449                 $link_text = '';
1450         }
1451
1452         if ( trim( $link_text ) == '' )
1453                 $link_text = $_post->post_title;
1454
1455         /**
1456          * Filter a retrieved attachment page link.
1457          *
1458          * @since 2.7.0
1459          *
1460          * @param string       $link_html The page link HTML output.
1461          * @param int          $id        Post ID.
1462          * @param string|array $size      Size of the image. Image size or array of width and height values (in that order).
1463          *                                Default 'thumbnail'.
1464          * @param bool         $permalink Whether to add permalink to image. Default false.
1465          * @param bool         $icon      Whether to include an icon. Default false.
1466          * @param string|bool  $text      If string, will be link text. Default false.
1467          */
1468         return apply_filters( 'wp_get_attachment_link', "<a href='$url'>$link_text</a>", $id, $size, $permalink, $icon, $text );
1469 }
1470
1471 /**
1472  * Wrap attachment in paragraph tag before content.
1473  *
1474  * @since 2.0.0
1475  *
1476  * @param string $content
1477  * @return string
1478  */
1479 function prepend_attachment($content) {
1480         $post = get_post();
1481
1482         if ( empty($post->post_type) || $post->post_type != 'attachment' )
1483                 return $content;
1484
1485         if ( wp_attachment_is( 'video', $post ) ) {
1486                 $meta = wp_get_attachment_metadata( get_the_ID() );
1487                 $atts = array( 'src' => wp_get_attachment_url() );
1488                 if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) {
1489                         $atts['width'] = (int) $meta['width'];
1490                         $atts['height'] = (int) $meta['height'];
1491                 }
1492                 if ( has_post_thumbnail() ) {
1493                         $atts['poster'] = wp_get_attachment_url( get_post_thumbnail_id() );
1494                 }
1495                 $p = wp_video_shortcode( $atts );
1496         } elseif ( wp_attachment_is( 'audio', $post ) ) {
1497                 $p = wp_audio_shortcode( array( 'src' => wp_get_attachment_url() ) );
1498         } else {
1499                 $p = '<p class="attachment">';
1500                 // show the medium sized image representation of the attachment if available, and link to the raw file
1501                 $p .= wp_get_attachment_link(0, 'medium', false);
1502                 $p .= '</p>';
1503         }
1504
1505         /**
1506          * Filter the attachment markup to be prepended to the post content.
1507          *
1508          * @since 2.0.0
1509          *
1510          * @see prepend_attachment()
1511          *
1512          * @param string $p The attachment HTML output.
1513          */
1514         $p = apply_filters( 'prepend_attachment', $p );
1515
1516         return "$p\n$content";
1517 }
1518
1519 //
1520 // Misc
1521 //
1522
1523 /**
1524  * Retrieve protected post password form content.
1525  *
1526  * @since 1.0.0
1527  *
1528  * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
1529  * @return string HTML content for password form for password protected post.
1530  */
1531 function get_the_password_form( $post = 0 ) {
1532         $post = get_post( $post );
1533         $label = 'pwbox-' . ( empty($post->ID) ? rand() : $post->ID );
1534         $output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form" method="post">
1535         <p>' . __( 'This content is password protected. To view it please enter your password below:' ) . '</p>
1536         <p><label for="' . $label . '">' . __( 'Password:' ) . ' <input name="post_password" id="' . $label . '" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . esc_attr_x( 'Enter', 'post password form' ) . '" /></p></form>
1537         ';
1538
1539         /**
1540          * Filter the HTML output for the protected post password form.
1541          *
1542          * If modifying the password field, please note that the core database schema
1543          * limits the password field to 20 characters regardless of the value of the
1544          * size attribute in the form input.
1545          *
1546          * @since 2.7.0
1547          *
1548          * @param string $output The password form HTML output.
1549          */
1550         return apply_filters( 'the_password_form', $output );
1551 }
1552
1553 /**
1554  * Whether currently in a page template.
1555  *
1556  * This template tag allows you to determine if you are in a page template.
1557  * You can optionally provide a template name or array of template names
1558  * and then the check will be specific to that template.
1559  *
1560  * @since 2.5.0
1561  * @since 4.2.0 The `$template` parameter was changed to also accept an array of page templates.
1562  *
1563  * @param string|array $template The specific template name or array of templates to match.
1564  * @return bool True on success, false on failure.
1565  */
1566 function is_page_template( $template = '' ) {
1567         if ( ! is_page() )
1568                 return false;
1569
1570         $page_template = get_page_template_slug( get_queried_object_id() );
1571
1572         if ( empty( $template ) )
1573                 return (bool) $page_template;
1574
1575         if ( $template == $page_template )
1576                 return true;
1577
1578         if ( is_array( $template ) ) {
1579                 if ( ( in_array( 'default', $template, true ) && ! $page_template )
1580                         || in_array( $page_template, $template, true )
1581                 ) {
1582                         return true;
1583                 }
1584         }
1585
1586         return ( 'default' === $template && ! $page_template );
1587 }
1588
1589 /**
1590  * Get the specific template name for a page.
1591  *
1592  * @since 3.4.0
1593  *
1594  * @param int $post_id Optional. The page ID to check. Defaults to the current post, when used in the loop.
1595  * @return string|false Page template filename. Returns an empty string when the default page template
1596  *      is in use. Returns false if the post is not a page.
1597  */
1598 function get_page_template_slug( $post_id = null ) {
1599         $post = get_post( $post_id );
1600         if ( ! $post || 'page' != $post->post_type )
1601                 return false;
1602         $template = get_post_meta( $post->ID, '_wp_page_template', true );
1603         if ( ! $template || 'default' == $template )
1604                 return '';
1605         return $template;
1606 }
1607
1608 /**
1609  * Retrieve formatted date timestamp of a revision (linked to that revisions's page).
1610  *
1611  * @since 2.6.0
1612  *
1613  * @param int|object $revision Revision ID or revision object.
1614  * @param bool       $link     Optional, default is true. Link to revisions's page?
1615  * @return string|false i18n formatted datetimestamp or localized 'Current Revision'.
1616  */
1617 function wp_post_revision_title( $revision, $link = true ) {
1618         if ( !$revision = get_post( $revision ) )
1619                 return $revision;
1620
1621         if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
1622                 return false;
1623
1624         /* translators: revision date format, see http://php.net/date */
1625         $datef = _x( 'F j, Y @ H:i:s', 'revision date format' );
1626         /* translators: 1: date */
1627         $autosavef = _x( '%1$s [Autosave]', 'post revision title extra' );
1628         /* translators: 1: date */
1629         $currentf  = _x( '%1$s [Current Revision]', 'post revision title extra' );
1630
1631         $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
1632         if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
1633                 $date = "<a href='$link'>$date</a>";
1634
1635         if ( !wp_is_post_revision( $revision ) )
1636                 $date = sprintf( $currentf, $date );
1637         elseif ( wp_is_post_autosave( $revision ) )
1638                 $date = sprintf( $autosavef, $date );
1639
1640         return $date;
1641 }
1642
1643 /**
1644  * Retrieve formatted date timestamp of a revision (linked to that revisions's page).
1645  *
1646  * @since 3.6.0
1647  *
1648  * @param int|object $revision Revision ID or revision object.
1649  * @param bool       $link     Optional, default is true. Link to revisions's page?
1650  * @return string|false gravatar, user, i18n formatted datetimestamp or localized 'Current Revision'.
1651  */
1652 function wp_post_revision_title_expanded( $revision, $link = true ) {
1653         if ( !$revision = get_post( $revision ) )
1654                 return $revision;
1655
1656         if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
1657                 return false;
1658
1659         $author = get_the_author_meta( 'display_name', $revision->post_author );
1660         /* translators: revision date format, see http://php.net/date */
1661         $datef = _x( 'F j, Y @ H:i:s', 'revision date format' );
1662
1663         $gravatar = get_avatar( $revision->post_author, 24 );
1664
1665         $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
1666         if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
1667                 $date = "<a href='$link'>$date</a>";
1668
1669         $revision_date_author = sprintf(
1670                 /* translators: post revision title: 1: author avatar, 2: author name, 3: time ago, 4: date */
1671                 _x( '%1$s %2$s, %3$s ago (%4$s)', 'post revision title' ),
1672                 $gravatar,
1673                 $author,
1674                 human_time_diff( strtotime( $revision->post_modified ), current_time( 'timestamp' ) ),
1675                 $date
1676         );
1677
1678         $autosavef = __( '%1$s [Autosave]' );
1679         $currentf  = __( '%1$s [Current Revision]' );
1680
1681         if ( !wp_is_post_revision( $revision ) )
1682                 $revision_date_author = sprintf( $currentf, $revision_date_author );
1683         elseif ( wp_is_post_autosave( $revision ) )
1684                 $revision_date_author = sprintf( $autosavef, $revision_date_author );
1685
1686         /**
1687          * Filter the formatted author and date for a revision.
1688          *
1689          * @since 4.4.0
1690          *
1691          * @param string  $revision_date_author The formatted string.
1692          * @param WP_Post $revision             The revision object.
1693          * @param bool    $link                 Whether to link to the revisions page, as passed into
1694          *                                      wp_post_revision_title_expanded().
1695          */
1696         return apply_filters( 'wp_post_revision_title_expanded', $revision_date_author, $revision, $link );
1697 }
1698
1699 /**
1700  * Display list of a post's revisions.
1701  *
1702  * Can output either a UL with edit links or a TABLE with diff interface, and
1703  * restore action links.
1704  *
1705  * @since 2.6.0
1706  *
1707  * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
1708  * @param string      $type    'all' (default), 'revision' or 'autosave'
1709  */
1710 function wp_list_post_revisions( $post_id = 0, $type = 'all' ) {
1711         if ( ! $post = get_post( $post_id ) )
1712                 return;
1713
1714         // $args array with (parent, format, right, left, type) deprecated since 3.6
1715         if ( is_array( $type ) ) {
1716                 $type = ! empty( $type['type'] ) ? $type['type']  : $type;
1717                 _deprecated_argument( __FUNCTION__, '3.6' );
1718         }
1719
1720         if ( ! $revisions = wp_get_post_revisions( $post->ID ) )
1721                 return;
1722
1723         $rows = '';
1724         foreach ( $revisions as $revision ) {
1725                 if ( ! current_user_can( 'read_post', $revision->ID ) )
1726                         continue;
1727
1728                 $is_autosave = wp_is_post_autosave( $revision );
1729                 if ( ( 'revision' === $type && $is_autosave ) || ( 'autosave' === $type && ! $is_autosave ) )
1730                         continue;
1731
1732                 $rows .= "\t<li>" . wp_post_revision_title_expanded( $revision ) . "</li>\n";
1733         }
1734
1735         echo "<div class='hide-if-js'><p>" . __( 'JavaScript must be enabled to use this feature.' ) . "</p></div>\n";
1736
1737         echo "<ul class='post-revisions hide-if-no-js'>\n";
1738         echo $rows;
1739         echo "</ul>";
1740 }