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