]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/post-template.php
Wordpress 2.3.3
[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         if ( !empty($post->post_password) )
58                 $title = sprintf(__('Protected: %s'), $title);
59         else if ( 'private' == $post->post_status )
60                 $title = sprintf(__('Private: %s'), $title);
61
62         return apply_filters( 'the_title', $title );
63 }
64
65 function the_guid( $id = 0 ) {
66         echo get_the_guid($id);
67 }
68
69 function get_the_guid( $id = 0 ) {
70         $post = &get_post($id);
71
72         return apply_filters('get_the_guid', $post->guid);
73 }
74
75 function the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
76         $content = get_the_content($more_link_text, $stripteaser, $more_file);
77         $content = apply_filters('the_content', $content);
78         $content = str_replace(']]>', ']]&gt;', $content);
79         echo $content;
80 }
81
82
83 function get_the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
84         global $id, $post, $more, $single, $withcomments, $page, $pages, $multipage, $numpages;
85         global $preview;
86         global $pagenow;
87         $output = '';
88
89         if ( !empty($post->post_password) ) { // if there's a password
90                 if ( stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password ) {      // and it doesn't match the cookie
91                         $output = get_the_password_form();
92                         return $output;
93                 }
94         }
95
96         if ( $more_file != '' )
97                 $file = $more_file;
98         else
99                 $file = $pagenow; //$_SERVER['PHP_SELF'];
100
101         if ( $page > count($pages) ) // if the requested page doesn't exist
102                 $page = count($pages); // give them the highest numbered page that DOES exist
103
104         $content = $pages[$page-1];
105         if ( preg_match('/<!--more(.*?)?-->/', $content, $matches) ) {
106                 $content = explode($matches[0], $content, 2);
107                 if ( !empty($matches[1]) && !empty($more_link_text) )
108                         $more_link_text = strip_tags(wp_kses_no_null(trim($matches[1])));
109         } else {
110                 $content = array($content);
111         }
112         if ( (false !== strpos($post->post_content, '<!--noteaser-->') && ((!$multipage) || ($page==1))) )
113                 $stripteaser = 1;
114         $teaser = $content[0];
115         if ( ($more) && ($stripteaser) )
116                 $teaser = '';
117         $output .= $teaser;
118         if ( count($content) > 1 ) {
119                 if ( $more ) {
120                         $output .= '<span id="more-'.$id.'"></span>'.$content[1];
121                 } else {
122                         $output = balanceTags($output);
123                         if ( ! empty($more_link_text) )
124                                 $output .= ' <a href="'. get_permalink() . "#more-$id\" class=\"more-link\">$more_link_text</a>";
125                 }
126
127         }
128         if ( $preview ) // preview fix for javascript bug with foreign languages
129                 $output =       preg_replace('/\%u([0-9A-F]{4,4})/e',   "'&#'.base_convert('\\1',16,10).';'", $output);
130
131         return $output;
132 }
133
134
135 function the_excerpt() {
136         echo apply_filters('the_excerpt', get_the_excerpt());
137 }
138
139
140 function get_the_excerpt($deprecated = true) {
141         global $id, $post;
142         $output = '';
143         $output = $post->post_excerpt;
144         if ( !empty($post->post_password) ) { // if there's a password
145                 if ( $_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password ) {  // and it doesn't match the cookie
146                         $output = __('There is no excerpt because this is a protected post.');
147                         return $output;
148                 }
149         }
150
151         return apply_filters('get_the_excerpt', $output);
152 }
153
154 function has_excerpt( $id = 0 ) {
155         $post = &get_post( $id );
156         return ( !empty( $post->post_excerpt ) );
157 }
158
159 function wp_link_pages($args = '') {
160         $defaults = array(
161                 'before' => '<p>' . __('Pages:'), 'after' => '</p>',
162                 'next_or_number' => 'number', 'nextpagelink' => __('Next page'),
163                 'previouspagelink' => __('Previous page'), 'pagelink' => '%',
164                 'more_file' => '', 'echo' => 1
165         );
166
167         $r = wp_parse_args( $args, $defaults );
168         extract( $r, EXTR_SKIP );
169
170         global $post, $id, $page, $numpages, $multipage, $more, $pagenow;
171         if ( $more_file != '' )
172                 $file = $more_file;
173         else
174                 $file = $pagenow;
175
176         $output = '';
177         if ( $multipage ) {
178                 if ( 'number' == $next_or_number ) {
179                         $output .= $before;
180                         for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) {
181                                 $j = str_replace('%',"$i",$pagelink);
182                                 $output .= ' ';
183                                 if ( ($i != $page) || ((!$more) && ($page==1)) ) {
184                                         if ( 1 == $i ) {
185                                                 $output .= '<a href="' . get_permalink() . '">';
186                                         } else {
187                                                 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
188                                                         $output .= '<a href="' . get_permalink() . '&amp;page=' . $i . '">';
189                                                 else
190                                                         $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">';
191                                         }
192                                 }
193                                 $output .= $j;
194                                 if ( ($i != $page) || ((!$more) && ($page==1)) )
195                                         $output .= '</a>';
196                         }
197                         $output .= $after;
198                 } else {
199                         if ( $more ) {
200                                 $output .= $before;
201                                 $i = $page - 1;
202                                 if ( $i && $more ) {
203                                         if ( 1 == $i ) {
204                                                 $output .= '<a href="' . get_permalink() . '">' . $previouspagelink . '</a>';
205                                         } else {
206                                                 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
207                                                         $output .= '<a href="' . get_permalink() . '&amp;page=' . $i . '">' . $previouspagelink . '</a>';
208                                                 else
209                                                         $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">' . $previouspagelink . '</a>';
210                                         }
211                                 }
212                                 $i = $page + 1;
213                                 if ( $i <= $numpages && $more ) {
214                                         if ( 1 == $i ) {
215                                                 $output .= '<a href="' . get_permalink() . '">' . $nextpagelink . '</a>';
216                                         } else {
217                                                 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
218                                                         $output .= '<a href="' . get_permalink() . '&amp;page=' . $i . '">' . $nextpagelink . '</a>';
219                                                 else
220                                                         $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">' . $nextpagelink . '</a>';
221                                         }
222                                 }
223                                 $output .= $after;
224                         }
225                 }
226         }
227
228         if ( $echo )
229                 echo $output;
230
231         return $output;
232 }
233
234
235 //
236 // Post-meta: Custom per-post fields.
237 //
238
239
240 function post_custom( $key = '' ) {
241         $custom = get_post_custom();
242
243         if ( 1 == count($custom[$key]) )
244                 return $custom[$key][0];
245         else
246                 return $custom[$key];
247 }
248
249
250 // this will probably change at some point...
251 function the_meta() {
252         global $id;
253
254         if ( $keys = get_post_custom_keys() ) {
255                 echo "<ul class='post-meta'>\n";
256                 foreach ( $keys as $key ) {
257                         $keyt = trim($key);
258                         if ( '_' == $keyt{0} )
259                                 continue;
260                         $values = array_map('trim', get_post_custom_values($key));
261                         $value = implode($values,', ');
262                         echo apply_filters('the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value);
263                 }
264                 echo "</ul>\n";
265         }
266 }
267
268
269 //
270 // Pages
271 //
272
273 function wp_dropdown_pages($args = '') {
274         $defaults = array(
275                 'depth' => 0, 'child_of' => 0,
276                 'selected' => 0, 'echo' => 1,
277                 'name' => 'page_id', 'show_option_none' => ''
278         );
279
280         $r = wp_parse_args( $args, $defaults );
281         extract( $r, EXTR_SKIP );
282
283         $pages = get_pages($r);
284         $output = '';
285
286         if ( ! empty($pages) ) {
287                 $output = "<select name='$name'>\n";
288                 if ( $show_option_none )
289                         $output .= "\t<option value=''>$show_option_none</option>\n";
290                 $output .= walk_page_dropdown_tree($pages, $depth, $r);
291                 $output .= "</select>\n";
292         }
293
294         $output = apply_filters('wp_dropdown_pages', $output);
295
296         if ( $echo )
297                 echo $output;
298
299         return $output;
300 }
301
302 function wp_list_pages($args = '') {
303         $defaults = array(
304                 'depth' => 0, 'show_date' => '',
305                 'date_format' => get_option('date_format'),
306                 'child_of' => 0, 'exclude' => '',
307                 'title_li' => __('Pages'), 'echo' => 1,
308                 'authors' => '', 'sort_column' => 'menu_order, post_title'
309         );
310
311         $r = wp_parse_args( $args, $defaults );
312         extract( $r, EXTR_SKIP );
313
314         $output = '';
315         $current_page = 0;
316
317         // sanitize, mostly to keep spaces out
318         $r['exclude'] = preg_replace('[^0-9,]', '', $r['exclude']);
319
320         // Allow plugins to filter an array of excluded pages
321         $r['exclude'] = implode(',', apply_filters('wp_list_pages_excludes', explode(',', $r['exclude'])));
322
323         // Query pages.
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() )
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, $max_dims = false) {
368         echo get_the_attachment_link($id, $fullsize, $max_dims);
369 }
370
371 function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false) {
372         $id = (int) $id;
373         $_post = & get_post($id);
374
375         if ( ('attachment' != $_post->post_type) || !$url = wp_get_attachment_url($_post->ID) )
376                 return __('Missing Attachment');
377
378         $post_title = attribute_escape($_post->post_title);
379
380         $innerHTML = get_attachment_innerHTML($_post->ID, $fullsize, $max_dims);
381         return "<a href='$url' title='$post_title'>$innerHTML</a>";
382 }
383
384 function get_attachment_icon_src( $id = 0, $fullsize = false ) {
385         $id = (int) $id;
386         if ( !$post = & get_post($id) )
387                 return false;
388
389         $imagedata = wp_get_attachment_metadata( $post->ID );
390
391         $file = get_attached_file( $post->ID );
392
393         if ( !$fullsize && $thumbfile = wp_get_attachment_thumb_file( $post->ID ) ) {
394                 // We have a thumbnail desired, specified and existing
395
396                 $src = wp_get_attachment_thumb_url( $post->ID );
397                 $src_file = $thumbfile;
398                 $class = 'attachmentthumb';
399         } elseif ( wp_attachment_is_image( $post->ID ) ) {
400                 // We have an image without a thumbnail
401
402                 $src = wp_get_attachment_url( $post->ID );
403                 $src_file = & $file;
404                 $class = 'attachmentimage';
405         } elseif ( $src = wp_mime_type_icon( $post->ID ) ) {
406                 // No thumb, no image. We'll look for a mime-related icon instead.
407
408                 $icon_dir = apply_filters( 'icon_dir', get_template_directory() . '/images' );
409                 $src_file = $icon_dir . '/' . basename($src);
410         }
411
412         if ( !isset($src) || !$src )
413                 return false;
414
415         return array($src, $src_file);
416 }
417
418 function get_attachment_icon( $id = 0, $fullsize = false, $max_dims = false ) {
419         $id = (int) $id;
420         if ( !$post = & get_post($id) )
421                 return false;
422
423         if ( !$src = get_attachment_icon_src( $post->ID, $fullsize ) )
424                 return false;
425
426         list($src, $src_file) = $src;
427
428         // Do we need to constrain the image?
429         if ( ($max_dims = apply_filters('attachment_max_dims', $max_dims)) && file_exists($src_file) ) {
430
431                 $imagesize = getimagesize($src_file);
432
433                 if (($imagesize[0] > $max_dims[0]) || $imagesize[1] > $max_dims[1] ) {
434                         $actual_aspect = $imagesize[0] / $imagesize[1];
435                         $desired_aspect = $max_dims[0] / $max_dims[1];
436
437                         if ( $actual_aspect >= $desired_aspect ) {
438                                 $height = $actual_aspect * $max_dims[0];
439                                 $constraint = "width='{$max_dims[0]}' ";
440                                 $post->iconsize = array($max_dims[0], $height);
441                         } else {
442                                 $width = $max_dims[1] / $actual_aspect;
443                                 $constraint = "height='{$max_dims[1]}' ";
444                                 $post->iconsize = array($width, $max_dims[1]);
445                         }
446                 } else {
447                         $post->iconsize = array($imagesize[0], $imagesize[1]);
448                 }
449         }
450
451         $post_title = attribute_escape($post->post_title);
452
453         $icon = "<img src='$src' title='$post_title' alt='$post_title' $constraint/>";
454
455         return apply_filters( 'attachment_icon', $icon, $post->ID );
456 }
457
458 function get_attachment_innerHTML($id = 0, $fullsize = false, $max_dims = false) {
459         $id = (int) $id;
460         if ( !$post = & get_post($id) )
461                 return false;
462
463         if ( $innerHTML = get_attachment_icon($post->ID, $fullsize, $max_dims))
464                 return $innerHTML;
465
466
467         $innerHTML = attribute_escape($post->post_title);
468
469         return apply_filters('attachment_innerHTML', $innerHTML, $post->ID);
470 }
471
472 function prepend_attachment($content) {
473         $p = '<p class="attachment">';
474         $p .= get_the_attachment_link(false, true, array(400, 300));
475         $p .= '</p>';
476         $p = apply_filters('prepend_attachment', $p);
477
478         return "$p\n$content";
479 }
480
481 //
482 // Misc
483 //
484
485 function get_the_password_form() {
486         $output = '<form action="' . get_option('siteurl') . '/wp-pass.php" method="post">
487         <p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
488         <p><label>' . __("Password:") . ' <input name="post_password" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . __("Submit") . '" /></p>
489         </form>
490         ';
491         return $output;
492 }
493
494 ?>