]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/link-template.php
Wordpress 2.8-scripts
[autoinstalls/wordpress.git] / wp-includes / link-template.php
1 <?php
2 /**
3  * WordPress Link Template Functions
4  *
5  * @package WordPress
6  * @subpackage Template
7  */
8
9 /**
10  * Display the permalink for the current post.
11  *
12  * @since 1.2.0
13  * @uses apply_filters() Calls 'the_permalink' filter on the permalink string.
14  */
15 function the_permalink() {
16         echo apply_filters('the_permalink', get_permalink());
17 }
18
19 /**
20  * Retrieve trailing slash string, if blog set for adding trailing slashes.
21  *
22  * Conditionally adds a trailing slash if the permalink structure has a trailing
23  * slash, strips the trailing slash if not. The string is passed through the
24  * 'user_trailingslashit' filter. Will remove trailing slash from string, if
25  * blog is not set to have them.
26  *
27  * @since 2.2.0
28  * @uses $wp_rewrite
29  *
30  * @param $string String a URL with or without a trailing slash.
31  * @param $type_of_url String the type of URL being considered (e.g. single, category, etc) for use in the filter.
32  * @return string
33  */
34 function user_trailingslashit($string, $type_of_url = '') {
35         global $wp_rewrite;
36         if ( $wp_rewrite->use_trailing_slashes )
37                 $string = trailingslashit($string);
38         else
39                 $string = untrailingslashit($string);
40
41         // Note that $type_of_url can be one of following:
42         // single, single_trackback, single_feed, single_paged, feed, category, page, year, month, day, paged
43         $string = apply_filters('user_trailingslashit', $string, $type_of_url);
44         return $string;
45 }
46
47 /**
48  * Display permalink anchor for current post.
49  *
50  * The permalink mode title will use the post title for the 'a' element 'id'
51  * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute.
52  *
53  * @since 0.71
54  *
55  * @param string $mode Permalink mode can be either 'title', 'id', or default, which is 'id'.
56  */
57 function permalink_anchor($mode = 'id') {
58         global $post;
59         switch ( strtolower($mode) ) {
60                 case 'title':
61                         $title = sanitize_title($post->post_title) . '-' . $post->ID;
62                         echo '<a id="'.$title.'"></a>';
63                         break;
64                 case 'id':
65                 default:
66                         echo '<a id="post-' . $post->ID . '"></a>';
67                         break;
68         }
69 }
70
71 /**
72  * Retrieve full permalink for current post or post ID.
73  *
74  * @since 1.0.0
75  *
76  * @param int $id Optional. Post ID.
77  * @param bool $leavename Optional, defaults to false. Whether to keep post name or page name.
78  * @return string
79  */
80 function get_permalink($id = 0, $leavename = false) {
81         $rewritecode = array(
82                 '%year%',
83                 '%monthnum%',
84                 '%day%',
85                 '%hour%',
86                 '%minute%',
87                 '%second%',
88                 $leavename? '' : '%postname%',
89                 '%post_id%',
90                 '%category%',
91                 '%author%',
92                 $leavename? '' : '%pagename%',
93         );
94
95         if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) {
96                 $post = $id;
97                 $sample = true;
98         } else {
99                 $post = &get_post($id);
100                 $sample = false;
101         }
102
103         if ( empty($post->ID) ) return false;
104
105         if ( $post->post_type == 'page' )
106                 return get_page_link($post->ID, $leavename, $sample);
107         elseif ($post->post_type == 'attachment')
108                 return get_attachment_link($post->ID);
109
110         $permalink = get_option('permalink_structure');
111
112         if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending')) ) {
113                 $unixtime = strtotime($post->post_date);
114
115                 $category = '';
116                 if ( strpos($permalink, '%category%') !== false ) {
117                         $cats = get_the_category($post->ID);
118                         if ( $cats ) {
119                                 usort($cats, '_usort_terms_by_ID'); // order by ID
120                                 $category = $cats[0]->slug;
121                                 if ( $parent = $cats[0]->parent )
122                                         $category = get_category_parents($parent, false, '/', true) . $category;
123                         }
124                         // show default category in permalinks, without
125                         // having to assign it explicitly
126                         if ( empty($category) ) {
127                                 $default_category = get_category( get_option( 'default_category' ) );
128                                 $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
129                         }
130                 }
131
132                 $author = '';
133                 if ( strpos($permalink, '%author%') !== false ) {
134                         $authordata = get_userdata($post->post_author);
135                         $author = $authordata->user_nicename;
136                 }
137
138                 $date = explode(" ",date('Y m d H i s', $unixtime));
139                 $rewritereplace =
140                 array(
141                         $date[0],
142                         $date[1],
143                         $date[2],
144                         $date[3],
145                         $date[4],
146                         $date[5],
147                         $post->post_name,
148                         $post->ID,
149                         $category,
150                         $author,
151                         $post->post_name,
152                 );
153                 $permalink = get_option('home') . str_replace($rewritecode, $rewritereplace, $permalink);
154                 $permalink = user_trailingslashit($permalink, 'single');
155                 return apply_filters('post_link', $permalink, $post, $leavename);
156         } else { // if they're not using the fancy permalink option
157                 $permalink = trailingslashit(get_option('home')) . '?p=' . $post->ID;
158                 return apply_filters('post_link', $permalink, $post, $leavename);
159         }
160 }
161
162 /**
163  * Retrieve permalink from post ID.
164  *
165  * @since 1.0.0
166  *
167  * @param int $post_id Optional. Post ID.
168  * @param mixed $deprecated Not used.
169  * @return string
170  */
171 function post_permalink($post_id = 0, $deprecated = '') {
172         return get_permalink($post_id);
173 }
174
175 /**
176  * Retrieve the permalink for current page or page ID.
177  *
178  * Respects page_on_front. Use this one.
179  *
180  * @since 1.5.0
181  *
182  * @param int $id Optional. Post ID.
183  * @param bool $leavename Optional, defaults to false. Whether to keep page name.
184  * @param bool $sample Optional, defaults to false. Is it a sample permalink.
185  * @return string
186  */
187 function get_page_link( $id = false, $leavename = false, $sample = false ) {
188         global $post;
189
190         $id = (int) $id;
191         if ( !$id )
192                 $id = (int) $post->ID;
193
194         if ( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') )
195                 $link = get_option('home');
196         else
197                 $link = _get_page_link( $id , $leavename, $sample );
198
199         return apply_filters('page_link', $link, $id);
200 }
201
202 /**
203  * Retrieve the page permalink.
204  *
205  * Ignores page_on_front. Internal use only.
206  *
207  * @since 2.1.0
208  * @access private
209  *
210  * @param int $id Optional. Post ID.
211  * @param bool $leavename Optional. Leave name.
212  * @param bool $sample Optional. Sample permalink.
213  * @return string
214  */
215 function _get_page_link( $id = false, $leavename = false, $sample = false ) {
216         global $post, $wp_rewrite;
217
218         if ( !$id )
219                 $id = (int) $post->ID;
220         else
221                 $post = &get_post($id);
222
223         $pagestruct = $wp_rewrite->get_page_permastruct();
224
225         if ( '' != $pagestruct && ( ( isset($post->post_status) && 'draft' != $post->post_status ) || $sample ) ) {
226                 $link = get_page_uri($id);
227                 $link = ( $leavename ) ? $pagestruct : str_replace('%pagename%', $link, $pagestruct);
228                 $link = trailingslashit(get_option('home')) . "$link";
229                 $link = user_trailingslashit($link, 'page');
230         } else {
231                 $link = trailingslashit(get_option('home')) . "?page_id=$id";
232         }
233
234         return apply_filters( '_get_page_link', $link, $id );
235 }
236
237 /**
238  * Retrieve permalink for attachment.
239  *
240  * This can be used in the WordPress Loop or outside of it.
241  *
242  * @since 2.0.0
243  *
244  * @param int $id Optional. Post ID.
245  * @return string
246  */
247 function get_attachment_link($id = false) {
248         global $post, $wp_rewrite;
249
250         $link = false;
251
252         if (! $id) {
253                 $id = (int) $post->ID;
254         }
255
256         $object = get_post($id);
257         if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) {
258                 $parent = get_post($object->post_parent);
259                 if ( 'page' == $parent->post_type )
260                         $parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front
261                 else
262                         $parentlink = get_permalink( $object->post_parent );
263                 if ( is_numeric($object->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
264                         $name = 'attachment/' . $object->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
265                 else
266                         $name = $object->post_name;
267                 if (strpos($parentlink, '?') === false)
268                         $link = user_trailingslashit( trailingslashit($parentlink) . $name );
269         }
270
271         if (! $link ) {
272                 $link = trailingslashit(get_bloginfo('url')) . "?attachment_id=$id";
273         }
274
275         return apply_filters('attachment_link', $link, $id);
276 }
277
278 /**
279  * Retrieve the permalink for the year archives.
280  *
281  * @since 1.5.0
282  *
283  * @param int|bool $year False for current year or year for permalink.
284  * @return string
285  */
286 function get_year_link($year) {
287         global $wp_rewrite;
288         if ( !$year )
289                 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
290         $yearlink = $wp_rewrite->get_year_permastruct();
291         if ( !empty($yearlink) ) {
292                 $yearlink = str_replace('%year%', $year, $yearlink);
293                 return apply_filters('year_link', get_option('home') . user_trailingslashit($yearlink, 'year'), $year);
294         } else {
295                 return apply_filters('year_link', trailingslashit(get_option('home')) . '?m=' . $year, $year);
296         }
297 }
298
299 /**
300  * Retrieve the permalink for the month archives with year.
301  *
302  * @since 1.0.0
303  *
304  * @param bool|int $year False for current year. Integer of year.
305  * @param bool|int $month False for current month. Integer of month.
306  * @return string
307  */
308 function get_month_link($year, $month) {
309         global $wp_rewrite;
310         if ( !$year )
311                 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
312         if ( !$month )
313                 $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
314         $monthlink = $wp_rewrite->get_month_permastruct();
315         if ( !empty($monthlink) ) {
316                 $monthlink = str_replace('%year%', $year, $monthlink);
317                 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
318                 return apply_filters('month_link', get_option('home') . user_trailingslashit($monthlink, 'month'), $year, $month);
319         } else {
320                 return apply_filters('month_link', trailingslashit(get_option('home')) . '?m=' . $year . zeroise($month, 2), $year, $month);
321         }
322 }
323
324 /**
325  * Retrieve the permalink for the day archives with year and month.
326  *
327  * @since 1.0.0
328  *
329  * @param bool|int $year False for current year. Integer of year.
330  * @param bool|int $month False for current month. Integer of month.
331  * @param bool|int $day False for current day. Integer of day.
332  * @return string
333  */
334 function get_day_link($year, $month, $day) {
335         global $wp_rewrite;
336         if ( !$year )
337                 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
338         if ( !$month )
339                 $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
340         if ( !$day )
341                 $day = gmdate('j', time()+(get_option('gmt_offset') * 3600));
342
343         $daylink = $wp_rewrite->get_day_permastruct();
344         if ( !empty($daylink) ) {
345                 $daylink = str_replace('%year%', $year, $daylink);
346                 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
347                 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
348                 return apply_filters('day_link', get_option('home') . user_trailingslashit($daylink, 'day'), $year, $month, $day);
349         } else {
350                 return apply_filters('day_link', trailingslashit(get_option('home')) . '?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day);
351         }
352 }
353
354 /**
355  * Retrieve the permalink for the feed type.
356  *
357  * @since 1.5.0
358  *
359  * @param string $feed Optional, defaults to default feed. Feed type.
360  * @return string
361  */
362 function get_feed_link($feed = '') {
363         global $wp_rewrite;
364
365         $permalink = $wp_rewrite->get_feed_permastruct();
366         if ( '' != $permalink ) {
367                 if ( false !== strpos($feed, 'comments_') ) {
368                         $feed = str_replace('comments_', '', $feed);
369                         $permalink = $wp_rewrite->get_comment_feed_permastruct();
370                 }
371
372                 if ( get_default_feed() == $feed )
373                         $feed = '';
374
375                 $permalink = str_replace('%feed%', $feed, $permalink);
376                 $permalink = preg_replace('#/+#', '/', "/$permalink");
377                 $output =  get_option('home') . user_trailingslashit($permalink, 'feed');
378         } else {
379                 if ( empty($feed) )
380                         $feed = get_default_feed();
381
382                 if ( false !== strpos($feed, 'comments_') )
383                         $feed = str_replace('comments_', 'comments-', $feed);
384
385                 $output = trailingslashit(get_option('home')) . "?feed={$feed}";
386         }
387
388         return apply_filters('feed_link', $output, $feed);
389 }
390
391 /**
392  * Retrieve the permalink for the post comments feed.
393  *
394  * @since 2.2.0
395  *
396  * @param int $post_id Optional. Post ID.
397  * @param string $feed Optional. Feed type.
398  * @return string
399  */
400 function get_post_comments_feed_link($post_id = '', $feed = '') {
401         global $id;
402
403         if ( empty($post_id) )
404                 $post_id = (int) $id;
405
406         if ( empty($feed) )
407                 $feed = get_default_feed();
408
409         if ( '' != get_option('permalink_structure') ) {
410                 $url = trailingslashit( get_permalink($post_id) ) . 'feed';
411                 if ( $feed != get_default_feed() )
412                         $url .= "/$feed";
413                 $url = user_trailingslashit($url, 'single_feed');
414         } else {
415                 $type = get_post_field('post_type', $post_id);
416                 if ( 'page' == $type )
417                         $url = trailingslashit(get_option('home')) . "?feed=$feed&amp;page_id=$post_id";
418                 else
419                         $url = trailingslashit(get_option('home')) . "?feed=$feed&amp;p=$post_id";
420         }
421
422         return apply_filters('post_comments_feed_link', $url);
423 }
424
425 /**
426  * Display the comment feed link for a post.
427  *
428  * Prints out the comment feed link for a post. Link text is placed in the
429  * anchor. If no link text is specified, default text is used. If no post ID is
430  * specified, the current post is used.
431  *
432  * @package WordPress
433  * @subpackage Feed
434  * @since 2.5.0
435  *
436  * @param string $link_text Descriptive text.
437  * @param int $post_id Optional post ID.  Default to current post.
438  * @param string $feed Optional. Feed format.
439  * @return string Link to the comment feed for the current post.
440 */
441 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
442         $url = get_post_comments_feed_link($post_id, $feed);
443         if ( empty($link_text) )
444                 $link_text = __('Comments Feed');
445
446         echo apply_filters( 'post_comments_feed_link_html', "<a href='$url'>$link_text</a>", $post_id, $feed );
447 }
448
449 /**
450  * Retrieve the feed link for a given author.
451  *
452  * Returns a link to the feed for all posts by a given author. A specific feed
453  * can be requested or left blank to get the default feed.
454  *
455  * @package WordPress
456  * @subpackage Feed
457  * @since 2.5.0
458  *
459  * @param int $author_id ID of an author.
460  * @param string $feed Optional. Feed type.
461  * @return string Link to the feed for the author specified by $author_id.
462 */
463 function get_author_feed_link( $author_id, $feed = '' ) {
464         $author_id = (int) $author_id;
465         $permalink_structure = get_option('permalink_structure');
466
467         if ( empty($feed) )
468                 $feed = get_default_feed();
469
470         if ( '' == $permalink_structure ) {
471                 $link = trailingslashit(get_option('home')) . "?feed=$feed&amp;author=" . $author_id;
472         } else {
473                 $link = get_author_posts_url($author_id);
474                 if ( $feed == get_default_feed() )
475                         $feed_link = 'feed';
476                 else
477                         $feed_link = "feed/$feed";
478
479                 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
480         }
481
482         $link = apply_filters('author_feed_link', $link, $feed);
483
484         return $link;
485 }
486
487 /**
488  * Retrieve the feed link for a category.
489  *
490  * Returns a link to the feed for all post in a given category. A specific feed
491  * can be requested or left blank to get the default feed.
492  *
493  * @package WordPress
494  * @subpackage Feed
495  * @since 2.5.0
496  *
497  * @param int $cat_id ID of a category.
498  * @param string $feed Optional. Feed type.
499  * @return string Link to the feed for the category specified by $cat_id.
500 */
501 function get_category_feed_link($cat_id, $feed = '') {
502         $cat_id = (int) $cat_id;
503
504         $category = get_category($cat_id);
505
506         if ( empty($category) || is_wp_error($category) )
507                 return false;
508
509         if ( empty($feed) )
510                 $feed = get_default_feed();
511
512         $permalink_structure = get_option('permalink_structure');
513
514         if ( '' == $permalink_structure ) {
515                 $link = trailingslashit(get_option('home')) . "?feed=$feed&amp;cat=" . $cat_id;
516         } else {
517                 $link = get_category_link($cat_id);
518                 if( $feed == get_default_feed() )
519                         $feed_link = 'feed';
520                 else
521                         $feed_link = "feed/$feed";
522
523                 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
524         }
525
526         $link = apply_filters('category_feed_link', $link, $feed);
527
528         return $link;
529 }
530
531 /**
532  * Retrieve permalink for feed of tag.
533  *
534  * @since 2.3.0
535  *
536  * @param int $tag_id Tag ID.
537  * @param string $feed Optional. Feed type.
538  * @return string
539  */
540 function get_tag_feed_link($tag_id, $feed = '') {
541         $tag_id = (int) $tag_id;
542
543         $tag = get_tag($tag_id);
544
545         if ( empty($tag) || is_wp_error($tag) )
546                 return false;
547
548         $permalink_structure = get_option('permalink_structure');
549
550         if ( empty($feed) )
551                 $feed = get_default_feed();
552
553         if ( '' == $permalink_structure ) {
554                 $link = trailingslashit(get_option('home')) . "?feed=$feed&amp;tag=" . $tag->slug;
555         } else {
556                 $link = get_tag_link($tag->term_id);
557                 if ( $feed == get_default_feed() )
558                         $feed_link = 'feed';
559                 else
560                         $feed_link = "feed/$feed";
561                 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
562         }
563
564         $link = apply_filters('tag_feed_link', $link, $feed);
565
566         return $link;
567 }
568
569 /**
570  * Retrieve edit tag link.
571  *
572  * @since 2.7.0
573  *
574  * @param int $tag_id Tag ID
575  * @return string
576  */
577 function get_edit_tag_link( $tag_id = 0, $taxonomy = 'post_tag' ) {
578         $tag = get_term($tag_id, $taxonomy);
579
580         if ( !current_user_can('manage_categories') )
581                 return;
582
583         $location = admin_url('edit-tags.php?action=edit&amp;taxonomy=' . $taxonomy . '&amp;tag_ID=' . $tag->term_id);
584         return apply_filters( 'get_edit_tag_link', $location );
585 }
586
587 /**
588  * Display or retrieve edit tag link with formatting.
589  *
590  * @since 2.7.0
591  *
592  * @param string $link Optional. Anchor text.
593  * @param string $before Optional. Display before edit link.
594  * @param string $after Optional. Display after edit link.
595  * @param int|object $tag Tag object or ID
596  * @return string|null HTML content, if $echo is set to false.
597  */
598 function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
599         $tag = get_term($tag, 'post_tag');
600
601         if ( !current_user_can('manage_categories') )
602                 return;
603
604         if ( empty($link) )
605                 $link = __('Edit This');
606
607         $link = '<a href="' . get_edit_tag_link( $tag->term_id ) . '" title="' . __( 'Edit tag' ) . '">' . $link . '</a>';
608         echo $before . apply_filters( 'edit_tag_link', $link, $tag->term_id ) . $after;
609 }
610
611 /**
612  * Retrieve the permalink for the feed of the search results.
613  *
614  * @since 2.5.0
615  *
616  * @param string $search_query Optional. Search query.
617  * @param string $feed Optional. Feed type.
618  * @return string
619  */
620 function get_search_feed_link($search_query = '', $feed = '') {
621         if ( empty($search_query) )
622                 $search = esc_attr(get_search_query());
623         else
624                 $search = esc_attr(stripslashes($search_query));
625
626         if ( empty($feed) )
627                 $feed = get_default_feed();
628
629         $link = trailingslashit(get_option('home')) . "?s=$search&amp;feed=$feed";
630
631         $link = apply_filters('search_feed_link', $link);
632
633         return $link;
634 }
635
636 /**
637  * Retrieve the permalink for the comments feed of the search results.
638  *
639  * @since 2.5.0
640  *
641  * @param string $search_query Optional. Search query.
642  * @param string $feed Optional. Feed type.
643  * @return string
644  */
645 function get_search_comments_feed_link($search_query = '', $feed = '') {
646         if ( empty($search_query) )
647                 $search = esc_attr(get_search_query());
648         else
649                 $search = esc_attr(stripslashes($search_query));
650
651         if ( empty($feed) )
652                 $feed = get_default_feed();
653
654         $link = trailingslashit(get_option('home')) . "?s=$search&amp;feed=comments-$feed";
655
656         $link = apply_filters('search_feed_link', $link);
657
658         return $link;
659 }
660
661 /**
662  * Retrieve edit posts link for post.
663  *
664  * Can be used within the WordPress loop or outside of it. Can be used with
665  * pages, posts, attachments, and revisions.
666  *
667  * @since 2.3.0
668  *
669  * @param int $id Optional. Post ID.
670  * @param string $context Optional, default to display. How to write the '&', defaults to '&amp;'.
671  * @return string
672  */
673 function get_edit_post_link( $id = 0, $context = 'display' ) {
674         if ( !$post = &get_post( $id ) )
675                 return;
676
677         if ( 'display' == $context )
678                 $action = 'action=edit&amp;';
679         else
680                 $action = 'action=edit&';
681
682         switch ( $post->post_type ) :
683         case 'page' :
684                 if ( !current_user_can( 'edit_page', $post->ID ) )
685                         return;
686                 $file = 'page';
687                 $var  = 'post';
688                 break;
689         case 'attachment' :
690                 if ( !current_user_can( 'edit_post', $post->ID ) )
691                         return;
692                 $file = 'media';
693                 $var  = 'attachment_id';
694                 break;
695         case 'revision' :
696                 if ( !current_user_can( 'edit_post', $post->ID ) )
697                         return;
698                 $file = 'revision';
699                 $var  = 'revision';
700                 $action = '';
701                 break;
702         default :
703                 if ( !current_user_can( 'edit_post', $post->ID ) )
704                         return;
705                 $file = 'post';
706                 $var  = 'post';
707                 break;
708         endswitch;
709
710         return apply_filters( 'get_edit_post_link', admin_url("$file.php?{$action}$var=$post->ID"), $post->ID, $context );
711 }
712
713 /**
714  * Display edit post link for post.
715  *
716  * @since 1.0.0
717  *
718  * @param string $link Optional. Anchor text.
719  * @param string $before Optional. Display before edit link.
720  * @param string $after Optional. Display after edit link.
721  * @param int $id Optional. Post ID.
722  */
723 function edit_post_link( $link = 'Edit This', $before = '', $after = '', $id = 0 ) {
724         if ( !$post = &get_post( $id ) )
725                 return;
726
727         if ( !$url = get_edit_post_link( $post->ID ) )
728                 return;
729
730         $link = '<a class="post-edit-link" href="' . $url . '" title="' . esc_attr( __( 'Edit post' ) ) . '">' . $link . '</a>';
731         echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after;
732 }
733
734 /**
735  * Retrieve edit comment link.
736  *
737  * @since 2.3.0
738  *
739  * @param int $comment_id Optional. Comment ID.
740  * @return string
741  */
742 function get_edit_comment_link( $comment_id = 0 ) {
743         $comment = &get_comment( $comment_id );
744         $post = &get_post( $comment->comment_post_ID );
745
746         if ( $post->post_type == 'page' ) {
747                 if ( !current_user_can( 'edit_page', $post->ID ) )
748                         return;
749         } else {
750                 if ( !current_user_can( 'edit_post', $post->ID ) )
751                         return;
752         }
753
754         $location = admin_url('comment.php?action=editcomment&amp;c=') . $comment->comment_ID;
755         return apply_filters( 'get_edit_comment_link', $location );
756 }
757
758 /**
759  * Display or retrieve edit comment link with formatting.
760  *
761  * @since 1.0.0
762  *
763  * @param string $link Optional. Anchor text.
764  * @param string $before Optional. Display before edit link.
765  * @param string $after Optional. Display after edit link.
766  * @return string|null HTML content, if $echo is set to false.
767  */
768 function edit_comment_link( $link = 'Edit This', $before = '', $after = '' ) {
769         global $comment, $post;
770
771         if ( $post->post_type == 'attachment' ) {
772         } elseif ( $post->post_type == 'page' ) {
773                 if ( !current_user_can( 'edit_page', $post->ID ) )
774                         return;
775         } else {
776                 if ( !current_user_can( 'edit_post', $post->ID ) )
777                         return;
778         }
779
780         $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . __( 'Edit comment' ) . '">' . $link . '</a>';
781         echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
782 }
783
784 /**
785  * Display edit bookmark (literally a URL external to blog) link.
786  *
787  * @since 2.7.0
788  *
789  * @param int $link Optional. Bookmark ID.
790  * @return string
791  */
792 function get_edit_bookmark_link( $link = 0 ) {
793         $link = get_bookmark( $link );
794
795         if ( !current_user_can('manage_links') )
796                 return;
797
798         $location = admin_url('link.php?action=edit&amp;link_id=') . $link->link_id;
799         return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
800 }
801
802 /**
803  * Display edit bookmark (literally a URL external to blog) link anchor content.
804  *
805  * @since 2.7.0
806  *
807  * @param string $link Optional. Anchor text.
808  * @param string $before Optional. Display before edit link.
809  * @param string $after Optional. Display after edit link.
810  * @param int $bookmark Optional. Bookmark ID.
811  */
812 function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
813         $bookmark = get_bookmark($bookmark);
814
815         if ( !current_user_can('manage_links') )
816                 return;
817
818         if ( empty($link) )
819                 $link = __('Edit This');
820
821         $link = '<a href="' . get_edit_bookmark_link( $link ) . '" title="' . __( 'Edit link' ) . '">' . $link . '</a>';
822         echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
823 }
824
825 // Navigation links
826
827 /**
828  * Retrieve previous post link that is adjacent to current post.
829  *
830  * @since 1.5.0
831  *
832  * @param bool $in_same_cat Optional. Whether link should be in same category.
833  * @param string $excluded_categories Optional. Excluded categories IDs.
834  * @return string
835  */
836 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
837         return get_adjacent_post($in_same_cat, $excluded_categories);
838 }
839
840 /**
841  * Retrieve next post link that is adjacent to current post.
842  *
843  * @since 1.5.0
844  *
845  * @param bool $in_same_cat Optional. Whether link should be in same category.
846  * @param string $excluded_categories Optional. Excluded categories IDs.
847  * @return string
848  */
849 function get_next_post($in_same_cat = false, $excluded_categories = '') {
850         return get_adjacent_post($in_same_cat, $excluded_categories, false);
851 }
852
853 /**
854  * Retrieve adjacent post link.
855  *
856  * Can either be next or previous post link.
857  *
858  * @since 2.5.0
859  *
860  * @param bool $in_same_cat Optional. Whether link should be in same category.
861  * @param string $excluded_categories Optional. Excluded categories IDs.
862  * @param bool $previous Optional. Whether to retrieve previous post.
863  * @return string
864  */
865 function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) {
866         global $post, $wpdb;
867
868         if ( empty($post) || !is_single() || is_attachment() )
869                 return null;
870
871         $current_post_date = $post->post_date;
872
873         $join = '';
874         $posts_in_ex_cats_sql = '';
875         if ( $in_same_cat || !empty($excluded_categories) ) {
876                 $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";
877
878                 if ( $in_same_cat ) {
879                         $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids');
880                         $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
881                 }
882
883                 $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
884                 if ( !empty($excluded_categories) ) {
885                         $excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
886                         if ( !empty($cat_array) ) {
887                                 $excluded_categories = array_diff($excluded_categories, $cat_array);
888                                 $posts_in_ex_cats_sql = '';
889                         }
890
891                         if ( !empty($excluded_categories) ) {
892                                 $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
893                         }
894                 }
895         }
896
897         $adjacent = $previous ? 'previous' : 'next';
898         $op = $previous ? '<' : '>';
899         $order = $previous ? 'DESC' : 'ASC';
900
901         $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
902         $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 );
903         $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
904
905         $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";
906         $query_key = 'adjacent_post_' . md5($query);
907         $result = wp_cache_get($query_key, 'counts');
908         if ( false !== $result )
909                 return $result;
910
911         $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
912         if ( null === $result )
913                 $result = '';
914
915         wp_cache_set($query_key, $result, 'counts');
916         return $result;
917 }
918
919 /**
920  * Get adjacent post relational link.
921  *
922  * Can either be next or previous post relational link.
923  *
924  * @since 2.8.0
925  *
926  * @param string $title Optional. Link title format.
927  * @param bool $in_same_cat Optional. Whether link should be in same category.
928  * @param string $excluded_categories Optional. Excluded categories IDs.
929  * @param bool $previous Optional, default is true. Whether display link to previous post.
930  * @return string
931  */
932 function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {
933         if ( $previous && is_attachment() )
934                 $post = & get_post($GLOBALS['post']->post_parent);
935         else
936                 $post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);
937
938         if ( empty($post) )
939                 return;
940
941         if ( empty($post->post_title) )
942                 $post->post_title = $previous ? __('Previous Post') : __('Next Post');
943
944         $date = mysql2date(get_option('date_format'), $post->post_date);
945
946         $title = str_replace('%title', $post->post_title, $title);
947         $title = str_replace('%date', $date, $title);
948         $title = apply_filters('the_title', $title, $post);
949
950         $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
951         $link .= esc_attr( $title );
952         $link .= "' href='" . get_permalink($post) . "' />\n";
953
954         $adjacent = $previous ? 'previous' : 'next';
955         return apply_filters( "{$adjacent}_post_rel_link", $link );
956 }
957
958 /**
959  * Display relational links for the posts adjacent to the current post.
960  *
961  * @since 2.8.0
962  *
963  * @param string $title Optional. Link title format.
964  * @param bool $in_same_cat Optional. Whether link should be in same category.
965  * @param string $excluded_categories Optional. Excluded categories IDs.
966  */
967 function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
968         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
969         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
970 }
971
972 /**
973  * Display relational link for the next post adjacent to the current post.
974  *
975  * @since 2.8.0
976  *
977  * @param string $title Optional. Link title format.
978  * @param bool $in_same_cat Optional. Whether link should be in same category.
979  * @param string $excluded_categories Optional. Excluded categories IDs.
980  */
981 function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
982         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
983 }
984
985 /**
986  * Display relational link for the previous post adjacent to the current post.
987  *
988  * @since 2.8.0
989  *
990  * @param string $title Optional. Link title format.
991  * @param bool $in_same_cat Optional. Whether link should be in same category.
992  * @param string $excluded_categories Optional. Excluded categories IDs.
993  */
994 function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
995         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
996 }
997
998 /**
999  * Retrieve boundary post.
1000  *
1001  * Boundary being either the first or last post by publish date within the contraitns specified
1002  * by in same category or excluded categories.
1003  *
1004  * @since 2.8.0
1005  *
1006  * @param bool $in_same_cat Optional. Whether returned post should be in same category.
1007  * @param string $excluded_categories Optional. Excluded categories IDs.
1008  * @param bool $previous Optional. Whether to retrieve first post.
1009  * @return object
1010  */
1011 function get_boundary_post($in_same_cat = false, $excluded_categories = '', $start = true) {
1012         global $post, $wpdb;
1013
1014         if ( empty($post) || !is_single() || is_attachment() )
1015                 return null;
1016
1017         $cat_array = array();
1018         $excluded_categories = array();
1019         if ( !empty($in_same_cat) || !empty($excluded_categories) ) {
1020                 if ( !empty($in_same_cat) ) {
1021                         $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids');
1022                 }
1023
1024                 if ( !empty($excluded_categories) ) {
1025                         $excluded_categories = array_map('intval', explode(',', $excluded_categories));
1026
1027                         if ( !empty($cat_array) )
1028                                 $excluded_categories = array_diff($excluded_categories, $cat_array);
1029
1030                         $inverse_cats = array();
1031                         foreach ( $excluded_categories as $excluded_category)
1032                                 $inverse_cats[] = $excluded_category * -1;
1033                         $excluded_categories = $inverse_cats;
1034                 }
1035         }
1036
1037         $categories = implode(',', array_merge($cat_array, $excluded_categories) );
1038
1039         $order = $start ? 'ASC' : 'DESC';
1040
1041         return get_posts( array('numberposts' => 1, 'order' => $order, 'orderby' => 'ID', 'category' => $categories) );
1042 }
1043
1044 /**
1045  * Get boundary post relational link.
1046  *
1047  * Can either be start or end post relational link.
1048  *
1049  * @since 2.8.0
1050  *
1051  * @param string $title Optional. Link title format.
1052  * @param bool $in_same_cat Optional. Whether link should be in same category.
1053  * @param string $excluded_categories Optional. Excluded categories IDs.
1054  * @param bool $start Optional, default is true. Whether display link to first post.
1055  * @return string
1056  */
1057 function get_boundary_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $start = true) {
1058         $posts = get_boundary_post($in_same_cat,$excluded_categories,$start);
1059         // Even though we limited get_posts to return only 1 item it still returns an array of objects.
1060         $post = $posts[0];
1061
1062         if ( empty($post) )
1063                          return;
1064
1065                 if ( empty($post->post_title) )
1066                                 $post->post_title = $start ? __('First Post') : __('Last Post');
1067
1068         $date = mysql2date(get_option('date_format'), $post->post_date);
1069
1070         $title = str_replace('%title', $post->post_title, $title);
1071         $title = str_replace('%date', $date, $title);
1072         $title = apply_filters('the_title', $title, $post);
1073
1074         $link = $start ? "<link rel='start' title='" : "<link rel='end' title='";
1075         $link .= esc_attr($title);
1076         $link .= "' href='" . get_permalink($post) . "' />\n";
1077
1078         $boundary = $start ? 'start' : 'end';
1079         return apply_filters( "{$boundary}_post_rel_link", $link );
1080 }
1081
1082 /**
1083  * Display relational link for the first post.
1084  *
1085  * @since 2.8.0
1086  *
1087  * @param string $title Optional. Link title format.
1088  * @param bool $in_same_cat Optional. Whether link should be in same category.
1089  * @param string $excluded_categories Optional. Excluded categories IDs.
1090  */
1091 function start_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
1092         echo get_boundary_post_rel_link($title, $in_same_cat, $excluded_categories, true);
1093 }
1094
1095 /**
1096  * Get site index relational link.
1097  *
1098  * @since 2.8.0
1099  *
1100  * @return string
1101  */
1102 function get_index_rel_link() {
1103         $link = "<link rel='index' title='" . esc_attr(get_bloginfo('name')) . "' href='" . get_bloginfo('siteurl') . "' />\n";
1104         return apply_filters( "index_rel_link", $link );
1105 }
1106
1107 /**
1108  * Display relational link for the site index.
1109  *
1110  * @since 2.8.0
1111  */
1112 function index_rel_link() {
1113         echo get_index_rel_link();
1114 }
1115
1116 /**
1117  * Get parent post relational link.
1118  *
1119  * @since 2.8.0
1120  *
1121  * @param string $title Optional. Link title format.
1122  * @return string
1123  */
1124 function get_parent_post_rel_link($title = '%title') {
1125         if ( ! empty( $GLOBALS['post'] ) && ! empty( $GLOBALS['post']->post_parent ) )
1126                 $post = & get_post($GLOBALS['post']->post_parent);
1127
1128         if ( empty($post) )
1129                 return;
1130
1131         $date = mysql2date(get_option('date_format'), $post->post_date);
1132
1133         $title = str_replace('%title', $post->post_title, $title);
1134         $title = str_replace('%date', $date, $title);
1135         $title = apply_filters('the_title', $title, $post);
1136
1137         $link = "<link rel='up' title='";
1138         $link .= esc_attr( $title );
1139         $link .= "' href='" . get_permalink($post) . "' />\n";
1140
1141         return apply_filters( "parent_post_rel_link", $link );
1142 }
1143
1144 /**
1145  * Display relational link for parent item
1146  *
1147  * @since 2.8.0
1148  */
1149 function parent_post_rel_link($title = '%title') {
1150         echo get_parent_post_rel_link($title);
1151 }
1152
1153 /**
1154  * Display previous post link that is adjacent to the current post.
1155  *
1156  * @since 1.5.0
1157  *
1158  * @param string $format Optional. Link anchor format.
1159  * @param string $link Optional. Link permalink format.
1160  * @param bool $in_same_cat Optional. Whether link should be in same category.
1161  * @param string $excluded_categories Optional. Excluded categories IDs.
1162  */
1163 function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
1164         adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
1165 }
1166
1167 /**
1168  * Display next post link that is adjacent to the current post.
1169  *
1170  * @since 1.5.0
1171  *
1172  * @param string $format Optional. Link anchor format.
1173  * @param string $link Optional. Link permalink format.
1174  * @param bool $in_same_cat Optional. Whether link should be in same category.
1175  * @param string $excluded_categories Optional. Excluded categories IDs.
1176  */
1177 function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
1178         adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
1179 }
1180
1181 /**
1182  * Display adjacent post link.
1183  *
1184  * Can be either next post link or previous.
1185  *
1186  * @since 2.5.0
1187  *
1188  * @param string $format Link anchor format.
1189  * @param string $link Link permalink format.
1190  * @param bool $in_same_cat Optional. Whether link should be in same category.
1191  * @param string $excluded_categories Optional. Excluded categories IDs.
1192  * @param bool $previous Optional, default is true. Whether display link to previous post.
1193  */
1194 function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {
1195         if ( $previous && is_attachment() )
1196                 $post = & get_post($GLOBALS['post']->post_parent);
1197         else
1198                 $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
1199
1200         if ( !$post )
1201                 return;
1202
1203         $title = $post->post_title;
1204
1205         if ( empty($post->post_title) )
1206                 $title = $previous ? __('Previous Post') : __('Next Post');
1207
1208         $title = apply_filters('the_title', $title, $post);
1209         $date = mysql2date(get_option('date_format'), $post->post_date);
1210
1211         $string = '<a href="'.get_permalink($post).'">';
1212         $link = str_replace('%title', $title, $link);
1213         $link = str_replace('%date', $date, $link);
1214         $link = $string . $link . '</a>';
1215
1216         $format = str_replace('%link', $link, $format);
1217
1218         $adjacent = $previous ? 'previous' : 'next';
1219         echo apply_filters( "{$adjacent}_post_link", $format, $link );
1220 }
1221
1222 /**
1223  * Retrieve get links for page numbers.
1224  *
1225  * @since 1.5.0
1226  *
1227  * @param int $pagenum Optional. Page ID.
1228  * @return string
1229  */
1230 function get_pagenum_link($pagenum = 1) {
1231         global $wp_rewrite;
1232
1233         $pagenum = (int) $pagenum;
1234
1235         $request = remove_query_arg( 'paged' );
1236
1237         $home_root = parse_url(get_option('home'));
1238         $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
1239         $home_root = preg_quote( trailingslashit( $home_root ), '|' );
1240
1241         $request = preg_replace('|^'. $home_root . '|', '', $request);
1242         $request = preg_replace('|^/+|', '', $request);
1243
1244         if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
1245                 $base = trailingslashit( get_bloginfo( 'home' ) );
1246
1247                 if ( $pagenum > 1 ) {
1248                         $result = add_query_arg( 'paged', $pagenum, $base . $request );
1249                 } else {
1250                         $result = $base . $request;
1251                 }
1252         } else {
1253                 $qs_regex = '|\?.*?$|';
1254                 preg_match( $qs_regex, $request, $qs_match );
1255
1256                 if ( !empty( $qs_match[0] ) ) {
1257                         $query_string = $qs_match[0];
1258                         $request = preg_replace( $qs_regex, '', $request );
1259                 } else {
1260                         $query_string = '';
1261                 }
1262
1263                 $request = preg_replace( '|page/\d+/?$|', '', $request);
1264                 $request = preg_replace( '|^index\.php|', '', $request);
1265                 $request = ltrim($request, '/');
1266
1267                 $base = trailingslashit( get_bloginfo( 'url' ) );
1268
1269                 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
1270                         $base .= 'index.php/';
1271
1272                 if ( $pagenum > 1 ) {
1273                         $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( 'page/' . $pagenum, 'paged' );
1274                 }
1275
1276                 $result = $base . $request . $query_string;
1277         }
1278
1279         $result = apply_filters('get_pagenum_link', $result);
1280
1281         return $result;
1282 }
1283
1284 /**
1285  * Retrieve next posts pages link.
1286  *
1287  * Backported from 2.1.3 to 2.0.10.
1288  *
1289  * @since 2.0.10
1290  *
1291  * @param int $max_page Optional. Max pages.
1292  * @return string
1293  */
1294 function get_next_posts_page_link($max_page = 0) {
1295         global $paged;
1296
1297         if ( !is_single() ) {
1298                 if ( !$paged )
1299                         $paged = 1;
1300                 $nextpage = intval($paged) + 1;
1301                 if ( !$max_page || $max_page >= $nextpage )
1302                         return get_pagenum_link($nextpage);
1303         }
1304 }
1305
1306 /**
1307  * Display or return the next posts pages link.
1308  *
1309  * @since 0.71
1310  *
1311  * @param int $max_page Optional. Max pages.
1312  * @param boolean $echo Optional. Echo or return;
1313  */
1314 function next_posts( $max_page = 0, $echo = true ) {
1315         $output = esc_url( get_next_posts_page_link( $max_page ) );
1316
1317         if ( $echo )
1318                 echo $output;
1319         else
1320                 return $output;
1321 }
1322
1323 /**
1324  * Return the next posts pages link.
1325  *
1326  * @since 2.7.0
1327  *
1328  * @param string $label Content for link text.
1329  * @param int $max_page Optional. Max pages.
1330  * @return string|null
1331  */
1332 function get_next_posts_link( $label = 'Next Page &raquo;', $max_page = 0 ) {
1333         global $paged, $wp_query;
1334
1335         if ( !$max_page ) {
1336                 $max_page = $wp_query->max_num_pages;
1337         }
1338
1339         if ( !$paged )
1340                 $paged = 1;
1341
1342         $nextpage = intval($paged) + 1;
1343
1344         if ( !is_single() && ( empty($paged) || $nextpage <= $max_page) ) {
1345                 $attr = apply_filters( 'next_posts_link_attributes', '' );
1346                 return '<a href="' . next_posts( $max_page, false ) . "\" $attr>". preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
1347         }
1348 }
1349
1350 /**
1351  * Display the next posts pages link.
1352  *
1353  * @since 0.71
1354  * @uses get_next_posts_link()
1355  *
1356  * @param string $label Content for link text.
1357  * @param int $max_page Optional. Max pages.
1358  */
1359 function next_posts_link( $label = 'Next Page &raquo;', $max_page = 0 ) {
1360         echo get_next_posts_link( $label, $max_page );
1361 }
1362
1363 /**
1364  * Retrieve previous post pages link.
1365  *
1366  * Will only return string, if not on a single page or post.
1367  *
1368  * Backported to 2.0.10 from 2.1.3.
1369  *
1370  * @since 2.0.10
1371  *
1372  * @return string|null
1373  */
1374 function get_previous_posts_page_link() {
1375         global $paged;
1376
1377         if ( !is_single() ) {
1378                 $nextpage = intval($paged) - 1;
1379                 if ( $nextpage < 1 )
1380                         $nextpage = 1;
1381                 return get_pagenum_link($nextpage);
1382         }
1383 }
1384
1385 /**
1386  * Display or return the previous posts pages link.
1387  *
1388  * @since 0.71
1389  *
1390  * @param boolean $echo Optional. Echo or return;
1391  */
1392 function previous_posts( $echo = true ) {
1393         $output = esc_url( get_previous_posts_page_link() );
1394
1395         if ( $echo )
1396                 echo $output;
1397         else
1398                 return $output;
1399 }
1400
1401 /**
1402  * Return the previous posts pages link.
1403  *
1404  * @since 2.7.0
1405  *
1406  * @param string $label Optional. Previous page link text.
1407  * @return string|null
1408  */
1409 function get_previous_posts_link( $label = '&laquo; Previous Page' ) {
1410         global $paged;
1411
1412         if ( !is_single() && $paged > 1 ) {
1413                 $attr = apply_filters( 'previous_posts_link_attributes', '' );
1414                 return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label ) .'</a>';
1415         }
1416 }
1417
1418 /**
1419  * Display the previous posts page link.
1420  *
1421  * @since 0.71
1422  * @uses get_previous_posts_link()
1423  *
1424  * @param string $label Optional. Previous page link text.
1425  */
1426 function previous_posts_link( $label = '&laquo; Previous Page' ) {
1427         echo get_previous_posts_link( $label );
1428 }
1429
1430 /**
1431  * Return post pages link navigation for previous and next pages.
1432  *
1433  * @since 2.8
1434  *
1435  * @param string|array $args Optional args.
1436  * @return string The posts link navigation.
1437  */
1438 function get_posts_nav_link( $args = array() ) {
1439         global $wp_query;
1440
1441         $return = '';
1442
1443         if ( !is_singular() ) {
1444                 $defaults = array(
1445                         'sep' => ' &#8212; ',
1446                         'prelabel' => __('&laquo; Previous Page'),
1447                         'nxtlabel' => __('Next Page &raquo;'),
1448                 );
1449                 $args = wp_parse_args( $args, $defaults );
1450
1451                 $max_num_pages = $wp_query->max_num_pages;
1452                 $paged = get_query_var('paged');
1453
1454                 //only have sep if there's both prev and next results
1455                 if ($paged < 2 || $paged >= $max_num_pages) {
1456                         $args['sep'] = '';
1457                 }
1458
1459                 if ( $max_num_pages > 1 ) {
1460                         $return = get_previous_posts_link($args['prelabel']);
1461                         $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $args['sep']);
1462                         $return .= get_next_posts_link($args['nxtlabel']);
1463                 }
1464         }
1465         return $return;
1466
1467 }
1468
1469 /**
1470  * Display post pages link navigation for previous and next pages.
1471  *
1472  * @since 0.71
1473  *
1474  * @param string $sep Optional. Separator for posts navigation links.
1475  * @param string $prelabel Optional. Label for previous pages.
1476  * @param string $nxtlabel Optional Label for next pages.
1477  */
1478 function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
1479         $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
1480         echo get_posts_nav_link($args);
1481 }
1482
1483 /**
1484  * Retrieve page numbers links.
1485  *
1486  * @since 2.7.0
1487  *
1488  * @param int $pagenum Optional. Page number.
1489  * @return string
1490  */
1491 function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
1492         global $post, $wp_rewrite;
1493
1494         $pagenum = (int) $pagenum;
1495
1496         $result = get_permalink( $post->ID );
1497
1498         if ( 'newest' == get_option('default_comments_page') ) {
1499                 if ( $pagenum != $max_page ) {
1500                         if ( $wp_rewrite->using_permalinks() )
1501                                 $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
1502                         else
1503                                 $result = add_query_arg( 'cpage', $pagenum, $result );
1504                 }
1505         } elseif ( $pagenum > 1 ) {
1506                 if ( $wp_rewrite->using_permalinks() )
1507                         $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
1508                 else
1509                         $result = add_query_arg( 'cpage', $pagenum, $result );
1510         }
1511
1512         $result .= '#comments';
1513
1514         $result = apply_filters('get_comments_pagenum_link', $result);
1515
1516         return $result;
1517 }
1518
1519 /**
1520  * Return the link to next comments pages.
1521  *
1522  * @since 2.7.1
1523  *
1524  * @param string $label Optional. Label for link text.
1525  * @param int $max_page Optional. Max page.
1526  * @return string|null
1527  */
1528 function get_next_comments_link( $label = '', $max_page = 0 ) {
1529         global $wp_query;
1530
1531         if ( !is_singular() || !get_option('page_comments') )
1532                 return;
1533
1534         $page = get_query_var('cpage');
1535
1536         $nextpage = intval($page) + 1;
1537
1538         if ( empty($max_page) )
1539                 $max_page = $wp_query->max_num_comment_pages;
1540
1541         if ( empty($max_page) )
1542                 $max_page = get_comment_pages_count();
1543
1544         if ( $nextpage > $max_page )
1545                 return;
1546
1547         if ( empty($label) )
1548                 $label = __('Newer Comments &raquo;');
1549
1550         return '<a href="' . esc_url( get_comments_pagenum_link( $nextpage, $max_page ) ) . '" ' . apply_filters( 'next_comments_link_attributes', '' ) . '>'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
1551 }
1552
1553 /**
1554  * Display the link to next comments pages.
1555  *
1556  * @since 2.7.0
1557  *
1558  * @param string $label Optional. Label for link text.
1559  * @param int $max_page Optional. Max page.
1560  */
1561 function next_comments_link( $label = '', $max_page = 0 ) {
1562         echo get_next_comments_link( $label, $max_page );
1563 }
1564
1565 /**
1566  * Return the previous comments page link.
1567  *
1568  * @since 2.7.1
1569  *
1570  * @param string $label Optional. Label for comments link text.
1571  * @return string|null
1572  */
1573 function get_previous_comments_link( $label = '' ) {
1574         if ( !is_singular() || !get_option('page_comments') )
1575                 return;
1576
1577         $page = get_query_var('cpage');
1578
1579         if ( intval($page) <= 1 )
1580                 return;
1581
1582         $prevpage = intval($page) - 1;
1583
1584         if ( empty($label) )
1585                 $label = __('&laquo; Older Comments');
1586
1587         return '<a href="' . esc_url( get_comments_pagenum_link( $prevpage ) ) . '" ' . apply_filters( 'previous_comments_link_attributes', '' ) . '>' . preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
1588 }
1589
1590 /**
1591  * Display the previous comments page link.
1592  *
1593  * @since 2.7.0
1594  *
1595  * @param string $label Optional. Label for comments link text.
1596  */
1597 function previous_comments_link( $label = '' ) {
1598         echo get_previous_comments_link( $label );
1599 }
1600
1601 /**
1602  * Create pagination links for the comments on the current post.
1603  *
1604  * @see paginate_links()
1605  * @since 2.7.0
1606  *
1607  * @param string|array $args Optional args. See paginate_links.
1608  * @return string Markup for pagination links.
1609 */
1610 function paginate_comments_links($args = array()) {
1611         global $wp_query, $wp_rewrite;
1612
1613         if ( !is_singular() || !get_option('page_comments') )
1614                 return;
1615
1616         $page = get_query_var('cpage');
1617         if ( !$page )
1618                 $page = 1;
1619         $max_page = get_comment_pages_count();
1620         $defaults = array(
1621                 'base' => add_query_arg( 'cpage', '%#%' ),
1622                 'format' => '',
1623                 'total' => $max_page,
1624                 'current' => $page,
1625                 'echo' => true,
1626                 'add_fragment' => '#comments'
1627         );
1628         if ( $wp_rewrite->using_permalinks() )
1629                 $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . 'comment-page-%#%', 'commentpaged');
1630
1631         $args = wp_parse_args( $args, $defaults );
1632         $page_links = paginate_links( $args );
1633
1634         if ( $args['echo'] )
1635                 echo $page_links;
1636         else
1637                 return $page_links;
1638 }
1639
1640 /**
1641  * Retrieve shortcut link.
1642  *
1643  * Use this in 'a' element 'href' attribute.
1644  *
1645  * @since 2.6.0
1646  *
1647  * @return string
1648  */
1649 function get_shortcut_link() {
1650         $link = "javascript:
1651                         var d=document,
1652                         w=window,
1653                         e=w.getSelection,
1654                         k=d.getSelection,
1655                         x=d.selection,
1656                         s=(e?e():(k)?k():(x?x.createRange().text:0)),
1657                         f='" . admin_url('press-this.php') . "',
1658                         l=d.location,
1659                         e=encodeURIComponent,
1660                         g=f+'?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&v=2';
1661                         function a(){
1662                                 if(!w.open(g,'t','toolbar=0,resizable=0,scrollbars=1,status=1,width=720,height=570')){
1663                                         l.href=g;
1664                                 }
1665                         }";
1666                         if (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false)
1667                                 $link .= 'setTimeout(a,0);';
1668                         else
1669                                 $link .= 'a();';
1670
1671                         $link .= "void(0);";
1672
1673         $link = str_replace(array("\r", "\n", "\t"),  '', $link);
1674
1675         return apply_filters('shortcut_link', $link);
1676 }
1677
1678 /**
1679  * Retrieve the site url.
1680  *
1681  * Returns the 'site_url' option with the appropriate protocol,  'https' if
1682  * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
1683  * overridden.
1684  *
1685  * @package WordPress
1686  * @since 2.6.0
1687  *
1688  * @param string $path Optional. Path relative to the site url.
1689  * @param string $scheme Optional. Scheme to give the site url context. Currently 'http','https', 'login', 'login_post', or 'admin'.
1690  * @return string Site url link with optional path appended.
1691 */
1692 function site_url($path = '', $scheme = null) {
1693         // should the list of allowed schemes be maintained elsewhere?
1694         $orig_scheme = $scheme;
1695         if ( !in_array($scheme, array('http', 'https')) ) {
1696                 if ( ('login_post' == $scheme) && ( force_ssl_login() || force_ssl_admin() ) )
1697                         $scheme = 'https';
1698                 elseif ( ('login' == $scheme) && ( force_ssl_admin() ) )
1699                         $scheme = 'https';
1700                 elseif ( ('admin' == $scheme) && force_ssl_admin() )
1701                         $scheme = 'https';
1702                 else
1703                         $scheme = ( is_ssl() ? 'https' : 'http' );
1704         }
1705
1706         $url = str_replace( 'http://', "{$scheme}://", get_option('siteurl') );
1707
1708         if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
1709                 $url .= '/' . ltrim($path, '/');
1710
1711         return apply_filters('site_url', $url, $path, $orig_scheme);
1712 }
1713
1714 /**
1715  * Retrieve the url to the admin area.
1716  *
1717  * @package WordPress
1718  * @since 2.6.0
1719  *
1720  * @param string $path Optional path relative to the admin url
1721  * @return string Admin url link with optional path appended
1722 */
1723 function admin_url($path = '') {
1724         $url = site_url('wp-admin/', 'admin');
1725
1726         if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
1727                 $url .= ltrim($path, '/');
1728
1729         return apply_filters('admin_url', $url, $path);
1730 }
1731
1732 /**
1733  * Retrieve the url to the includes directory.
1734  *
1735  * @package WordPress
1736  * @since 2.6.0
1737  *
1738  * @param string $path Optional. Path relative to the includes url.
1739  * @return string Includes url link with optional path appended.
1740 */
1741 function includes_url($path = '') {
1742         $url = site_url() . '/' . WPINC . '/';
1743
1744         if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
1745                 $url .= ltrim($path, '/');
1746
1747         return apply_filters('includes_url', $url, $path);
1748 }
1749
1750 /**
1751  * Retrieve the url to the content directory.
1752  *
1753  * @package WordPress
1754  * @since 2.6.0
1755  *
1756  * @param string $path Optional. Path relative to the content url.
1757  * @return string Content url link with optional path appended.
1758 */
1759 function content_url($path = '') {
1760         $scheme = ( is_ssl() ? 'https' : 'http' );
1761         $url = WP_CONTENT_URL;
1762         if ( 0 === strpos($url, 'http') ) {
1763                 if ( is_ssl() )
1764                         $url = str_replace( 'http://', "{$scheme}://", $url );
1765         }
1766
1767         if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
1768                 $url .= '/' . ltrim($path, '/');
1769
1770         return apply_filters('content_url', $url, $path);
1771 }
1772
1773 /**
1774  * Retrieve the url to the plugins directory or to a specific file within that directory.
1775  * You can hardcode the plugin slug in $path or pass __FILE__ as a second argument to get the correct folder name.
1776  *
1777  * @package WordPress
1778  * @since 2.6.0
1779  *
1780  * @param string $path Optional. Path relative to the plugins url.
1781  * @param string $plugin Optional. The plugin file that you want to be relative to - i.e. pass in __FILE__
1782  * @return string Plugins url link with optional path appended.
1783 */
1784 function plugins_url($path = '', $plugin = '') {
1785         $scheme = ( is_ssl() ? 'https' : 'http' );
1786
1787         if ( $plugin !== '' && preg_match('#^' . preg_quote(WPMU_PLUGIN_DIR . DIRECTORY_SEPARATOR, '#') . '#', $plugin) ) {
1788                 $url = WPMU_PLUGIN_URL;
1789         } else {
1790                 $url = WP_PLUGIN_URL;
1791         }
1792
1793         if ( 0 === strpos($url, 'http') ) {
1794                 if ( is_ssl() )
1795                         $url = str_replace( 'http://', "{$scheme}://", $url );
1796         }
1797
1798         if ( !empty($plugin) && is_string($plugin) ) {
1799                 $folder = dirname(plugin_basename($plugin));
1800                 if ('.' != $folder)
1801                         $url .= '/' . ltrim($folder, '/');
1802         }
1803
1804         if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
1805                 $url .= '/' . ltrim($path, '/');
1806
1807         return apply_filters('plugins_url', $url, $path, $plugin);
1808 }
1809
1810 ?>