Make WordPress Core

Ticket #9510: link-template.php

File link-template.php, 52.2 KB (added by peaceablewhale, 18 years ago)

Initial attempt to add new functions to link-template.php

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