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