]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/post-template.php
Wordpress 2.5.1
[autoinstalls/wordpress.git] / wp-includes / post-template.php
1 <?php
2
3 //
4 // "The Loop" post functions
5 //
6
7 function the_ID() {
8         global $id;
9         echo $id;
10 }
11
12
13 function get_the_ID() {
14         global $id;
15         return $id;
16 }
17
18
19 function the_title($before = '', $after = '', $echo = true) {
20         $title = get_the_title();
21
22         if ( strlen($title) == 0 )
23                 return;
24
25         $title = $before . $title . $after;
26
27         if ( $echo )
28                 echo $title;
29         else
30                 return $title;
31 }
32
33 function the_title_attribute( $args = '' ) {
34         $title = get_the_title();
35
36         if ( strlen($title) == 0 )
37                 return;
38
39         $defaults = array('before' => '', 'after' =>  '', 'echo' => true);
40         $r = wp_parse_args($args, $defaults);
41         extract( $r, EXTR_SKIP );
42
43
44         $title = $before . $title . $after;
45         $title = attribute_escape(strip_tags($title));
46
47         if ( $echo )
48                 echo $title;
49         else
50                 return $title;
51 }
52
53 function get_the_title( $id = 0 ) {
54         $post = &get_post($id);
55
56         $title = $post->post_title;
57
58         if ( !is_admin() ) {
59                 if ( !empty($post->post_password) )
60                         $title = sprintf(__('Protected: %s'), $title);
61                 else if ( isset($post->post_status) && 'private' == $post->post_status )
62                         $title = sprintf(__('Private: %s'), $title);
63         }
64         return apply_filters( 'the_title', $title );
65 }
66
67 function the_guid( $id = 0 ) {
68         echo get_the_guid($id);
69 }
70
71 function get_the_guid( $id = 0 ) {
72         $post = &get_post($id);
73
74         return apply_filters('get_the_guid', $post->guid);
75 }
76
77 function the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
78         $content = get_the_content($more_link_text, $stripteaser, $more_file);
79         $content = apply_filters('the_content', $content);
80         $content = str_replace(']]>', ']]&gt;', $content);
81         echo $content;
82 }
83
84
85 function get_the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
86         global $id, $post, $more, $page, $pages, $multipage, $preview, $pagenow;
87
88         $output = '';
89
90         if ( !empty($post->post_password) ) { // if there's a password
91                 if ( !isset($_COOKIE['wp-postpass_'.COOKIEHASH]) || stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password ) {       // and it doesn't match the cookie
92                         $output = get_the_password_form();
93                         return $output;
94                 }
95         }
96
97         if ( $more_file != '' )
98                 $file = $more_file;
99         else
100                 $file = $pagenow; //$_SERVER['PHP_SELF'];
101
102         if ( $page > count($pages) ) // if the requested page doesn't exist
103                 $page = count($pages); // give them the highest numbered page that DOES exist
104
105         $content = $pages[$page-1];
106         if ( preg_match('/<!--more(.*?)?-->/', $content, $matches) ) {
107                 $content = explode($matches[0], $content, 2);
108                 if ( !empty($matches[1]) && !empty($more_link_text) )
109                         $more_link_text = strip_tags(wp_kses_no_null(trim($matches[1])));
110         } else {
111                 $content = array($content);
112         }
113         if ( (false !== strpos($post->post_content, '<!--noteaser-->') && ((!$multipage) || ($page==1))) )
114                 $stripteaser = 1;
115         $teaser = $content[0];
116         if ( ($more) && ($stripteaser) )
117                 $teaser = '';
118         $output .= $teaser;
119         if ( count($content) > 1 ) {
120                 if ( $more ) {
121                         $output .= '<span id="more-'.$id.'"></span>'.$content[1];
122                 } else {
123                         $output = balanceTags($output);
124                         if ( ! empty($more_link_text) )
125                                 $output .= ' <a href="'. get_permalink() . "#more-$id\" class=\"more-link\">$more_link_text</a>";
126                 }
127
128         }
129         if ( $preview ) // preview fix for javascript bug with foreign languages
130                 $output =       preg_replace('/\%u([0-9A-F]{4,4})/e',   "'&#'.base_convert('\\1',16,10).';'", $output);
131
132         return $output;
133 }
134
135
136 function the_excerpt() {
137         echo apply_filters('the_excerpt', get_the_excerpt());
138 }
139
140
141 function get_the_excerpt($deprecated = '') {
142         global $post;
143         $output = '';
144         $output = $post->post_excerpt;
145         if ( !empty($post->post_password) ) { // if there's a password
146                 if ( !isset($_COOKIE['wp-postpass_'.COOKIEHASH]) || $_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password ) {  // and it doesn't match the cookie
147                         $output = __('There is no excerpt because this is a protected post.');
148                         return $output;
149                 }
150         }
151
152         return apply_filters('get_the_excerpt', $output);
153 }
154
155 function has_excerpt( $id = 0 ) {
156         $post = &get_post( $id );
157         return ( !empty( $post->post_excerpt ) );
158 }
159
160 function wp_link_pages($args = '') {
161         $defaults = array(
162                 'before' => '<p>' . __('Pages:'), 'after' => '</p>',
163                 'next_or_number' => 'number', 'nextpagelink' => __('Next page'),
164                 'previouspagelink' => __('Previous page'), 'pagelink' => '%',
165                 'more_file' => '', 'echo' => 1
166         );
167
168         $r = wp_parse_args( $args, $defaults );
169         extract( $r, EXTR_SKIP );
170
171         global $post, $page, $numpages, $multipage, $more, $pagenow;
172         if ( $more_file != '' )
173                 $file = $more_file;
174         else
175                 $file = $pagenow;
176
177         $output = '';
178         if ( $multipage ) {
179                 if ( 'number' == $next_or_number ) {
180                         $output .= $before;
181                         for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) {
182                                 $j = str_replace('%',"$i",$pagelink);
183                                 $output .= ' ';
184                                 if ( ($i != $page) || ((!$more) && ($page==1)) ) {
185                                         if ( 1 == $i ) {
186                                                 $output .= '<a href="' . get_permalink() . '">';
187                                         } else {
188                                                 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
189                                                         $output .= '<a href="' . get_permalink() . '&amp;page=' . $i . '">';
190                                                 else
191                                                         $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">';
192                                         }
193                                 }
194                                 $output .= $j;
195                                 if ( ($i != $page) || ((!$more) && ($page==1)) )
196                                         $output .= '</a>';
197                         }
198                         $output .= $after;
199                 } else {
200                         if ( $more ) {
201                                 $output .= $before;
202                                 $i = $page - 1;
203                                 if ( $i && $more ) {
204                                         if ( 1 == $i ) {
205                                                 $output .= '<a href="' . get_permalink() . '">' . $previouspagelink . '</a>';
206                                         } else {
207                                                 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
208                                                         $output .= '<a href="' . get_permalink() . '&amp;page=' . $i . '">' . $previouspagelink . '</a>';
209                                                 else
210                                                         $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">' . $previouspagelink . '</a>';
211                                         }
212                                 }
213                                 $i = $page + 1;
214                                 if ( $i <= $numpages && $more ) {
215                                         if ( 1 == $i ) {
216                                                 $output .= '<a href="' . get_permalink() . '">' . $nextpagelink . '</a>';
217                                         } else {
218                                                 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
219                                                         $output .= '<a href="' . get_permalink() . '&amp;page=' . $i . '">' . $nextpagelink . '</a>';
220                                                 else
221                                                         $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">' . $nextpagelink . '</a>';
222                                         }
223                                 }
224                                 $output .= $after;
225                         }
226                 }
227         }
228
229         if ( $echo )
230                 echo $output;
231
232         return $output;
233 }
234
235
236 //
237 // Post-meta: Custom per-post fields.
238 //
239
240
241 function post_custom( $key = '' ) {
242         $custom = get_post_custom();
243
244         if ( 1 == count($custom[$key]) )
245                 return $custom[$key][0];
246         else
247                 return $custom[$key];
248 }
249
250
251 // this will probably change at some point...
252 function the_meta() {
253         if ( $keys = get_post_custom_keys() ) {
254                 echo "<ul class='post-meta'>\n";
255                 foreach ( $keys as $key ) {
256                         $keyt = trim($key);
257                         if ( '_' == $keyt{0} )
258                                 continue;
259                         $values = array_map('trim', get_post_custom_values($key));
260                         $value = implode($values,', ');
261                         echo apply_filters('the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value);
262                 }
263                 echo "</ul>\n";
264         }
265 }
266
267
268 //
269 // Pages
270 //
271
272 function wp_dropdown_pages($args = '') {
273         $defaults = array(
274                 'depth' => 0, 'child_of' => 0,
275                 'selected' => 0, 'echo' => 1,
276                 'name' => 'page_id', 'show_option_none' => ''
277         );
278
279         $r = wp_parse_args( $args, $defaults );
280         extract( $r, EXTR_SKIP );
281
282         $pages = get_pages($r);
283         $output = '';
284
285         if ( ! empty($pages) ) {
286                 $output = "<select name='$name'>\n";
287                 if ( $show_option_none )
288                         $output .= "\t<option value=''>$show_option_none</option>\n";
289                 $output .= walk_page_dropdown_tree($pages, $depth, $r);
290                 $output .= "</select>\n";
291         }
292
293         $output = apply_filters('wp_dropdown_pages', $output);
294
295         if ( $echo )
296                 echo $output;
297
298         return $output;
299 }
300
301 function wp_list_pages($args = '') {
302         $defaults = array(
303                 'depth' => 0, 'show_date' => '',
304                 'date_format' => get_option('date_format'),
305                 'child_of' => 0, 'exclude' => '',
306                 'title_li' => __('Pages'), 'echo' => 1,
307                 'authors' => '', 'sort_column' => 'menu_order, post_title'
308         );
309
310         $r = wp_parse_args( $args, $defaults );
311         extract( $r, EXTR_SKIP );
312
313         $output = '';
314         $current_page = 0;
315
316         // sanitize, mostly to keep spaces out
317         $r['exclude'] = preg_replace('[^0-9,]', '', $r['exclude']);
318
319         // Allow plugins to filter an array of excluded pages
320         $r['exclude'] = implode(',', apply_filters('wp_list_pages_excludes', explode(',', $r['exclude'])));
321
322         // Query pages.
323         $r['hierarchical'] = 0;
324         $pages = get_pages($r);
325
326         if ( !empty($pages) ) {
327                 if ( $r['title_li'] )
328                         $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
329
330                 global $wp_query;
331                 if ( is_page() || $wp_query->is_posts_page )
332                         $current_page = $wp_query->get_queried_object_id();
333                 $output .= walk_page_tree($pages, $r['depth'], $current_page, $r);
334
335                 if ( $r['title_li'] )
336                         $output .= '</ul></li>';
337         }
338
339         $output = apply_filters('wp_list_pages', $output);
340
341         if ( $r['echo'] )
342                 echo $output;
343         else
344                 return $output;
345 }
346
347 //
348 // Page helpers
349 //
350
351 function walk_page_tree() {
352         $walker = new Walker_Page;
353         $args = func_get_args();
354         return call_user_func_array(array(&$walker, 'walk'), $args);
355 }
356
357 function walk_page_dropdown_tree() {
358         $walker = new Walker_PageDropdown;
359         $args = func_get_args();
360         return call_user_func_array(array(&$walker, 'walk'), $args);
361 }
362
363 //
364 // Attachments
365 //
366
367 function the_attachment_link($id = 0, $fullsize = false, $deprecated = false, $permalink = false) {
368         if ( $fullsize )
369                 echo wp_get_attachment_link($id, 'full', $permalink);
370         else
371                 echo wp_get_attachment_link($id, 'thumbnail', $permalink);
372 }
373
374 // get an attachment page link using an image or icon if possible
375 function wp_get_attachment_link($id = 0, $size = 'thumbnail', $permalink = false, $icon = false) {
376         $id = intval($id);
377         $_post = & get_post( $id );
378
379         if ( ('attachment' != $_post->post_type) || !$url = wp_get_attachment_url($_post->ID) )
380                 return __('Missing Attachment');
381
382         if ( $permalink )
383                 $url = get_attachment_link($_post->ID);
384
385         $post_title = attribute_escape($_post->post_title);
386
387         $link_text = wp_get_attachment_image($id, $size, $icon);
388         if ( !$link_text )
389                 $link_text = $_post->post_title;
390
391         return "<a href='$url' title='$post_title'>$link_text</a>";
392
393 }
394
395 // deprecated - use wp_get_attachment_link()
396 function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false, $permalink = false) {
397         $id = (int) $id;
398         $_post = & get_post($id);
399
400         if ( ('attachment' != $_post->post_type) || !$url = wp_get_attachment_url($_post->ID) )
401                 return __('Missing Attachment');
402
403         if ( $permalink )
404                 $url = get_attachment_link($_post->ID);
405
406         $post_title = attribute_escape($_post->post_title);
407
408         $innerHTML = get_attachment_innerHTML($_post->ID, $fullsize, $max_dims);
409         return "<a href='$url' title='$post_title'>$innerHTML</a>";
410 }
411
412
413 // deprecated: use wp_get_attachment_image_src()
414 function get_attachment_icon_src( $id = 0, $fullsize = false ) {
415         $id = (int) $id;
416         if ( !$post = & get_post($id) )
417                 return false;
418
419         $file = get_attached_file( $post->ID );
420
421         if ( !$fullsize && $src = wp_get_attachment_thumb_url( $post->ID ) ) {
422                 // We have a thumbnail desired, specified and existing
423
424                 $src_file = basename($src);
425                 $class = 'attachmentthumb';
426         } elseif ( wp_attachment_is_image( $post->ID ) ) {
427                 // We have an image without a thumbnail
428
429                 $src = wp_get_attachment_url( $post->ID );
430                 $src_file = & $file;
431                 $class = 'attachmentimage';
432         } elseif ( $src = wp_mime_type_icon( $post->ID ) ) {
433                 // No thumb, no image. We'll look for a mime-related icon instead.
434
435                 $icon_dir = apply_filters( 'icon_dir', get_template_directory() . '/images' );
436                 $src_file = $icon_dir . '/' . basename($src);
437         }
438
439         if ( !isset($src) || !$src )
440                 return false;
441
442         return array($src, $src_file);
443 }
444
445 // deprecated: use wp_get_attachment_image()
446 function get_attachment_icon( $id = 0, $fullsize = false, $max_dims = false ) {
447         $id = (int) $id;
448         if ( !$post = & get_post($id) )
449                 return false;
450                 
451         if ( !$src = get_attachment_icon_src( $post->ID, $fullsize ) )
452                 return false;
453
454         list($src, $src_file) = $src;
455
456         // Do we need to constrain the image?
457         if ( ($max_dims = apply_filters('attachment_max_dims', $max_dims)) && file_exists($src_file) ) {
458
459                 $imagesize = getimagesize($src_file);
460
461                 if (($imagesize[0] > $max_dims[0]) || $imagesize[1] > $max_dims[1] ) {
462                         $actual_aspect = $imagesize[0] / $imagesize[1];
463                         $desired_aspect = $max_dims[0] / $max_dims[1];
464
465                         if ( $actual_aspect >= $desired_aspect ) {
466                                 $height = $actual_aspect * $max_dims[0];
467                                 $constraint = "width='{$max_dims[0]}' ";
468                                 $post->iconsize = array($max_dims[0], $height);
469                         } else {
470                                 $width = $max_dims[1] / $actual_aspect;
471                                 $constraint = "height='{$max_dims[1]}' ";
472                                 $post->iconsize = array($width, $max_dims[1]);
473                         }
474                 } else {
475                         $post->iconsize = array($imagesize[0], $imagesize[1]);
476                         $constraint = '';
477                 }
478         } else {
479                 $constraint = '';
480         }
481
482         $post_title = attribute_escape($post->post_title);
483
484         $icon = "<img src='$src' title='$post_title' alt='$post_title' $constraint/>";
485
486         return apply_filters( 'attachment_icon', $icon, $post->ID );
487 }
488
489 // deprecated: use wp_get_attachment_image()
490 function get_attachment_innerHTML($id = 0, $fullsize = false, $max_dims = false) {
491         $id = (int) $id;
492         if ( !$post = & get_post($id) )
493                 return false;
494
495         if ( $innerHTML = get_attachment_icon($post->ID, $fullsize, $max_dims))
496                 return $innerHTML;
497
498
499         $innerHTML = attribute_escape($post->post_title);
500
501         return apply_filters('attachment_innerHTML', $innerHTML, $post->ID);
502 }
503
504 function prepend_attachment($content) {
505         global $post;
506
507         if ( empty($post->post_type) || $post->post_type != 'attachment' )
508                 return $content;
509
510         $p = '<p class="attachment">';
511         // show the medium sized image representation of the attachment if available, and link to the raw file
512         $p .= wp_get_attachment_link(0, 'medium', false);
513         $p .= '</p>';
514         $p = apply_filters('prepend_attachment', $p);
515
516         return "$p\n$content";
517 }
518
519 //
520 // Misc
521 //
522
523 function get_the_password_form() {
524         global $post;
525         $label = 'pwbox-'.(empty($post->ID) ? rand() : $post->ID);
526         $output = '<form action="' . get_option('siteurl') . '/wp-pass.php" method="post">
527         <p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
528         <p><label for="' . $label . '">' . __("Password:") . ' <input name="post_password" id="' . $label . '" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . __("Submit") . '" /></p>
529         </form>
530         ';
531         return $output;
532 }
533
534 /**
535  * is_page_template() - Determine wether or not we are in a page template
536  *
537  * This template tag allows you to determine wether or not you are in a page template.
538  * You can optional provide a template name and then the check will be specific to
539  * that template.
540  *
541  * @package Template Tags
542  * @global object $wp_query
543  * @param string $template The specific template name if specific matching is required
544  */
545 function is_page_template($template = '') {
546         if (!is_page()) {
547                 return false;
548         }
549
550         global $wp_query;
551
552         $page = $wp_query->get_queried_object();
553         $custom_fields = get_post_custom_values('_wp_page_template',$page->ID);
554         $page_template = $custom_fields[0];
555
556         // We have no argument passed so just see if a page_template has been specified
557         if ( empty( $template ) ) {
558                 if (!empty( $page_template ) ) {
559                         return true;
560                 }
561         } elseif ( $template == $page_template) {
562                 return true;
563         }
564
565         return false;
566 }
567
568 ?>