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