]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/post-template.php
Wordpress 3.0.3
[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  * @uses $id
16  */
17 function the_ID() {
18         global $id;
19         echo $id;
20 }
21
22 /**
23  * Retrieve the ID of the current item in the WordPress Loop.
24  *
25  * @since 2.1.0
26  * @uses $id
27  *
28  * @return unknown
29  */
30 function get_the_ID() {
31         global $id;
32         return $id;
33 }
34
35 /**
36  * Display or retrieve the current post title with optional content.
37  *
38  * @since 0.71
39  *
40  * @param string $before Optional. Content to prepend to the title.
41  * @param string $after Optional. Content to append to the title.
42  * @param bool $echo Optional, default to true.Whether to display or return.
43  * @return null|string Null on no title. String if $echo parameter is false.
44  */
45 function the_title($before = '', $after = '', $echo = true) {
46         $title = get_the_title();
47
48         if ( strlen($title) == 0 )
49                 return;
50
51         $title = $before . $title . $after;
52
53         if ( $echo )
54                 echo $title;
55         else
56                 return $title;
57 }
58
59 /**
60  * Sanitize the current title when retrieving or displaying.
61  *
62  * Works like {@link the_title()}, except the parameters can be in a string or
63  * an array. See the function for what can be override in the $args parameter.
64  *
65  * The title before it is displayed will have the tags stripped and {@link
66  * esc_attr()} before it is passed to the user or displayed. The default
67  * as with {@link the_title()}, is to display the title.
68  *
69  * @since 2.3.0
70  *
71  * @param string|array $args Optional. Override the defaults.
72  * @return string|null Null on failure or display. String when echo is false.
73  */
74 function the_title_attribute( $args = '' ) {
75         $title = get_the_title();
76
77         if ( strlen($title) == 0 )
78                 return;
79
80         $defaults = array('before' => '', 'after' =>  '', 'echo' => true);
81         $r = wp_parse_args($args, $defaults);
82         extract( $r, EXTR_SKIP );
83
84
85         $title = $before . $title . $after;
86         $title = esc_attr(strip_tags($title));
87
88         if ( $echo )
89                 echo $title;
90         else
91                 return $title;
92 }
93
94 /**
95  * Retrieve post title.
96  *
97  * If the post is protected and the visitor is not an admin, then "Protected"
98  * will be displayed before the post title. If the post is private, then
99  * "Private" will be located before the post title.
100  *
101  * @since 0.71
102  *
103  * @param int $id Optional. Post ID.
104  * @return string
105  */
106 function get_the_title( $id = 0 ) {
107         $post = &get_post($id);
108
109         $title = isset($post->post_title) ? $post->post_title : '';
110         $id = isset($post->ID) ? $post->ID : (int) $id;
111
112         if ( !is_admin() ) {
113                 if ( !empty($post->post_password) ) {
114                         $protected_title_format = apply_filters('protected_title_format', __('Protected: %s'));
115                         $title = sprintf($protected_title_format, $title);
116                 } else if ( isset($post->post_status) && 'private' == $post->post_status ) {
117                         $private_title_format = apply_filters('private_title_format', __('Private: %s'));
118                         $title = sprintf($private_title_format, $title);
119                 }
120         }
121         return apply_filters( 'the_title', $title, $id );
122 }
123
124 /**
125  * Display the Post Global Unique Identifier (guid).
126  *
127  * The guid will appear to be a link, but should not be used as an link to the
128  * post. The reason you should not use it as a link, is because of moving the
129  * blog across domains.
130  *
131  * Url is escaped to make it xml safe
132  *
133  * @since 1.5.0
134  *
135  * @param int $id Optional. Post ID.
136  */
137 function the_guid( $id = 0 ) {
138         echo esc_url( get_the_guid( $id ) );
139 }
140
141 /**
142  * Retrieve the Post Global Unique Identifier (guid).
143  *
144  * The guid will appear to be a link, but should not be used as an link to the
145  * post. The reason you should not use it as a link, is because of moving the
146  * blog across domains.
147  *
148  * @since 1.5.0
149  *
150  * @param int $id Optional. Post ID.
151  * @return string
152  */
153 function get_the_guid( $id = 0 ) {
154         $post = &get_post($id);
155
156         return apply_filters('get_the_guid', $post->guid);
157 }
158
159 /**
160  * Display the post content.
161  *
162  * @since 0.71
163  *
164  * @param string $more_link_text Optional. Content for when there is more text.
165  * @param string $stripteaser Optional. Teaser content before the more text.
166  */
167 function the_content($more_link_text = null, $stripteaser = 0) {
168         $content = get_the_content($more_link_text, $stripteaser);
169         $content = apply_filters('the_content', $content);
170         $content = str_replace(']]>', ']]&gt;', $content);
171         echo $content;
172 }
173
174 /**
175  * Retrieve the post content.
176  *
177  * @since 0.71
178  *
179  * @param string $more_link_text Optional. Content for when there is more text.
180  * @param string $stripteaser Optional. Teaser content before the more text.
181  * @return string
182  */
183 function get_the_content($more_link_text = null, $stripteaser = 0) {
184         global $id, $post, $more, $page, $pages, $multipage, $preview;
185
186         if ( null === $more_link_text )
187                 $more_link_text = __( '(more...)' );
188
189         $output = '';
190         $hasTeaser = false;
191
192         // If post password required and it doesn't match the cookie.
193         if ( post_password_required($post) ) {
194                 $output = get_the_password_form();
195                 return $output;
196         }
197
198         if ( $page > count($pages) ) // if the requested page doesn't exist
199                 $page = count($pages); // give them the highest numbered page that DOES exist
200
201         $content = $pages[$page-1];
202         if ( preg_match('/<!--more(.*?)?-->/', $content, $matches) ) {
203                 $content = explode($matches[0], $content, 2);
204                 if ( !empty($matches[1]) && !empty($more_link_text) )
205                         $more_link_text = strip_tags(wp_kses_no_null(trim($matches[1])));
206
207                 $hasTeaser = true;
208         } else {
209                 $content = array($content);
210         }
211         if ( (false !== strpos($post->post_content, '<!--noteaser-->') && ((!$multipage) || ($page==1))) )
212                 $stripteaser = 1;
213         $teaser = $content[0];
214         if ( ($more) && ($stripteaser) && ($hasTeaser) )
215                 $teaser = '';
216         $output .= $teaser;
217         if ( count($content) > 1 ) {
218                 if ( $more ) {
219                         $output .= '<span id="more-' . $id . '"></span>' . $content[1];
220                 } else {
221                         if ( ! empty($more_link_text) )
222                                 $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-$id\" class=\"more-link\">$more_link_text</a>", $more_link_text );
223                         $output = force_balance_tags($output);
224                 }
225
226         }
227         if ( $preview ) // preview fix for javascript bug with foreign languages
228                 $output =       preg_replace_callback('/\%u([0-9A-F]{4})/', create_function('$match', 'return "&#" . base_convert($match[1], 16, 10) . ";";'), $output);
229
230         return $output;
231 }
232
233 /**
234  * Display the post excerpt.
235  *
236  * @since 0.71
237  * @uses apply_filters() Calls 'the_excerpt' hook on post excerpt.
238  */
239 function the_excerpt() {
240         echo apply_filters('the_excerpt', get_the_excerpt());
241 }
242
243 /**
244  * Retrieve the post excerpt.
245  *
246  * @since 0.71
247  *
248  * @param mixed $deprecated Not used.
249  * @return string
250  */
251 function get_the_excerpt( $deprecated = '' ) {
252         if ( !empty( $deprecated ) )
253                 _deprecated_argument( __FUNCTION__, '2.3' );
254
255         global $post;
256         $output = $post->post_excerpt;
257         if ( post_password_required($post) ) {
258                 $output = __('There is no excerpt because this is a protected post.');
259                 return $output;
260         }
261
262         return apply_filters('get_the_excerpt', $output);
263 }
264
265 /**
266  * Whether post has excerpt.
267  *
268  * @since 2.3.0
269  *
270  * @param int $id Optional. Post ID.
271  * @return bool
272  */
273 function has_excerpt( $id = 0 ) {
274         $post = &get_post( $id );
275         return ( !empty( $post->post_excerpt ) );
276 }
277
278 /**
279  * Display the classes for the post div.
280  *
281  * @since 2.7.0
282  *
283  * @param string|array $class One or more classes to add to the class list.
284  * @param int $post_id An optional post ID.
285  */
286 function post_class( $class = '', $post_id = null ) {
287         // Separates classes with a single space, collates classes for post DIV
288         echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';
289 }
290
291 /**
292  * Retrieve the classes for the post div as an array.
293  *
294  * The class names are add are many. If the post is a sticky, then the 'sticky'
295  * class name. The class 'hentry' is always added to each post. For each
296  * category, the class will be added with 'category-' with category slug is
297  * added. The tags are the same way as the categories with 'tag-' before the tag
298  * slug. All classes are passed through the filter, 'post_class' with the list
299  * of classes, followed by $class parameter value, with the post ID as the last
300  * parameter.
301  *
302  * @since 2.7.0
303  *
304  * @param string|array $class One or more classes to add to the class list.
305  * @param int $post_id An optional post ID.
306  * @return array Array of classes.
307  */
308 function get_post_class( $class = '', $post_id = null ) {
309         $post = get_post($post_id);
310
311         $classes = array();
312
313         if ( empty($post) )
314                 return $classes;
315
316         $classes[] = 'post-' . $post->ID;
317         $classes[] = $post->post_type;
318         $classes[] = 'type-' . $post->post_type;
319
320         // sticky for Sticky Posts
321         if ( is_sticky($post->ID) && is_home() && !is_paged() )
322                 $classes[] = 'sticky';
323
324         // hentry for hAtom compliace
325         $classes[] = 'hentry';
326
327         // Categories
328         foreach ( (array) get_the_category($post->ID) as $cat ) {
329                 if ( empty($cat->slug ) )
330                         continue;
331                 $classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->cat_ID);
332         }
333
334         // Tags
335         foreach ( (array) get_the_tags($post->ID) as $tag ) {
336                 if ( empty($tag->slug ) )
337                         continue;
338                 $classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id);
339         }
340
341         if ( !empty($class) ) {
342                 if ( !is_array( $class ) )
343                         $class = preg_split('#\s+#', $class);
344                 $classes = array_merge($classes, $class);
345         }
346
347         $classes = array_map('esc_attr', $classes);
348
349         return apply_filters('post_class', $classes, $class, $post->ID);
350 }
351
352 /**
353  * Display the classes for the body element.
354  *
355  * @since 2.8.0
356  *
357  * @param string|array $class One or more classes to add to the class list.
358  */
359 function body_class( $class = '' ) {
360         // Separates classes with a single space, collates classes for body element
361         echo 'class="' . join( ' ', get_body_class( $class ) ) . '"';
362 }
363
364 /**
365  * Retrieve the classes for the body element as an array.
366  *
367  * @since 2.8.0
368  *
369  * @param string|array $class One or more classes to add to the class list.
370  * @return array Array of classes.
371  */
372 function get_body_class( $class = '' ) {
373         global $wp_query, $wpdb;
374
375         $classes = array();
376
377         if ( is_rtl() )
378                 $classes[] = 'rtl';
379
380         if ( is_front_page() )
381                 $classes[] = 'home';
382         if ( is_home() )
383                 $classes[] = 'blog';
384         if ( is_archive() )
385                 $classes[] = 'archive';
386         if ( is_date() )
387                 $classes[] = 'date';
388         if ( is_search() )
389                 $classes[] = 'search';
390         if ( is_paged() )
391                 $classes[] = 'paged';
392         if ( is_attachment() )
393                 $classes[] = 'attachment';
394         if ( is_404() )
395                 $classes[] = 'error404';
396
397         if ( is_single() ) {
398                 $post_id = $wp_query->get_queried_object_id();
399                 $post = $wp_query->get_queried_object();
400
401                 $classes[] = 'single';
402                 $classes[] = 'single-' . sanitize_html_class($post->post_type, $post_id);
403                 $classes[] = 'postid-' . $post_id;
404
405                 if ( is_attachment() ) {
406                         $mime_type = get_post_mime_type($post_id);
407                         $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
408                         $classes[] = 'attachmentid-' . $post_id;
409                         $classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type );
410                 }
411         } elseif ( is_archive() ) {
412                 if ( is_author() ) {
413                         $author = $wp_query->get_queried_object();
414                         $classes[] = 'author';
415                         $classes[] = 'author-' . sanitize_html_class( $author->user_nicename , $author->ID );
416                 } elseif ( is_category() ) {
417                         $cat = $wp_query->get_queried_object();
418                         $classes[] = 'category';
419                         $classes[] = 'category-' . sanitize_html_class( $cat->slug, $cat->cat_ID );
420                 } elseif ( is_tag() ) {
421                         $tags = $wp_query->get_queried_object();
422                         $classes[] = 'tag';
423                         $classes[] = 'tag-' . sanitize_html_class( $tags->slug, $tags->term_id );
424                 }
425         } elseif ( is_page() ) {
426                 $classes[] = 'page';
427
428                 $page_id = $wp_query->get_queried_object_id();
429
430                 $post = get_page($page_id);
431
432                 $classes[] = 'page-id-' . $page_id;
433
434                 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) ) )
435                         $classes[] = 'page-parent';
436
437                 if ( $post->post_parent ) {
438                         $classes[] = 'page-child';
439                         $classes[] = 'parent-pageid-' . $post->post_parent;
440                 }
441                 if ( is_page_template() ) {
442                         $classes[] = 'page-template';
443                         $classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', get_post_meta( $page_id, '_wp_page_template', true ) ), '' );
444                 }
445         } elseif ( is_search() ) {
446                 if ( !empty( $wp_query->posts ) )
447                         $classes[] = 'search-results';
448                 else
449                         $classes[] = 'search-no-results';
450         }
451
452         if ( is_user_logged_in() )
453                 $classes[] = 'logged-in';
454
455         $page = $wp_query->get( 'page' );
456
457         if ( !$page || $page < 2)
458                 $page = $wp_query->get( 'paged' );
459
460         if ( $page && $page > 1 ) {
461                 $classes[] = 'paged-' . $page;
462
463                 if ( is_single() )
464                         $classes[] = 'single-paged-' . $page;
465                 elseif ( is_page() )
466                         $classes[] = 'page-paged-' . $page;
467                 elseif ( is_category() )
468                         $classes[] = 'category-paged-' . $page;
469                 elseif ( is_tag() )
470                         $classes[] = 'tag-paged-' . $page;
471                 elseif ( is_date() )
472                         $classes[] = 'date-paged-' . $page;
473                 elseif ( is_author() )
474                         $classes[] = 'author-paged-' . $page;
475                 elseif ( is_search() )
476                         $classes[] = 'search-paged-' . $page;
477         }
478
479         if ( !empty( $class ) ) {
480                 if ( !is_array( $class ) )
481                         $class = preg_split( '#\s+#', $class );
482                 $classes = array_merge( $classes, $class );
483         }
484
485         $classes = array_map( 'esc_attr', $classes );
486
487         return apply_filters( 'body_class', $classes, $class );
488 }
489
490 /**
491  * Whether post requires password and correct password has been provided.
492  *
493  * @since 2.7.0
494  *
495  * @param int|object $post An optional post.  Global $post used if not provided.
496  * @return bool false if a password is not required or the correct password cookie is present, true otherwise.
497  */
498 function post_password_required( $post = null ) {
499         $post = get_post($post);
500
501         if ( empty($post->post_password) )
502                 return false;
503
504         if ( !isset($_COOKIE['wp-postpass_' . COOKIEHASH]) )
505                 return true;
506
507         if ( $_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password )
508                 return true;
509
510         return false;
511 }
512
513 /**
514  * Display "sticky" CSS class, if a post is sticky.
515  *
516  * @since 2.7.0
517  *
518  * @param int $post_id An optional post ID.
519  */
520 function sticky_class( $post_id = null ) {
521         if ( !is_sticky($post_id) )
522                 return;
523
524         echo " sticky";
525 }
526
527 /**
528  * Page Template Functions for usage in Themes
529  *
530  * @package WordPress
531  * @subpackage Template
532  */
533
534 /**
535  * The formatted output of a list of pages.
536  *
537  * Displays page links for paginated posts (i.e. includes the <!--nextpage-->.
538  * Quicktag one or more times). This tag must be within The Loop.
539  *
540  * The defaults for overwriting are:
541  * 'next_or_number' - Default is 'number' (string). Indicates whether page
542  *      numbers should be used. Valid values are number and next.
543  * 'nextpagelink' - Default is 'Next Page' (string). Text for link to next page.
544  *      of the bookmark.
545  * 'previouspagelink' - Default is 'Previous Page' (string). Text for link to
546  *      previous page, if available.
547  * 'pagelink' - Default is '%' (String).Format string for page numbers. The % in
548  *      the parameter string will be replaced with the page number, so Page %
549  *      generates "Page 1", "Page 2", etc. Defaults to %, just the page number.
550  * 'before' - Default is '<p> Pages:' (string). The html or text to prepend to
551  *      each bookmarks.
552  * 'after' - Default is '</p>' (string). The html or text to append to each
553  *      bookmarks.
554  * 'link_before' - Default is '' (string). The html or text to prepend to each
555  *      Pages link inside the <a> tag. Also prepended to the current item, which
556  *      is not linked.
557  * 'link_after' - Default is '' (string). The html or text to append to each
558  *      Pages link inside the <a> tag. Also appended to the current item, which
559  *      is not linked.
560  *
561  * @since 1.2.0
562  * @access private
563  *
564  * @param string|array $args Optional. Overwrite the defaults.
565  * @return string Formatted output in HTML.
566  */
567 function wp_link_pages($args = '') {
568         $defaults = array(
569                 'before' => '<p>' . __('Pages:'), 'after' => '</p>',
570                 'link_before' => '', 'link_after' => '',
571                 'next_or_number' => 'number', 'nextpagelink' => __('Next page'),
572                 'previouspagelink' => __('Previous page'), 'pagelink' => '%',
573                 'echo' => 1
574         );
575
576         $r = wp_parse_args( $args, $defaults );
577         $r = apply_filters( 'wp_link_pages_args', $r );
578         extract( $r, EXTR_SKIP );
579
580         global $post, $page, $numpages, $multipage, $more, $pagenow;
581
582         $output = '';
583         if ( $multipage ) {
584                 if ( 'number' == $next_or_number ) {
585                         $output .= $before;
586                         for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) {
587                                 $j = str_replace('%',$i,$pagelink);
588                                 $output .= ' ';
589                                 if ( ($i != $page) || ((!$more) && ($page==1)) ) {
590                                         if ( 1 == $i ) {
591                                                 $output .= '<a href="' . get_permalink() . '">';
592                                         } else {
593                                                 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
594                                                         $output .= '<a href="' . add_query_arg('page', $i, get_permalink()) . '">';
595                                                 elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') == $post->ID )
596                                                         $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit('page/' . $i, 'single_paged'). '">';
597                                                 else
598                                                         $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">';
599                                         }
600
601                                 }
602                                 $output .= $link_before;
603                                 $output .= $j;
604                                 $output .= $link_after;
605                                 if ( ($i != $page) || ((!$more) && ($page==1)) )
606                                         $output .= '</a>';
607                         }
608                         $output .= $after;
609                 } else {
610                         if ( $more ) {
611                                 $output .= $before;
612                                 $i = $page - 1;
613                                 if ( $i && $more ) {
614                                         if ( 1 == $i ) {
615                                                 $output .= '<a href="' . get_permalink() . '">';
616                                         } else {
617                                                 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
618                                                         $output .= '<a href="' . add_query_arg('page', $i, get_permalink()) . '">';
619                                                 elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') == $post->ID )
620                                                         $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit('page/' . $i, 'single_paged'). '">';
621                                                 else
622                                                         $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">';
623                                         }
624                                         $output .= $link_before. $previouspagelink . $link_after . '</a>';
625                                 }
626                                 $i = $page + 1;
627                                 if ( $i <= $numpages && $more ) {
628                                         if ( 1 == $i ) {
629                                                 $output .= '<a href="' . get_permalink() . '">';
630                                         } else {
631                                                 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
632                                                         $output .= '<a href="' . add_query_arg('page', $i, get_permalink()) . '">';
633                                                 elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') == $post->ID )
634                                                         $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit('page/' . $i, 'single_paged'). '">';
635                                                 else
636                                                         $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">';
637                                         }
638                                         $output .= $link_before. $nextpagelink . $link_after . '</a>';
639                                 }
640                                 $output .= $after;
641                         }
642                 }
643         }
644
645         if ( $echo )
646                 echo $output;
647
648         return $output;
649 }
650
651
652 //
653 // Post-meta: Custom per-post fields.
654 //
655
656 /**
657  * Retrieve post custom meta data field.
658  *
659  * @since 1.5.0
660  *
661  * @param string $key Meta data key name.
662  * @return bool|string|array Array of values or single value, if only one element exists. False will be returned if key does not exist.
663  */
664 function post_custom( $key = '' ) {
665         $custom = get_post_custom();
666
667         if ( !isset( $custom[$key] ) )
668                 return false;
669         elseif ( 1 == count($custom[$key]) )
670                 return $custom[$key][0];
671         else
672                 return $custom[$key];
673 }
674
675 /**
676  * Display list of post custom fields.
677  *
678  * @internal This will probably change at some point...
679  * @since 1.2.0
680  * @uses apply_filters() Calls 'the_meta_key' on list item HTML content, with key and value as separate parameters.
681  */
682 function the_meta() {
683         if ( $keys = get_post_custom_keys() ) {
684                 echo "<ul class='post-meta'>\n";
685                 foreach ( (array) $keys as $key ) {
686                         $keyt = trim($key);
687                         if ( '_' == $keyt{0} )
688                                 continue;
689                         $values = array_map('trim', get_post_custom_values($key));
690                         $value = implode($values,', ');
691                         echo apply_filters('the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value);
692                 }
693                 echo "</ul>\n";
694         }
695 }
696
697 //
698 // Pages
699 //
700
701 /**
702  * Retrieve or display list of pages as a dropdown (select list).
703  *
704  * @since 2.1.0
705  *
706  * @param array|string $args Optional. Override default arguments.
707  * @return string HTML content, if not displaying.
708  */
709 function wp_dropdown_pages($args = '') {
710         $defaults = array(
711                 'depth' => 0, 'child_of' => 0,
712                 'selected' => 0, 'echo' => 1,
713                 'name' => 'page_id', 'id' => '',
714                 'show_option_none' => '', 'show_option_no_change' => '',
715                 'option_none_value' => ''
716         );
717
718         $r = wp_parse_args( $args, $defaults );
719         extract( $r, EXTR_SKIP );
720
721         $pages = get_pages($r);
722         $output = '';
723         $name = esc_attr($name);
724         // Back-compat with old system where both id and name were based on $name argument
725         if ( empty($id) )
726                 $id = $name;
727
728         if ( ! empty($pages) ) {
729                 $output = "<select name=\"$name\" id=\"$id\">\n";
730                 if ( $show_option_no_change )
731                         $output .= "\t<option value=\"-1\">$show_option_no_change</option>";
732                 if ( $show_option_none )
733                         $output .= "\t<option value=\"" . esc_attr($option_none_value) . "\">$show_option_none</option>\n";
734                 $output .= walk_page_dropdown_tree($pages, $depth, $r);
735                 $output .= "</select>\n";
736         }
737
738         $output = apply_filters('wp_dropdown_pages', $output);
739
740         if ( $echo )
741                 echo $output;
742
743         return $output;
744 }
745
746 /**
747  * Retrieve or display list of pages in list (li) format.
748  *
749  * @since 1.5.0
750  *
751  * @param array|string $args Optional. Override default arguments.
752  * @return string HTML content, if not displaying.
753  */
754 function wp_list_pages($args = '') {
755         $defaults = array(
756                 'depth' => 0, 'show_date' => '',
757                 'date_format' => get_option('date_format'),
758                 'child_of' => 0, 'exclude' => '',
759                 'title_li' => __('Pages'), 'echo' => 1,
760                 'authors' => '', 'sort_column' => 'menu_order, post_title',
761                 'link_before' => '', 'link_after' => '', 'walker' => '',
762         );
763
764         $r = wp_parse_args( $args, $defaults );
765         extract( $r, EXTR_SKIP );
766
767         $output = '';
768         $current_page = 0;
769
770         // sanitize, mostly to keep spaces out
771         $r['exclude'] = preg_replace('/[^0-9,]/', '', $r['exclude']);
772
773         // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array)
774         $exclude_array = ( $r['exclude'] ) ? explode(',', $r['exclude']) : array();
775         $r['exclude'] = implode( ',', apply_filters('wp_list_pages_excludes', $exclude_array) );
776
777         // Query pages.
778         $r['hierarchical'] = 0;
779         $pages = get_pages($r);
780
781         if ( !empty($pages) ) {
782                 if ( $r['title_li'] )
783                         $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
784
785                 global $wp_query;
786                 if ( is_page() || is_attachment() || $wp_query->is_posts_page )
787                         $current_page = $wp_query->get_queried_object_id();
788                 $output .= walk_page_tree($pages, $r['depth'], $current_page, $r);
789
790                 if ( $r['title_li'] )
791                         $output .= '</ul></li>';
792         }
793
794         $output = apply_filters('wp_list_pages', $output, $r);
795
796         if ( $r['echo'] )
797                 echo $output;
798         else
799                 return $output;
800 }
801
802 /**
803  * Display or retrieve list of pages with optional home link.
804  *
805  * The arguments are listed below and part of the arguments are for {@link
806  * wp_list_pages()} function. Check that function for more info on those
807  * arguments.
808  *
809  * <ul>
810  * <li><strong>sort_column</strong> - How to sort the list of pages. Defaults
811  * to page title. Use column for posts table.</li>
812  * <li><strong>menu_class</strong> - Class to use for the div ID which contains
813  * the page list. Defaults to 'menu'.</li>
814  * <li><strong>echo</strong> - Whether to echo list or return it. Defaults to
815  * echo.</li>
816  * <li><strong>link_before</strong> - Text before show_home argument text.</li>
817  * <li><strong>link_after</strong> - Text after show_home argument text.</li>
818  * <li><strong>show_home</strong> - If you set this argument, then it will
819  * display the link to the home page. The show_home argument really just needs
820  * to be set to the value of the text of the link.</li>
821  * </ul>
822  *
823  * @since 2.7.0
824  *
825  * @param array|string $args
826  */
827 function wp_page_menu( $args = array() ) {
828         $defaults = array('sort_column' => 'menu_order, post_title', 'menu_class' => 'menu', 'echo' => true, 'link_before' => '', 'link_after' => '');
829         $args = wp_parse_args( $args, $defaults );
830         $args = apply_filters( 'wp_page_menu_args', $args );
831
832         $menu = '';
833
834         $list_args = $args;
835
836         // Show Home in the menu
837         if ( ! empty($args['show_home']) ) {
838                 if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] )
839                         $text = __('Home');
840                 else
841                         $text = $args['show_home'];
842                 $class = '';
843                 if ( is_front_page() && !is_paged() )
844                         $class = 'class="current_page_item"';
845                 $menu .= '<li ' . $class . '><a href="' . home_url( '/' ) . '" title="' . esc_attr($text) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>';
846                 // If the front page is a page, add it to the exclude list
847                 if (get_option('show_on_front') == 'page') {
848                         if ( !empty( $list_args['exclude'] ) ) {
849                                 $list_args['exclude'] .= ',';
850                         } else {
851                                 $list_args['exclude'] = '';
852                         }
853                         $list_args['exclude'] .= get_option('page_on_front');
854                 }
855         }
856
857         $list_args['echo'] = false;
858         $list_args['title_li'] = '';
859         $menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages($list_args) );
860
861         if ( $menu )
862                 $menu = '<ul>' . $menu . '</ul>';
863
864         $menu = '<div class="' . esc_attr($args['menu_class']) . '">' . $menu . "</div>\n";
865         $menu = apply_filters( 'wp_page_menu', $menu, $args );
866         if ( $args['echo'] )
867                 echo $menu;
868         else
869                 return $menu;
870 }
871
872 //
873 // Page helpers
874 //
875
876 /**
877  * Retrieve HTML list content for page list.
878  *
879  * @uses Walker_Page to create HTML list content.
880  * @since 2.1.0
881  * @see Walker_Page::walk() for parameters and return description.
882  */
883 function walk_page_tree($pages, $depth, $current_page, $r) {
884         if ( empty($r['walker']) )
885                 $walker = new Walker_Page;
886         else
887                 $walker = $r['walker'];
888
889         $args = array($pages, $depth, $r, $current_page);
890         return call_user_func_array(array(&$walker, 'walk'), $args);
891 }
892
893 /**
894  * Retrieve HTML dropdown (select) content for page list.
895  *
896  * @uses Walker_PageDropdown to create HTML dropdown content.
897  * @since 2.1.0
898  * @see Walker_PageDropdown::walk() for parameters and return description.
899  */
900 function walk_page_dropdown_tree() {
901         $args = func_get_args();
902         if ( empty($args[2]['walker']) ) // the user's options are the third parameter
903                 $walker = new Walker_PageDropdown;
904         else
905                 $walker = $args[2]['walker'];
906
907         return call_user_func_array(array(&$walker, 'walk'), $args);
908 }
909
910 //
911 // Attachments
912 //
913
914 /**
915  * Display an attachment page link using an image or icon.
916  *
917  * @since 2.0.0
918  *
919  * @param int $id Optional. Post ID.
920  * @param bool $fullsize Optional, default is false. Whether to use full size.
921  * @param bool $deprecated Deprecated. Not used.
922  * @param bool $permalink Optional, default is false. Whether to include permalink.
923  */
924 function the_attachment_link( $id = 0, $fullsize = false, $deprecated = false, $permalink = false ) {
925         if ( !empty( $deprecated ) )
926                 _deprecated_argument( __FUNCTION__, '2.5' );
927
928         if ( $fullsize )
929                 echo wp_get_attachment_link($id, 'full', $permalink);
930         else
931                 echo wp_get_attachment_link($id, 'thumbnail', $permalink);
932 }
933
934 /**
935  * Retrieve an attachment page link using an image or icon, if possible.
936  *
937  * @since 2.5.0
938  * @uses apply_filters() Calls 'wp_get_attachment_link' filter on HTML content with same parameters as function.
939  *
940  * @param int $id Optional. Post ID.
941  * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string.
942  * @param bool $permalink Optional, default is false. Whether to add permalink to image.
943  * @param bool $icon Optional, default is false. Whether to include icon.
944  * @param string $text Optional, default is false. If string, then will be link text.
945  * @return string HTML content.
946  */
947 function wp_get_attachment_link($id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false) {
948         $id = intval($id);
949         $_post = & get_post( $id );
950
951         if ( ('attachment' != $_post->post_type) || !$url = wp_get_attachment_url($_post->ID) )
952                 return __('Missing Attachment');
953
954         if ( $permalink )
955                 $url = get_attachment_link($_post->ID);
956
957         $post_title = esc_attr($_post->post_title);
958
959         if ( $text ) {
960                 $link_text = esc_attr($text);
961         } elseif ( ( is_int($size) && $size != 0 ) or ( is_string($size) && $size != 'none' ) or $size != false ) {
962                 $link_text = wp_get_attachment_image($id, $size, $icon);
963         } else {
964                 $link_text = '';
965         }
966
967         if( trim($link_text) == '' )
968                 $link_text = $_post->post_title;
969
970         return apply_filters( 'wp_get_attachment_link', "<a href='$url' title='$post_title'>$link_text</a>", $id, $size, $permalink, $icon, $text );
971 }
972
973 /**
974  * Wrap attachment in <<p>> element before content.
975  *
976  * @since 2.0.0
977  * @uses apply_filters() Calls 'prepend_attachment' hook on HTML content.
978  *
979  * @param string $content
980  * @return string
981  */
982 function prepend_attachment($content) {
983         global $post;
984
985         if ( empty($post->post_type) || $post->post_type != 'attachment' )
986                 return $content;
987
988         $p = '<p class="attachment">';
989         // show the medium sized image representation of the attachment if available, and link to the raw file
990         $p .= wp_get_attachment_link(0, 'medium', false);
991         $p .= '</p>';
992         $p = apply_filters('prepend_attachment', $p);
993
994         return "$p\n$content";
995 }
996
997 //
998 // Misc
999 //
1000
1001 /**
1002  * Retrieve protected post password form content.
1003  *
1004  * @since 1.0.0
1005  * @uses apply_filters() Calls 'the_password_form' filter on output.
1006  *
1007  * @return string HTML content for password form for password protected post.
1008  */
1009 function get_the_password_form() {
1010         global $post;
1011         $label = 'pwbox-'.(empty($post->ID) ? rand() : $post->ID);
1012         $output = '<form action="' . get_option('siteurl') . '/wp-pass.php" method="post">
1013         <p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
1014         <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>
1015         </form>
1016         ';
1017         return apply_filters('the_password_form', $output);
1018 }
1019
1020 /**
1021  * Whether currently in a page template.
1022  *
1023  * This template tag allows you to determine if you are in a page template.
1024  * You can optionally provide a template name and then the check will be
1025  * specific to that template.
1026  *
1027  * @since 2.5.0
1028  * @uses $wp_query
1029  *
1030  * @param string $template The specific template name if specific matching is required.
1031  * @return bool False on failure, true if success.
1032  */
1033 function is_page_template($template = '') {
1034         if (!is_page()) {
1035                 return false;
1036         }
1037
1038         global $wp_query;
1039
1040         $page = $wp_query->get_queried_object();
1041         $custom_fields = get_post_custom_values('_wp_page_template',$page->ID);
1042         $page_template = $custom_fields[0];
1043
1044         // We have no argument passed so just see if a page_template has been specified
1045         if ( empty( $template ) ) {
1046                 if (!empty( $page_template ) ) {
1047                         return true;
1048                 }
1049         } elseif ( $template == $page_template) {
1050                 return true;
1051         }
1052
1053         return false;
1054 }
1055
1056 /**
1057  * Retrieve formatted date timestamp of a revision (linked to that revisions's page).
1058  *
1059  * @package WordPress
1060  * @subpackage Post_Revisions
1061  * @since 2.6.0
1062  *
1063  * @uses date_i18n()
1064  *
1065  * @param int|object $revision Revision ID or revision object.
1066  * @param bool $link Optional, default is true. Link to revisions's page?
1067  * @return string i18n formatted datetimestamp or localized 'Current Revision'.
1068  */
1069 function wp_post_revision_title( $revision, $link = true ) {
1070         if ( !$revision = get_post( $revision ) )
1071                 return $revision;
1072
1073         if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
1074                 return false;
1075
1076         /* translators: revision date format, see http://php.net/date */
1077         $datef = _x( 'j F, Y @ G:i', 'revision date format');
1078         /* translators: 1: date */
1079         $autosavef = __( '%1$s [Autosave]' );
1080         /* translators: 1: date */
1081         $currentf  = __( '%1$s [Current Revision]' );
1082
1083         $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
1084         if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
1085                 $date = "<a href='$link'>$date</a>";
1086
1087         if ( !wp_is_post_revision( $revision ) )
1088                 $date = sprintf( $currentf, $date );
1089         elseif ( wp_is_post_autosave( $revision ) )
1090                 $date = sprintf( $autosavef, $date );
1091
1092         return $date;
1093 }
1094
1095 /**
1096  * Display list of a post's revisions.
1097  *
1098  * Can output either a UL with edit links or a TABLE with diff interface, and
1099  * restore action links.
1100  *
1101  * Second argument controls parameters:
1102  *   (bool)   parent : include the parent (the "Current Revision") in the list.
1103  *   (string) format : 'list' or 'form-table'.  'list' outputs UL, 'form-table'
1104  *                     outputs TABLE with UI.
1105  *   (int)    right  : what revision is currently being viewed - used in
1106  *                     form-table format.
1107  *   (int)    left   : what revision is currently being diffed against right -
1108  *                     used in form-table format.
1109  *
1110  * @package WordPress
1111  * @subpackage Post_Revisions
1112  * @since 2.6.0
1113  *
1114  * @uses wp_get_post_revisions()
1115  * @uses wp_post_revision_title()
1116  * @uses get_edit_post_link()
1117  * @uses get_the_author_meta()
1118  *
1119  * @todo split into two functions (list, form-table) ?
1120  *
1121  * @param int|object $post_id Post ID or post object.
1122  * @param string|array $args See description {@link wp_parse_args()}.
1123  * @return null
1124  */
1125 function wp_list_post_revisions( $post_id = 0, $args = null ) {
1126         if ( !$post = get_post( $post_id ) )
1127                 return;
1128
1129         $defaults = array( 'parent' => false, 'right' => false, 'left' => false, 'format' => 'list', 'type' => 'all' );
1130         extract( wp_parse_args( $args, $defaults ), EXTR_SKIP );
1131
1132         switch ( $type ) {
1133                 case 'autosave' :
1134                         if ( !$autosave = wp_get_post_autosave( $post->ID ) )
1135                                 return;
1136                         $revisions = array( $autosave );
1137                         break;
1138                 case 'revision' : // just revisions - remove autosave later
1139                 case 'all' :
1140                 default :
1141                         if ( !$revisions = wp_get_post_revisions( $post->ID ) )
1142                                 return;
1143                         break;
1144         }
1145
1146         /* translators: post revision: 1: when, 2: author name */
1147         $titlef = _x( '%1$s by %2$s', 'post revision' );
1148
1149         if ( $parent )
1150                 array_unshift( $revisions, $post );
1151
1152         $rows = '';
1153         $class = false;
1154         $can_edit_post = current_user_can( 'edit_post', $post->ID );
1155         foreach ( $revisions as $revision ) {
1156                 if ( !current_user_can( 'read_post', $revision->ID ) )
1157                         continue;
1158                 if ( 'revision' === $type && wp_is_post_autosave( $revision ) )
1159                         continue;
1160
1161                 $date = wp_post_revision_title( $revision );
1162                 $name = get_the_author_meta( 'display_name', $revision->post_author );
1163
1164                 if ( 'form-table' == $format ) {
1165                         if ( $left )
1166                                 $left_checked = $left == $revision->ID ? ' checked="checked"' : '';
1167                         else
1168                                 $left_checked = $right_checked ? ' checked="checked"' : ''; // [sic] (the next one)
1169                         $right_checked = $right == $revision->ID ? ' checked="checked"' : '';
1170
1171                         $class = $class ? '' : " class='alternate'";
1172
1173                         if ( $post->ID != $revision->ID && $can_edit_post )
1174                                 $actions = '<a href="' . wp_nonce_url( add_query_arg( array( 'revision' => $revision->ID, 'action' => 'restore' ) ), "restore-post_$post->ID|$revision->ID" ) . '">' . __( 'Restore' ) . '</a>';
1175                         else
1176                                 $actions = '';
1177
1178                         $rows .= "<tr$class>\n";
1179                         $rows .= "\t<th style='white-space: nowrap' scope='row'><input type='radio' name='left' value='$revision->ID'$left_checked /></th>\n";
1180                         $rows .= "\t<th style='white-space: nowrap' scope='row'><input type='radio' name='right' value='$revision->ID'$right_checked /></th>\n";
1181                         $rows .= "\t<td>$date</td>\n";
1182                         $rows .= "\t<td>$name</td>\n";
1183                         $rows .= "\t<td class='action-links'>$actions</td>\n";
1184                         $rows .= "</tr>\n";
1185                 } else {
1186                         $title = sprintf( $titlef, $date, $name );
1187                         $rows .= "\t<li>$title</li>\n";
1188                 }
1189         }
1190
1191         if ( 'form-table' == $format ) : ?>
1192
1193 <form action="revision.php" method="get">
1194
1195 <div class="tablenav">
1196         <div class="alignleft">
1197                 <input type="submit" class="button-secondary" value="<?php esc_attr_e( 'Compare Revisions' ); ?>" />
1198                 <input type="hidden" name="action" value="diff" />
1199                 <input type="hidden" name="post_type" value="<?php echo esc_attr($post->post_type); ?>" />
1200         </div>
1201 </div>
1202
1203 <br class="clear" />
1204
1205 <table class="widefat post-revisions" cellspacing="0" id="post-revisions">
1206         <col />
1207         <col />
1208         <col style="width: 33%" />
1209         <col style="width: 33%" />
1210         <col style="width: 33%" />
1211 <thead>
1212 <tr>
1213         <th scope="col"><?php /* translators: column name in revisons */ _ex( 'Old', 'revisions column name' ); ?></th>
1214         <th scope="col"><?php /* translators: column name in revisons */ _ex( 'New', 'revisions column name' ); ?></th>
1215         <th scope="col"><?php /* translators: column name in revisons */ _ex( 'Date Created', 'revisions column name' ); ?></th>
1216         <th scope="col"><?php _e( 'Author' ); ?></th>
1217         <th scope="col" class="action-links"><?php _e( 'Actions' ); ?></th>
1218 </tr>
1219 </thead>
1220 <tbody>
1221
1222 <?php echo $rows; ?>
1223
1224 </tbody>
1225 </table>
1226
1227 </form>
1228
1229 <?php
1230         else :
1231                 echo "<ul class='post-revisions'>\n";
1232                 echo $rows;
1233                 echo "</ul>";
1234         endif;
1235
1236 }