]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/link-template.php
WordPress 4.3-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  */
14 function the_permalink() {
15         /**
16          * Filter the display of the permalink for the current post.
17          *
18          * @since 1.5.0
19          *
20          * @param string $permalink The permalink for the current post.
21          */
22         echo esc_url( apply_filters( 'the_permalink', get_permalink() ) );
23 }
24
25 /**
26  * Retrieve trailing slash string, if blog set for adding trailing slashes.
27  *
28  * Conditionally adds a trailing slash if the permalink structure has a trailing
29  * slash, strips the trailing slash if not. The string is passed through the
30  * 'user_trailingslashit' filter. Will remove trailing slash from string, if
31  * blog is not set to have them.
32  *
33  * @since 2.2.0
34  * @global WP_Rewrite $wp_rewrite
35  *
36  * @param string $string URL with or without a trailing slash.
37  * @param string $type_of_url The type of URL being considered (e.g. single, category, etc) for use in the filter.
38  * @return string The URL with the trailing slash appended or stripped.
39  */
40 function user_trailingslashit($string, $type_of_url = '') {
41         global $wp_rewrite;
42         if ( $wp_rewrite->use_trailing_slashes )
43                 $string = trailingslashit($string);
44         else
45                 $string = untrailingslashit($string);
46
47         /**
48          * Filter the trailing slashed string, depending on whether the site is set
49          * to use training slashes.
50          *
51          * @since 2.2.0
52          *
53          * @param string $string      URL with or without a trailing slash.
54          * @param string $type_of_url The type of URL being considered. Accepts 'single', 'single_trackback',
55          *                            'single_feed', 'single_paged', 'feed', 'category', 'page', 'year',
56          *                            'month', 'day', 'paged', 'post_type_archive'.
57          */
58         return apply_filters( 'user_trailingslashit', $string, $type_of_url );
59 }
60
61 /**
62  * Display permalink anchor for current post.
63  *
64  * The permalink mode title will use the post title for the 'a' element 'id'
65  * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute.
66  *
67  * @since 0.71
68  *
69  * @param string $mode Permalink mode can be either 'title', 'id', or default, which is 'id'.
70  */
71 function permalink_anchor( $mode = 'id' ) {
72         $post = get_post();
73         switch ( strtolower( $mode ) ) {
74                 case 'title':
75                         $title = sanitize_title( $post->post_title ) . '-' . $post->ID;
76                         echo '<a id="'.$title.'"></a>';
77                         break;
78                 case 'id':
79                 default:
80                         echo '<a id="post-' . $post->ID . '"></a>';
81                         break;
82         }
83 }
84
85 /**
86  * Retrieve full permalink for current post or post ID.
87  *
88  * This function is an alias for get_permalink().
89  *
90  * @since 3.9.0
91  *
92  * @see get_permalink()
93  *
94  * @param int|WP_Post $id        Optional. Post ID or post object. Default is the current post.
95  * @param bool        $leavename Optional. Whether to keep post name or page name. Default false.
96  * @return string|false The permalink URL or false if post does not exist.
97  */
98 function get_the_permalink( $id = 0, $leavename = false ) {
99         return get_permalink( $id, $leavename );
100 }
101
102 /**
103  * Retrieve full permalink for current post or post ID.
104  *
105  * @since 1.0.0
106  *
107  * @param int|WP_Post $id        Optional. Post ID or post object. Default current post.
108  * @param bool        $leavename Optional. Whether to keep post name or page name. Default false.
109  * @return string|false The permalink URL or false if post does not exist.
110  */
111 function get_permalink( $id = 0, $leavename = false ) {
112         $rewritecode = array(
113                 '%year%',
114                 '%monthnum%',
115                 '%day%',
116                 '%hour%',
117                 '%minute%',
118                 '%second%',
119                 $leavename? '' : '%postname%',
120                 '%post_id%',
121                 '%category%',
122                 '%author%',
123                 $leavename? '' : '%pagename%',
124         );
125
126         if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) {
127                 $post = $id;
128                 $sample = true;
129         } else {
130                 $post = get_post($id);
131                 $sample = false;
132         }
133
134         if ( empty($post->ID) )
135                 return false;
136
137         if ( $post->post_type == 'page' )
138                 return get_page_link($post, $leavename, $sample);
139         elseif ( $post->post_type == 'attachment' )
140                 return get_attachment_link( $post, $leavename );
141         elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
142                 return get_post_permalink($post, $leavename, $sample);
143
144         $permalink = get_option('permalink_structure');
145
146         /**
147          * Filter the permalink structure for a post before token replacement occurs.
148          *
149          * Only applies to posts with post_type of 'post'.
150          *
151          * @since 3.0.0
152          *
153          * @param string  $permalink The site's permalink structure.
154          * @param WP_Post $post      The post in question.
155          * @param bool    $leavename Whether to keep the post name.
156          */
157         $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
158
159         if ( '' != $permalink && !in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) ) ) {
160                 $unixtime = strtotime($post->post_date);
161
162                 $category = '';
163                 if ( strpos($permalink, '%category%') !== false ) {
164                         $cats = get_the_category($post->ID);
165                         if ( $cats ) {
166                                 usort($cats, '_usort_terms_by_ID'); // order by ID
167
168                                 /**
169                                  * Filter the category that gets used in the %category% permalink token.
170                                  *
171                                  * @since 3.5.0
172                                  *
173                                  * @param stdClass $cat  The category to use in the permalink.
174                                  * @param array    $cats Array of all categories associated with the post.
175                                  * @param WP_Post  $post The post in question.
176                                  */
177                                 $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
178
179                                 $category_object = get_term( $category_object, 'category' );
180                                 $category = $category_object->slug;
181                                 if ( $parent = $category_object->parent )
182                                         $category = get_category_parents($parent, false, '/', true) . $category;
183                         }
184                         // show default category in permalinks, without
185                         // having to assign it explicitly
186                         if ( empty($category) ) {
187                                 $default_category = get_term( get_option( 'default_category' ), 'category' );
188                                 $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
189                         }
190                 }
191
192                 $author = '';
193                 if ( strpos($permalink, '%author%') !== false ) {
194                         $authordata = get_userdata($post->post_author);
195                         $author = $authordata->user_nicename;
196                 }
197
198                 $date = explode(" ",date('Y m d H i s', $unixtime));
199                 $rewritereplace =
200                 array(
201                         $date[0],
202                         $date[1],
203                         $date[2],
204                         $date[3],
205                         $date[4],
206                         $date[5],
207                         $post->post_name,
208                         $post->ID,
209                         $category,
210                         $author,
211                         $post->post_name,
212                 );
213                 $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
214                 $permalink = user_trailingslashit($permalink, 'single');
215         } else { // if they're not using the fancy permalink option
216                 $permalink = home_url('?p=' . $post->ID);
217         }
218
219         /**
220          * Filter the permalink for a post.
221          *
222          * Only applies to posts with post_type of 'post'.
223          *
224          * @since 1.5.0
225          *
226          * @param string  $permalink The post's permalink.
227          * @param WP_Post $post      The post in question.
228          * @param bool    $leavename Whether to keep the post name.
229          */
230         return apply_filters( 'post_link', $permalink, $post, $leavename );
231 }
232
233 /**
234  * Retrieve the permalink for a post with a custom post type.
235  *
236  * @since 3.0.0
237  *
238  * @global WP_Rewrite $wp_rewrite
239  *
240  * @param int $id         Optional. Post ID.
241  * @param bool $leavename Optional, defaults to false. Whether to keep post name.
242  * @param bool $sample    Optional, defaults to false. Is it a sample permalink.
243  * @return string|WP_Error The post permalink.
244  */
245 function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
246         global $wp_rewrite;
247
248         $post = get_post($id);
249
250         if ( is_wp_error( $post ) )
251                 return $post;
252
253         $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);
254
255         $slug = $post->post_name;
256
257         $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) );
258
259         $post_type = get_post_type_object($post->post_type);
260
261         if ( $post_type->hierarchical ) {
262                 $slug = get_page_uri( $id );
263         }
264
265         if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
266                 if ( ! $leavename ) {
267                         $post_link = str_replace("%$post->post_type%", $slug, $post_link);
268                 }
269                 $post_link = home_url( user_trailingslashit($post_link) );
270         } else {
271                 if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
272                         $post_link = add_query_arg($post_type->query_var, $slug, '');
273                 else
274                         $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');
275                 $post_link = home_url($post_link);
276         }
277
278         /**
279          * Filter the permalink for a post with a custom post type.
280          *
281          * @since 3.0.0
282          *
283          * @param string  $post_link The post's permalink.
284          * @param WP_Post $post      The post in question.
285          * @param bool    $leavename Whether to keep the post name.
286          * @param bool    $sample    Is it a sample permalink.
287          */
288         return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );
289 }
290
291 /**
292  * Retrieve permalink from post ID.
293  *
294  * @since 1.0.0
295  *
296  * @param int|WP_Post $post_id    Optional. Post ID or WP_Post object. Default is global $post.
297  * @param mixed       $deprecated Not used.
298  * @return string|false
299  */
300 function post_permalink( $post_id = 0, $deprecated = '' ) {
301         if ( !empty( $deprecated ) )
302                 _deprecated_argument( __FUNCTION__, '1.3' );
303
304         return get_permalink($post_id);
305 }
306
307 /**
308  * Retrieve the permalink for current page or page ID.
309  *
310  * Respects page_on_front. Use this one.
311  *
312  * @since 1.5.0
313  *
314  * @param int|object $post      Optional. Post ID or object.
315  * @param bool       $leavename Optional, defaults to false. Whether to keep page name.
316  * @param bool       $sample    Optional, defaults to false. Is it a sample permalink.
317  * @return string The page permalink.
318  */
319 function get_page_link( $post = false, $leavename = false, $sample = false ) {
320         $post = get_post( $post );
321
322         if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) )
323                 $link = home_url('/');
324         else
325                 $link = _get_page_link( $post, $leavename, $sample );
326
327         /**
328          * Filter the permalink for a page.
329          *
330          * @since 1.5.0
331          *
332          * @param string $link    The page's permalink.
333          * @param int    $post_id The ID of the page.
334          * @param bool   $sample  Is it a sample permalink.
335          */
336         return apply_filters( 'page_link', $link, $post->ID, $sample );
337 }
338
339 /**
340  * Retrieve the page permalink.
341  *
342  * Ignores page_on_front. Internal use only.
343  *
344  * @since 2.1.0
345  * @access private
346  *
347  * @global WP_Rewrite $wp_rewrite
348  *
349  * @param int|object $post      Optional. Post ID or object.
350  * @param bool       $leavename Optional. Leave name.
351  * @param bool       $sample    Optional. Sample permalink.
352  * @return string The page permalink.
353  */
354 function _get_page_link( $post = false, $leavename = false, $sample = false ) {
355         global $wp_rewrite;
356
357         $post = get_post( $post );
358
359         $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
360
361         $link = $wp_rewrite->get_page_permastruct();
362
363         if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
364                 if ( ! $leavename ) {
365                         $link = str_replace('%pagename%', get_page_uri( $post ), $link);
366                 }
367
368                 $link = home_url($link);
369                 $link = user_trailingslashit($link, 'page');
370         } else {
371                 $link = home_url( '?page_id=' . $post->ID );
372         }
373
374         /**
375          * Filter the permalink for a non-page_on_front page.
376          *
377          * @since 2.1.0
378          *
379          * @param string $link    The page's permalink.
380          * @param int    $post_id The ID of the page.
381          */
382         return apply_filters( '_get_page_link', $link, $post->ID );
383 }
384
385 /**
386  * Retrieve permalink for attachment.
387  *
388  * This can be used in the WordPress Loop or outside of it.
389  *
390  * @since 2.0.0
391  *
392  * @global WP_Rewrite $wp_rewrite
393  *
394  * @param int|object $post      Optional. Post ID or object.
395  * @param bool       $leavename Optional. Leave name.
396  * @return string The attachment permalink.
397  */
398 function get_attachment_link( $post = null, $leavename = false ) {
399         global $wp_rewrite;
400
401         $link = false;
402
403         $post = get_post( $post );
404         $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false;
405
406         if ( $wp_rewrite->using_permalinks() && $parent ) {
407                 if ( 'page' == $parent->post_type )
408                         $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front
409                 else
410                         $parentlink = get_permalink( $post->post_parent );
411
412                 if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
413                         $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
414                 else
415                         $name = $post->post_name;
416
417                 if ( strpos($parentlink, '?') === false )
418                         $link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' );
419
420                 if ( ! $leavename )
421                         $link = str_replace( '%postname%', $name, $link );
422         }
423
424         if ( ! $link )
425                 $link = home_url( '/?attachment_id=' . $post->ID );
426
427         /**
428          * Filter the permalink for an attachment.
429          *
430          * @since 2.0.0
431          *
432          * @param string $link    The attachment's permalink.
433          * @param int    $post_id Attachment ID.
434          */
435         return apply_filters( 'attachment_link', $link, $post->ID );
436 }
437
438 /**
439  * Retrieve the permalink for the year archives.
440  *
441  * @since 1.5.0
442  *
443  * @global WP_Rewrite $wp_rewrite
444  *
445  * @param int|bool $year False for current year or year for permalink.
446  * @return string The permalink for the specified year archive.
447  */
448 function get_year_link($year) {
449         global $wp_rewrite;
450         if ( !$year )
451                 $year = gmdate('Y', current_time('timestamp'));
452         $yearlink = $wp_rewrite->get_year_permastruct();
453         if ( !empty($yearlink) ) {
454                 $yearlink = str_replace('%year%', $year, $yearlink);
455                 $yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) );
456         } else {
457                 $yearlink = home_url( '?m=' . $year );
458         }
459
460         /**
461          * Filter the year archive permalink.
462          *
463          * @since 1.5.0
464          *
465          * @param string $yearlink Permalink for the year archive.
466          * @param int    $year     Year for the archive.
467          */
468         return apply_filters( 'year_link', $yearlink, $year );
469 }
470
471 /**
472  * Retrieve the permalink for the month archives with year.
473  *
474  * @since 1.0.0
475  *
476  * @global WP_Rewrite $wp_rewrite
477  *
478  * @param bool|int $year  False for current year. Integer of year.
479  * @param bool|int $month False for current month. Integer of month.
480  * @return string The permalink for the specified month and year archive.
481  */
482 function get_month_link($year, $month) {
483         global $wp_rewrite;
484         if ( !$year )
485                 $year = gmdate('Y', current_time('timestamp'));
486         if ( !$month )
487                 $month = gmdate('m', current_time('timestamp'));
488         $monthlink = $wp_rewrite->get_month_permastruct();
489         if ( !empty($monthlink) ) {
490                 $monthlink = str_replace('%year%', $year, $monthlink);
491                 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
492                 $monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) );
493         } else {
494                 $monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) );
495         }
496
497         /**
498          * Filter the month archive permalink.
499          *
500          * @since 1.5.0
501          *
502          * @param string $monthlink Permalink for the month archive.
503          * @param int    $year      Year for the archive.
504          * @param int    $month     The month for the archive.
505          */
506         return apply_filters( 'month_link', $monthlink, $year, $month );
507 }
508
509 /**
510  * Retrieve the permalink for the day archives with year and month.
511  *
512  * @since 1.0.0
513  *
514  * @global WP_Rewrite $wp_rewrite
515  *
516  * @param bool|int $year  False for current year. Integer of year.
517  * @param bool|int $month False for current month. Integer of month.
518  * @param bool|int $day   False for current day. Integer of day.
519  * @return string The permalink for the specified day, month, and year archive.
520  */
521 function get_day_link($year, $month, $day) {
522         global $wp_rewrite;
523         if ( !$year )
524                 $year = gmdate('Y', current_time('timestamp'));
525         if ( !$month )
526                 $month = gmdate('m', current_time('timestamp'));
527         if ( !$day )
528                 $day = gmdate('j', current_time('timestamp'));
529
530         $daylink = $wp_rewrite->get_day_permastruct();
531         if ( !empty($daylink) ) {
532                 $daylink = str_replace('%year%', $year, $daylink);
533                 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
534                 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
535                 $daylink = home_url( user_trailingslashit( $daylink, 'day' ) );
536         } else {
537                 $daylink = home_url( '?m=' . $year . zeroise( $month, 2 ) . zeroise( $day, 2 ) );
538         }
539
540         /**
541          * Filter the day archive permalink.
542          *
543          * @since 1.5.0
544          *
545          * @param string $daylink Permalink for the day archive.
546          * @param int    $year    Year for the archive.
547          * @param int    $month   Month for the archive.
548          * @param int    $day     The day for the archive.
549          */
550         return apply_filters( 'day_link', $daylink, $year, $month, $day );
551 }
552
553 /**
554  * Display the permalink for the feed type.
555  *
556  * @since 3.0.0
557  *
558  * @param string $anchor The link's anchor text.
559  * @param string $feed   Optional, defaults to default feed. Feed type.
560  */
561 function the_feed_link( $anchor, $feed = '' ) {
562         $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
563
564         /**
565          * Filter the feed link anchor tag.
566          *
567          * @since 3.0.0
568          *
569          * @param string $link The complete anchor tag for a feed link.
570          * @param string $feed The feed type, or an empty string for the
571          *                     default feed type.
572          */
573         echo apply_filters( 'the_feed_link', $link, $feed );
574 }
575
576 /**
577  * Retrieve the permalink for the feed type.
578  *
579  * @since 1.5.0
580  *
581  * @global WP_Rewrite $wp_rewrite
582  *
583  * @param string $feed Optional, defaults to default feed. Feed type.
584  * @return string The feed permalink.
585  */
586 function get_feed_link($feed = '') {
587         global $wp_rewrite;
588
589         $permalink = $wp_rewrite->get_feed_permastruct();
590         if ( '' != $permalink ) {
591                 if ( false !== strpos($feed, 'comments_') ) {
592                         $feed = str_replace('comments_', '', $feed);
593                         $permalink = $wp_rewrite->get_comment_feed_permastruct();
594                 }
595
596                 if ( get_default_feed() == $feed )
597                         $feed = '';
598
599                 $permalink = str_replace('%feed%', $feed, $permalink);
600                 $permalink = preg_replace('#/+#', '/', "/$permalink");
601                 $output =  home_url( user_trailingslashit($permalink, 'feed') );
602         } else {
603                 if ( empty($feed) )
604                         $feed = get_default_feed();
605
606                 if ( false !== strpos($feed, 'comments_') )
607                         $feed = str_replace('comments_', 'comments-', $feed);
608
609                 $output = home_url("?feed={$feed}");
610         }
611
612         /**
613          * Filter the feed type permalink.
614          *
615          * @since 1.5.0
616          *
617          * @param string $output The feed permalink.
618          * @param string $feed   Feed type.
619          */
620         return apply_filters( 'feed_link', $output, $feed );
621 }
622
623 /**
624  * Retrieve the permalink for the post comments feed.
625  *
626  * @since 2.2.0
627  *
628  * @param int    $post_id Optional. Post ID.
629  * @param string $feed    Optional. Feed type.
630  * @return string The permalink for the comments feed for the given post.
631  */
632 function get_post_comments_feed_link($post_id = 0, $feed = '') {
633         $post_id = absint( $post_id );
634
635         if ( ! $post_id )
636                 $post_id = get_the_ID();
637
638         if ( empty( $feed ) )
639                 $feed = get_default_feed();
640
641         if ( '' != get_option('permalink_structure') ) {
642                 if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') )
643                         $url = _get_page_link( $post_id );
644                 else
645                         $url = get_permalink($post_id);
646
647                 $url = trailingslashit($url) . 'feed';
648                 if ( $feed != get_default_feed() )
649                         $url .= "/$feed";
650                 $url = user_trailingslashit($url, 'single_feed');
651         } else {
652                 $type = get_post_field('post_type', $post_id);
653                 if ( 'page' == $type )
654                         $url = add_query_arg( array( 'feed' => $feed, 'page_id' => $post_id ), home_url( '/' ) );
655                 else
656                         $url = add_query_arg( array( 'feed' => $feed, 'p' => $post_id ), home_url( '/' ) );
657         }
658
659         /**
660          * Filter the post comments feed permalink.
661          *
662          * @since 1.5.1
663          *
664          * @param string $url Post comments feed permalink.
665          */
666         return apply_filters( 'post_comments_feed_link', $url );
667 }
668
669 /**
670  * Display the comment feed link for a post.
671  *
672  * Prints out the comment feed link for a post. Link text is placed in the
673  * anchor. If no link text is specified, default text is used. If no post ID is
674  * specified, the current post is used.
675  *
676  * @since 2.5.0
677  *
678  * @param string $link_text Descriptive text.
679  * @param int    $post_id   Optional post ID. Default to current post.
680  * @param string $feed      Optional. Feed format.
681 */
682 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
683         $url = esc_url( get_post_comments_feed_link( $post_id, $feed ) );
684         if ( empty($link_text) )
685                 $link_text = __('Comments Feed');
686
687         /**
688          * Filter the post comment feed link anchor tag.
689          *
690          * @since 2.8.0
691          *
692          * @param string $link    The complete anchor tag for the comment feed link.
693          * @param int    $post_id Post ID.
694          * @param string $feed    The feed type, or an empty string for the default feed type.
695          */
696         echo apply_filters( 'post_comments_feed_link_html', "<a href='$url'>$link_text</a>", $post_id, $feed );
697 }
698
699 /**
700  * Retrieve the feed link for a given author.
701  *
702  * Returns a link to the feed for all posts by a given author. A specific feed
703  * can be requested or left blank to get the default feed.
704  *
705  * @since 2.5.0
706  *
707  * @param int    $author_id ID of an author.
708  * @param string $feed      Optional. Feed type.
709  * @return string Link to the feed for the author specified by $author_id.
710 */
711 function get_author_feed_link( $author_id, $feed = '' ) {
712         $author_id = (int) $author_id;
713         $permalink_structure = get_option('permalink_structure');
714
715         if ( empty($feed) )
716                 $feed = get_default_feed();
717
718         if ( '' == $permalink_structure ) {
719                 $link = home_url("?feed=$feed&amp;author=" . $author_id);
720         } else {
721                 $link = get_author_posts_url($author_id);
722                 if ( $feed == get_default_feed() )
723                         $feed_link = 'feed';
724                 else
725                         $feed_link = "feed/$feed";
726
727                 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
728         }
729
730         /**
731          * Filter the feed link for a given author.
732          *
733          * @since 1.5.1
734          *
735          * @param string $link The author feed link.
736          * @param string $feed Feed type.
737          */
738         $link = apply_filters( 'author_feed_link', $link, $feed );
739
740         return $link;
741 }
742
743 /**
744  * Retrieve the feed link for a category.
745  *
746  * Returns a link to the feed for all posts in a given category. A specific feed
747  * can be requested or left blank to get the default feed.
748  *
749  * @since 2.5.0
750  *
751  * @param int    $cat_id ID of a category.
752  * @param string $feed   Optional. Feed type.
753  * @return string Link to the feed for the category specified by $cat_id.
754 */
755 function get_category_feed_link( $cat_id, $feed = '' ) {
756         return get_term_feed_link( $cat_id, 'category', $feed );
757 }
758
759 /**
760  * Retrieve the feed link for a term.
761  *
762  * Returns a link to the feed for all posts in a given term. A specific feed
763  * can be requested or left blank to get the default feed.
764  *
765  * @since 3.0.0
766  *
767  * @param int    $term_id  ID of a category.
768  * @param string $taxonomy Optional. Taxonomy of $term_id
769  * @param string $feed     Optional. Feed type.
770  * @return string|false Link to the feed for the term specified by $term_id and $taxonomy.
771 */
772 function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
773         $term_id = ( int ) $term_id;
774
775         $term = get_term( $term_id, $taxonomy  );
776
777         if ( empty( $term ) || is_wp_error( $term ) )
778                 return false;
779
780         if ( empty( $feed ) )
781                 $feed = get_default_feed();
782
783         $permalink_structure = get_option( 'permalink_structure' );
784
785         if ( '' == $permalink_structure ) {
786                 if ( 'category' == $taxonomy ) {
787                         $link = home_url("?feed=$feed&amp;cat=$term_id");
788                 }
789                 elseif ( 'post_tag' == $taxonomy ) {
790                         $link = home_url("?feed=$feed&amp;tag=$term->slug");
791                 } else {
792                         $t = get_taxonomy( $taxonomy );
793                         $link = home_url("?feed=$feed&amp;$t->query_var=$term->slug");
794                 }
795         } else {
796                 $link = get_term_link( $term_id, $term->taxonomy );
797                 if ( $feed == get_default_feed() )
798                         $feed_link = 'feed';
799                 else
800                         $feed_link = "feed/$feed";
801
802                 $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
803         }
804
805         if ( 'category' == $taxonomy ) {
806                 /**
807                  * Filter the category feed link.
808                  *
809                  * @since 1.5.1
810                  *
811                  * @param string $link The category feed link.
812                  * @param string $feed Feed type.
813                  */
814                 $link = apply_filters( 'category_feed_link', $link, $feed );
815         } elseif ( 'post_tag' == $taxonomy ) {
816                 /**
817                  * Filter the post tag feed link.
818                  *
819                  * @since 2.3.0
820                  *
821                  * @param string $link The tag feed link.
822                  * @param string $feed Feed type.
823                  */
824                 $link = apply_filters( 'tag_feed_link', $link, $feed );
825         } else {
826                 /**
827                  * Filter the feed link for a taxonomy other than 'category' or 'post_tag'.
828                  *
829                  * @since 3.0.0
830                  *
831                  * @param string $link The taxonomy feed link.
832                  * @param string $feed Feed type.
833                  * @param string $feed The taxonomy name.
834                  */
835                 $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
836         }
837
838         return $link;
839 }
840
841 /**
842  * Retrieve permalink for feed of tag.
843  *
844  * @since 2.3.0
845  *
846  * @param int    $tag_id Tag ID.
847  * @param string $feed   Optional. Feed type.
848  * @return string The feed permalink for the given tag.
849  */
850 function get_tag_feed_link( $tag_id, $feed = '' ) {
851         return get_term_feed_link( $tag_id, 'post_tag', $feed );
852 }
853
854 /**
855  * Retrieve edit tag link.
856  *
857  * @since 2.7.0
858  *
859  * @param int    $tag_id   Tag ID
860  * @param string $taxonomy Taxonomy
861  * @return string The edit tag link URL for the given tag.
862  */
863 function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
864         /**
865          * Filter the edit link for a tag (or term in another taxonomy).
866          *
867          * @since 2.7.0
868          *
869          * @param string $link The term edit link.
870          */
871         return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
872 }
873
874 /**
875  * Display or retrieve edit tag link with formatting.
876  *
877  * @since 2.7.0
878  *
879  * @param string $link   Optional. Anchor text.
880  * @param string $before Optional. Display before edit link.
881  * @param string $after  Optional. Display after edit link.
882  * @param object $tag    Tag object.
883  */
884 function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
885         $link = edit_term_link( $link, '', '', $tag, false );
886
887         /**
888          * Filter the anchor tag for the edit link for a tag (or term in another taxonomy).
889          *
890          * @since 2.7.0
891          *
892          * @param string $link The anchor tag for the edit link.
893          */
894         echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
895 }
896
897 /**
898  * Retrieve edit term url.
899  *
900  * @since 3.1.0
901  *
902  * @param int    $term_id     Term ID.
903  * @param string $taxonomy    Taxonomy.
904  * @param string $object_type The object type. Used to highlight the proper post type menu on the linked page.
905  *                            Defaults to the first object_type associated with the taxonomy.
906  * @return string|null The edit term link URL for the given term, or null on failure.
907  */
908 function get_edit_term_link( $term_id, $taxonomy, $object_type = '' ) {
909         $tax = get_taxonomy( $taxonomy );
910         if ( ! $tax || ! current_user_can( $tax->cap->edit_terms ) ) {
911                 return;
912         }
913
914         $term = get_term( $term_id, $taxonomy );
915         if ( ! $term || is_wp_error( $term ) ) {
916                 return;
917         }
918
919         $args = array(
920                 'action' => 'edit',
921                 'taxonomy' => $taxonomy,
922                 'tag_ID' => $term->term_id,
923         );
924
925         if ( $object_type ) {
926                 $args['post_type'] = $object_type;
927         } elseif ( ! empty( $tax->object_type ) ) {
928                 $args['post_type'] = reset( $tax->object_type );
929         }
930
931         $location = add_query_arg( $args, admin_url( 'edit-tags.php' ) );
932
933         /**
934          * Filter the edit link for a term.
935          *
936          * @since 3.1.0
937          *
938          * @param string $location    The edit link.
939          * @param int    $term_id     Term ID.
940          * @param string $taxonomy    Taxonomy name.
941          * @param string $object_type The object type (eg. the post type).
942          */
943         return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
944 }
945
946 /**
947  * Display or retrieve edit term link with formatting.
948  *
949  * @since 3.1.0
950  *
951  * @param string $link   Optional. Anchor text. Default empty.
952  * @param string $before Optional. Display before edit link. Default empty.
953  * @param string $after  Optional. Display after edit link. Default empty.
954  * @param object $term   Optional. Term object. If null, the queried object will be inspected. Default null.
955  * @param bool   $echo   Optional. Whether or not to echo the return. Default true.
956  * @return string|void HTML content.
957  */
958 function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
959         if ( is_null( $term ) )
960                 $term = get_queried_object();
961
962         if ( ! $term )
963                 return;
964
965         $tax = get_taxonomy( $term->taxonomy );
966         if ( ! current_user_can( $tax->cap->edit_terms ) )
967                 return;
968
969         if ( empty( $link ) )
970                 $link = __('Edit This');
971
972         $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>';
973
974         /**
975          * Filter the anchor tag for the edit link of a term.
976          *
977          * @since 3.1.0
978          *
979          * @param string $link    The anchor tag for the edit link.
980          * @param int    $term_id Term ID.
981          */
982         $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
983
984         if ( $echo )
985                 echo $link;
986         else
987                 return $link;
988 }
989
990 /**
991  * Retrieve permalink for search.
992  *
993  * @since  3.0.0
994  *
995  * @global WP_Rewrite $wp_rewrite
996  *
997  * @param string $query Optional. The query string to use. If empty the current query is used.
998  * @return string The search permalink.
999  */
1000 function get_search_link( $query = '' ) {
1001         global $wp_rewrite;
1002
1003         if ( empty($query) )
1004                 $search = get_search_query( false );
1005         else
1006                 $search = stripslashes($query);
1007
1008         $permastruct = $wp_rewrite->get_search_permastruct();
1009
1010         if ( empty( $permastruct ) ) {
1011                 $link = home_url('?s=' . urlencode($search) );
1012         } else {
1013                 $search = urlencode($search);
1014                 $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
1015                 $link = str_replace( '%search%', $search, $permastruct );
1016                 $link = home_url( user_trailingslashit( $link, 'search' ) );
1017         }
1018
1019         /**
1020          * Filter the search permalink.
1021          *
1022          * @since 3.0.0
1023          *
1024          * @param string $link   Search permalink.
1025          * @param string $search The URL-encoded search term.
1026          */
1027         return apply_filters( 'search_link', $link, $search );
1028 }
1029
1030 /**
1031  * Retrieve the permalink for the feed of the search results.
1032  *
1033  * @since 2.5.0
1034  *
1035  * @global WP_Rewrite $wp_rewrite
1036  *
1037  * @param string $search_query Optional. Search query.
1038  * @param string $feed         Optional. Feed type.
1039  * @return string The search results feed permalink.
1040  */
1041 function get_search_feed_link($search_query = '', $feed = '') {
1042         global $wp_rewrite;
1043         $link = get_search_link($search_query);
1044
1045         if ( empty($feed) )
1046                 $feed = get_default_feed();
1047
1048         $permastruct = $wp_rewrite->get_search_permastruct();
1049
1050         if ( empty($permastruct) ) {
1051                 $link = add_query_arg('feed', $feed, $link);
1052         } else {
1053                 $link = trailingslashit($link);
1054                 $link .= "feed/$feed/";
1055         }
1056
1057         /**
1058          * Filter the search feed link.
1059          *
1060          * @since 2.5.0
1061          *
1062          * @param string $link Search feed link.
1063          * @param string $feed Feed type.
1064          * @param string $type The search type. One of 'posts' or 'comments'.
1065          */
1066         return apply_filters( 'search_feed_link', $link, $feed, 'posts' );
1067 }
1068
1069 /**
1070  * Retrieve the permalink for the comments feed of the search results.
1071  *
1072  * @since 2.5.0
1073  *
1074  * @global WP_Rewrite $wp_rewrite
1075  *
1076  * @param string $search_query Optional. Search query.
1077  * @param string $feed         Optional. Feed type.
1078  * @return string The comments feed search results permalink.
1079  */
1080 function get_search_comments_feed_link($search_query = '', $feed = '') {
1081         global $wp_rewrite;
1082
1083         if ( empty($feed) )
1084                 $feed = get_default_feed();
1085
1086         $link = get_search_feed_link($search_query, $feed);
1087
1088         $permastruct = $wp_rewrite->get_search_permastruct();
1089
1090         if ( empty($permastruct) )
1091                 $link = add_query_arg('feed', 'comments-' . $feed, $link);
1092         else
1093                 $link = add_query_arg('withcomments', 1, $link);
1094
1095         /** This filter is documented in wp-includes/link-template.php */
1096         return apply_filters( 'search_feed_link', $link, $feed, 'comments' );
1097 }
1098
1099 /**
1100  * Retrieve the permalink for a post type archive.
1101  *
1102  * @since 3.1.0
1103  *
1104  * @global WP_Rewrite $wp_rewrite
1105  *
1106  * @param string $post_type Post type
1107  * @return string|false The post type archive permalink.
1108  */
1109 function get_post_type_archive_link( $post_type ) {
1110         global $wp_rewrite;
1111         if ( ! $post_type_obj = get_post_type_object( $post_type ) )
1112                 return false;
1113
1114         if ( ! $post_type_obj->has_archive )
1115                 return false;
1116
1117         if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
1118                 $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
1119                 if ( $post_type_obj->rewrite['with_front'] )
1120                         $struct = $wp_rewrite->front . $struct;
1121                 else
1122                         $struct = $wp_rewrite->root . $struct;
1123                 $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
1124         } else {
1125                 $link = home_url( '?post_type=' . $post_type );
1126         }
1127
1128         /**
1129          * Filter the post type archive permalink.
1130          *
1131          * @since 3.1.0
1132          *
1133          * @param string $link      The post type archive permalink.
1134          * @param string $post_type Post type name.
1135          */
1136         return apply_filters( 'post_type_archive_link', $link, $post_type );
1137 }
1138
1139 /**
1140  * Retrieve the permalink for a post type archive feed.
1141  *
1142  * @since 3.1.0
1143  *
1144  * @param string $post_type Post type
1145  * @param string $feed      Optional. Feed type
1146  * @return string|false The post type feed permalink.
1147  */
1148 function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
1149         $default_feed = get_default_feed();
1150         if ( empty( $feed ) )
1151                 $feed = $default_feed;
1152
1153         if ( ! $link = get_post_type_archive_link( $post_type ) )
1154                 return false;
1155
1156         $post_type_obj = get_post_type_object( $post_type );
1157         if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
1158                 $link = trailingslashit( $link );
1159                 $link .= 'feed/';
1160                 if ( $feed != $default_feed )
1161                         $link .= "$feed/";
1162         } else {
1163                 $link = add_query_arg( 'feed', $feed, $link );
1164         }
1165
1166         /**
1167          * Filter the post type archive feed link.
1168          *
1169          * @since 3.1.0
1170          *
1171          * @param string $link The post type archive feed link.
1172          * @param string $feed Feed type.
1173          */
1174         return apply_filters( 'post_type_archive_feed_link', $link, $feed );
1175 }
1176
1177 /**
1178  * Retrieve edit posts link for post.
1179  *
1180  * Can be used within the WordPress loop or outside of it. Can be used with
1181  * pages, posts, attachments, and revisions.
1182  *
1183  * @since 2.3.0
1184  *
1185  * @param int    $id      Optional. Post ID.
1186  * @param string $context Optional, defaults to display. How to write the '&', defaults to '&amp;'.
1187  * @return string|void The edit post link for the given post.
1188  */
1189 function get_edit_post_link( $id = 0, $context = 'display' ) {
1190         if ( ! $post = get_post( $id ) )
1191                 return;
1192
1193         if ( 'revision' === $post->post_type )
1194                 $action = '';
1195         elseif ( 'display' == $context )
1196                 $action = '&amp;action=edit';
1197         else
1198                 $action = '&action=edit';
1199
1200         $post_type_object = get_post_type_object( $post->post_type );
1201         if ( !$post_type_object )
1202                 return;
1203
1204         if ( !current_user_can( 'edit_post', $post->ID ) )
1205                 return;
1206
1207         /**
1208          * Filter the post edit link.
1209          *
1210          * @since 2.3.0
1211          *
1212          * @param string $link    The edit link.
1213          * @param int    $post_id Post ID.
1214          * @param string $context The link context. If set to 'display' then ampersands
1215          *                        are encoded.
1216          */
1217         return apply_filters( 'get_edit_post_link', admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) ), $post->ID, $context );
1218 }
1219
1220 /**
1221  * Display edit post link for post.
1222  *
1223  * @since 1.0.0
1224  *
1225  * @param string $text   Optional. Anchor text.
1226  * @param string $before Optional. Display before edit link.
1227  * @param string $after  Optional. Display after edit link.
1228  * @param int    $id     Optional. Post ID.
1229  */
1230 function edit_post_link( $text = null, $before = '', $after = '', $id = 0 ) {
1231         if ( ! $post = get_post( $id ) ) {
1232                 return;
1233         }
1234
1235         if ( ! $url = get_edit_post_link( $post->ID ) ) {
1236                 return;
1237         }
1238
1239         if ( null === $text ) {
1240                 $text = __( 'Edit This' );
1241         }
1242
1243         $link = '<a class="post-edit-link" href="' . $url . '">' . $text . '</a>';
1244
1245         /**
1246          * Filter the post edit link anchor tag.
1247          *
1248          * @since 2.3.0
1249          *
1250          * @param string $link    Anchor tag for the edit link.
1251          * @param int    $post_id Post ID.
1252          * @param string $text    Anchor text.
1253          */
1254         echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after;
1255 }
1256
1257 /**
1258  * Retrieve delete posts link for post.
1259  *
1260  * Can be used within the WordPress loop or outside of it, with any post type.
1261  *
1262  * @since 2.9.0
1263  *
1264  * @param int    $id           Optional. Post ID.
1265  * @param string $deprecated   Not used.
1266  * @param bool   $force_delete Whether to bypass trash and force deletion. Default is false.
1267  * @return string|void The delete post link URL for the given post.
1268  */
1269 function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
1270         if ( ! empty( $deprecated ) )
1271                 _deprecated_argument( __FUNCTION__, '3.0' );
1272
1273         if ( !$post = get_post( $id ) )
1274                 return;
1275
1276         $post_type_object = get_post_type_object( $post->post_type );
1277         if ( !$post_type_object )
1278                 return;
1279
1280         if ( !current_user_can( 'delete_post', $post->ID ) )
1281                 return;
1282
1283         $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
1284
1285         $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
1286
1287         /**
1288          * Filter the post delete link.
1289          *
1290          * @since 2.9.0
1291          *
1292          * @param string $link         The delete link.
1293          * @param int    $post_id      Post ID.
1294          * @param bool   $force_delete Whether to bypass the trash and force deletion. Default false.
1295          */
1296         return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
1297 }
1298
1299 /**
1300  * Retrieve edit comment link.
1301  *
1302  * @since 2.3.0
1303  *
1304  * @param int $comment_id Optional. Comment ID.
1305  * @return string|void The edit comment link URL for the given comment.
1306  */
1307 function get_edit_comment_link( $comment_id = 0 ) {
1308         $comment = get_comment( $comment_id );
1309
1310         if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
1311                 return;
1312
1313         $location = admin_url('comment.php?action=editcomment&amp;c=') . $comment->comment_ID;
1314
1315         /**
1316          * Filter the comment edit link.
1317          *
1318          * @since 2.3.0
1319          *
1320          * @param string $location The edit link.
1321          */
1322         return apply_filters( 'get_edit_comment_link', $location );
1323 }
1324
1325 /**
1326  * Display edit comment link with formatting.
1327  *
1328  * @since 1.0.0
1329  *
1330  * @global object $comment
1331  *
1332  * @param string $text   Optional. Anchor text.
1333  * @param string $before Optional. Display before edit link.
1334  * @param string $after  Optional. Display after edit link.
1335  */
1336 function edit_comment_link( $text = null, $before = '', $after = '' ) {
1337         global $comment;
1338
1339         if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
1340                 return;
1341         }
1342
1343         if ( null === $text ) {
1344                 $text = __( 'Edit This' );
1345         }
1346
1347         $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '">' . $text . '</a>';
1348
1349         /**
1350          * Filter the comment edit link anchor tag.
1351          *
1352          * @since 2.3.0
1353          *
1354          * @param string $link       Anchor tag for the edit link.
1355          * @param int    $comment_id Comment ID.
1356          * @param string $text       Anchor text.
1357          */
1358         echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after;
1359 }
1360
1361 /**
1362  * Display edit bookmark (literally a URL external to blog) link.
1363  *
1364  * @since 2.7.0
1365  *
1366  * @param int|stdClass $link Optional. Bookmark ID.
1367  * @return string|void The edit bookmark link URL.
1368  */
1369 function get_edit_bookmark_link( $link = 0 ) {
1370         $link = get_bookmark( $link );
1371
1372         if ( !current_user_can('manage_links') )
1373                 return;
1374
1375         $location = admin_url('link.php?action=edit&amp;link_id=') . $link->link_id;
1376
1377         /**
1378          * Filter the bookmark (link) edit link.
1379          *
1380          * @since 2.7.0
1381          *
1382          * @param string $location The edit link.
1383          * @param int    $link_id  Bookmark ID.
1384          */
1385         return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
1386 }
1387
1388 /**
1389  * Display edit bookmark (literally a URL external to blog) link anchor content.
1390  *
1391  * @since 2.7.0
1392  *
1393  * @param string $link     Optional. Anchor text.
1394  * @param string $before   Optional. Display before edit link.
1395  * @param string $after    Optional. Display after edit link.
1396  * @param int    $bookmark Optional. Bookmark ID.
1397  */
1398 function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
1399         $bookmark = get_bookmark($bookmark);
1400
1401         if ( !current_user_can('manage_links') )
1402                 return;
1403
1404         if ( empty($link) )
1405                 $link = __('Edit This');
1406
1407         $link = '<a href="' . get_edit_bookmark_link( $bookmark ) . '">' . $link . '</a>';
1408
1409         /**
1410          * Filter the bookmark edit link anchor tag.
1411          *
1412          * @since 2.7.0
1413          *
1414          * @param string $link    Anchor tag for the edit link.
1415          * @param int    $link_id Bookmark ID.
1416          */
1417         echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
1418 }
1419
1420 /**
1421  * Retrieve edit user link
1422  *
1423  * @since 3.5.0
1424  *
1425  * @param int $user_id Optional. User ID. Defaults to the current user.
1426  * @return string URL to edit user page or empty string.
1427  */
1428 function get_edit_user_link( $user_id = null ) {
1429         if ( ! $user_id )
1430                 $user_id = get_current_user_id();
1431
1432         if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) )
1433                 return '';
1434
1435         $user = get_userdata( $user_id );
1436
1437         if ( ! $user )
1438                 return '';
1439
1440         if ( get_current_user_id() == $user->ID )
1441                 $link = get_edit_profile_url( $user->ID );
1442         else
1443                 $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
1444
1445         /**
1446          * Filter the user edit link.
1447          *
1448          * @since 3.5.0
1449          *
1450          * @param string $link    The edit link.
1451          * @param int    $user_id User ID.
1452          */
1453         return apply_filters( 'get_edit_user_link', $link, $user->ID );
1454 }
1455
1456 // Navigation links
1457
1458 /**
1459  * Retrieve previous post that is adjacent to current post.
1460  *
1461  * @since 1.5.0
1462  *
1463  * @param bool         $in_same_term   Optional. Whether post should be in a same taxonomy term.
1464  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1465  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1466  * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1467  */
1468 function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1469         return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
1470 }
1471
1472 /**
1473  * Retrieve next post that is adjacent to current post.
1474  *
1475  * @since 1.5.0
1476  *
1477  * @param bool         $in_same_term   Optional. Whether post should be in a same taxonomy term.
1478  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1479  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1480  * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1481  */
1482 function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1483         return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
1484 }
1485
1486 /**
1487  * Retrieve adjacent post.
1488  *
1489  * Can either be next or previous post.
1490  *
1491  * @since 2.5.0
1492  *
1493  * @global wpdb $wpdb
1494  *
1495  * @param bool         $in_same_term   Optional. Whether post should be in a same taxonomy term.
1496  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1497  * @param bool         $previous       Optional. Whether to retrieve previous post.
1498  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1499  * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1500  */
1501 function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1502         global $wpdb;
1503
1504         if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
1505                 return null;
1506
1507         $current_post_date = $post->post_date;
1508
1509         $join = '';
1510         $where = '';
1511
1512         if ( $in_same_term || ! empty( $excluded_terms ) ) {
1513                 $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";
1514                 $where = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
1515
1516                 if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
1517                         // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
1518                         if ( false !== strpos( $excluded_terms, ' and ' ) ) {
1519                                 _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
1520                                 $excluded_terms = explode( ' and ', $excluded_terms );
1521                         } else {
1522                                 $excluded_terms = explode( ',', $excluded_terms );
1523                         }
1524
1525                         $excluded_terms = array_map( 'intval', $excluded_terms );
1526                 }
1527
1528                 if ( $in_same_term ) {
1529                         if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
1530                                 return '';
1531                         $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1532
1533                         // Remove any exclusions from the term array to include.
1534                         $term_array = array_diff( $term_array, (array) $excluded_terms );
1535                         $term_array = array_map( 'intval', $term_array );
1536
1537                         if ( ! $term_array || is_wp_error( $term_array ) )
1538                                 return '';
1539
1540                         $where .= " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
1541                 }
1542
1543                 if ( ! empty( $excluded_terms ) ) {
1544                         $where .= " AND p.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships tr LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.term_id IN (" . implode( $excluded_terms, ',' ) . ') )';
1545                 }
1546         }
1547
1548         // 'post_status' clause depends on the current user.
1549         if ( is_user_logged_in() ) {
1550                 $user_id = get_current_user_id();
1551
1552                 $post_type_object = get_post_type_object( $post->post_type );
1553                 if ( empty( $post_type_object ) ) {
1554                         $post_type_cap    = $post->post_type;
1555                         $read_private_cap = 'read_private_' . $post_type_cap . 's';
1556                 } else {
1557                         $read_private_cap = $post_type_object->cap->read_private_posts;
1558                 }
1559
1560                 /*
1561                  * Results should include private posts belonging to the current user, or private posts where the
1562                  * current user has the 'read_private_posts' cap.
1563                  */
1564                 $private_states = get_post_stati( array( 'private' => true ) );
1565                 $where .= " AND ( p.post_status = 'publish'";
1566                 foreach ( (array) $private_states as $state ) {
1567                         if ( current_user_can( $read_private_cap ) ) {
1568                                 $where .= $wpdb->prepare( " OR p.post_status = %s", $state );
1569                         } else {
1570                                 $where .= $wpdb->prepare( " OR (p.post_author = %d AND p.post_status = %s)", $user_id, $state );
1571                         }
1572                 }
1573                 $where .= " )";
1574         } else {
1575                 $where .= " AND p.post_status = 'publish'";
1576         }
1577
1578         $adjacent = $previous ? 'previous' : 'next';
1579         $op = $previous ? '<' : '>';
1580         $order = $previous ? 'DESC' : 'ASC';
1581
1582         /**
1583          * Filter the JOIN clause in the SQL for an adjacent post query.
1584          *
1585          * The dynamic portion of the hook name, `$adjacent`, refers to the type
1586          * of adjacency, 'next' or 'previous'.
1587          *
1588          * @since 2.5.0
1589          *
1590          * @param string $join           The JOIN clause in the SQL.
1591          * @param bool   $in_same_term   Whether post should be in a same taxonomy term.
1592          * @param array  $excluded_terms Array of excluded term IDs.
1593          */
1594         $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
1595
1596         /**
1597          * Filter the WHERE clause in the SQL for an adjacent post query.
1598          *
1599          * The dynamic portion of the hook name, `$adjacent`, refers to the type
1600          * of adjacency, 'next' or 'previous'.
1601          *
1602          * @since 2.5.0
1603          *
1604          * @param string $where          The `WHERE` clause in the SQL.
1605          * @param bool   $in_same_term   Whether post should be in a same taxonomy term.
1606          * @param array  $excluded_terms Array of excluded term IDs.
1607          */
1608         $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms );
1609
1610         /**
1611          * Filter the ORDER BY clause in the SQL for an adjacent post query.
1612          *
1613          * The dynamic portion of the hook name, `$adjacent`, refers to the type
1614          * of adjacency, 'next' or 'previous'.
1615          *
1616          * @since 2.5.0
1617          *
1618          * @param string $order_by The `ORDER BY` clause in the SQL.
1619          */
1620         $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
1621
1622         $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
1623         $query_key = 'adjacent_post_' . md5( $query );
1624         $result = wp_cache_get( $query_key, 'counts' );
1625         if ( false !== $result ) {
1626                 if ( $result )
1627                         $result = get_post( $result );
1628                 return $result;
1629         }
1630
1631         $result = $wpdb->get_var( $query );
1632         if ( null === $result )
1633                 $result = '';
1634
1635         wp_cache_set( $query_key, $result, 'counts' );
1636
1637         if ( $result )
1638                 $result = get_post( $result );
1639
1640         return $result;
1641 }
1642
1643 /**
1644  * Get adjacent post relational link.
1645  *
1646  * Can either be next or previous post relational link.
1647  *
1648  * @since 2.8.0
1649  *
1650  * @param string       $title          Optional. Link title format.
1651  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1652  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1653  * @param bool         $previous       Optional. Whether to display link to previous or next post. Default true.
1654  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1655  * @return string|void The adjacent post relational link URL.
1656  */
1657 function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1658         if ( $previous && is_attachment() && $post = get_post() )
1659                 $post = get_post( $post->post_parent );
1660         else
1661                 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1662
1663         if ( empty( $post ) )
1664                 return;
1665
1666         $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
1667
1668         if ( empty( $post_title ) )
1669                 $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1670
1671         $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1672
1673         $title = str_replace( '%title', $post_title, $title );
1674         $title = str_replace( '%date', $date, $title );
1675
1676         $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
1677         $link .= esc_attr( $title );
1678         $link .= "' href='" . get_permalink( $post ) . "' />\n";
1679
1680         $adjacent = $previous ? 'previous' : 'next';
1681
1682         /**
1683          * Filter the adjacent post relational link.
1684          *
1685          * The dynamic portion of the hook name, `$adjacent`, refers to the type
1686          * of adjacency, 'next' or 'previous'.
1687          *
1688          * @since 2.8.0
1689          *
1690          * @param string $link The relational link.
1691          */
1692         return apply_filters( "{$adjacent}_post_rel_link", $link );
1693 }
1694
1695 /**
1696  * Display relational links for the posts adjacent to the current post.
1697  *
1698  * @since 2.8.0
1699  *
1700  * @param string       $title          Optional. Link title format.
1701  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1702  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1703  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1704  */
1705 function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1706         echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1707         echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1708 }
1709
1710 /**
1711  * Display relational links for the posts adjacent to the current post for single post pages.
1712  *
1713  * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
1714  * @since 3.0.0
1715  *
1716  */
1717 function adjacent_posts_rel_link_wp_head() {
1718         if ( ! is_single() || is_attachment() ) {
1719                 return;
1720         }
1721         adjacent_posts_rel_link();
1722 }
1723
1724 /**
1725  * Display relational link for the next post adjacent to the current post.
1726  *
1727  * @since 2.8.0
1728  *
1729  * @param string       $title          Optional. Link title format.
1730  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1731  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1732  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1733  */
1734 function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1735         echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1736 }
1737
1738 /**
1739  * Display relational link for the previous post adjacent to the current post.
1740  *
1741  * @since 2.8.0
1742  *
1743  * @param string       $title          Optional. Link title format.
1744  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1745  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
1746  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1747  */
1748 function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1749         echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1750 }
1751
1752 /**
1753  * Retrieve boundary post.
1754  *
1755  * Boundary being either the first or last post by publish date within the constraints specified
1756  * by $in_same_term or $excluded_terms.
1757  *
1758  * @since 2.8.0
1759  *
1760  * @param bool         $in_same_term   Optional. Whether returned post should be in a same taxonomy term.
1761  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1762  * @param bool         $start          Optional. Whether to retrieve first or last post.
1763  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1764  * @return null|array Array containing the boundary post object if successful, null otherwise.
1765  */
1766 function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
1767         $post = get_post();
1768         if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
1769                 return null;
1770
1771         $query_args = array(
1772                 'posts_per_page' => 1,
1773                 'order' => $start ? 'ASC' : 'DESC',
1774                 'update_post_term_cache' => false,
1775                 'update_post_meta_cache' => false
1776         );
1777
1778         $term_array = array();
1779
1780         if ( ! is_array( $excluded_terms ) ) {
1781                 if ( ! empty( $excluded_terms ) )
1782                         $excluded_terms = explode( ',', $excluded_terms );
1783                 else
1784                         $excluded_terms = array();
1785         }
1786
1787         if ( $in_same_term || ! empty( $excluded_terms ) ) {
1788                 if ( $in_same_term )
1789                         $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1790
1791                 if ( ! empty( $excluded_terms ) ) {
1792                         $excluded_terms = array_map( 'intval', $excluded_terms );
1793                         $excluded_terms = array_diff( $excluded_terms, $term_array );
1794
1795                         $inverse_terms = array();
1796                         foreach ( $excluded_terms as $excluded_term )
1797                                 $inverse_terms[] = $excluded_term * -1;
1798                         $excluded_terms = $inverse_terms;
1799                 }
1800
1801                 $query_args[ 'tax_query' ] = array( array(
1802                         'taxonomy' => $taxonomy,
1803                         'terms' => array_merge( $term_array, $excluded_terms )
1804                 ) );
1805         }
1806
1807         return get_posts( $query_args );
1808 }
1809
1810 /*
1811  * Get previous post link that is adjacent to the current post.
1812  *
1813  * @since 3.7.0
1814  *
1815  * @param string       $format         Optional. Link anchor format.
1816  * @param string       $link           Optional. Link permalink format.
1817  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1818  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1819  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1820  * @return string The link URL of the previous post in relation to the current post.
1821  */
1822 function get_previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1823         return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy );
1824 }
1825
1826 /**
1827  * Display previous post link that is adjacent to the current post.
1828  *
1829  * @since 1.5.0
1830  * @see get_previous_post_link()
1831  *
1832  * @param string       $format         Optional. Link anchor format.
1833  * @param string       $link           Optional. Link permalink format.
1834  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1835  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1836  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1837  */
1838 function previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1839         echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1840 }
1841
1842 /**
1843  * Get next post link that is adjacent to the current post.
1844  *
1845  * @since 3.7.0
1846  *
1847  * @param string       $format         Optional. Link anchor format.
1848  * @param string       $link           Optional. Link permalink format.
1849  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1850  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1851  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1852  * @return string The link URL of the next post in relation to the current post.
1853  */
1854 function get_next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1855         return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy );
1856 }
1857
1858 /**
1859  * Display next post link that is adjacent to the current post.
1860  *
1861  * @since 1.5.0
1862  * @see get_next_post_link()
1863  *
1864  * @param string       $format         Optional. Link anchor format.
1865  * @param string       $link           Optional. Link permalink format.
1866  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1867  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1868  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1869  */
1870 function next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1871          echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1872 }
1873
1874 /**
1875  * Get adjacent post link.
1876  *
1877  * Can be either next post link or previous.
1878  *
1879  * @since 3.7.0
1880  *
1881  * @param string       $format         Link anchor format.
1882  * @param string       $link           Link permalink format.
1883  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1884  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs.
1885  * @param bool         $previous       Optional. Whether to display link to previous or next post. Default true.
1886  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1887  * @return string The link URL of the previous or next post in relation to the current post.
1888  */
1889 function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1890         if ( $previous && is_attachment() )
1891                 $post = get_post( get_post()->post_parent );
1892         else
1893                 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1894
1895         if ( ! $post ) {
1896                 $output = '';
1897         } else {
1898                 $title = $post->post_title;
1899
1900                 if ( empty( $post->post_title ) )
1901                         $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1902
1903                 /** This filter is documented in wp-includes/post-template.php */
1904                 $title = apply_filters( 'the_title', $title, $post->ID );
1905
1906                 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1907                 $rel = $previous ? 'prev' : 'next';
1908
1909                 $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
1910                 $inlink = str_replace( '%title', $title, $link );
1911                 $inlink = str_replace( '%date', $date, $inlink );
1912                 $inlink = $string . $inlink . '</a>';
1913
1914                 $output = str_replace( '%link', $inlink, $format );
1915         }
1916
1917         $adjacent = $previous ? 'previous' : 'next';
1918
1919         /**
1920          * Filter the adjacent post link.
1921          *
1922          * The dynamic portion of the hook name, `$adjacent`, refers to the type
1923          * of adjacency, 'next' or 'previous'.
1924          *
1925          * @since 2.6.0
1926          * @since 4.2.0 Added the `$adjacent` parameter.
1927          *
1928          * @param string  $output   The adjacent post link.
1929          * @param string  $format   Link anchor format.
1930          * @param string  $link     Link permalink format.
1931          * @param WP_Post $post     The adjacent post.
1932          * @param string  $adjacent Whether the post is previous or next.
1933          */
1934         return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post, $adjacent );
1935 }
1936
1937 /**
1938  * Display adjacent post link.
1939  *
1940  * Can be either next post link or previous.
1941  *
1942  * @since 2.5.0
1943  *
1944  * @param string       $format         Link anchor format.
1945  * @param string       $link           Link permalink format.
1946  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1947  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs.
1948  * @param bool         $previous       Optional. Whether to display link to previous or next post. Default true.
1949  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1950  */
1951 function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1952         echo get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy );
1953 }
1954
1955 /**
1956  * Retrieve links for page numbers.
1957  *
1958  * @since 1.5.0
1959  *
1960  * @global WP_Rewrite $wp_rewrite
1961  *
1962  * @param int  $pagenum Optional. Page ID.
1963  * @param bool $escape  Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
1964 *                           Otherwise, prepares the URL with esc_url_raw().
1965  * @return string The link URL for the given page number.
1966  */
1967 function get_pagenum_link($pagenum = 1, $escape = true ) {
1968         global $wp_rewrite;
1969
1970         $pagenum = (int) $pagenum;
1971
1972         $request = remove_query_arg( 'paged' );
1973
1974         $home_root = parse_url(home_url());
1975         $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
1976         $home_root = preg_quote( $home_root, '|' );
1977
1978         $request = preg_replace('|^'. $home_root . '|i', '', $request);
1979         $request = preg_replace('|^/+|', '', $request);
1980
1981         if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
1982                 $base = trailingslashit( get_bloginfo( 'url' ) );
1983
1984                 if ( $pagenum > 1 ) {
1985                         $result = add_query_arg( 'paged', $pagenum, $base . $request );
1986                 } else {
1987                         $result = $base . $request;
1988                 }
1989         } else {
1990                 $qs_regex = '|\?.*?$|';
1991                 preg_match( $qs_regex, $request, $qs_match );
1992
1993                 if ( !empty( $qs_match[0] ) ) {
1994                         $query_string = $qs_match[0];
1995                         $request = preg_replace( $qs_regex, '', $request );
1996                 } else {
1997                         $query_string = '';
1998                 }
1999
2000                 $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
2001                 $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request);
2002                 $request = ltrim($request, '/');
2003
2004                 $base = trailingslashit( get_bloginfo( 'url' ) );
2005
2006                 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
2007                         $base .= $wp_rewrite->index . '/';
2008
2009                 if ( $pagenum > 1 ) {
2010                         $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
2011                 }
2012
2013                 $result = $base . $request . $query_string;
2014         }
2015
2016         /**
2017          * Filter the page number link for the current request.
2018          *
2019          * @since 2.5.0
2020          *
2021          * @param string $result The page number link.
2022          */
2023         $result = apply_filters( 'get_pagenum_link', $result );
2024
2025         if ( $escape )
2026                 return esc_url( $result );
2027         else
2028                 return esc_url_raw( $result );
2029 }
2030
2031 /**
2032  * Retrieve next posts page link.
2033  *
2034  * Backported from 2.1.3 to 2.0.10.
2035  *
2036  * @since 2.0.10
2037  *
2038  * @global int $paged
2039  *
2040  * @param int $max_page Optional. Max pages.
2041  * @return string|void The link URL for next posts page.
2042  */
2043 function get_next_posts_page_link($max_page = 0) {
2044         global $paged;
2045
2046         if ( !is_single() ) {
2047                 if ( !$paged )
2048                         $paged = 1;
2049                 $nextpage = intval($paged) + 1;
2050                 if ( !$max_page || $max_page >= $nextpage )
2051                         return get_pagenum_link($nextpage);
2052         }
2053 }
2054
2055 /**
2056  * Display or return the next posts page link.
2057  *
2058  * @since 0.71
2059  *
2060  * @param int   $max_page Optional. Max pages.
2061  * @param bool  $echo     Optional. Echo or return;
2062  * @return string|void The link URL for next posts page if `$echo = false`.
2063  */
2064 function next_posts( $max_page = 0, $echo = true ) {
2065         $output = esc_url( get_next_posts_page_link( $max_page ) );
2066
2067         if ( $echo )
2068                 echo $output;
2069         else
2070                 return $output;
2071 }
2072
2073 /**
2074  * Return the next posts page link.
2075  *
2076  * @since 2.7.0
2077  *
2078  * @global int      $paged
2079  * @global WP_Query $wp_query
2080  *
2081  * @param string $label    Content for link text.
2082  * @param int    $max_page Optional. Max pages.
2083  * @return string|void HTML-formatted next posts page link.
2084  */
2085 function get_next_posts_link( $label = null, $max_page = 0 ) {
2086         global $paged, $wp_query;
2087
2088         if ( !$max_page )
2089                 $max_page = $wp_query->max_num_pages;
2090
2091         if ( !$paged )
2092                 $paged = 1;
2093
2094         $nextpage = intval($paged) + 1;
2095
2096         if ( null === $label )
2097                 $label = __( 'Next Page &raquo;' );
2098
2099         if ( !is_single() && ( $nextpage <= $max_page ) ) {
2100                 /**
2101                  * Filter the anchor tag attributes for the next posts page link.
2102                  *
2103                  * @since 2.7.0
2104                  *
2105                  * @param string $attributes Attributes for the anchor tag.
2106                  */
2107                 $attr = apply_filters( 'next_posts_link_attributes', '' );
2108
2109                 return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) . '</a>';
2110         }
2111 }
2112
2113 /**
2114  * Display the next posts page link.
2115  *
2116  * @since 0.71
2117  *
2118  * @param string $label    Content for link text.
2119  * @param int    $max_page Optional. Max pages.
2120  */
2121 function next_posts_link( $label = null, $max_page = 0 ) {
2122         echo get_next_posts_link( $label, $max_page );
2123 }
2124
2125 /**
2126  * Retrieve previous posts page link.
2127  *
2128  * Will only return string, if not on a single page or post.
2129  *
2130  * Backported to 2.0.10 from 2.1.3.
2131  *
2132  * @since 2.0.10
2133  *
2134  * @global int $paged
2135  *
2136  * @return string|void The link for the previous posts page.
2137  */
2138 function get_previous_posts_page_link() {
2139         global $paged;
2140
2141         if ( !is_single() ) {
2142                 $nextpage = intval($paged) - 1;
2143                 if ( $nextpage < 1 )
2144                         $nextpage = 1;
2145                 return get_pagenum_link($nextpage);
2146         }
2147 }
2148
2149 /**
2150  * Display or return the previous posts page link.
2151  *
2152  * @since 0.71
2153  *
2154  * @param bool $echo Optional. Echo or return;
2155  * @return string|void The previous posts page link if `$echo = false`.
2156  */
2157 function previous_posts( $echo = true ) {
2158         $output = esc_url( get_previous_posts_page_link() );
2159
2160         if ( $echo )
2161                 echo $output;
2162         else
2163                 return $output;
2164 }
2165
2166 /**
2167  * Return the previous posts page link.
2168  *
2169  * @since 2.7.0
2170  *
2171  * @global int $paged
2172  *
2173  * @param string $label Optional. Previous page link text.
2174  * @return string|void HTML-formatted previous page link.
2175  */
2176 function get_previous_posts_link( $label = null ) {
2177         global $paged;
2178
2179         if ( null === $label )
2180                 $label = __( '&laquo; Previous Page' );
2181
2182         if ( !is_single() && $paged > 1 ) {
2183                 /**
2184                  * Filter the anchor tag attributes for the previous posts page link.
2185                  *
2186                  * @since 2.7.0
2187                  *
2188                  * @param string $attributes Attributes for the anchor tag.
2189                  */
2190                 $attr = apply_filters( 'previous_posts_link_attributes', '' );
2191                 return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) .'</a>';
2192         }
2193 }
2194
2195 /**
2196  * Display the previous posts page link.
2197  *
2198  * @since 0.71
2199  *
2200  * @param string $label Optional. Previous page link text.
2201  */
2202 function previous_posts_link( $label = null ) {
2203         echo get_previous_posts_link( $label );
2204 }
2205
2206 /**
2207  * Return post pages link navigation for previous and next pages.
2208  *
2209  * @since 2.8.0
2210  *
2211  * @global WP_Query $wp_query
2212  *
2213  * @param string|array $args Optional args.
2214  * @return string The posts link navigation.
2215  */
2216 function get_posts_nav_link( $args = array() ) {
2217         global $wp_query;
2218
2219         $return = '';
2220
2221         if ( !is_singular() ) {
2222                 $defaults = array(
2223                         'sep' => ' &#8212; ',
2224                         'prelabel' => __('&laquo; Previous Page'),
2225                         'nxtlabel' => __('Next Page &raquo;'),
2226                 );
2227                 $args = wp_parse_args( $args, $defaults );
2228
2229                 $max_num_pages = $wp_query->max_num_pages;
2230                 $paged = get_query_var('paged');
2231
2232                 //only have sep if there's both prev and next results
2233                 if ($paged < 2 || $paged >= $max_num_pages) {
2234                         $args['sep'] = '';
2235                 }
2236
2237                 if ( $max_num_pages > 1 ) {
2238                         $return = get_previous_posts_link($args['prelabel']);
2239                         $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $args['sep']);
2240                         $return .= get_next_posts_link($args['nxtlabel']);
2241                 }
2242         }
2243         return $return;
2244
2245 }
2246
2247 /**
2248  * Display post pages link navigation for previous and next pages.
2249  *
2250  * @since 0.71
2251  *
2252  * @param string $sep      Optional. Separator for posts navigation links.
2253  * @param string $prelabel Optional. Label for previous pages.
2254  * @param string $nxtlabel Optional Label for next pages.
2255  */
2256 function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
2257         $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
2258         echo get_posts_nav_link($args);
2259 }
2260
2261 /**
2262  * Return navigation to next/previous post when applicable.
2263  *
2264  * @since 4.1.0
2265  *
2266  * @param array $args {
2267  *     Optional. Default post navigation arguments. Default empty array.
2268  *
2269  *     @type string $prev_text          Anchor text to display in the previous post link. Default `%title`.
2270  *     @type string $next_text          Anchor text to display in the next post link. Default `%title`.
2271  *     @type string $screen_reader_text Screen reader text for nav element. Default 'Post navigation'.
2272  * }
2273  * @return string Markup for post links.
2274  */
2275 function get_the_post_navigation( $args = array() ) {
2276         $args = wp_parse_args( $args, array(
2277                 'prev_text'          => '%title',
2278                 'next_text'          => '%title',
2279                 'screen_reader_text' => __( 'Post navigation' ),
2280         ) );
2281
2282         $navigation = '';
2283         $previous   = get_previous_post_link( '<div class="nav-previous">%link</div>', $args['prev_text'] );
2284         $next       = get_next_post_link( '<div class="nav-next">%link</div>', $args['next_text'] );
2285
2286         // Only add markup if there's somewhere to navigate to.
2287         if ( $previous || $next ) {
2288                 $navigation = _navigation_markup( $previous . $next, 'post-navigation', $args['screen_reader_text'] );
2289         }
2290
2291         return $navigation;
2292 }
2293
2294 /**
2295  * Display navigation to next/previous post when applicable.
2296  *
2297  * @since 4.1.0
2298  *
2299  * @param array $args Optional. See {@see get_the_post_navigation()} for available
2300  *                    arguments. Default empty array.
2301  */
2302 function the_post_navigation( $args = array() ) {
2303         echo get_the_post_navigation( $args );
2304 }
2305
2306 /**
2307  * Return navigation to next/previous set of posts when applicable.
2308  *
2309  * @since 4.1.0
2310  *
2311  * @global WP_Query $wp_query WordPress Query object.
2312  *
2313  * @param array $args {
2314  *     Optional. Default posts navigation arguments. Default empty array.
2315  *
2316  *     @type string $prev_text          Anchor text to display in the previous posts link.
2317  *                                      Default 'Older posts'.
2318  *     @type string $next_text          Anchor text to display in the next posts link.
2319  *                                      Default 'Newer posts'.
2320  *     @type string $screen_reader_text Screen reader text for nav element.
2321  *                                      Default 'Posts navigation'.
2322  * }
2323  * @return string Markup for posts links.
2324  */
2325 function get_the_posts_navigation( $args = array() ) {
2326         $navigation = '';
2327
2328         // Don't print empty markup if there's only one page.
2329         if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2330                 $args = wp_parse_args( $args, array(
2331                         'prev_text'          => __( 'Older posts' ),
2332                         'next_text'          => __( 'Newer posts' ),
2333                         'screen_reader_text' => __( 'Posts navigation' ),
2334                 ) );
2335
2336                 $next_link = get_previous_posts_link( $args['next_text'] );
2337                 $prev_link = get_next_posts_link( $args['prev_text'] );
2338
2339                 if ( $prev_link ) {
2340                         $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
2341                 }
2342
2343                 if ( $next_link ) {
2344                         $navigation .= '<div class="nav-next">' . $next_link . '</div>';
2345                 }
2346
2347                 $navigation = _navigation_markup( $navigation, 'posts-navigation', $args['screen_reader_text'] );
2348         }
2349
2350         return $navigation;
2351 }
2352
2353 /**
2354  * Display navigation to next/previous set of posts when applicable.
2355  *
2356  * @since 4.1.0
2357  *
2358  * @param array $args Optional. See {@see get_the_posts_navigation()} for available
2359  *                    arguments. Default empty array.
2360  */
2361 function the_posts_navigation( $args = array() ) {
2362         echo get_the_posts_navigation( $args );
2363 }
2364
2365 /**
2366  * Return a paginated navigation to next/previous set of posts,
2367  * when applicable.
2368  *
2369  * @since 4.1.0
2370  *
2371  * @param array $args {
2372  *     Optional. Default pagination arguments, {@see paginate_links()}.
2373  *
2374  *     @type string $screen_reader_text Screen reader text for navigation element.
2375  *                                      Default 'Posts navigation'.
2376  * }
2377  * @return string Markup for pagination links.
2378  */
2379 function get_the_posts_pagination( $args = array() ) {
2380         $navigation = '';
2381
2382         // Don't print empty markup if there's only one page.
2383         if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2384                 $args = wp_parse_args( $args, array(
2385                         'mid_size'           => 1,
2386                         'prev_text'          => _x( 'Previous', 'previous post' ),
2387                         'next_text'          => _x( 'Next', 'next post' ),
2388                         'screen_reader_text' => __( 'Posts navigation' ),
2389                 ) );
2390
2391                 // Make sure we get a string back. Plain is the next best thing.
2392                 if ( isset( $args['type'] ) && 'array' == $args['type'] ) {
2393                         $args['type'] = 'plain';
2394                 }
2395
2396                 // Set up paginated links.
2397                 $links = paginate_links( $args );
2398
2399                 if ( $links ) {
2400                         $navigation = _navigation_markup( $links, 'pagination', $args['screen_reader_text'] );
2401                 }
2402         }
2403
2404         return $navigation;
2405 }
2406
2407 /**
2408  * Display a paginated navigation to next/previous set of posts,
2409  * when applicable.
2410  *
2411  * @since 4.1.0
2412  *
2413  * @param array $args Optional. See {@see get_the_posts_pagination()} for available arguments.
2414  *                    Default empty array.
2415  */
2416 function the_posts_pagination( $args = array() ) {
2417         echo get_the_posts_pagination( $args );
2418 }
2419
2420 /**
2421  * Wraps passed links in navigational markup.
2422  *
2423  * @since 4.1.0
2424  * @access private
2425  *
2426  * @param string $links              Navigational links.
2427  * @param string $class              Optional. Custom class for nav element. Default: 'posts-navigation'.
2428  * @param string $screen_reader_text Optional. Screen reader text for nav element. Default: 'Posts navigation'.
2429  * @return string Navigation template tag.
2430  */
2431 function _navigation_markup( $links, $class = 'posts-navigation', $screen_reader_text = '' ) {
2432         if ( empty( $screen_reader_text ) ) {
2433                 $screen_reader_text = __( 'Posts navigation' );
2434         }
2435
2436         $template = '
2437         <nav class="navigation %1$s" role="navigation">
2438                 <h2 class="screen-reader-text">%2$s</h2>
2439                 <div class="nav-links">%3$s</div>
2440         </nav>';
2441
2442         return sprintf( $template, sanitize_html_class( $class ), esc_html( $screen_reader_text ), $links );
2443 }
2444
2445 /**
2446  * Retrieve comments page number link.
2447  *
2448  * @since 2.7.0
2449  *
2450  * @global WP_Rewrite $wp_rewrite
2451  *
2452  * @param int $pagenum  Optional. Page number.
2453  * @param int $max_page Optional. The maximum number of comment pages.
2454  * @return string The comments page number link URL.
2455  */
2456 function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
2457         global $wp_rewrite;
2458
2459         $pagenum = (int) $pagenum;
2460
2461         $result = get_permalink();
2462
2463         if ( 'newest' == get_option('default_comments_page') ) {
2464                 if ( $pagenum != $max_page ) {
2465                         if ( $wp_rewrite->using_permalinks() )
2466                                 $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged');
2467                         else
2468                                 $result = add_query_arg( 'cpage', $pagenum, $result );
2469                 }
2470         } elseif ( $pagenum > 1 ) {
2471                 if ( $wp_rewrite->using_permalinks() )
2472                         $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged');
2473                 else
2474                         $result = add_query_arg( 'cpage', $pagenum, $result );
2475         }
2476
2477         $result .= '#comments';
2478
2479         /**
2480          * Filter the comments page number link for the current request.
2481          *
2482          * @since 2.7.0
2483          *
2484          * @param string $result The comments page number link.
2485          */
2486         return apply_filters( 'get_comments_pagenum_link', $result );
2487 }
2488
2489 /**
2490  * Return the link to next comments page.
2491  *
2492  * @since 2.7.1
2493  *
2494  * @global WP_Query $wp_query
2495  *
2496  * @param string $label    Optional. Label for link text.
2497  * @param int    $max_page Optional. Max page.
2498  * @return string|void HTML-formatted link for the next page of comments.
2499  */
2500 function get_next_comments_link( $label = '', $max_page = 0 ) {
2501         global $wp_query;
2502
2503         if ( !is_singular() || !get_option('page_comments') )
2504                 return;
2505
2506         $page = get_query_var('cpage');
2507
2508         if ( ! $page ) {
2509                 $page = 1;
2510         }
2511
2512         $nextpage = intval($page) + 1;
2513
2514         if ( empty($max_page) )
2515                 $max_page = $wp_query->max_num_comment_pages;
2516
2517         if ( empty($max_page) )
2518                 $max_page = get_comment_pages_count();
2519
2520         if ( $nextpage > $max_page )
2521                 return;
2522
2523         if ( empty($label) )
2524                 $label = __('Newer Comments &raquo;');
2525
2526         /**
2527          * Filter the anchor tag attributes for the next comments page link.
2528          *
2529          * @since 2.7.0
2530          *
2531          * @param string $attributes Attributes for the anchor tag.
2532          */
2533         return '<a href="' . esc_url( get_comments_pagenum_link( $nextpage, $max_page ) ) . '" ' . apply_filters( 'next_comments_link_attributes', '' ) . '>'. preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) .'</a>';
2534 }
2535
2536 /**
2537  * Display the link to next comments page.
2538  *
2539  * @since 2.7.0
2540  *
2541  * @param string $label    Optional. Label for link text.
2542  * @param int    $max_page Optional. Max page.
2543  */
2544 function next_comments_link( $label = '', $max_page = 0 ) {
2545         echo get_next_comments_link( $label, $max_page );
2546 }
2547
2548 /**
2549  * Return the previous comments page link.
2550  *
2551  * @since 2.7.1
2552  *
2553  * @param string $label Optional. Label for comments link text.
2554  * @return string|void HTML-formatted link for the previous page of comments.
2555  */
2556 function get_previous_comments_link( $label = '' ) {
2557         if ( !is_singular() || !get_option('page_comments') )
2558                 return;
2559
2560         $page = get_query_var('cpage');
2561
2562         if ( intval($page) <= 1 )
2563                 return;
2564
2565         $prevpage = intval($page) - 1;
2566
2567         if ( empty($label) )
2568                 $label = __('&laquo; Older Comments');
2569
2570         /**
2571          * Filter the anchor tag attributes for the previous comments page link.
2572          *
2573          * @since 2.7.0
2574          *
2575          * @param string $attributes Attributes for the anchor tag.
2576          */
2577         return '<a href="' . esc_url( get_comments_pagenum_link( $prevpage ) ) . '" ' . apply_filters( 'previous_comments_link_attributes', '' ) . '>' . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) .'</a>';
2578 }
2579
2580 /**
2581  * Display the previous comments page link.
2582  *
2583  * @since 2.7.0
2584  *
2585  * @param string $label Optional. Label for comments link text.
2586  */
2587 function previous_comments_link( $label = '' ) {
2588         echo get_previous_comments_link( $label );
2589 }
2590
2591 /**
2592  * Create pagination links for the comments on the current post.
2593  *
2594  * @see paginate_links()
2595  * @since 2.7.0
2596  *
2597  * @global WP_Rewrite $wp_rewrite
2598  *
2599  * @param string|array $args Optional args. See paginate_links().
2600  * @return string|void Markup for pagination links.
2601 */
2602 function paginate_comments_links($args = array()) {
2603         global $wp_rewrite;
2604
2605         if ( !is_singular() || !get_option('page_comments') )
2606                 return;
2607
2608         $page = get_query_var('cpage');
2609         if ( !$page )
2610                 $page = 1;
2611         $max_page = get_comment_pages_count();
2612         $defaults = array(
2613                 'base' => add_query_arg( 'cpage', '%#%' ),
2614                 'format' => '',
2615                 'total' => $max_page,
2616                 'current' => $page,
2617                 'echo' => true,
2618                 'add_fragment' => '#comments'
2619         );
2620         if ( $wp_rewrite->using_permalinks() )
2621                 $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged');
2622
2623         $args = wp_parse_args( $args, $defaults );
2624         $page_links = paginate_links( $args );
2625
2626         if ( $args['echo'] )
2627                 echo $page_links;
2628         else
2629                 return $page_links;
2630 }
2631
2632 /**
2633  * Retrieve the Press This bookmarklet link.
2634  *
2635  * Use this in 'a' element 'href' attribute.
2636  *
2637  * @since 2.6.0
2638  *
2639  * @global bool          $is_IE
2640  * @global string        $wp_version
2641  * @global WP_Press_This $wp_press_this
2642  *
2643  * @return string The Press This bookmarklet link URL.
2644  */
2645 function get_shortcut_link() {
2646         global $is_IE, $wp_version;
2647
2648         include_once( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
2649         $bookmarklet_version = $GLOBALS['wp_press_this']->version;
2650         $link = '';
2651
2652         if ( $is_IE ) {
2653                 /**
2654                  * Return the old/shorter bookmarklet code for MSIE 8 and lower,
2655                  * since they only support a max length of ~2000 characters for
2656                  * bookmark[let] URLs, which is way to small for our smarter one.
2657                  * Do update the version number so users do not get the "upgrade your
2658                  * bookmarklet" notice when using PT in those browsers.
2659                  */
2660                 $ua = $_SERVER['HTTP_USER_AGENT'];
2661
2662                 if ( ! empty( $ua ) && preg_match( '/\bMSIE (\d)/', $ua, $matches ) && (int) $matches[1] <= 8 ) {
2663                         $url = wp_json_encode( admin_url( 'press-this.php' ) );
2664
2665                         $link = 'javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,' .
2666                                 's=(e?e():(k)?k():(x?x.createRange().text:0)),f=' . $url . ',l=d.location,e=encodeURIComponent,' .
2667                                 'u=f+"?u="+e(l.href)+"&t="+e(d.title)+"&s="+e(s)+"&v=' . $bookmarklet_version . '";' .
2668                                 'a=function(){if(!w.open(u,"t","toolbar=0,resizable=1,scrollbars=1,status=1,width=600,height=700"))l.href=u;};' .
2669                                 'if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else a();void(0)';
2670                 }
2671         }
2672
2673         if ( empty( $link ) ) {
2674                 $src = @file_get_contents( ABSPATH . 'wp-admin/js/bookmarklet.min.js' );
2675
2676                 if ( $src ) {
2677                         $url = wp_json_encode( admin_url( 'press-this.php' ) . '?v=' . $bookmarklet_version );
2678                         $link = 'javascript:' . str_replace( 'window.pt_url', $url, $src );
2679                 }
2680         }
2681
2682         $link = str_replace( array( "\r", "\n", "\t" ),  '', $link );
2683
2684         /**
2685          * Filter the Press This bookmarklet link.
2686          *
2687          * @since 2.6.0
2688          *
2689          * @param string $link The Press This bookmarklet link.
2690          */
2691         return apply_filters( 'shortcut_link', $link );
2692 }
2693
2694 /**
2695  * Retrieve the home url for the current site.
2696  *
2697  * Returns the 'home' option with the appropriate protocol, 'https' if
2698  * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
2699  * `is_ssl()` is overridden.
2700  *
2701  * @since 3.0.0
2702  *
2703  * @param  string $path   Optional. Path relative to the home url. Default empty.
2704  * @param  string $scheme Optional. Scheme to give the home url context. Accepts
2705  *                        'http', 'https', or 'relative'. Default null.
2706  * @return string Home url link with optional path appended.
2707 */
2708 function home_url( $path = '', $scheme = null ) {
2709         return get_home_url( null, $path, $scheme );
2710 }
2711
2712 /**
2713  * Retrieve the home url for a given site.
2714  *
2715  * Returns the 'home' option with the appropriate protocol, 'https' if
2716  * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
2717  * `is_ssl()` is
2718  * overridden.
2719  *
2720  * @since 3.0.0
2721  *
2722  * @global string $pagenow
2723  *
2724  * @param  int         $blog_id     Optional. Blog ID. Default null (current blog).
2725  * @param  string      $path        Optional. Path relative to the home URL. Default empty.
2726  * @param  string|null $orig_scheme Optional. Scheme to give the home URL context. Accepts
2727  *                                  'http', 'https', 'relative', or null. Default null.
2728  * @return string Home URL link with optional path appended.
2729 */
2730 function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
2731         $orig_scheme = $scheme;
2732
2733         if ( empty( $blog_id ) || !is_multisite() ) {
2734                 $url = get_option( 'home' );
2735         } else {
2736                 switch_to_blog( $blog_id );
2737                 $url = get_option( 'home' );
2738                 restore_current_blog();
2739         }
2740
2741         if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
2742                 if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] )
2743                         $scheme = 'https';
2744                 else
2745                         $scheme = parse_url( $url, PHP_URL_SCHEME );
2746         }
2747
2748         $url = set_url_scheme( $url, $scheme );
2749
2750         if ( $path && is_string( $path ) )
2751                 $url .= '/' . ltrim( $path, '/' );
2752
2753         /**
2754          * Filter the home URL.
2755          *
2756          * @since 3.0.0
2757          *
2758          * @param string      $url         The complete home URL including scheme and path.
2759          * @param string      $path        Path relative to the home URL. Blank string if no path is specified.
2760          * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https', 'relative' or null.
2761          * @param int|null    $blog_id     Blog ID, or null for the current blog.
2762          */
2763         return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
2764 }
2765
2766 /**
2767  * Retrieve the site url for the current site.
2768  *
2769  * Returns the 'site_url' option with the appropriate protocol, 'https' if
2770  * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2771  * overridden.
2772  *
2773  * @since 3.0.0
2774  *
2775  * @param string $path   Optional. Path relative to the site url.
2776  * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
2777  * @return string Site url link with optional path appended.
2778 */
2779 function site_url( $path = '', $scheme = null ) {
2780         return get_site_url( null, $path, $scheme );
2781 }
2782
2783 /**
2784  * Retrieve the site url for a given site.
2785  *
2786  * Returns the 'site_url' option with the appropriate protocol, 'https' if
2787  * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
2788  * `is_ssl()` is overridden.
2789  *
2790  * @since 3.0.0
2791  *
2792  * @param int    $blog_id Optional. Blog ID. Default null (current site).
2793  * @param string $path    Optional. Path relative to the site url. Default empty.
2794  * @param string $scheme  Optional. Scheme to give the site url context. Accepts
2795  *                        'http', 'https', 'login', 'login_post', 'admin', or
2796  *                        'relative'. Default null.
2797  * @return string Site url link with optional path appended.
2798 */
2799 function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
2800         if ( empty( $blog_id ) || !is_multisite() ) {
2801                 $url = get_option( 'siteurl' );
2802         } else {
2803                 switch_to_blog( $blog_id );
2804                 $url = get_option( 'siteurl' );
2805                 restore_current_blog();
2806         }
2807
2808         $url = set_url_scheme( $url, $scheme );
2809
2810         if ( $path && is_string( $path ) )
2811                 $url .= '/' . ltrim( $path, '/' );
2812
2813         /**
2814          * Filter the site URL.
2815          *
2816          * @since 2.7.0
2817          *
2818          * @param string      $url     The complete site URL including scheme and path.
2819          * @param string      $path    Path relative to the site URL. Blank string if no path is specified.
2820          * @param string|null $scheme  Scheme to give the site URL context. Accepts 'http', 'https', 'login',
2821          *                             'login_post', 'admin', 'relative' or null.
2822          * @param int|null    $blog_id Blog ID, or null for the current blog.
2823          */
2824         return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
2825 }
2826
2827 /**
2828  * Retrieve the url to the admin area for the current site.
2829  *
2830  * @since 2.6.0
2831  *
2832  * @param string $path   Optional path relative to the admin url.
2833  * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
2834  * @return string Admin url link with optional path appended.
2835 */
2836 function admin_url( $path = '', $scheme = 'admin' ) {
2837         return get_admin_url( null, $path, $scheme );
2838 }
2839
2840 /**
2841  * Retrieves the url to the admin area for a given site.
2842  *
2843  * @since 3.0.0
2844  *
2845  * @param int    $blog_id Optional. Blog ID. Default null (current site).
2846  * @param string $path    Optional. Path relative to the admin url. Default empty.
2847  * @param string $scheme  Optional. The scheme to use. Accepts 'http' or 'https',
2848  *                        to force those schemes. Default 'admin', which obeys
2849  *                        {@see force_ssl_admin()} and {@see is_ssl()}.
2850  * @return string Admin url link with optional path appended.
2851 */
2852 function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
2853         $url = get_site_url($blog_id, 'wp-admin/', $scheme);
2854
2855         if ( $path && is_string( $path ) )
2856                 $url .= ltrim( $path, '/' );
2857
2858         /**
2859          * Filter the admin area URL.
2860          *
2861          * @since 2.8.0
2862          *
2863          * @param string   $url     The complete admin area URL including scheme and path.
2864          * @param string   $path    Path relative to the admin area URL. Blank string if no path is specified.
2865          * @param int|null $blog_id Blog ID, or null for the current blog.
2866          */
2867         return apply_filters( 'admin_url', $url, $path, $blog_id );
2868 }
2869
2870 /**
2871  * Retrieve the url to the includes directory.
2872  *
2873  * @since 2.6.0
2874  *
2875  * @param string $path   Optional. Path relative to the includes url.
2876  * @param string $scheme Optional. Scheme to give the includes url context.
2877  * @return string Includes url link with optional path appended.
2878 */
2879 function includes_url( $path = '', $scheme = null ) {
2880         $url = site_url( '/' . WPINC . '/', $scheme );
2881
2882         if ( $path && is_string( $path ) )
2883                 $url .= ltrim($path, '/');
2884
2885         /**
2886          * Filter the URL to the includes directory.
2887          *
2888          * @since 2.8.0
2889          *
2890          * @param string $url  The complete URL to the includes directory including scheme and path.
2891          * @param string $path Path relative to the URL to the wp-includes directory. Blank string
2892          *                     if no path is specified.
2893          */
2894         return apply_filters( 'includes_url', $url, $path );
2895 }
2896
2897 /**
2898  * Retrieve the url to the content directory.
2899  *
2900  * @since 2.6.0
2901  *
2902  * @param string $path Optional. Path relative to the content url.
2903  * @return string Content url link with optional path appended.
2904 */
2905 function content_url($path = '') {
2906         $url = set_url_scheme( WP_CONTENT_URL );
2907
2908         if ( $path && is_string( $path ) )
2909                 $url .= '/' . ltrim($path, '/');
2910
2911         /**
2912          * Filter the URL to the content directory.
2913          *
2914          * @since 2.8.0
2915          *
2916          * @param string $url  The complete URL to the content directory including scheme and path.
2917          * @param string $path Path relative to the URL to the content directory. Blank string
2918          *                     if no path is specified.
2919          */
2920         return apply_filters( 'content_url', $url, $path);
2921 }
2922
2923 /**
2924  * Retrieve a URL within the plugins or mu-plugins directory.
2925  *
2926  * Defaults to the plugins directory URL if no arguments are supplied.
2927  *
2928  * @since 2.6.0
2929  *
2930  * @param  string $path   Optional. Extra path appended to the end of the URL, including
2931  *                        the relative directory if $plugin is supplied. Default empty.
2932  * @param  string $plugin Optional. A full path to a file inside a plugin or mu-plugin.
2933  *                        The URL will be relative to its directory. Default empty.
2934  *                        Typically this is done by passing `__FILE__` as the argument.
2935  * @return string Plugins URL link with optional paths appended.
2936 */
2937 function plugins_url( $path = '', $plugin = '' ) {
2938
2939         $path = wp_normalize_path( $path );
2940         $plugin = wp_normalize_path( $plugin );
2941         $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
2942
2943         if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) )
2944                 $url = WPMU_PLUGIN_URL;
2945         else
2946                 $url = WP_PLUGIN_URL;
2947
2948
2949         $url = set_url_scheme( $url );
2950
2951         if ( !empty($plugin) && is_string($plugin) ) {
2952                 $folder = dirname(plugin_basename($plugin));
2953                 if ( '.' != $folder )
2954                         $url .= '/' . ltrim($folder, '/');
2955         }
2956
2957         if ( $path && is_string( $path ) )
2958                 $url .= '/' . ltrim($path, '/');
2959
2960         /**
2961          * Filter the URL to the plugins directory.
2962          *
2963          * @since 2.8.0
2964          *
2965          * @param string $url    The complete URL to the plugins directory including scheme and path.
2966          * @param string $path   Path relative to the URL to the plugins directory. Blank string
2967          *                       if no path is specified.
2968          * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
2969          *                       is specified.
2970          */
2971         return apply_filters( 'plugins_url', $url, $path, $plugin );
2972 }
2973
2974 /**
2975  * Retrieve the site url for the current network.
2976  *
2977  * Returns the site url with the appropriate protocol, 'https' if
2978  * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
2979  * overridden.
2980  *
2981  * @since 3.0.0
2982  *
2983  * @param string $path   Optional. Path relative to the site url.
2984  * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
2985  * @return string Site url link with optional path appended.
2986 */
2987 function network_site_url( $path = '', $scheme = null ) {
2988         if ( ! is_multisite() )
2989                 return site_url($path, $scheme);
2990
2991         $current_site = get_current_site();
2992
2993         if ( 'relative' == $scheme )
2994                 $url = $current_site->path;
2995         else
2996                 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
2997
2998         if ( $path && is_string( $path ) )
2999                 $url .= ltrim( $path, '/' );
3000
3001         /**
3002          * Filter the network site URL.
3003          *
3004          * @since 3.0.0
3005          *
3006          * @param string      $url    The complete network site URL including scheme and path.
3007          * @param string      $path   Path relative to the network site URL. Blank string if
3008          *                            no path is specified.
3009          * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',
3010          *                            'relative' or null.
3011          */
3012         return apply_filters( 'network_site_url', $url, $path, $scheme );
3013 }
3014
3015 /**
3016  * Retrieves the home url for the current network.
3017  *
3018  * Returns the home url with the appropriate protocol, 'https' {@see is_ssl()}
3019  * and 'http' otherwise. If `$scheme` is 'http' or 'https', `is_ssl()` is
3020  * overridden.
3021  *
3022  * @since 3.0.0
3023  *
3024  * @param  string $path   Optional. Path relative to the home url. Default empty.
3025  * @param  string $scheme Optional. Scheme to give the home url context. Accepts
3026  *                        'http', 'https', or 'relative'. Default null.
3027  * @return string Home url link with optional path appended.
3028 */
3029 function network_home_url( $path = '', $scheme = null ) {
3030         if ( ! is_multisite() )
3031                 return home_url($path, $scheme);
3032
3033         $current_site = get_current_site();
3034         $orig_scheme = $scheme;
3035
3036         if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
3037                 $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
3038
3039         if ( 'relative' == $scheme )
3040                 $url = $current_site->path;
3041         else
3042                 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
3043
3044         if ( $path && is_string( $path ) )
3045                 $url .= ltrim( $path, '/' );
3046
3047         /**
3048          * Filter the network home URL.
3049          *
3050          * @since 3.0.0
3051          *
3052          * @param string      $url         The complete network home URL including scheme and path.
3053          * @param string      $path        Path relative to the network home URL. Blank string
3054          *                                 if no path is specified.
3055          * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',
3056          *                                 'relative' or null.
3057          */
3058         return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
3059 }
3060
3061 /**
3062  * Retrieve the url to the admin area for the network.
3063  *
3064  * @since 3.0.0
3065  *
3066  * @param string $path   Optional path relative to the admin url.
3067  * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3068  * @return string Admin url link with optional path appended.
3069 */
3070 function network_admin_url( $path = '', $scheme = 'admin' ) {
3071         if ( ! is_multisite() )
3072                 return admin_url( $path, $scheme );
3073
3074         $url = network_site_url('wp-admin/network/', $scheme);
3075
3076         if ( $path && is_string( $path ) )
3077                 $url .= ltrim($path, '/');
3078
3079         /**
3080          * Filter the network admin URL.
3081          *
3082          * @since 3.0.0
3083          *
3084          * @param string $url  The complete network admin URL including scheme and path.
3085          * @param string $path Path relative to the network admin URL. Blank string if
3086          *                     no path is specified.
3087          */
3088         return apply_filters( 'network_admin_url', $url, $path );
3089 }
3090
3091 /**
3092  * Retrieve the url to the admin area for the current user.
3093  *
3094  * @since 3.0.0
3095  *
3096  * @param string $path   Optional path relative to the admin url.
3097  * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3098  * @return string Admin url link with optional path appended.
3099 */
3100 function user_admin_url( $path = '', $scheme = 'admin' ) {
3101         $url = network_site_url('wp-admin/user/', $scheme);
3102
3103         if ( $path && is_string( $path ) )
3104                 $url .= ltrim($path, '/');
3105
3106         /**
3107          * Filter the user admin URL for the current user.
3108          *
3109          * @since 3.1.0
3110          *
3111          * @param string $url  The complete URL including scheme and path.
3112          * @param string $path Path relative to the URL. Blank string if
3113          *                     no path is specified.
3114          */
3115         return apply_filters( 'user_admin_url', $url, $path );
3116 }
3117
3118 /**
3119  * Retrieve the url to the admin area for either the current blog or the network depending on context.
3120  *
3121  * @since 3.1.0
3122  *
3123  * @param string $path   Optional path relative to the admin url.
3124  * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3125  * @return string Admin url link with optional path appended.
3126 */
3127 function self_admin_url($path = '', $scheme = 'admin') {
3128         if ( is_network_admin() )
3129                 return network_admin_url($path, $scheme);
3130         elseif ( is_user_admin() )
3131                 return user_admin_url($path, $scheme);
3132         else
3133                 return admin_url($path, $scheme);
3134 }
3135
3136 /**
3137  * Set the scheme for a URL
3138  *
3139  * @since 3.4.0
3140  *
3141  * @param string $url    Absolute url that includes a scheme
3142  * @param string $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
3143  * @return string $url URL with chosen scheme.
3144  */
3145 function set_url_scheme( $url, $scheme = null ) {
3146         $orig_scheme = $scheme;
3147
3148         if ( ! $scheme ) {
3149                 $scheme = is_ssl() ? 'https' : 'http';
3150         } elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) {
3151                 $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
3152         } elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) {
3153                 $scheme = is_ssl() ? 'https' : 'http';
3154         }
3155
3156         $url = trim( $url );
3157         if ( substr( $url, 0, 2 ) === '//' )
3158                 $url = 'http:' . $url;
3159
3160         if ( 'relative' == $scheme ) {
3161                 $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
3162                 if ( $url !== '' && $url[0] === '/' )
3163                         $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" );
3164         } else {
3165                 $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
3166         }
3167
3168         /**
3169          * Filter the resulting URL after setting the scheme.
3170          *
3171          * @since 3.4.0
3172          *
3173          * @param string $url         The complete URL including scheme and path.
3174          * @param string $scheme      Scheme applied to the URL. One of 'http', 'https', or 'relative'.
3175          * @param string $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
3176          *                            'login_post', 'admin', 'rpc', or 'relative'.
3177          */
3178         return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
3179 }
3180
3181 /**
3182  * Get the URL to the user's dashboard.
3183  *
3184  * If a user does not belong to any site, the global user dashboard is used. If the user belongs to the current site,
3185  * the dashboard for the current site is returned. If the user cannot edit the current site, the dashboard to the user's
3186  * primary blog is returned.
3187  *
3188  * @since 3.1.0
3189  *
3190  * @param int    $user_id Optional. User ID. Defaults to current user.
3191  * @param string $path    Optional path relative to the dashboard. Use only paths known to both blog and user admins.
3192  * @param string $scheme  The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3193  * @return string Dashboard url link with optional path appended.
3194  */
3195 function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
3196         $user_id = $user_id ? (int) $user_id : get_current_user_id();
3197
3198         $blogs = get_blogs_of_user( $user_id );
3199         if ( ! is_super_admin() && empty($blogs) ) {
3200                 $url = user_admin_url( $path, $scheme );
3201         } elseif ( ! is_multisite() ) {
3202                 $url = admin_url( $path, $scheme );
3203         } else {
3204                 $current_blog = get_current_blog_id();
3205                 if ( $current_blog  && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
3206                         $url = admin_url( $path, $scheme );
3207                 } else {
3208                         $active = get_active_blog_for_user( $user_id );
3209                         if ( $active )
3210                                 $url = get_admin_url( $active->blog_id, $path, $scheme );
3211                         else
3212                                 $url = user_admin_url( $path, $scheme );
3213                 }
3214         }
3215
3216         /**
3217          * Filter the dashboard URL for a user.
3218          *
3219          * @since 3.1.0
3220          *
3221          * @param string $url     The complete URL including scheme and path.
3222          * @param int    $user_id The user ID.
3223          * @param string $path    Path relative to the URL. Blank string if no path is specified.
3224          * @param string $scheme  Scheme to give the URL context. Accepts 'http', 'https', 'login',
3225          *                        'login_post', 'admin', 'relative' or null.
3226          */
3227         return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
3228 }
3229
3230 /**
3231  * Get the URL to the user's profile editor.
3232  *
3233  * @since 3.1.0
3234  *
3235  * @param int    $user_id Optional. User ID. Defaults to current user.
3236  * @param string $scheme  The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
3237  *                        'http' or 'https' can be passed to force those schemes.
3238  * @return string Dashboard url link with optional path appended.
3239  */
3240 function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) {
3241         $user_id = $user_id ? (int) $user_id : get_current_user_id();
3242
3243         if ( is_user_admin() )
3244                 $url = user_admin_url( 'profile.php', $scheme );
3245         elseif ( is_network_admin() )
3246                 $url = network_admin_url( 'profile.php', $scheme );
3247         else
3248                 $url = get_dashboard_url( $user_id, 'profile.php', $scheme );
3249
3250         /**
3251          * Filter the URL for a user's profile editor.
3252          *
3253          * @since 3.1.0
3254          *
3255          * @param string $url     The complete URL including scheme and path.
3256          * @param int    $user_id The user ID.
3257          * @param string $scheme  Scheme to give the URL context. Accepts 'http', 'https', 'login',
3258          *                        'login_post', 'admin', 'relative' or null.
3259          */
3260         return apply_filters( 'edit_profile_url', $url, $user_id, $scheme);
3261 }
3262
3263 /**
3264  * Output rel=canonical for singular queries.
3265  *
3266  * @since 2.9.0
3267  *
3268  * @global WP_Query $wp_the_query
3269 */
3270 function rel_canonical() {
3271         if ( !is_singular() )
3272                 return;
3273
3274         global $wp_the_query;
3275         if ( !$id = $wp_the_query->get_queried_object_id() )
3276                 return;
3277
3278         $link = get_permalink( $id );
3279
3280         if ( $page = get_query_var('cpage') )
3281                 $link = get_comments_pagenum_link( $page );
3282
3283         echo "<link rel='canonical' href='$link' />\n";
3284 }
3285
3286 /**
3287  * Return a shortlink for a post, page, attachment, or blog.
3288  *
3289  * This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to
3290  * provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts.
3291  * Plugins can short-circuit this function via the pre_get_shortlink filter or filter the output
3292  * via the get_shortlink filter.
3293  *
3294  * @since 3.0.0.
3295  *
3296  * @global WP_Query $wp_query
3297  *
3298  * @param int    $id          A post or blog id. Default is 0, which means the current post or blog.
3299  * @param string $context     Whether the id is a 'blog' id, 'post' id, or 'media' id.
3300  *                            If 'post', the post_type of the post is consulted.
3301  *                            If 'query', the current query is consulted to determine the id and context.
3302  *                            Default is 'post'.
3303  * @param bool   $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
3304  * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
3305  */
3306 function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
3307         /**
3308          * Filter whether to preempt generating a shortlink for the given post.
3309          *
3310          * Passing a truthy value to the filter will effectively short-circuit the
3311          * shortlink-generation process, returning that value instead.
3312          *
3313          * @since 3.0.0
3314          *
3315          * @param bool|string $return      Short-circuit return value. Either false or a URL string.
3316          * @param int         $id          Post ID, or 0 for the current post.
3317          * @param string      $context     The context for the link. One of 'post' or 'query',
3318          * @param bool        $allow_slugs Whether to allow post slugs in the shortlink.
3319          */
3320         $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );
3321
3322         if ( false !== $shortlink )
3323                 return $shortlink;
3324
3325         global $wp_query;
3326         $post_id = 0;
3327         if ( 'query' == $context && is_singular() ) {
3328                 $post_id = $wp_query->get_queried_object_id();
3329                 $post = get_post( $post_id );
3330         } elseif ( 'post' == $context ) {
3331                 $post = get_post( $id );
3332                 if ( ! empty( $post->ID ) )
3333                         $post_id = $post->ID;
3334         }
3335
3336         $shortlink = '';
3337
3338         // Return p= link for all public post types.
3339         if ( ! empty( $post_id ) ) {
3340                 $post_type = get_post_type_object( $post->post_type );
3341
3342                 if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) {
3343                         $shortlink = home_url( '/' );
3344                 } elseif ( $post_type->public ) {
3345                         $shortlink = home_url( '?p=' . $post_id );
3346                 }
3347         }
3348
3349         /**
3350          * Filter the shortlink for a post.
3351          *
3352          * @since 3.0.0
3353          *
3354          * @param string $shortlink   Shortlink URL.
3355          * @param int    $id          Post ID, or 0 for the current post.
3356          * @param string $context     The context for the link. One of 'post' or 'query',
3357          * @param bool   $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
3358          */
3359         return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
3360 }
3361
3362 /**
3363  *  Inject rel=shortlink into head if a shortlink is defined for the current page.
3364  *
3365  *  Attached to the wp_head action.
3366  *
3367  * @since 3.0.0
3368  */
3369 function wp_shortlink_wp_head() {
3370         $shortlink = wp_get_shortlink( 0, 'query' );
3371
3372         if ( empty( $shortlink ) )
3373                 return;
3374
3375         echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
3376 }
3377
3378 /**
3379  * Send a Link: rel=shortlink header if a shortlink is defined for the current page.
3380  *
3381  * Attached to the wp action.
3382  *
3383  * @since 3.0.0
3384  */
3385 function wp_shortlink_header() {
3386         if ( headers_sent() )
3387                 return;
3388
3389         $shortlink = wp_get_shortlink(0, 'query');
3390
3391         if ( empty($shortlink) )
3392                 return;
3393
3394         header('Link: <' . $shortlink . '>; rel=shortlink', false);
3395 }
3396
3397 /**
3398  * Display the Short Link for a Post
3399  *
3400  * Must be called from inside "The Loop"
3401  *
3402  * Call like the_shortlink(__('Shortlinkage FTW'))
3403  *
3404  * @since 3.0.0
3405  *
3406  * @param string $text   Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
3407  * @param string $title  Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
3408  * @param string $before Optional HTML to display before the link.
3409  * @param string $after  Optional HTML to display after the link.
3410  */
3411 function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
3412         $post = get_post();
3413
3414         if ( empty( $text ) )
3415                 $text = __('This is the short link.');
3416
3417         if ( empty( $title ) )
3418                 $title = the_title_attribute( array( 'echo' => false ) );
3419
3420         $shortlink = wp_get_shortlink( $post->ID );
3421
3422         if ( !empty( $shortlink ) ) {
3423                 $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
3424
3425                 /**
3426                  * Filter the shortlink anchor tag for a post.
3427                  *
3428                  * @since 3.0.0
3429                  *
3430                  * @param string $link      Shortlink anchor tag.
3431                  * @param string $shortlink Shortlink URL.
3432                  * @param string $text      Shortlink's text.
3433                  * @param string $title     Shortlink's title attribute.
3434                  */
3435                 $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
3436                 echo $before, $link, $after;
3437         }
3438 }
3439
3440
3441 /**
3442  * Retrieve the avatar URL.
3443  *
3444  * @since 4.2.0
3445  *
3446  * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,
3447  *                           user email, WP_User object, WP_Post object, or comment object.
3448  * @param array $args {
3449  *     Optional. Arguments to return instead of the default arguments.
3450  *
3451  *     @type int    $size           Height and width of the avatar in pixels. Default 96.
3452  *     @type string $default        URL for the default image or a default type. Accepts '404' (return
3453  *                                  a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
3454  *                                  'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
3455  *                                  or 'mysterman' (The Oyster Man), 'blank' (transparent GIF), or
3456  *                                  'gravatar_default' (the Gravatar logo). Default is the value of the
3457  *                                  'avatar_default' option, with a fallback of 'mystery'.
3458  *     @type bool   $force_default  Whether to always show the default image, never the Gravatar. Default false.
3459  *     @type string $rating         What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
3460  *                                  judged in that order. Default is the value of the 'avatar_rating' option.
3461  *     @type string $scheme         URL scheme to use. See set_url_scheme() for accepted values.
3462  *                                  Default null.
3463  *     @type array  $processed_args When the function returns, the value will be the processed/sanitized $args
3464  *                                  plus a "found_avatar" guess. Pass as a reference. Default null.
3465  * }
3466  * @return false|string The URL of the avatar we found, or false if we couldn't find an avatar.
3467  */
3468 function get_avatar_url( $id_or_email, $args = null ) {
3469         $args = get_avatar_data( $id_or_email, $args );
3470         return $args['url'];
3471 }
3472
3473 /**
3474  * Retrieve default data about the avatar.
3475  *
3476  * @since 4.2.0
3477  *
3478  * @param mixed $id_or_email The Gravatar to check the data against. Accepts a user_id, gravatar md5 hash,
3479  *                           user email, WP_User object, WP_Post object, or comment object.
3480  * @param array $args {
3481  *     Optional. Arguments to return instead of the default arguments.
3482  *
3483  *     @type int    $size           Height and width of the avatar image file in pixels. Default 96.
3484  *     @type int    $height         Display height of the avatar in pixels. Defaults to $size.
3485  *     @type int    $width          Display width of the avatar in pixels. Defaults to $size.
3486  *     @type string $default        URL for the default image or a default type. Accepts '404' (return
3487  *                                  a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
3488  *                                  'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
3489  *                                  or 'mysterman' (The Oyster Man), 'blank' (transparent GIF), or
3490  *                                  'gravatar_default' (the Gravatar logo). Default is the value of the
3491  *                                  'avatar_default' option, with a fallback of 'mystery'.
3492  *     @type bool   $force_default  Whether to always show the default image, never the Gravatar. Default false.
3493  *     @type string $rating         What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
3494  *                                  judged in that order. Default is the value of the 'avatar_rating' option.
3495  *     @type string $scheme         URL scheme to use. See set_url_scheme() for accepted values.
3496  *                                  Default null.
3497  *     @type array  $processed_args When the function returns, the value will be the processed/sanitized $args
3498  *                                  plus a "found_avatar" guess. Pass as a reference. Default null.
3499  *     @type string $extra_attr     HTML attributes to insert in the IMG element. Is not sanitized. Default empty.
3500  * }
3501  * @return array $processed_args {
3502  *     Along with the arguments passed in `$args`, this will contain a couple of extra arguments.
3503  *
3504  *     @type bool   $found_avatar True if we were able to find an avatar for this user,
3505  *                                false or not set if we couldn't.
3506  *     @type string $url          The URL of the avatar we found.
3507  * }
3508  */
3509 function get_avatar_data( $id_or_email, $args = null ) {
3510         $args = wp_parse_args( $args, array(
3511                 'size'           => 96,
3512                 'height'         => null,
3513                 'width'          => null,
3514                 'default'        => get_option( 'avatar_default', 'mystery' ),
3515                 'force_default'  => false,
3516                 'rating'         => get_option( 'avatar_rating' ),
3517                 'scheme'         => null,
3518                 'processed_args' => null, // if used, should be a reference
3519                 'extra_attr'     => '',
3520         ) );
3521
3522         if ( is_numeric( $args['size'] ) ) {
3523                 $args['size'] = absint( $args['size'] );
3524                 if ( ! $args['size'] ) {
3525                         $args['size'] = 96;
3526                 }
3527         } else {
3528                 $args['size'] = 96;
3529         }
3530
3531         if ( is_numeric( $args['height'] ) ) {
3532                 $args['height'] = absint( $args['height'] );
3533                 if ( ! $args['height'] ) {
3534                         $args['height'] = $args['size'];
3535                 }
3536         } else {
3537                 $args['height'] = $args['size'];
3538         }
3539
3540         if ( is_numeric( $args['width'] ) ) {
3541                 $args['width'] = absint( $args['width'] );
3542                 if ( ! $args['width'] ) {
3543                         $args['width'] = $args['size'];
3544                 }
3545         } else {
3546                 $args['width'] = $args['size'];
3547         }
3548
3549         if ( empty( $args['default'] ) ) {
3550                 $args['default'] = get_option( 'avatar_default', 'mystery' );
3551         }
3552
3553         switch ( $args['default'] ) {
3554                 case 'mm' :
3555                 case 'mystery' :
3556                 case 'mysteryman' :
3557                         $args['default'] = 'mm';
3558                         break;
3559                 case 'gravatar_default' :
3560                         $args['default'] = false;
3561                         break;
3562         }
3563
3564         $args['force_default'] = (bool) $args['force_default'];
3565
3566         $args['rating'] = strtolower( $args['rating'] );
3567
3568         $args['found_avatar'] = false;
3569
3570         /**
3571          * Filter whether to retrieve the avatar URL early.
3572          *
3573          * Passing a non-null value in the 'url' member of the return array will
3574          * effectively short circuit get_avatar_data(), passing the value through
3575          * the {@see 'get_avatar_data'} filter and returning early.
3576          *
3577          * @since 4.2.0
3578          *
3579          * @param array             $args          Arguments passed to get_avatar_data(), after processing.
3580          * @param int|object|string $id_or_email   A user ID, email address, or comment object.
3581          */
3582         $args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email );
3583
3584         if ( isset( $args['url'] ) && ! is_null( $args['url'] ) ) {
3585                 /** This filter is documented in wp-includes/link-template.php */
3586                 return apply_filters( 'get_avatar_data', $args, $id_or_email );
3587         }
3588
3589         $email_hash = '';
3590         $user = $email = false;
3591
3592         // Process the user identifier.
3593         if ( is_numeric( $id_or_email ) ) {
3594                 $user = get_user_by( 'id', absint( $id_or_email ) );
3595         } elseif ( is_string( $id_or_email ) ) {
3596                 if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
3597                         // md5 hash
3598                         list( $email_hash ) = explode( '@', $id_or_email );
3599                 } else {
3600                         // email address
3601                         $email = $id_or_email;
3602                 }
3603         } elseif ( $id_or_email instanceof WP_User ) {
3604                 // User Object
3605                 $user = $id_or_email;
3606         } elseif ( $id_or_email instanceof WP_Post ) {
3607                 // Post Object
3608                 $user = get_user_by( 'id', (int) $id_or_email->post_author );
3609         } elseif ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
3610                 // Comment Object
3611
3612                 /**
3613                  * Filter the list of allowed comment types for retrieving avatars.
3614                  *
3615                  * @since 3.0.0
3616                  *
3617                  * @param array $types An array of content types. Default only contains 'comment'.
3618                  */
3619                 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
3620                 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) {
3621                         $args['url'] = false;
3622                         /** This filter is documented in wp-includes/link-template.php */
3623                         return apply_filters( 'get_avatar_data', $args, $id_or_email );
3624                 }
3625
3626                 if ( ! empty( $id_or_email->user_id ) ) {
3627                         $user = get_user_by( 'id', (int) $id_or_email->user_id );
3628                 }
3629                 if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) {
3630                         $email = $id_or_email->comment_author_email;
3631                 }
3632         }
3633
3634         if ( ! $email_hash ) {
3635                 if ( $user ) {
3636                         $email = $user->user_email;
3637                 }
3638
3639                 if ( $email ) {
3640                         $email_hash = md5( strtolower( trim( $email ) ) );
3641                 }
3642         }
3643
3644         if ( $email_hash ) {
3645                 $args['found_avatar'] = true;
3646                 $gravatar_server = hexdec( $email_hash[0] ) % 3;
3647         } else {
3648                 $gravatar_server = rand( 0, 2 );
3649         }
3650
3651         $url_args = array(
3652                 's' => $args['size'],
3653                 'd' => $args['default'],
3654                 'f' => $args['force_default'] ? 'y' : false,
3655                 'r' => $args['rating'],
3656         );
3657
3658         if ( is_ssl() ) {
3659                 $url = 'https://secure.gravatar.com/avatar/' . $email_hash;
3660         } else {
3661                 $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash );
3662         }
3663
3664         $url = add_query_arg(
3665                 rawurlencode_deep( array_filter( $url_args ) ),
3666                 set_url_scheme( $url, $args['scheme'] )
3667         );
3668
3669         /**
3670          * Filter the avatar URL.
3671          *
3672          * @since 4.2.0
3673          *
3674          * @param string            $url         The URL of the avatar.
3675          * @param int|object|string $id_or_email A user ID, email address, or comment object.
3676          * @param array             $args        Arguments passed to get_avatar_data(), after processing.
3677          */
3678         $args['url'] = apply_filters( 'get_avatar_url', $url, $id_or_email, $args );
3679
3680         /**
3681          * Filter the avatar data.
3682          *
3683          * @since 4.2.0
3684          *
3685          * @param array             $args        Arguments passed to get_avatar_data(), after processing.
3686          * @param int|object|string $id_or_email A user ID, email address, or comment object.
3687          */
3688         return apply_filters( 'get_avatar_data', $args, $id_or_email );
3689 }