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