]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/link-template.php
Wordpress 2.3.3-scripts
[autoinstalls/wordpress.git] / wp-includes / link-template.php
1 <?php
2
3
4 function the_permalink() {
5         echo apply_filters('the_permalink', get_permalink());
6 }
7
8
9 /**
10  * Conditionally adds a trailing slash if the permalink structure
11  * has a trailing slash, strips the trailing slash if not
12  * @global object Uses $wp_rewrite
13  * @param $string string a URL with or without a trailing slash
14  * @param $type_of_url string the type of URL being considered (e.g. single, category, etc) for use in the filter
15  * @return string
16  */
17 function user_trailingslashit($string, $type_of_url = '') {
18         global $wp_rewrite;
19         if ( $wp_rewrite->use_trailing_slashes )
20                 $string = trailingslashit($string);
21         else
22                 $string = untrailingslashit($string);
23
24         // Note that $type_of_url can be one of following:
25         // single, single_trackback, single_feed, single_paged, feed, category, page, year, month, day, paged
26         $string = apply_filters('user_trailingslashit', $string, $type_of_url);
27         return $string;
28 }
29
30
31 function permalink_anchor($mode = 'id') {
32         global $post;
33         switch ( strtolower($mode) ) {
34                 case 'title':
35                         $title = sanitize_title($post->post_title) . '-' . $id;
36                         echo '<a id="'.$title.'"></a>';
37                         break;
38                 case 'id':
39                 default:
40                         echo '<a id="post-' . $post->ID . '"></a>';
41                         break;
42         }
43 }
44
45
46 function get_permalink($id = 0) {
47         $rewritecode = array(
48                 '%year%',
49                 '%monthnum%',
50                 '%day%',
51                 '%hour%',
52                 '%minute%',
53                 '%second%',
54                 '%postname%',
55                 '%post_id%',
56                 '%category%',
57                 '%author%',
58                 '%pagename%'
59         );
60
61         $post = &get_post($id);
62
63         if ( empty($post->ID) ) return FALSE;
64
65         if ( $post->post_type == 'page' )
66                 return get_page_link($post->ID);
67         elseif ($post->post_type == 'attachment')
68                 return get_attachment_link($post->ID);
69
70         $permalink = get_option('permalink_structure');
71
72         if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending')) ) {
73                 $unixtime = strtotime($post->post_date);
74
75                 $category = '';
76                 if (strpos($permalink, '%category%') !== false) {
77                         $cats = get_the_category($post->ID);
78                         if ( $cats )
79                                 usort($cats, '_usort_terms_by_ID'); // order by ID
80                         $category = $cats[0]->slug;
81                         if ( $parent=$cats[0]->parent )
82                                 $category = get_category_parents($parent, FALSE, '/', TRUE) . $category;
83                 }
84
85                 $authordata = get_userdata($post->post_author);
86                 $author = $authordata->user_nicename;
87                 $date = explode(" ",date('Y m d H i s', $unixtime));
88                 $rewritereplace =
89                 array(
90                         $date[0],
91                         $date[1],
92                         $date[2],
93                         $date[3],
94                         $date[4],
95                         $date[5],
96                         $post->post_name,
97                         $post->ID,
98                         $category,
99                         $author,
100                         $post->post_name,
101                 );
102                 $permalink = get_option('home') . str_replace($rewritecode, $rewritereplace, $permalink);
103                 $permalink = user_trailingslashit($permalink, 'single');
104                 return apply_filters('post_link', $permalink, $post);
105         } else { // if they're not using the fancy permalink option
106                 $permalink = get_option('home') . '/?p=' . $post->ID;
107                 return apply_filters('post_link', $permalink, $post);
108         }
109 }
110
111 // get permalink from post ID
112 function post_permalink($post_id = 0, $deprecated = '') {
113         return get_permalink($post_id);
114 }
115
116 // Respects page_on_front.  Use this one.
117 function get_page_link($id = false) {
118         global $post;
119
120         $id = (int) $id;
121         if ( !$id )
122                 $id = (int) $post->ID;
123
124         if ( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') )
125                 $link = get_option('home');
126         else
127                 $link = _get_page_link( $id );
128
129         return apply_filters('page_link', $link, $id);
130 }
131
132 // Ignores page_on_front.  Internal use only.
133 function _get_page_link( $id = false ) {
134         global $post, $wp_rewrite;
135
136         if ( !$id )
137                 $id = (int) $post->ID;
138
139         $pagestruct = $wp_rewrite->get_page_permastruct();
140
141         if ( '' != $pagestruct && 'draft' != $post->post_status ) {
142                 $link = get_page_uri($id);
143                 $link = str_replace('%pagename%', $link, $pagestruct);
144                 $link = get_option('home') . "/$link";
145                 $link = user_trailingslashit($link, 'page');
146         } else {
147                 $link = get_option('home') . "/?page_id=$id";
148         }
149
150         return apply_filters( '_get_page_link', $link, $id );
151 }
152
153 function get_attachment_link($id = false) {
154         global $post, $wp_rewrite;
155
156         $link = false;
157
158         if (! $id) {
159                 $id = (int) $post->ID;
160         }
161
162         $object = get_post($id);
163         if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) {
164                 $parent = get_post($object->post_parent);
165                 if ( 'page' == $parent->post_type )
166                         $parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front
167                 else
168                         $parentlink = get_permalink( $object->post_parent );
169                 if (strpos($parentlink, '?') === false)
170                         $link = trailingslashit($parentlink) . $object->post_name . '/';
171         }
172
173         if (! $link ) {
174                 $link = get_bloginfo('url') . "/?attachment_id=$id";
175         }
176
177         return apply_filters('attachment_link', $link, $id);
178 }
179
180 function get_year_link($year) {
181         global $wp_rewrite;
182         if ( !$year )
183                 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
184         $yearlink = $wp_rewrite->get_year_permastruct();
185         if ( !empty($yearlink) ) {
186                 $yearlink = str_replace('%year%', $year, $yearlink);
187                 return apply_filters('year_link', get_option('home') . user_trailingslashit($yearlink, 'year'), $year);
188         } else {
189                 return apply_filters('year_link', get_option('home') . '/?m=' . $year, $year);
190         }
191 }
192
193 function get_month_link($year, $month) {
194         global $wp_rewrite;
195         if ( !$year )
196                 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
197         if ( !$month )
198                 $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
199         $monthlink = $wp_rewrite->get_month_permastruct();
200         if ( !empty($monthlink) ) {
201                 $monthlink = str_replace('%year%', $year, $monthlink);
202                 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
203                 return apply_filters('month_link', get_option('home') . user_trailingslashit($monthlink, 'month'), $year, $month);
204         } else {
205                 return apply_filters('month_link', get_option('home') . '/?m=' . $year . zeroise($month, 2), $year, $month);
206         }
207 }
208
209 function get_day_link($year, $month, $day) {
210         global $wp_rewrite;
211         if ( !$year )
212                 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
213         if ( !$month )
214                 $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
215         if ( !$day )
216                 $day = gmdate('j', time()+(get_option('gmt_offset') * 3600));
217
218         $daylink = $wp_rewrite->get_day_permastruct();
219         if ( !empty($daylink) ) {
220                 $daylink = str_replace('%year%', $year, $daylink);
221                 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
222                 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
223                 return apply_filters('day_link', get_option('home') . user_trailingslashit($daylink, 'day'), $year, $month, $day);
224         } else {
225                 return apply_filters('day_link', get_option('home') . '/?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day);
226         }
227 }
228
229 function get_feed_link($feed='rss2') {
230         global $wp_rewrite;
231         $do_perma = 0;
232         $feed_url = get_option('siteurl');
233         $comment_feed_url = $feed_url;
234
235         $permalink = $wp_rewrite->get_feed_permastruct();
236         if ( '' != $permalink ) {
237                 if ( false !== strpos($feed, 'comments_') ) {
238                         $feed = str_replace('comments_', '', $feed);
239                         $permalink = $wp_rewrite->get_comment_feed_permastruct();
240                 }
241
242                 if ( 'rss2' == $feed )
243                         $feed = '';
244
245                 $permalink = str_replace('%feed%', $feed, $permalink);
246                 $permalink = preg_replace('#/+#', '/', "/$permalink");
247                 $output =  get_option('home') . user_trailingslashit($permalink, 'feed');
248         } else {
249                 if ( false !== strpos($feed, 'comments_') )
250                         $feed = str_replace('comments_', 'comments-', $feed);
251
252                 $output = get_option('home') . "/?feed={$feed}";
253         }
254
255         return apply_filters('feed_link', $output, $feed);
256 }
257
258 function get_post_comments_feed_link($post_id = '', $feed = 'rss2') {
259         global $id;
260
261         if ( empty($post_id) )
262                 $post_id = (int) $id;
263
264         if ( '' != get_option('permalink_structure') ) {
265                 $url = trailingslashit( get_permalink($post_id) ) . 'feed';
266                 if ( 'rss2' != $feed )
267                         $url .= "/$feed";
268                 $url = user_trailingslashit($url, 'single_feed');
269         } else {
270                 $type = get_post_field('post_type', $post_id);
271                 if ( 'page' == $type )
272                         $url = get_option('home') . "/?feed=$feed&amp;page_id=$post_id";
273                 else
274                         $url = get_option('home') . "/?feed=$feed&amp;p=$post_id";
275         }
276
277         return apply_filters('post_comments_feed_link', $url);
278 }
279
280 function get_edit_post_link( $id = 0 ) {
281         $post = &get_post( $id );
282
283         if ( $post->post_type == 'attachment' ) {
284                 return;
285         } elseif ( $post->post_type == 'page' ) {
286                 if ( !current_user_can( 'edit_page', $post->ID ) )
287                         return;
288
289                 $file = 'page';
290         } else {
291                 if ( !current_user_can( 'edit_post', $post->ID ) )
292                         return;
293
294                 $file = 'post';
295         }
296
297         return apply_filters( 'get_edit_post_link', get_bloginfo( 'wpurl' ) . '/wp-admin/' . $file . '.php?action=edit&amp;post=' . $post->ID, $post->ID );
298 }
299
300 function edit_post_link( $link = 'Edit This', $before = '', $after = '' ) {
301         global $post;
302
303         if ( $post->post_type == 'attachment' ) {
304                 return;
305         } elseif ( $post->post_type == 'page' ) {
306                 if ( !current_user_can( 'edit_page', $post->ID ) )
307                         return;
308
309                 $file = 'page';
310         } else {
311                 if ( !current_user_can( 'edit_post', $post->ID ) )
312                         return;
313
314                 $file = 'post';
315         }
316
317         $link = '<a href="' . get_edit_post_link( $post->ID ) . '" title="' . __( 'Edit post' ) . '">' . $link . '</a>';
318         echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after;
319 }
320
321 function get_edit_comment_link( $comment_id = 0 ) {
322         $comment = &get_comment( $comment_id );
323         $post = &get_post( $comment->comment_post_ID );
324
325         if ( $post->post_type == 'attachment' ) {
326                 return;
327         } elseif ( $post->post_type == 'page' ) {
328                 if ( !current_user_can( 'edit_page', $post->ID ) )
329                         return;
330         } else {
331                 if ( !current_user_can( 'edit_post', $post->ID ) )
332                         return;
333         }
334
335         $location = get_bloginfo( 'wpurl' ) . '/wp-admin/comment.php?action=editcomment&amp;c=' . $comment->comment_ID;
336         return apply_filters( 'get_edit_comment_link', $location );
337 }
338
339 function edit_comment_link( $link = 'Edit This', $before = '', $after = '' ) {
340         global $comment, $post;
341
342         if ( $post->post_type == 'attachment' ) {
343                 return;
344         } elseif ( $post->post_type == 'page' ) {
345                 if ( !current_user_can( 'edit_page', $post->ID ) )
346                         return;
347         } else {
348                 if ( !current_user_can( 'edit_post', $post->ID ) )
349                         return;
350         }
351
352         $link = '<a href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . __( 'Edit comment' ) . '">' . $link . '</a>';
353         echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
354 }
355
356 // Navigation links
357
358 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
359         global $post, $wpdb;
360
361         if( empty($post) || !is_single() || is_attachment() )
362                 return null;
363
364         $current_post_date = $post->post_date;
365
366         $join = '';
367         if ( $in_same_cat ) {
368                 $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id ";
369                 $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=tt_ids');
370                 $join .= ' AND (tr.term_taxonomy_id = ' . intval($cat_array[0]);
371                 for ( $i = 1; $i < (count($cat_array)); $i++ ) {
372                         $join .= ' OR tr.term_taxonomy_id = ' . intval($cat_array[$i]);
373                 }
374                 $join .= ')';
375         }
376
377         $sql_exclude_cats = '';
378         if ( !empty($excluded_categories) ) {
379                 $blah = explode(' and ', $excluded_categories);
380                 $posts_in_ex_cats = get_objects_in_term($blah, 'category');
381                 $posts_in_ex_cats_sql = 'AND p.ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')';
382         }
383
384         $join  = apply_filters( 'get_previous_post_join', $join, $in_same_cat, $excluded_categories );
385         $where = apply_filters( 'get_previous_post_where', "WHERE p.post_date < '$current_post_date' AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql", $in_same_cat, $excluded_categories );
386         $sort  = apply_filters( 'get_previous_post_sort', 'ORDER BY p.post_date DESC LIMIT 1' );
387
388         return @$wpdb->get_row("SELECT p.ID, p.post_title FROM $wpdb->posts AS p $join $where $sort");
389 }
390
391 function get_next_post($in_same_cat = false, $excluded_categories = '') {
392         global $post, $wpdb;
393
394         if( empty($post) || !is_single() || is_attachment() )
395                 return null;
396
397         $current_post_date = $post->post_date;
398
399         $join = '';
400         if ( $in_same_cat ) {
401                 $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id ";
402                 $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=tt_ids');
403                 $join .= ' AND (tr.term_taxonomy_id = ' . intval($cat_array[0]);
404                 for ( $i = 1; $i < (count($cat_array)); $i++ ) {
405                         $join .= ' OR tr.term_taxonomy_id = ' . intval($cat_array[$i]);
406                 }
407                 $join .= ')';
408         }
409
410         $sql_exclude_cats = '';
411         if ( !empty($excluded_categories) ) {
412                 $blah = explode(' and ', $excluded_categories);
413                 $posts_in_ex_cats = get_objects_in_term($blah, 'category');
414                 $posts_in_ex_cats_sql = 'AND p.ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')';
415         }
416
417         $join  = apply_filters( 'get_next_post_join', $join, $in_same_cat, $excluded_categories );
418         $where = apply_filters( 'get_next_post_where', "WHERE p.post_date > '$current_post_date' AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql AND p.ID != $post->ID", $in_same_cat, $excluded_categories );
419         $sort  = apply_filters( 'get_next_post_sort', 'ORDER BY p.post_date ASC LIMIT 1' );
420
421         return @$wpdb->get_row("SELECT p.ID, p.post_title FROM $wpdb->posts AS p $join $where $sort");
422 }
423
424
425 function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
426
427         if ( is_attachment() )
428                 $post = & get_post($GLOBALS['post']->post_parent);
429         else
430                 $post = get_previous_post($in_same_cat, $excluded_categories);
431
432         if ( !$post )
433                 return;
434
435         $title = $post->post_title;
436
437         if ( empty($post->post_title) )
438                 $title = __('Previous Post');
439
440         $title = apply_filters('the_title', $title, $post);
441         $string = '<a href="'.get_permalink($post->ID).'">';
442         $link = str_replace('%title', $title, $link);
443         $link = $pre . $string . $link . '</a>';
444
445         $format = str_replace('%link', $link, $format);
446
447         echo $format;
448 }
449
450 function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
451         $post = get_next_post($in_same_cat, $excluded_categories);
452
453         if ( !$post )
454                 return;
455
456         $title = $post->post_title;
457
458         if ( empty($post->post_title) )
459                 $title = __('Next Post');
460
461         $title = apply_filters('the_title', $title, $post);
462         $string = '<a href="'.get_permalink($post->ID).'">';
463         $link = str_replace('%title', $title, $link);
464         $link = $string . $link . '</a>';
465         $format = str_replace('%link', $link, $format);
466
467         echo $format;
468 }
469
470 function get_pagenum_link($pagenum = 1) {
471         global $wp_rewrite;
472
473         $pagenum = (int) $pagenum;
474
475         $request = remove_query_arg( 'paged' );
476
477         $home_root = parse_url(get_option('home'));
478         $home_root = $home_root['path'];
479         $home_root = preg_quote( trailingslashit( $home_root ), '|' );
480
481         $request = preg_replace('|^'. $home_root . '|', '', $request);
482         $request = preg_replace('|^/+|', '', $request);
483
484         if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
485                 $base = trailingslashit( get_bloginfo( 'home' ) );
486
487                 if ( $pagenum > 1 ) {
488                         $result = add_query_arg( 'paged', $pagenum, $base . $request );
489                 } else {
490                         $result = $base . $request;
491                 }
492         } else {
493                 $qs_regex = '|\?.*?$|';
494                 preg_match( $qs_regex, $request, $qs_match );
495
496                 if ( $qs_match[0] ) {
497                         $query_string = $qs_match[0];
498                         $request = preg_replace( $qs_regex, '', $request );
499                 } else {
500                         $query_string = '';
501                 }
502
503                 $request = preg_replace( '|page/(.+)/?$|', '', $request);
504                 $request = preg_replace( '|^index\.php|', '', $request);
505                 $request = ltrim($request, '/');
506
507                 $base = trailingslashit( get_bloginfo( 'url' ) );
508
509                 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
510                         $base .= 'index.php/';
511
512                 if ( $pagenum > 1 ) {
513                         $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( 'page/' . $pagenum, 'paged' );
514                 }
515
516                 $result = $base . $request . $query_string;
517         }
518
519         return $result;
520 }
521
522 function get_next_posts_page_link($max_page = 0) {
523         global $paged, $pagenow;
524
525         if ( !is_single() ) {
526                 if ( !$paged )
527                         $paged = 1;
528                 $nextpage = intval($paged) + 1;
529                 if ( !$max_page || $max_page >= $nextpage )
530                         return get_pagenum_link($nextpage);
531         }
532 }
533
534 function next_posts($max_page = 0) {
535         echo clean_url(get_next_posts_page_link($max_page));
536 }
537
538 function next_posts_link($label='Next Page &raquo;', $max_page=0) {
539         global $paged, $wpdb, $wp_query;
540         if ( !$max_page ) {
541                 $max_page = $wp_query->max_num_pages;
542         }
543         if ( !$paged )
544                 $paged = 1;
545         $nextpage = intval($paged) + 1;
546         if ( (! is_single()) && (empty($paged) || $nextpage <= $max_page) ) {
547                 echo '<a href="';
548                 next_posts($max_page);
549                 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
550         }
551 }
552
553 function get_previous_posts_page_link() {
554         global $paged, $pagenow;
555
556         if ( !is_single() ) {
557                 $nextpage = intval($paged) - 1;
558                 if ( $nextpage < 1 )
559                         $nextpage = 1;
560                 return get_pagenum_link($nextpage);
561         }
562 }
563
564 function previous_posts() {
565         echo clean_url(get_previous_posts_page_link());
566 }
567
568 function previous_posts_link($label='&laquo; Previous Page') {
569         global $paged;
570         if ( (!is_single())     && ($paged > 1) ) {
571                 echo '<a href="';
572                 previous_posts();
573                 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
574         }
575 }
576
577 function posts_nav_link($sep=' &#8212; ', $prelabel='&laquo; Previous Page', $nxtlabel='Next Page &raquo;') {
578         global $wp_query;
579         if ( !is_singular() ) {
580                 $max_num_pages = $wp_query->max_num_pages;
581                 $paged = get_query_var('paged');
582
583                 //only have sep if there's both prev and next results
584                 if ($paged < 2 || $paged >= $max_num_pages) {
585                         $sep = '';
586                 }
587
588                 if ( $max_num_pages > 1 ) {
589                         previous_posts_link($prelabel);
590                         echo preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $sep);
591                         next_posts_link($nxtlabel);
592                 }
593         }
594 }
595
596 ?>