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