]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/template-functions-post.php
Wordpress 2.0.2
[autoinstalls/wordpress.git] / wp-includes / template-functions-post.php
1 <?php
2
3 function get_the_password_form() {
4         $output = '<form action="' . get_settings('siteurl') . '/wp-pass.php" method="post">
5         <p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
6         <p><label>' . __("Password:") . ' <input name="post_password" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . __("Submit") . '" /></p>
7         </form>
8         ';
9         return $output;
10 }
11
12
13 function the_ID() {
14         global $id;
15         echo $id;
16 }
17
18
19 function the_title($before = '', $after = '', $echo = true) {
20         $title = get_the_title();
21         if ( strlen($title) > 0 ) {
22                 $title = apply_filters('the_title', $before . $title . $after, $before, $after);
23                 if ( $echo )
24                         echo $title;
25                 else
26                         return $title;
27         }
28 }
29
30
31 function get_the_title($id = 0) {
32         $post = &get_post($id);
33
34         $title = $post->post_title;
35         if ( !empty($post->post_password) )
36                 $title = sprintf(__('Protected: %s'), $title);
37
38         return $title;
39 }
40
41
42 function get_the_guid( $id = 0 ) {
43         $post = &get_post($id);
44
45         return apply_filters('get_the_guid', $post->guid);
46 }
47
48
49 function the_guid( $id = 0 ) {
50         echo get_the_guid($id);
51 }
52
53
54 function the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
55         $content = get_the_content($more_link_text, $stripteaser, $more_file);
56         $content = apply_filters('the_content', $content);
57         $content = str_replace(']]>', ']]&gt;', $content);
58         echo $content;
59 }
60
61
62 function get_the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
63         global $id, $post, $more, $single, $withcomments, $page, $pages, $multipage, $numpages;
64         global $preview;
65         global $pagenow;
66         $output = '';
67
68         if ( !empty($post->post_password) ) { // if there's a password
69                 if ( stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password ) {      // and it doesn't match the cookie
70                         $output = get_the_password_form();
71                         return $output;
72                 }
73         }
74
75         if ( $more_file != '' )
76                 $file = $more_file;
77         else
78                 $file = $pagenow; //$_SERVER['PHP_SELF'];
79
80         $content = $pages[$page-1];
81         $content = explode('<!--more-->', $content, 2);
82         if ( (preg_match('/<!--noteaser-->/', $post->post_content) && ((!$multipage) || ($page==1))) )
83                 $stripteaser = 1;
84         $teaser = $content[0];
85         if ( ($more) && ($stripteaser) )
86                 $teaser = '';
87         $output .= $teaser;
88         if ( count($content) > 1 ) {
89                 if ( $more )
90                         $output .= '<a id="more-'.$id.'"></a>'.$content[1];
91                 else
92                         $output .= ' <a href="'. get_permalink() . "#more-$id\">$more_link_text</a>";
93         }
94         if ( $preview ) // preview fix for javascript bug with foreign languages
95                 $output =       preg_replace('/\%u([0-9A-F]{4,4})/e',   "'&#'.base_convert('\\1',16,10).';'", $output);
96
97         return $output;
98 }
99
100
101 function the_excerpt() {
102         echo apply_filters('the_excerpt', get_the_excerpt());
103 }
104
105
106 function get_the_excerpt($fakeit = true) {
107         global $id, $post;
108         $output = '';
109         $output = $post->post_excerpt;
110         if ( !empty($post->post_password) ) { // if there's a password
111                 if ( $_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password ) {  // and it doesn't match the cookie
112                         $output = __('There is no excerpt because this is a protected post.');
113                         return $output;
114                 }
115         }
116
117         return apply_filters('get_the_excerpt', $output);
118 }
119
120
121 function wp_link_pages($args = '') {
122         parse_str($args, $r);
123         if ( !isset($r['before']) )
124                 $r['before'] = '<p>' . __('Pages:');
125         if ( !isset($r['after']) )
126                 $r['after'] = '</p>';
127         if ( !isset($r['next_or_number']) )
128                 $r['next_or_number'] = 'number';
129         if ( !isset($r['nextpagelink']) )
130                 $r['nextpagelink'] = 'Next page';
131         if ( !isset($r['previouspagelink']) )
132                 $r['previouspagelink'] = 'Previous page';
133         if ( !isset($r['pagelink']) )
134                 $r['pagelink'] = '%';
135         if ( !isset($r['more_file']) )
136                 $r['more_file'] = '';
137
138         link_pages($r['before'], $r['after'], $r['next_or_number'], $r['nextpagelink'], $r['previouspagelink'], $r['pagelink'], $r['more_file']);
139 }
140
141
142 function link_pages($before='<br />', $after='<br />', $next_or_number='number', $nextpagelink='next page', $previouspagelink='previous page', $pagelink='%', $more_file='') {
143         global $id, $page, $numpages, $multipage, $more, $pagenow;
144         if ( $more_file != '' )
145                 $file = $more_file;
146         else
147                 $file = $pagenow;
148         if ( $multipage ) {
149                 if ( 'number' == $next_or_number ) {
150                         echo $before;
151                         for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) {
152                                 $j = str_replace('%',"$i",$pagelink);
153                                 echo ' ';
154                                 if ( ($i != $page) || ((!$more) && ($page==1)) ) {
155                                         if ( '' == get_settings('permalink_structure') )
156                                                 echo '<a href="' . get_permalink() . '&amp;page=' . $i . '">';
157                                         else
158                                                 echo '<a href="' . trailingslashit( get_permalink() ) . $i . '/">';
159                                 }
160                                 echo $j;
161                                 if ( ($i != $page) || ((!$more) && ($page==1)) )
162                                         echo '</a>';
163                         }
164                         echo $after;
165                 } else {
166                         if ( $more ) {
167                                 echo $before;
168                                 $i = $page - 1;
169                                 if ( $i && $more ) {
170                                         if ( '' == get_settings('permalink_structure') )
171                                                 echo '<a href="' . get_permalink() . '&amp;page=' . $i . '">'.$previouspagelink.'</a>';
172                                         else
173                                                 echo '<a href="' . get_permalink() . $i . '/">'.$previouspagelink.'</a>';
174                                 }
175                                 $i = $page + 1;
176                                 if ( $i <= $numpages && $more ) {
177                                         if ( '' == get_settings('permalink_structure') )
178                                                 echo '<a href="'.get_permalink() . '&amp;page=' . $i . '">'.$nextpagelink.'</a>';
179                                         else
180                                                 echo '<a href="'.get_permalink().$i.'/">'.$nextpagelink.'</a>';
181                                 }
182                                 echo $after;
183                         }
184                 }
185         }
186 }
187
188
189 /*
190 Post-meta: Custom per-post fields.
191 */
192
193
194 function get_post_custom( $post_id = 0 ) {
195         global $id, $post_meta_cache, $wpdb;
196
197         if ( ! $post_id )
198                 $post_id = $id;
199
200         if ( isset($post_meta_cache[$post_id]) )
201                 return $post_meta_cache[$post_id];
202
203         if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = '$post_id' ORDER BY post_id, meta_key", ARRAY_A) ) {
204                 // Change from flat structure to hierarchical:
205                 $post_meta_cache = array();
206                 foreach ( $meta_list as $metarow ) {
207                         $mpid = $metarow['post_id'];
208                         $mkey = $metarow['meta_key'];
209                         $mval = $metarow['meta_value'];
210
211                         // Force subkeys to be array type:
212                         if ( !isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]) )
213                                 $post_meta_cache[$mpid] = array();
214                                 
215                         if ( !isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]) )
216                                 $post_meta_cache[$mpid]["$mkey"] = array();
217
218                         // Add a value to the current pid/key:
219                         $post_meta_cache[$mpid][$mkey][] = $mval;
220                 }
221                 return $post_meta_cache[$mpid];
222         }
223 }
224
225
226 function get_post_custom_keys() {
227         $custom = get_post_custom();
228
229         if ( ! is_array($custom) )
230                 return;
231
232         if ( $keys = array_keys($custom) )
233                 return $keys;
234 }
235
236
237 function get_post_custom_values( $key = '' ) {
238         $custom = get_post_custom();
239
240         return $custom[$key];
241 }
242
243
244 function post_custom( $key = '' ) {
245         $custom = get_post_custom();
246
247         if ( 1 == count($custom[$key]) )
248                 return $custom[$key][0];
249         else
250                 return $custom[$key];
251 }
252
253
254 // this will probably change at some point...
255 function the_meta() {
256         global $id, $post_meta_cache;
257
258         if ( $keys = get_post_custom_keys() ) {
259                 echo "<ul class='post-meta'>\n";
260                 foreach ( $keys as $key ) {
261                         $values = array_map('trim', get_post_custom_values($key));
262                         $value = implode($values,', ');
263                         echo "<li><span class='post-meta-key'>$key:</span> $value</li>\n";
264                 }
265                 echo "</ul>\n";
266         }
267 }
268
269
270 /*
271 Pages
272 */
273
274
275 function &get_page_children($page_id, $pages) {
276         global $page_cache;
277
278         if ( empty($pages) )
279                 $pages = &$page_cache;
280
281         $page_list = array();
282         foreach ( $pages as $page ) {
283                 if ( $page->post_parent == $page_id ) {
284                         $page_list[] = $page;
285                         if ( $children = get_page_children($page->ID, $pages) )
286                                 $page_list = array_merge($page_list, $children);
287                 }
288         }
289         return $page_list;
290 }
291
292
293 function &get_pages($args = '') {
294         global $wpdb;
295
296         parse_str($args, $r);
297
298         if ( !isset($r['child_of']) )
299                 $r['child_of'] = 0;
300         if ( !isset($r['sort_column']) )
301                 $r['sort_column'] = 'post_title';
302         if ( !isset($r['sort_order']) )
303                 $r['sort_order'] = 'ASC';
304
305         $exclusions = '';
306         if ( !empty($r['exclude']) ) {
307                 $expages = preg_split('/[\s,]+/',$r['exclude']);
308                 if ( count($expages) ) {
309                         foreach ( $expages as $expage ) {
310                                 $exclusions .= ' AND ID <> ' . intval($expage) . ' ';
311                         }
312                 }
313         }
314
315         $pages = $wpdb->get_results("SELECT * " .
316                 "FROM $wpdb->posts " .
317                 "WHERE post_status = 'static' " .
318                 "$exclusions " .
319                 "ORDER BY " . $r['sort_column'] . " " . $r['sort_order']);
320
321         if ( empty($pages) )
322                 return array();
323
324         // Update cache.
325         update_page_cache($pages);
326
327         if ( $r['child_of'] )
328                 $pages = & get_page_children($r['child_of'], $pages);
329
330         return $pages;
331 }
332
333
334 function wp_list_pages($args = '') {
335         parse_str($args, $r);
336         if ( !isset($r['depth']) )
337                 $r['depth'] = 0;
338         if ( !isset($r['show_date']) )
339                 $r['show_date'] = '';
340         if ( !isset($r['child_of']) )
341                 $r['child_of'] = 0;
342         if ( !isset($r['title_li']) )
343                 $r['title_li'] = __('Pages');
344         if ( !isset($r['echo']) )
345                 $r['echo'] = 1;
346
347         $output = '';
348
349         // Query pages.
350         $pages = & get_pages($args);
351         if ( $pages ) {
352
353                 if ( $r['title_li'] )
354                         $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
355
356                 // Now loop over all pages that were selected
357                 $page_tree = Array();
358                 foreach ( $pages as $page ) {
359                         // set the title for the current page
360                         $page_tree[$page->ID]['title'] = $page->post_title;
361                         $page_tree[$page->ID]['name'] = $page->post_name;
362
363                         // set the selected date for the current page
364                         // depending on the query arguments this is either
365                         // the createtion date or the modification date
366                         // as a unix timestamp. It will also always be in the
367                         // ts field.
368                         if ( !empty($r['show_date']) ) {
369                                 if ( 'modified' == $r['show_date'] )
370                                         $page_tree[$page->ID]['ts'] = $page->post_modified;
371                                 else
372                                         $page_tree[$page->ID]['ts'] = $page->post_date;
373                         }
374
375                         // The tricky bit!!
376                         // Using the parent ID of the current page as the
377                         // array index we set the curent page as a child of that page.
378                         // We can now start looping over the $page_tree array
379                         // with any ID which will output the page links from that ID downwards.
380                         if ( $page->post_parent != $page->ID)
381                                 $page_tree[$page->post_parent]['children'][] = $page->ID;
382                 }
383                 // Output of the pages starting with child_of as the root ID.
384                 // child_of defaults to 0 if not supplied in the query.
385                 $output .= _page_level_out($r['child_of'],$page_tree, $r, 0, false);
386                 if ( $r['title_li'] )
387                         $output .= '</ul></li>';
388         }
389
390         $output = apply_filters('wp_list_pages', $output);
391
392         if ( $r['echo'] )
393                 echo $output;
394         else
395                 return $output;
396 }
397
398
399 function _page_level_out($parent, $page_tree, $args, $depth = 0, $echo = true) {
400         global $wp_query;
401         $queried_obj = $wp_query->get_queried_object();
402         $output = '';
403
404         if ( $depth )
405                 $indent = str_repeat("\t", $depth);
406                 //$indent = join('', array_fill(0,$depth,"\t"));
407
408         if ( !is_array($page_tree[$parent]['children']) )
409                 return false;
410
411         foreach ( $page_tree[$parent]['children'] as $page_id ) {
412                 $cur_page = $page_tree[$page_id];
413                 $title = $cur_page['title'];
414
415                 $css_class = 'page_item';
416                 if ( $page_id == $queried_obj->ID )
417                         $css_class .= ' current_page_item';
418
419                 $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_page_link($page_id) . '" title="' . wp_specialchars($title) . '">' . $title . '</a>';
420
421                 if ( isset($cur_page['ts']) ) {
422                         $format = get_settings('date_format');
423                         if ( isset($args['date_format']) )
424                                 $format = $args['date_format'];
425                         $output .= " " . mysql2date($format, $cur_page['ts']);
426                 }
427
428                 if ( isset($cur_page['children']) && is_array($cur_page['children']) ) {
429                         $new_depth = $depth + 1;
430
431                         if ( !$args['depth'] || $depth < ($args['depth']-1) ) {
432                                 $output .= "$indent<ul>\n";
433                                 $output .= _page_level_out($page_id, $page_tree, $args, $new_depth, false);
434                                 $output .= "$indent</ul>\n";
435                         }
436                 }
437                 $output .= "$indent</li>\n";
438         }
439         if ( $echo )
440                 echo $output;
441         else
442                 return $output;
443 }
444
445 function the_attachment_link($id = 0, $fullsize = false, $max_dims = false) {
446         echo get_the_attachment_link($id, $fullsize, $max_dims);
447 }
448
449 function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false) {
450         $id = (int) $id;
451         $_post = & get_post($id);
452
453         if ( ('attachment' != $_post->post_status) || ('' == $_post->guid) )
454                 return __('Missing Attachment');
455
456         if (! empty($_post->guid) ) {
457                 $innerHTML = get_attachment_innerHTML($_post->ID, $fullsize, $max_dims);
458
459                 return "<a href=\"{$_post->guid}\" title=\"{$_post->post_title}\" >{$innerHTML}</a>";
460
461         } else {
462                 $p .= __('Missing attachment');
463         }
464         return $p;
465 }
466
467 function get_attachment_icon($id = 0, $fullsize = false, $max_dims = false) {
468         $id = (int) $id;
469         $post = & get_post($id);
470
471         $mime = $post->post_mime_type;
472
473         $imagedata = get_post_meta($post->ID, '_wp_attachment_metadata', true);
474
475         $file = get_post_meta($post->ID, '_wp_attached_file', true);
476
477         if ( !$fullsize && !empty($imagedata['thumb'])
478                         && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file))
479                         && file_exists($thumbfile) ) {
480
481                 // We have a thumbnail desired, specified and existing
482
483                 $src = str_replace(basename($post->guid), $imagedata['thumb'], $post->guid);
484                 $src_file = $thumbfile;
485                 $class = 'attachmentthumb';
486
487         } elseif ( substr($mime, 0, 6) == 'image/'
488                         && file_exists($file) ) {
489
490                 // We have an image without a thumbnail
491
492                 $src = $post->guid;
493                 $src_file = & $file;
494                 $class = 'attachmentimage';
495         } elseif (! empty($mime) ) {
496
497                 // No thumb, no image. We'll look for a mime-related icon instead.
498                 $icon_dir = apply_filters('icon_dir', get_template_directory().'/images');
499                 $icon_dir_uri = apply_filters('icon_dir_uri', get_template_directory_uri().'/images');
500
501                 $types = array(substr($mime, 0, strpos($mime, '/')), substr($mime, strpos($mime, '/') + 1), str_replace('/', '_', $mime));
502                 $exts = array('jpg', 'gif', 'png');
503                 foreach ($types as $type) {
504                         foreach ($exts as $ext) {
505                                 $src_file = "$icon_dir/$type.$ext";
506                                 if ( file_exists($src_file) ) {
507                                         $src = "$icon_dir_uri/$type.$ext";
508                                         break 2;
509                                 }
510                         }
511                 }
512         }
513
514         if (! isset($src) )
515                 return false;
516
517         // Do we need to constrain the image?
518         if ( ($max_dims = apply_filters('attachment_max_dims', $max_dims)) && file_exists($src_file) ) {
519
520                 $imagesize = getimagesize($src_file);
521
522                 if (($imagesize[0] > $max_dims[0]) || $imagesize[1] > $max_dims[1] ) {
523                         $actual_aspect = $imagesize[0] / $imagesize[1];
524                         $desired_aspect = $max_dims[0] / $max_dims[1];
525
526                         if ( $actual_aspect >= $desired_aspect ) {
527                                 $height = $actual_aspect * $max_dims[0];
528                                 $constraint = "width=\"{$max_dims[0]}\" ";
529                                 $post->iconsize = array($max_dims[0], $height);
530                         } else {
531                                 $width = $max_dims[1] / $actual_aspect;
532                                 $constraint = "height=\"{$max_dims[1]}\" ";
533                                 $post->iconsize = array($width, $max_dims[1]);
534                         }
535                 } else {
536                         $post->iconsize = array($imagesize[0], $imagesize[1]);
537                 }
538         }
539
540         $icon = "<img src=\"{$src}\" title=\"{$post->post_title}\" alt=\"{$post->post_title}\" {$constraint}/>";
541
542         return apply_filters('attachment_icon', $icon, $post->ID);
543 }
544
545 function get_attachment_innerHTML($id = 0, $fullsize = false, $max_dims = false) {
546         $id = (int) $id;
547
548         if ( $innerHTML = get_attachment_icon($id, $fullsize, $max_dims))
549                 return $innerHTML;
550
551         $post = & get_post($id);
552
553         $innerHTML = $post->post_title;
554
555         return apply_filters('attachment_innerHTML', $innerHTML, $post->ID);
556 }
557
558 function prepend_attachment($content) {
559         $p = '<p class="attachment">';
560         $p .= get_the_attachment_link(false, true, array(400, 300));
561         $p .= '</p>';
562         $p = apply_filters('prepend_attachment', $p);
563
564         return "$p\n$content";
565 }
566
567 ?>