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