]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/link-template.php
Wordpress 2.6.2
[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) . '-' . $post->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, $leavename=false) {
47         $rewritecode = array(
48                 '%year%',
49                 '%monthnum%',
50                 '%day%',
51                 '%hour%',
52                 '%minute%',
53                 '%second%',
54                 $leavename? '' : '%postname%',
55                 '%post_id%',
56                 '%category%',
57                 '%author%',
58                 $leavename? '' : '%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, $leavename);
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                         // show default category in permalinks, without
85                         // having to assign it explicitly
86                         if ( empty($category) ) {
87                                 $default_category = get_category( get_option( 'default_category' ) );
88                                 $category = is_wp_error( $default_category ) ? '' : $default_category->slug; 
89                         }
90                 }
91
92                 $author = '';
93                 if ( strpos($permalink, '%author%') !== false ) {
94                         $authordata = get_userdata($post->post_author);
95                         $author = $authordata->user_nicename;
96                 }
97
98                 $date = explode(" ",date('Y m d H i s', $unixtime));
99                 $rewritereplace =
100                 array(
101                         $date[0],
102                         $date[1],
103                         $date[2],
104                         $date[3],
105                         $date[4],
106                         $date[5],
107                         $post->post_name,
108                         $post->ID,
109                         $category,
110                         $author,
111                         $post->post_name,
112                 );
113                 $permalink = get_option('home') . str_replace($rewritecode, $rewritereplace, $permalink);
114                 $permalink = user_trailingslashit($permalink, 'single');
115                 return apply_filters('post_link', $permalink, $post);
116         } else { // if they're not using the fancy permalink option
117                 $permalink = get_option('home') . '/?p=' . $post->ID;
118                 return apply_filters('post_link', $permalink, $post);
119         }
120 }
121
122 // get permalink from post ID
123 function post_permalink($post_id = 0, $deprecated = '') {
124         return get_permalink($post_id);
125 }
126
127 // Respects page_on_front.  Use this one.
128 function get_page_link($id = false, $leavename = false) {
129         global $post;
130
131         $id = (int) $id;
132         if ( !$id )
133                 $id = (int) $post->ID;
134
135         if ( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') )
136                 $link = get_option('home');
137         else
138                 $link = _get_page_link( $id , $leavename );
139
140         return apply_filters('page_link', $link, $id);
141 }
142
143 // Ignores page_on_front.  Internal use only.
144 function _get_page_link( $id = false, $leavename = false ) {
145         global $post, $wp_rewrite;
146
147         if ( !$id )
148                 $id = (int) $post->ID;
149         else
150                 $post = &get_post($id);
151
152         $pagestruct = $wp_rewrite->get_page_permastruct();
153
154         if ( '' != $pagestruct && isset($post->post_status) && 'draft' != $post->post_status ) {
155                 $link = get_page_uri($id);
156                 $link = ( $leavename ) ? $pagestruct : str_replace('%pagename%', $link, $pagestruct);
157                 $link = get_option('home') . "/$link";
158                 $link = user_trailingslashit($link, 'page');
159         } else {
160                 $link = get_option('home') . "/?page_id=$id";
161         }
162
163         return apply_filters( '_get_page_link', $link, $id );
164 }
165
166 function get_attachment_link($id = false) {
167         global $post, $wp_rewrite;
168
169         $link = false;
170
171         if (! $id) {
172                 $id = (int) $post->ID;
173         }
174
175         $object = get_post($id);
176         if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) {
177                 $parent = get_post($object->post_parent);
178                 if ( 'page' == $parent->post_type )
179                         $parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front
180                 else
181                         $parentlink = get_permalink( $object->post_parent );
182                 if ( is_numeric($object->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
183                         $name = 'attachment/' . $object->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
184                 else
185                         $name = $object->post_name;
186                 if (strpos($parentlink, '?') === false)
187                         $link = user_trailingslashit( trailingslashit($parentlink) . $name );
188         }
189
190         if (! $link ) {
191                 $link = get_bloginfo('url') . "/?attachment_id=$id";
192         }
193
194         return apply_filters('attachment_link', $link, $id);
195 }
196
197 function get_year_link($year) {
198         global $wp_rewrite;
199         if ( !$year )
200                 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
201         $yearlink = $wp_rewrite->get_year_permastruct();
202         if ( !empty($yearlink) ) {
203                 $yearlink = str_replace('%year%', $year, $yearlink);
204                 return apply_filters('year_link', get_option('home') . user_trailingslashit($yearlink, 'year'), $year);
205         } else {
206                 return apply_filters('year_link', get_option('home') . '/?m=' . $year, $year);
207         }
208 }
209
210 function get_month_link($year, $month) {
211         global $wp_rewrite;
212         if ( !$year )
213                 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
214         if ( !$month )
215                 $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
216         $monthlink = $wp_rewrite->get_month_permastruct();
217         if ( !empty($monthlink) ) {
218                 $monthlink = str_replace('%year%', $year, $monthlink);
219                 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
220                 return apply_filters('month_link', get_option('home') . user_trailingslashit($monthlink, 'month'), $year, $month);
221         } else {
222                 return apply_filters('month_link', get_option('home') . '/?m=' . $year . zeroise($month, 2), $year, $month);
223         }
224 }
225
226 function get_day_link($year, $month, $day) {
227         global $wp_rewrite;
228         if ( !$year )
229                 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
230         if ( !$month )
231                 $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
232         if ( !$day )
233                 $day = gmdate('j', time()+(get_option('gmt_offset') * 3600));
234
235         $daylink = $wp_rewrite->get_day_permastruct();
236         if ( !empty($daylink) ) {
237                 $daylink = str_replace('%year%', $year, $daylink);
238                 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
239                 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
240                 return apply_filters('day_link', get_option('home') . user_trailingslashit($daylink, 'day'), $year, $month, $day);
241         } else {
242                 return apply_filters('day_link', get_option('home') . '/?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day);
243         }
244 }
245
246 function get_feed_link($feed = '') {
247         global $wp_rewrite;
248
249         $permalink = $wp_rewrite->get_feed_permastruct();
250         if ( '' != $permalink ) {
251                 if ( false !== strpos($feed, 'comments_') ) {
252                         $feed = str_replace('comments_', '', $feed);
253                         $permalink = $wp_rewrite->get_comment_feed_permastruct();
254                 }
255
256                 if ( get_default_feed() == $feed )
257                         $feed = '';
258
259                 $permalink = str_replace('%feed%', $feed, $permalink);
260                 $permalink = preg_replace('#/+#', '/', "/$permalink");
261                 $output =  get_option('home') . user_trailingslashit($permalink, 'feed');
262         } else {
263                 if ( empty($feed) )
264                         $feed = get_default_feed();
265
266                 if ( false !== strpos($feed, 'comments_') )
267                         $feed = str_replace('comments_', 'comments-', $feed);
268
269                 $output = get_option('home') . "/?feed={$feed}";
270         }
271
272         return apply_filters('feed_link', $output, $feed);
273 }
274
275 function get_post_comments_feed_link($post_id = '', $feed = '') {
276         global $id;
277
278         if ( empty($post_id) )
279                 $post_id = (int) $id;
280
281         if ( empty($feed) )
282                 $feed = get_default_feed();
283
284         if ( '' != get_option('permalink_structure') ) {
285                 $url = trailingslashit( get_permalink($post_id) ) . 'feed';
286                 if ( $feed != get_default_feed() )
287                         $url .= "/$feed";
288                 $url = user_trailingslashit($url, 'single_feed');
289         } else {
290                 $type = get_post_field('post_type', $post_id);
291                 if ( 'page' == $type )
292                         $url = get_option('home') . "/?feed=$feed&amp;page_id=$post_id";
293                 else
294                         $url = get_option('home') . "/?feed=$feed&amp;p=$post_id";
295         }
296
297         return apply_filters('post_comments_feed_link', $url);
298 }
299
300 /** post_comments_feed_link() - Output the comment feed link for a post.
301  *
302  * Prints out the comment feed link for a post.  Link text is placed in the
303  * anchor.  If no link text is specified, default text is used.  If no post ID
304  * is specified, the current post is used.
305  *
306  * @package WordPress
307  * @subpackage Feed
308  * @since 2.5
309  *
310  * @param string Descriptive text
311  * @param int Optional post ID.  Default to current post.
312  * @return string Link to the comment feed for the current post
313 */
314 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
315         $url = get_post_comments_feed_link($post_id, $feed);
316         if ( empty($link_text) )
317                 $link_text = __('Comments Feed');
318
319         echo "<a href='$url'>$link_text</a>";
320 }
321
322 function get_author_feed_link( $author_id, $feed = '' ) {
323         $author_id = (int) $author_id;
324         $permalink_structure = get_option('permalink_structure');
325
326         if ( empty($feed) )
327                 $feed = get_default_feed();
328
329         if ( '' == $permalink_structure ) {
330                 $link = get_option('home') . '?feed=rss2&amp;author=' . $author_id;
331         } else {
332                 $link = get_author_posts_url($author_id);
333                 $link = trailingslashit($link) . user_trailingslashit('feed', 'feed');
334         }
335
336         $link = apply_filters('author_feed_link', $link);
337
338         return $link;
339 }
340
341 /** get_category_feed_link() - Get the feed link for a given category
342  *
343  * Returns a link to the feed for all post in a given category.  A specific feed can be requested
344  * or left blank to get the default feed.
345  *
346  * @package WordPress
347  * @subpackage Feed
348  * @since 2.5
349  *
350  * @param int $cat_id ID of a category
351  * @param string $feed Feed type
352  * @return string Link to the feed for the category specified by $cat_id
353 */
354 function get_category_feed_link($cat_id, $feed = '') {
355         $cat_id = (int) $cat_id;
356
357         $category = get_category($cat_id);
358
359         if ( empty($category) || is_wp_error($category) )
360                 return false;
361
362         if ( empty($feed) )
363                 $feed = get_default_feed();
364
365         $permalink_structure = get_option('permalink_structure');
366
367         if ( '' == $permalink_structure ) {
368                 $link = get_option('home') . "?feed=$feed&amp;cat=" . $cat_id;
369         } else {
370                 $link = get_category_link($cat_id);
371                 if( $feed == get_default_feed() )
372                         $feed_link = 'feed';
373                 else
374                         $feed_link = "feed/$feed";
375
376                 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
377         }
378
379         $link = apply_filters('category_feed_link', $link, $feed);
380
381         return $link;
382 }
383
384 function get_tag_feed_link($tag_id, $feed = '') {
385         $tag_id = (int) $tag_id;
386
387         $tag = get_tag($tag_id);
388
389         if ( empty($tag) || is_wp_error($tag) )
390                 return false;
391
392         $permalink_structure = get_option('permalink_structure');
393
394         if ( empty($feed) )
395                 $feed = get_default_feed();
396
397         if ( '' == $permalink_structure ) {
398                 $link = get_option('home') . "?feed=$feed&amp;tag=" . $tag->slug;
399         } else {
400                 $link = get_tag_link($tag->term_id);
401                 if ( $feed == get_default_feed() )
402                         $feed_link = 'feed';
403                 else
404                         $feed_link = "feed/$feed";
405                 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
406         }
407
408         $link = apply_filters('tag_feed_link', $link, $feed);
409
410         return $link;
411 }
412
413 function get_search_feed_link($search_query = '', $feed = '') {
414         if ( empty($search_query) )
415                 $search = attribute_escape(get_search_query());
416         else
417                 $search = attribute_escape(stripslashes($search_query));
418
419         if ( empty($feed) )
420                 $feed = get_default_feed();
421
422         $link = get_option('home') . "?s=$search&amp;feed=$feed";
423
424         $link = apply_filters('search_feed_link', $link);
425
426         return $link;
427 }
428
429 function get_search_comments_feed_link($search_query = '', $feed = '') {
430         if ( empty($search_query) )
431                 $search = attribute_escape(get_search_query());
432         else
433                 $search = attribute_escape(stripslashes($search_query));
434
435         if ( empty($feed) )
436                 $feed = get_default_feed();
437
438         $link = get_option('home') . "?s=$search&amp;feed=comments-$feed";
439
440         $link = apply_filters('search_feed_link', $link);
441
442         return $link;
443 }
444
445 function get_edit_post_link( $id = 0, $context = 'display' ) {
446         if ( !$post = &get_post( $id ) )
447                 return;
448
449         if ( 'display' == $context )
450                 $action = 'action=edit&amp;';
451         else
452                 $action = 'action=edit&';
453
454         switch ( $post->post_type ) :
455         case 'page' :
456                 if ( !current_user_can( 'edit_page', $post->ID ) )
457                         return;
458                 $file = 'page';
459                 $var  = 'post';
460                 break;
461         case 'attachment' :
462                 if ( !current_user_can( 'edit_post', $post->ID ) )
463                         return;
464                 $file = 'media';
465                 $var  = 'attachment_id';
466                 break;
467         case 'revision' :
468                 if ( !current_user_can( 'edit_post', $post->ID ) )
469                         return;
470                 $file = 'revision';
471                 $var  = 'revision';
472                 $action = '';
473                 break;
474         default :
475                 if ( !current_user_can( 'edit_post', $post->ID ) )
476                         return;
477                 $file = 'post';
478                 $var  = 'post';
479                 break;
480         endswitch;
481         
482         return apply_filters( 'get_edit_post_link', admin_url("$file.php?{$action}$var=$post->ID"), $post->ID, $context );
483 }
484
485 function edit_post_link( $link = 'Edit This', $before = '', $after = '' ) {
486         global $post;
487
488         if ( $post->post_type == 'page' ) {
489                 if ( !current_user_can( 'edit_page', $post->ID ) )
490                         return;
491         } else {
492                 if ( !current_user_can( 'edit_post', $post->ID ) )
493                         return;
494         }
495
496         $link = '<a href="' . get_edit_post_link( $post->ID ) . '" title="' . __( 'Edit post' ) . '">' . $link . '</a>';
497         echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after;
498 }
499
500 function get_edit_comment_link( $comment_id = 0 ) {
501         $comment = &get_comment( $comment_id );
502         $post = &get_post( $comment->comment_post_ID );
503
504         if ( $post->post_type == 'page' ) {
505                 if ( !current_user_can( 'edit_page', $post->ID ) )
506                         return;
507         } else {
508                 if ( !current_user_can( 'edit_post', $post->ID ) )
509                         return;
510         }
511
512         $location = admin_url('comment.php?action=editcomment&amp;c=') . $comment->comment_ID;
513         return apply_filters( 'get_edit_comment_link', $location );
514 }
515
516 function edit_comment_link( $link = 'Edit This', $before = '', $after = '' ) {
517         global $comment, $post;
518
519         if ( $post->post_type == 'attachment' ) {
520         } elseif ( $post->post_type == 'page' ) {
521                 if ( !current_user_can( 'edit_page', $post->ID ) )
522                         return;
523         } else {
524                 if ( !current_user_can( 'edit_post', $post->ID ) )
525                         return;
526         }
527
528         $link = '<a href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . __( 'Edit comment' ) . '">' . $link . '</a>';
529         echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
530 }
531
532 // Navigation links
533
534 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
535         return get_adjacent_post($in_same_cat, $excluded_categories);
536 }
537
538 function get_next_post($in_same_cat = false, $excluded_categories = '') {
539         return get_adjacent_post($in_same_cat, $excluded_categories, false);
540 }
541
542 function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) {
543         global $post, $wpdb;
544
545         if( empty($post) || !is_single() || is_attachment() )
546                 return null;
547
548         $current_post_date = $post->post_date;
549
550         $join = '';
551         $posts_in_ex_cats_sql = '';
552         if ( $in_same_cat || !empty($excluded_categories) ) {
553                 $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
554
555                 if ( $in_same_cat ) {
556                         $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids');
557                         $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode($cat_array, ',') . ')';
558                 }
559
560                 $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
561                 if ( !empty($excluded_categories) ) {
562                         $excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
563                         if ( !empty($cat_array) ) {
564                                 $excluded_categories = array_diff($excluded_categories, $cat_array);
565                                 $posts_in_ex_cats_sql = '';
566                         }
567
568                         if ( !empty($excluded_categories) ) {
569                                 $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
570                         }
571                 }
572         }
573
574         $adjacent = $previous ? 'previous' : 'next';
575         $op = $previous ? '<' : '>';
576         $order = $previous ? 'DESC' : 'ASC';
577
578         $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
579         $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date), $in_same_cat, $excluded_categories );
580         $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
581
582         return $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
583 }
584
585 function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
586         adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
587 }
588
589 function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
590         adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
591 }
592
593 function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {
594         if ( $previous && is_attachment() )
595                 $post = & get_post($GLOBALS['post']->post_parent);
596         else
597                 $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
598
599         if ( !$post )
600                 return;
601
602         $title = $post->post_title;
603
604         if ( empty($post->post_title) )
605                 $title = $previous ? __('Previous Post') : __('Next Post');
606
607         $title = apply_filters('the_title', $title, $post);
608         $string = '<a href="'.get_permalink($post).'">';
609         $link = str_replace('%title', $title, $link);
610         $link = $string . $link . '</a>';
611
612         $format = str_replace('%link', $link, $format);
613
614         $adjacent = $previous ? 'previous' : 'next';
615         echo apply_filters( "{$adjacent}_post_link", $format, $link );
616 }
617
618 function get_pagenum_link($pagenum = 1) {
619         global $wp_rewrite;
620
621         $pagenum = (int) $pagenum;
622
623         $request = remove_query_arg( 'paged' );
624
625         $home_root = parse_url(get_option('home'));
626         $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
627         $home_root = preg_quote( trailingslashit( $home_root ), '|' );
628
629         $request = preg_replace('|^'. $home_root . '|', '', $request);
630         $request = preg_replace('|^/+|', '', $request);
631
632         if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
633                 $base = trailingslashit( get_bloginfo( 'home' ) );
634
635                 if ( $pagenum > 1 ) {
636                         $result = add_query_arg( 'paged', $pagenum, $base . $request );
637                 } else {
638                         $result = $base . $request;
639                 }
640         } else {
641                 $qs_regex = '|\?.*?$|';
642                 preg_match( $qs_regex, $request, $qs_match );
643
644                 if ( !empty( $qs_match[0] ) ) {
645                         $query_string = $qs_match[0];
646                         $request = preg_replace( $qs_regex, '', $request );
647                 } else {
648                         $query_string = '';
649                 }
650
651                 $request = preg_replace( '|page/\d+/?$|', '', $request);
652                 $request = preg_replace( '|^index\.php|', '', $request);
653                 $request = ltrim($request, '/');
654
655                 $base = trailingslashit( get_bloginfo( 'url' ) );
656
657                 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
658                         $base .= 'index.php/';
659
660                 if ( $pagenum > 1 ) {
661                         $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( 'page/' . $pagenum, 'paged' );
662                 }
663
664                 $result = $base . $request . $query_string;
665         }
666
667         $result = apply_filters('get_pagenum_link', $result);
668
669         return $result;
670 }
671
672 function get_next_posts_page_link($max_page = 0) {
673         global $paged;
674
675         if ( !is_single() ) {
676                 if ( !$paged )
677                         $paged = 1;
678                 $nextpage = intval($paged) + 1;
679                 if ( !$max_page || $max_page >= $nextpage )
680                         return get_pagenum_link($nextpage);
681         }
682 }
683
684 function next_posts($max_page = 0) {
685         echo clean_url(get_next_posts_page_link($max_page));
686 }
687
688 function next_posts_link($label='Next Page &raquo;', $max_page=0) {
689         global $paged, $wp_query;
690         if ( !$max_page ) {
691                 $max_page = $wp_query->max_num_pages;
692         }
693         if ( !$paged )
694                 $paged = 1;
695         $nextpage = intval($paged) + 1;
696         if ( (! is_single()) && (empty($paged) || $nextpage <= $max_page) ) {
697                 echo '<a href="';
698                 next_posts($max_page);
699                 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
700         }
701 }
702
703 function get_previous_posts_page_link() {
704         global $paged;
705
706         if ( !is_single() ) {
707                 $nextpage = intval($paged) - 1;
708                 if ( $nextpage < 1 )
709                         $nextpage = 1;
710                 return get_pagenum_link($nextpage);
711         }
712 }
713
714 function previous_posts() {
715         echo clean_url(get_previous_posts_page_link());
716 }
717
718 function previous_posts_link($label='&laquo; Previous Page') {
719         global $paged;
720         if ( (!is_single())     && ($paged > 1) ) {
721                 echo '<a href="';
722                 previous_posts();
723                 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
724         }
725 }
726
727 function posts_nav_link($sep=' &#8212; ', $prelabel='&laquo; Previous Page', $nxtlabel='Next Page &raquo;') {
728         global $wp_query;
729         if ( !is_singular() ) {
730                 $max_num_pages = $wp_query->max_num_pages;
731                 $paged = get_query_var('paged');
732
733                 //only have sep if there's both prev and next results
734                 if ($paged < 2 || $paged >= $max_num_pages) {
735                         $sep = '';
736                 }
737
738                 if ( $max_num_pages > 1 ) {
739                         previous_posts_link($prelabel);
740                         echo preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $sep);
741                         next_posts_link($nxtlabel);
742                 }
743         }
744 }
745
746 function get_shortcut_link() {
747         $link = "javascript:
748                         var d=document,
749                         w=window,
750                         e=w.getSelection,
751                         k=d.getSelection,
752                         x=d.selection,
753                         s=(e?e():(k)?k():(x?x.createRange().text:0)),
754                         f='" . admin_url('press-this.php') . "',
755                         l=d.location,
756                         e=encodeURIComponent,
757                         g=f+'?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&v=2';
758                         function a(){
759                                 if(!w.open(g,'t','toolbar=0,resizable=0,scrollbars=1,status=1,width=700,height=500')){
760                                         l.href=g;
761                                 }
762                         }";
763                         if (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false)
764                                 $link .= 'setTimeout(a,0);';
765                         else
766                                 $link .= 'a();';
767
768                         $link .= "void(0);";
769
770         $link = str_replace(array("\r", "\n", "\t"),  '', $link);
771
772         return apply_filters('shortcut_link', $link);
773 }
774
775 /** Return the site url
776  *
777  *
778  * @package WordPress
779  * @since 2.6
780  *
781  * Returns the 'site_url' option with the appropriate protocol,  'https' if is_ssl() and 'http' otherwise. 
782  * If $scheme is 'http' or 'https', is_ssl() is overridden.
783  *
784  * @param string $path Optional path relative to the site url
785  * @param string $scheme Optional scheme to give the site url context. Currently 'http','https', 'login', 'login_post', or 'admin'
786  * @return string Site url link with optional path appended
787 */
788 function site_url($path = '', $scheme = null) {
789         // should the list of allowed schemes be maintained elsewhere?
790         if ( !in_array($scheme, array('http', 'https')) ) {
791                 if ( ('login_post' == $scheme) && ( force_ssl_login() || force_ssl_admin() ) )
792                         $scheme = 'https';
793                 elseif ( ('login' == $scheme) && ( force_ssl_admin() ) )
794                         $scheme = 'https';
795                 elseif ( ('admin' == $scheme) && force_ssl_admin() )
796                         $scheme = 'https';
797                 else
798                         $scheme = ( is_ssl() ? 'https' : 'http' );
799         }
800
801         $url = str_replace( 'http://', "{$scheme}://", get_option('siteurl') );
802
803         if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
804                 $url .= '/' . ltrim($path, '/');
805
806         return $url;
807 }
808
809 /** Return the admin url
810  *
811  *
812  * @package WordPress
813  * @since 2.6
814  *
815  * Returns the url to the admin area
816  *
817  * @param string $path Optional path relative to the admin url
818  * @return string Admin url link with optional path appended
819 */
820 function admin_url($path = '') {
821         $url = site_url('wp-admin/', 'admin');
822
823         if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
824                 $url .= ltrim($path, '/');
825
826         return $url;
827 }
828
829 /** Return the includes url
830  *
831  *
832  * @package WordPress
833  * @since 2.6
834  *
835  * Returns the url to the includes directory
836  *
837  * @param string $path Optional path relative to the includes url
838  * @return string Includes url link with optional path appended
839 */
840 function includes_url($path = '') {
841         $url = site_url() . '/' . WPINC . '/';
842
843         if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
844                 $url .= ltrim($path, '/');
845
846         return $url;
847 }
848
849 /** Return the content url
850  *
851  *
852  * @package WordPress
853  * @since 2.6
854  *
855  * Returns the url to the content directory
856  *
857  * @param string $path Optional path relative to the content url
858  * @return string Content url link with optional path appended
859 */
860 function content_url($path = '') {
861         $scheme = ( is_ssl() ? 'https' : 'http' );
862         $url = WP_CONTENT_URL;
863         if ( 0 === strpos($url, 'http') ) {
864                 if ( is_ssl() )
865                         $url = str_replace( 'http://', "{$scheme}://", $url );
866         }
867
868         if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
869                 $url .= '/' . ltrim($path, '/');
870
871         return $url;
872 }
873
874 /** Return the plugins url
875  *
876  *
877  * @package WordPress
878  * @since 2.6
879  *
880  * Returns the url to the plugins directory
881  *
882  * @param string $path Optional path relative to the plugins url
883  * @return string Plugins url link with optional path appended
884 */
885 function plugins_url($path = '') {
886         $scheme = ( is_ssl() ? 'https' : 'http' );
887         $url = WP_PLUGIN_URL;
888         if ( 0 === strpos($url, 'http') ) {
889                 if ( is_ssl() )
890                         $url = str_replace( 'http://', "{$scheme}://", $url );
891         }
892
893         if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
894                 $url .= '/' . ltrim($path, '/');
895
896         return $url;
897 }
898
899 ?>