]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/template-functions-links.php
Wordpress 2.0.4
[autoinstalls/wordpress.git] / wp-includes / template-functions-links.php
1 <?php
2
3
4 function the_permalink() {
5         echo apply_filters('the_permalink', get_permalink());
6 }
7
8
9 function permalink_link() { // For backwards compatibility
10         echo apply_filters('the_permalink', get_permalink());
11 }
12
13
14 function permalink_anchor($mode = 'id') {
15         global $post;
16         switch ( strtolower($mode) ) {
17                 case 'title':
18                         $title = sanitize_title($post->post_title) . '-' . $id;
19                         echo '<a id="'.$title.'"></a>';
20                         break;
21                 case 'id':
22                 default:
23                         echo '<a id="post-' . $post->ID . '"></a>';
24                         break;
25         }
26 }
27
28
29 function get_permalink($id = 0) {
30         $rewritecode = array(
31                 '%year%',
32                 '%monthnum%',
33                 '%day%',
34                 '%hour%',
35                 '%minute%',
36                 '%second%',
37                 '%postname%',
38                 '%post_id%',
39                 '%category%',
40                 '%author%',
41                 '%pagename%'
42         );
43
44         $post = &get_post($id);
45         if ( $post->post_status == 'static' )
46                 return get_page_link($post->ID);
47         elseif ($post->post_status == 'attachment')
48                 return get_attachment_link($post->ID);
49
50         $permalink = get_settings('permalink_structure');
51
52         if ( '' != $permalink && 'draft' != $post->post_status ) {
53                 $unixtime = strtotime($post->post_date);
54
55                 $category = '';
56                 if ( strstr($permalink, '%category%') ) {
57                         $cats = get_the_category($post->ID);
58                         $category = $cats[0]->category_nicename;
59                         if ( $parent=$cats[0]->category_parent )
60                                 $category = get_category_parents($parent, FALSE, '/', TRUE) . $category;
61                 }
62
63                 $authordata = get_userdata($post->post_author);
64                 $author = $authordata->user_nicename;
65                 $date = explode(" ",date('Y m d H i s', $unixtime));
66                 $rewritereplace = 
67                 array(
68                         $date[0],
69                         $date[1],
70                         $date[2],
71                         $date[3],
72                         $date[4],
73                         $date[5],
74                         $post->post_name,
75                         $post->ID,
76                         $category,
77                         $author,
78                         $post->post_name,
79                 );
80                 return apply_filters('post_link', get_settings('home') . str_replace($rewritecode, $rewritereplace, $permalink), $post);
81         } else { // if they're not using the fancy permalink option
82                 $permalink = get_settings('home') . '/?p=' . $post->ID;
83                 return apply_filters('post_link', $permalink, $post);
84         }
85 }
86
87 function get_page_link($id = false) {
88         global $post, $wp_rewrite;
89
90         if ( !$id )
91                 $id = $post->ID;
92
93         $pagestruct = $wp_rewrite->get_page_permastruct();
94
95         if ( '' != $pagestruct ) {
96                 $link = get_page_uri($id);
97                 $link = str_replace('%pagename%', $link, $pagestruct);
98                 $link = get_settings('home') . "/$link/";
99         } else {
100                 $link = get_settings('home') . "/?page_id=$id";
101         }
102
103         return apply_filters('page_link', $link, $id);
104 }
105
106 function get_attachment_link($id = false) {
107         global $post, $wp_rewrite;
108
109         $link = false;
110
111         if (! $id) {
112                 $id = $post->ID;
113         }
114
115         $object = get_post($id);
116         if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) ) {
117                 $parent = get_post($object->post_parent);
118                 $parentlink = get_permalink($object->post_parent);
119                 if (! strstr($parentlink, '?') )
120                         $link = trim($parentlink, '/') . '/' . $object->post_name . '/';
121         }
122
123         if (! $link ) {
124                 $link = get_bloginfo('home') . "/?attachment_id=$id";
125         }
126
127         return apply_filters('attachment_link', $link, $id);
128 }
129
130 function get_year_link($year) {
131         global $wp_rewrite;
132         if ( !$year )
133                 $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
134         $yearlink = $wp_rewrite->get_year_permastruct();
135         if ( !empty($yearlink) ) {
136                 $yearlink = str_replace('%year%', $year, $yearlink);
137                 return apply_filters('year_link', get_settings('home') . trailingslashit($yearlink), $year);
138         } else {
139                 return apply_filters('year_link', get_settings('home') . '/?m=' . $year, $year);
140         }
141 }
142
143 function get_month_link($year, $month) {
144         global $wp_rewrite;
145         if ( !$year )
146                 $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
147         if ( !$month )
148                 $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
149         $monthlink = $wp_rewrite->get_month_permastruct();
150         if ( !empty($monthlink) ) {
151                 $monthlink = str_replace('%year%', $year, $monthlink);
152                 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
153                 return apply_filters('month_link', get_settings('home') . trailingslashit($monthlink), $year, $month);
154         } else {
155                 return apply_filters('month_link', get_settings('home') . '/?m=' . $year . zeroise($month, 2), $year, $month);
156         }
157 }
158
159 function get_day_link($year, $month, $day) {
160         global $wp_rewrite;
161         if ( !$year )
162                 $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
163         if ( !$month )
164                 $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
165         if ( !$day )
166                 $day = gmdate('j', time()+(get_settings('gmt_offset') * 3600));
167
168         $daylink = $wp_rewrite->get_day_permastruct();
169         if ( !empty($daylink) ) {
170                 $daylink = str_replace('%year%', $year, $daylink);
171                 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
172                 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
173                 return apply_filters('day_link', get_settings('home') . trailingslashit($daylink), $year, $month, $day);
174         } else {
175                 return apply_filters('day_link', get_settings('home') . '/?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day);
176         }
177 }
178
179 function get_feed_link($feed='rss2') {
180         global $wp_rewrite;
181         $do_perma = 0;
182         $feed_url = get_settings('siteurl');
183         $comment_feed_url = $feed_url;
184
185         $permalink = $wp_rewrite->get_feed_permastruct();
186         if ( '' != $permalink ) {
187                 if ( false !== strpos($feed, 'comments_') ) {
188                         $feed = str_replace('comments_', '', $feed);
189                         $permalink = $wp_rewrite->get_comment_feed_permastruct();
190                 }
191
192                 if ( 'rss2' == $feed )
193                         $feed = '';
194
195                 $permalink = str_replace('%feed%', $feed, $permalink);
196                 $permalink = preg_replace('#/+#', '/', "/$permalink/");
197                 $output =  get_settings('home') . $permalink;
198         } else {
199                 if ( false !== strpos($feed, 'comments_') )
200                         $feed = str_replace('comments_', 'comments-', $feed);
201
202                 $output = get_settings('home') . "/?feed={$feed}";
203         }
204
205         return apply_filters('feed_link', $output, $feed);
206 }
207
208 function edit_post_link($link = 'Edit This', $before = '', $after = '') {
209         global $post;
210
211         if ( ! current_user_can('edit_post', $post->ID) )
212                 return;
213
214         if ( is_attachment() )
215                 return;
216         else
217                 $file = 'post';
218
219         $location = get_settings('siteurl') . "/wp-admin/{$file}.php?action=edit&amp;post=$post->ID";
220         echo $before . "<a href=\"$location\">$link</a>" . $after;
221 }
222
223 function edit_comment_link($link = 'Edit This', $before = '', $after = '') {
224         global $post, $comment;
225
226         if ( ! current_user_can('edit_post', $post->ID) )
227                 return;
228
229         $location = get_settings('siteurl') . "/wp-admin/post.php?action=editcomment&amp;comment=$comment->comment_ID";
230         echo $before . "<a href='$location'>$link</a>" . $after;
231 }
232
233 // Navigation links
234
235 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
236         global $post, $wpdb;
237
238         if( !is_single() || is_attachment() )
239                 return null;
240
241         $current_post_date = $post->post_date;
242
243         $join = '';
244         if ( $in_same_cat ) {
245                 $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
246                 $cat_array = get_the_category($post->ID);
247                 $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
248                 for ( $i = 1; $i < (count($cat_array)); $i++ ) {
249                         $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID);
250                 }
251                 $join .= ')'; 
252         }
253
254         $sql_exclude_cats = '';
255         if ( !empty($excluded_categories) ) {
256                 $blah = explode('and', $excluded_categories);
257                 foreach ( $blah as $category ) {
258                         $category = intval($category);
259                         $sql_exclude_cats .= " AND post_category != $category";
260                 }
261         }
262
263         return @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts $join WHERE post_date < '$current_post_date' AND post_status = 'publish' $sqlcat $sql_exclude_cats ORDER BY post_date DESC LIMIT 1");
264 }
265
266 function get_next_post($in_same_cat = false, $excluded_categories = '') {
267         global $post, $wpdb;
268
269         if( !is_single() || is_attachment() )
270                 return null;
271
272         $current_post_date = $post->post_date;
273         
274         $join = '';
275         if ( $in_same_cat ) {
276                 $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
277                 $cat_array = get_the_category($post->ID);
278                 $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
279                 for ( $i = 1; $i < (count($cat_array)); $i++ ) {
280                         $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID);
281                 }
282                 $join .= ')'; 
283         }
284
285         $sql_exclude_cats = '';
286         if ( !empty($excluded_categories) ) {
287                 $blah = explode('and', $excluded_categories);
288                 foreach ( $blah as $category ) {
289                         $category = intval($category);
290                         $sql_exclude_cats .= " AND post_category != $category";
291                 }
292         }
293
294         $now = current_time('mysql');
295         
296         return @$wpdb->get_row("SELECT ID,post_title FROM $wpdb->posts $join WHERE post_date > '$current_post_date' AND post_date < '$now' AND post_status = 'publish' $sqlcat $sql_exclude_cats AND ID != $post->ID ORDER BY post_date ASC LIMIT 1");
297 }
298
299
300 function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
301
302         if ( is_attachment() )
303                 $post = & get_post($GLOBALS['post']->post_parent);
304         else
305                 $post = get_previous_post($in_same_cat, $excluded_categories);
306
307         if ( !$post )
308                 return;
309
310         $title = apply_filters('the_title', $post->post_title, $post);
311         $string = '<a href="'.get_permalink($post->ID).'">';
312         $link = str_replace('%title', $title, $link);
313         $link = $pre . $string . $link . '</a>';
314
315         $format = str_replace('%link', $link, $format);
316
317         echo $format;       
318 }
319
320 function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
321         $post = get_next_post($in_same_cat, $excluded_categories);
322
323         if ( !$post )
324                 return;
325
326         $title = apply_filters('the_title', $post->post_title, $post);
327         $string = '<a href="'.get_permalink($post->ID).'">';
328         $link = str_replace('%title', $title, $link);
329         $link = $string . $link . '</a>';
330         $format = str_replace('%link', $link, $format);
331
332         echo $format;       
333 }
334
335
336 // Deprecated.  Use previous_post_link().
337 function previous_post($format='%', $previous='previous post: ', $title='yes', $in_same_cat='no', $limitprev=1, $excluded_categories='') {
338
339         if ( empty($in_same_cat) || 'no' == $in_same_cat )
340                 $in_same_cat = false;
341         else
342                 $in_same_cat = true;
343
344         $post = get_previous_post($in_same_cat, $excluded_categories);
345
346         if ( !$post )
347                 return;
348
349         $string = '<a href="'.get_permalink($post->ID).'">'.$previous;
350         if ( 'yes' == $title )
351                 $string .= apply_filters('the_title', $post->post_title, $post);
352         $string .= '</a>';
353         $format = str_replace('%', $string, $format);
354         echo $format;
355 }
356
357 // Deprecated.  Use next_post_link().
358 function next_post($format='%', $next='next post: ', $title='yes', $in_same_cat='no', $limitnext=1, $excluded_categories='') {
359
360         if ( empty($in_same_cat) || 'no' == $in_same_cat )
361                 $in_same_cat = false;
362         else
363                 $in_same_cat = true;
364
365         $post = get_next_post($in_same_cat, $excluded_categories);
366
367         if ( !$post     )
368                 return;
369
370         $string = '<a href="'.get_permalink($post->ID).'">'.$next;
371         if ( 'yes' == $title )
372                 $string .= apply_filters('the_title', $post->post_title, $nextpost);
373         $string .= '</a>';
374         $format = str_replace('%', $string, $format);
375         echo $format;
376 }
377
378 function get_pagenum_link($pagenum = 1) {
379         global $wp_rewrite;
380
381         $qstr = wp_specialchars($_SERVER['REQUEST_URI']);
382
383         $page_querystring = "paged"; 
384         $page_modstring = "page/";
385         $page_modregex = "page/?";
386         $permalink = 0;
387
388         $home_root = parse_url(get_settings('home'));
389         $home_root = $home_root['path'];
390         $home_root = trailingslashit($home_root);
391         $qstr = preg_replace('|^'. $home_root . '|', '', $qstr);
392         $qstr = preg_replace('|^/+|', '', $qstr);
393
394         $index = $_SERVER['PHP_SELF'];
395         $index = preg_replace('|^'. $home_root . '|', '', $index);
396         $index = preg_replace('|^/+|', '', $index);
397
398         // if we already have a QUERY style page string
399         if ( stristr( $qstr, $page_querystring ) ) {
400                 $replacement = "$page_querystring=$pagenum";
401                 $qstr = preg_replace("/".$page_querystring."[^\d]+\d+/", $replacement, $qstr);
402                 // if we already have a mod_rewrite style page string
403         } elseif ( preg_match( '|'.$page_modregex.'\d+|', $qstr ) ) {
404                 $permalink = 1;
405                 $qstr = preg_replace('|'.$page_modregex.'\d+|',"$page_modstring$pagenum",$qstr);
406
407                 // if we don't have a page string at all ...
408                 // lets see what sort of URL we have...
409         } else {
410                 // we need to know the way queries are being written
411                 // if there's a querystring_start (a "?" usually), it's definitely not mod_rewritten
412                 if ( stristr( $qstr, '?' ) ) {
413                         // so append the query string (using &, since we already have ?)
414                         $qstr .=        '&amp;' . $page_querystring . '=' . $pagenum;
415                         // otherwise, it could be rewritten, OR just the default index ...
416                 } elseif( '' != get_settings('permalink_structure') && ! is_admin() ) {
417                         $permalink = 1;
418                         $index = $wp_rewrite->index;
419                         // If it's not a path info permalink structure, trim the index.
420                         if ( !$wp_rewrite->using_index_permalinks() ) {
421                                 $qstr = preg_replace("#/*" . $index . "/*#", '/', $qstr);
422                         } else {
423                                 // If using path info style permalinks, make sure the index is in
424                                 // the URI.
425                                 if ( strpos($qstr, $index) === false )
426                                         $qstr = '/' . $index . $qstr;
427                         }
428
429                         $qstr = trailingslashit($qstr) . $page_modstring . $pagenum;
430                 } else {
431                         $qstr = $index . '?' . $page_querystring . '=' . $pagenum;
432                 }
433         }
434
435         $qstr = preg_replace('|^/+|', '', $qstr);
436         if ( $permalink )
437                 $qstr = trailingslashit($qstr);
438         $qstr = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', trailingslashit( get_settings('home') ) . $qstr );
439         
440         // showing /page/1/ or ?paged=1 is redundant
441         if ( 1 === $pagenum ) {
442                 $qstr = str_replace('page/1/', '', $qstr); // for mod_rewrite style
443                 $qstr = remove_query_arg('paged', $qstr); // for query style
444         }
445         return $qstr;
446 }
447
448 function next_posts($max_page = 0) { // original by cfactor at cooltux.org
449         global $paged, $pagenow;
450
451         if ( !is_single() ) {
452                 if ( !$paged )
453                         $paged = 1;
454                 $nextpage = intval($paged) + 1;
455                 if ( !$max_page || $max_page >= $nextpage )
456                         echo get_pagenum_link($nextpage);
457         }
458 }
459
460 function next_posts_link($label='Next Page &raquo;', $max_page=0) {
461         global $paged, $result, $request, $posts_per_page, $wpdb, $max_num_pages;
462         if ( !$max_page ) {
463                         if ( isset($max_num_pages) ) {
464                                 $max_page = $max_num_pages;
465                         } else {
466                                 preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);
467                                 $fromwhere = $matches[1];
468                                 $numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
469                                 $max_page = $max_num_pages = ceil($numposts / $posts_per_page);
470                         }
471         }
472         if ( !$paged )
473                 $paged = 1;
474         $nextpage = intval($paged) + 1;
475         if ( (! is_single()) && (empty($paged) || $nextpage <= $max_page) ) {
476                 echo '<a href="';
477                 next_posts($max_page);
478                 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
479         }
480 }
481
482
483 function previous_posts() { // original by cfactor at cooltux.org
484         global $paged, $pagenow;
485
486         if ( !is_single() ) {
487                 $nextpage = intval($paged) - 1;
488                 if ( $nextpage < 1 )
489                         $nextpage = 1;
490                 echo get_pagenum_link($nextpage);
491         }
492 }
493
494
495 function previous_posts_link($label='&laquo; Previous Page') {
496         global $paged;
497         if ( (!is_single())     && ($paged > 1) ) {
498                 echo '<a href="';
499                 previous_posts();
500                 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
501         }
502 }
503
504 function posts_nav_link($sep=' &#8212; ', $prelabel='&laquo; Previous Page', $nxtlabel='Next Page &raquo;') {
505         global $request, $posts_per_page, $wpdb, $max_num_pages;
506         if ( !is_single() ) {
507
508                 if ( 'posts' == get_query_var('what_to_show') ) {
509                         if ( !isset($max_num_pages) ) {
510                                 preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);
511                                 $fromwhere = $matches[1];
512                                 $numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
513                                 $max_num_pages = ceil($numposts / $posts_per_page);
514                         }
515                 } else {
516                         $max_num_pages = 999999;
517                 }
518
519                 if ( $max_num_pages > 1 ) {
520                         previous_posts_link($prelabel);
521                         echo preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $sep);
522                         next_posts_link($nxtlabel, $max_page);
523                 }
524         }
525 }
526
527 ?>