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