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