]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/link-template.php
WordPress 4.5
[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 site is 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  * site 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  * Retrieves the URL for editing a given term.
908  *
909  * @since 3.1.0
910  * @since 4.5.0 The `$taxonomy` argument was made optional.
911  *
912  * @param int    $term_id     Term ID.
913  * @param string $taxonomy    Optional. Taxonomy. Defaults to the taxonomy of the term identified by `$term_id`.
914  * @param string $object_type Optional. The object type. Used to highlight the proper post type menu on the linked page.
915  *                            Defaults to the first object_type associated with the taxonomy.
916  * @return string|null The edit term link URL for the given term, or null on failure.
917  */
918 function get_edit_term_link( $term_id, $taxonomy = '', $object_type = '' ) {
919         $term = get_term( $term_id, $taxonomy );
920         if ( ! $term || is_wp_error( $term ) ) {
921                 return;
922         }
923
924         $tax = get_taxonomy( $term->taxonomy );
925         if ( ! $tax || ! current_user_can( $tax->cap->edit_terms ) ) {
926                 return;
927         }
928
929         $args = array(
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( 'term.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  * Retrieves the permalink for a post type archive.
1114  *
1115  * @since 3.1.0
1116  * @since 4.5.0 Support for posts was added.
1117  *
1118  * @global WP_Rewrite $wp_rewrite
1119  *
1120  * @param string $post_type Post type.
1121  * @return string|false The post type archive permalink.
1122  */
1123 function get_post_type_archive_link( $post_type ) {
1124         global $wp_rewrite;
1125         if ( ! $post_type_obj = get_post_type_object( $post_type ) )
1126                 return false;
1127
1128         if ( 'post' === $post_type ) {
1129                 $show_on_front = get_option( 'show_on_front' );
1130                 $page_for_posts  = get_option( 'page_for_posts' );
1131
1132                 if ( 'page' == $show_on_front && $page_for_posts ) {
1133                         $link = get_permalink( $page_for_posts );
1134                 } else {
1135                         $link = get_home_url();
1136                 }
1137                 /** This filter is documented in wp-includes/link-template.php */
1138                 return apply_filters( 'post_type_archive_link', $link, $post_type );
1139         }
1140
1141         if ( ! $post_type_obj->has_archive )
1142                 return false;
1143
1144         if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
1145                 $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
1146                 if ( $post_type_obj->rewrite['with_front'] )
1147                         $struct = $wp_rewrite->front . $struct;
1148                 else
1149                         $struct = $wp_rewrite->root . $struct;
1150                 $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
1151         } else {
1152                 $link = home_url( '?post_type=' . $post_type );
1153         }
1154
1155         /**
1156          * Filter the post type archive permalink.
1157          *
1158          * @since 3.1.0
1159          *
1160          * @param string $link      The post type archive permalink.
1161          * @param string $post_type Post type name.
1162          */
1163         return apply_filters( 'post_type_archive_link', $link, $post_type );
1164 }
1165
1166 /**
1167  * Retrieve the permalink for a post type archive feed.
1168  *
1169  * @since 3.1.0
1170  *
1171  * @param string $post_type Post type
1172  * @param string $feed      Optional. Feed type
1173  * @return string|false The post type feed permalink.
1174  */
1175 function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
1176         $default_feed = get_default_feed();
1177         if ( empty( $feed ) )
1178                 $feed = $default_feed;
1179
1180         if ( ! $link = get_post_type_archive_link( $post_type ) )
1181                 return false;
1182
1183         $post_type_obj = get_post_type_object( $post_type );
1184         if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
1185                 $link = trailingslashit( $link );
1186                 $link .= 'feed/';
1187                 if ( $feed != $default_feed )
1188                         $link .= "$feed/";
1189         } else {
1190                 $link = add_query_arg( 'feed', $feed, $link );
1191         }
1192
1193         /**
1194          * Filter the post type archive feed link.
1195          *
1196          * @since 3.1.0
1197          *
1198          * @param string $link The post type archive feed link.
1199          * @param string $feed Feed type.
1200          */
1201         return apply_filters( 'post_type_archive_feed_link', $link, $feed );
1202 }
1203
1204 /**
1205  * Retrieve URL used for the post preview.
1206  *
1207  * Get the preview post URL. Allows additional query args to be appended.
1208  *
1209  * @since 4.4.0
1210  *
1211  * @param int|WP_Post $post         Optional. Post ID or `WP_Post` object. Defaults to global post.
1212  * @param array       $query_args   Optional. Array of additional query args to be appended to the link.
1213  * @param string      $preview_link Optional. Base preview link to be used if it should differ from the post permalink.
1214  * @return string URL used for the post preview.
1215  */
1216 function get_preview_post_link( $post = null, $query_args = array(), $preview_link = '' ) {
1217         $post = get_post( $post );
1218         if ( ! $post ) {
1219                 return;
1220         }
1221
1222         $post_type_object = get_post_type_object( $post->post_type );
1223         if ( is_post_type_viewable( $post_type_object ) ) {
1224                 if ( ! $preview_link ) {
1225                         $preview_link = set_url_scheme( get_permalink( $post ) );
1226                 }
1227
1228                 $query_args['preview'] = 'true';
1229                 $preview_link = add_query_arg( $query_args, $preview_link );
1230         }
1231
1232         /**
1233          * Filter the URL used for a post preview.
1234          *
1235          * @since 2.0.5
1236          * @since 4.0.0 Added the `$post` parameter.
1237          *
1238          * @param string  $preview_link URL used for the post preview.
1239          * @param WP_Post $post         Post object.
1240          */
1241         return apply_filters( 'preview_post_link', $preview_link, $post );
1242 }
1243
1244 /**
1245  * Retrieve edit posts link for post.
1246  *
1247  * Can be used within the WordPress loop or outside of it. Can be used with
1248  * pages, posts, attachments, and revisions.
1249  *
1250  * @since 2.3.0
1251  *
1252  * @param int    $id      Optional. Post ID.
1253  * @param string $context Optional, defaults to display. How to write the '&', defaults to '&amp;'.
1254  * @return string|null The edit post link for the given post. null if the post type is invalid or does
1255  *                     not allow an editing UI.
1256  */
1257 function get_edit_post_link( $id = 0, $context = 'display' ) {
1258         if ( ! $post = get_post( $id ) )
1259                 return;
1260
1261         if ( 'revision' === $post->post_type )
1262                 $action = '';
1263         elseif ( 'display' == $context )
1264                 $action = '&amp;action=edit';
1265         else
1266                 $action = '&action=edit';
1267
1268         $post_type_object = get_post_type_object( $post->post_type );
1269         if ( !$post_type_object )
1270                 return;
1271
1272         if ( !current_user_can( 'edit_post', $post->ID ) )
1273                 return;
1274
1275         if ( $post_type_object->_edit_link ) {
1276                 $link = admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) );
1277         } else {
1278                 $link = '';
1279         }
1280
1281         /**
1282          * Filter the post edit link.
1283          *
1284          * @since 2.3.0
1285          *
1286          * @param string $link    The edit link.
1287          * @param int    $post_id Post ID.
1288          * @param string $context The link context. If set to 'display' then ampersands
1289          *                        are encoded.
1290          */
1291         return apply_filters( 'get_edit_post_link', $link, $post->ID, $context );
1292 }
1293
1294 /**
1295  * Display edit post link for post.
1296  *
1297  * @since 1.0.0
1298  * @since 4.4.0 The `$class` argument was added.
1299  *
1300  * @param string $text   Optional. Anchor text.
1301  * @param string $before Optional. Display before edit link.
1302  * @param string $after  Optional. Display after edit link.
1303  * @param int    $id     Optional. Post ID.
1304  * @param string $class  Optional. Add custom class to link.
1305  */
1306 function edit_post_link( $text = null, $before = '', $after = '', $id = 0, $class = 'post-edit-link' ) {
1307         if ( ! $post = get_post( $id ) ) {
1308                 return;
1309         }
1310
1311         if ( ! $url = get_edit_post_link( $post->ID ) ) {
1312                 return;
1313         }
1314
1315         if ( null === $text ) {
1316                 $text = __( 'Edit This' );
1317         }
1318
1319         $link = '<a class="' . esc_attr( $class ) . '" href="' . esc_url( $url ) . '">' . $text . '</a>';
1320
1321         /**
1322          * Filter the post edit link anchor tag.
1323          *
1324          * @since 2.3.0
1325          *
1326          * @param string $link    Anchor tag for the edit link.
1327          * @param int    $post_id Post ID.
1328          * @param string $text    Anchor text.
1329          */
1330         echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after;
1331 }
1332
1333 /**
1334  * Retrieve delete posts link for post.
1335  *
1336  * Can be used within the WordPress loop or outside of it, with any post type.
1337  *
1338  * @since 2.9.0
1339  *
1340  * @param int    $id           Optional. Post ID.
1341  * @param string $deprecated   Not used.
1342  * @param bool   $force_delete Whether to bypass trash and force deletion. Default is false.
1343  * @return string|void The delete post link URL for the given post.
1344  */
1345 function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
1346         if ( ! empty( $deprecated ) )
1347                 _deprecated_argument( __FUNCTION__, '3.0' );
1348
1349         if ( !$post = get_post( $id ) )
1350                 return;
1351
1352         $post_type_object = get_post_type_object( $post->post_type );
1353         if ( !$post_type_object )
1354                 return;
1355
1356         if ( !current_user_can( 'delete_post', $post->ID ) )
1357                 return;
1358
1359         $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
1360
1361         $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
1362
1363         /**
1364          * Filter the post delete link.
1365          *
1366          * @since 2.9.0
1367          *
1368          * @param string $link         The delete link.
1369          * @param int    $post_id      Post ID.
1370          * @param bool   $force_delete Whether to bypass the trash and force deletion. Default false.
1371          */
1372         return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
1373 }
1374
1375 /**
1376  * Retrieve edit comment link.
1377  *
1378  * @since 2.3.0
1379  *
1380  * @param int|WP_Comment $comment_id Optional. Comment ID or WP_Comment object.
1381  * @return string|void The edit comment link URL for the given comment.
1382  */
1383 function get_edit_comment_link( $comment_id = 0 ) {
1384         $comment = get_comment( $comment_id );
1385
1386         if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
1387                 return;
1388
1389         $location = admin_url('comment.php?action=editcomment&amp;c=') . $comment->comment_ID;
1390
1391         /**
1392          * Filter the comment edit link.
1393          *
1394          * @since 2.3.0
1395          *
1396          * @param string $location The edit link.
1397          */
1398         return apply_filters( 'get_edit_comment_link', $location );
1399 }
1400
1401 /**
1402  * Display edit comment link with formatting.
1403  *
1404  * @since 1.0.0
1405  *
1406  * @param string $text   Optional. Anchor text.
1407  * @param string $before Optional. Display before edit link.
1408  * @param string $after  Optional. Display after edit link.
1409  */
1410 function edit_comment_link( $text = null, $before = '', $after = '' ) {
1411         $comment = get_comment();
1412
1413         if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
1414                 return;
1415         }
1416
1417         if ( null === $text ) {
1418                 $text = __( 'Edit This' );
1419         }
1420
1421         $link = '<a class="comment-edit-link" href="' . esc_url( get_edit_comment_link( $comment ) ) . '">' . $text . '</a>';
1422
1423         /**
1424          * Filter the comment edit link anchor tag.
1425          *
1426          * @since 2.3.0
1427          *
1428          * @param string $link       Anchor tag for the edit link.
1429          * @param int    $comment_id Comment ID.
1430          * @param string $text       Anchor text.
1431          */
1432         echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after;
1433 }
1434
1435 /**
1436  * Display edit bookmark link.
1437  *
1438  * @since 2.7.0
1439  *
1440  * @param int|stdClass $link Optional. Bookmark ID.
1441  * @return string|void The edit bookmark link URL.
1442  */
1443 function get_edit_bookmark_link( $link = 0 ) {
1444         $link = get_bookmark( $link );
1445
1446         if ( !current_user_can('manage_links') )
1447                 return;
1448
1449         $location = admin_url('link.php?action=edit&amp;link_id=') . $link->link_id;
1450
1451         /**
1452          * Filter the bookmark edit link.
1453          *
1454          * @since 2.7.0
1455          *
1456          * @param string $location The edit link.
1457          * @param int    $link_id  Bookmark ID.
1458          */
1459         return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
1460 }
1461
1462 /**
1463  * Display edit bookmark link anchor content.
1464  *
1465  * @since 2.7.0
1466  *
1467  * @param string $link     Optional. Anchor text.
1468  * @param string $before   Optional. Display before edit link.
1469  * @param string $after    Optional. Display after edit link.
1470  * @param int    $bookmark Optional. Bookmark ID.
1471  */
1472 function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
1473         $bookmark = get_bookmark($bookmark);
1474
1475         if ( !current_user_can('manage_links') )
1476                 return;
1477
1478         if ( empty($link) )
1479                 $link = __('Edit This');
1480
1481         $link = '<a href="' . esc_url( get_edit_bookmark_link( $bookmark ) ) . '">' . $link . '</a>';
1482
1483         /**
1484          * Filter the bookmark edit link anchor tag.
1485          *
1486          * @since 2.7.0
1487          *
1488          * @param string $link    Anchor tag for the edit link.
1489          * @param int    $link_id Bookmark ID.
1490          */
1491         echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
1492 }
1493
1494 /**
1495  * Retrieve edit user link
1496  *
1497  * @since 3.5.0
1498  *
1499  * @param int $user_id Optional. User ID. Defaults to the current user.
1500  * @return string URL to edit user page or empty string.
1501  */
1502 function get_edit_user_link( $user_id = null ) {
1503         if ( ! $user_id )
1504                 $user_id = get_current_user_id();
1505
1506         if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) )
1507                 return '';
1508
1509         $user = get_userdata( $user_id );
1510
1511         if ( ! $user )
1512                 return '';
1513
1514         if ( get_current_user_id() == $user->ID )
1515                 $link = get_edit_profile_url( $user->ID );
1516         else
1517                 $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
1518
1519         /**
1520          * Filter the user edit link.
1521          *
1522          * @since 3.5.0
1523          *
1524          * @param string $link    The edit link.
1525          * @param int    $user_id User ID.
1526          */
1527         return apply_filters( 'get_edit_user_link', $link, $user->ID );
1528 }
1529
1530 // Navigation links
1531
1532 /**
1533  * Retrieve previous 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_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1543         return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
1544 }
1545
1546 /**
1547  * Retrieve next post that is adjacent to current post.
1548  *
1549  * @since 1.5.0
1550  *
1551  * @param bool         $in_same_term   Optional. Whether post should be in a same taxonomy term.
1552  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1553  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1554  * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1555  */
1556 function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1557         return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
1558 }
1559
1560 /**
1561  * Retrieve adjacent post.
1562  *
1563  * Can either be next or previous post.
1564  *
1565  * @since 2.5.0
1566  *
1567  * @global wpdb $wpdb WordPress database abstraction object.
1568  *
1569  * @param bool         $in_same_term   Optional. Whether post should be in a same taxonomy term.
1570  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1571  * @param bool         $previous       Optional. Whether to retrieve previous post.
1572  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1573  * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
1574  */
1575 function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1576         global $wpdb;
1577
1578         if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
1579                 return null;
1580
1581         $current_post_date = $post->post_date;
1582
1583         $join = '';
1584         $where = '';
1585         $adjacent = $previous ? 'previous' : 'next';
1586
1587         if ( $in_same_term || ! empty( $excluded_terms ) ) {
1588                 if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
1589                         // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
1590                         if ( false !== strpos( $excluded_terms, ' and ' ) ) {
1591                                 _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
1592                                 $excluded_terms = explode( ' and ', $excluded_terms );
1593                         } else {
1594                                 $excluded_terms = explode( ',', $excluded_terms );
1595                         }
1596
1597                         $excluded_terms = array_map( 'intval', $excluded_terms );
1598                 }
1599
1600                 if ( $in_same_term ) {
1601                         $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";
1602                         $where .= $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
1603
1604                         if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
1605                                 return '';
1606                         $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1607
1608                         // Remove any exclusions from the term array to include.
1609                         $term_array = array_diff( $term_array, (array) $excluded_terms );
1610                         $term_array = array_map( 'intval', $term_array );
1611
1612                         if ( ! $term_array || is_wp_error( $term_array ) )
1613                                 return '';
1614
1615                         $where .= " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
1616                 }
1617
1618                 /**
1619                  * Filter the IDs of terms excluded from adjacent post queries.
1620                  *
1621                  * The dynamic portion of the hook name, `$adjacent`, refers to the type
1622                  * of adjacency, 'next' or 'previous'.
1623                  *
1624                  * @since 4.4.0
1625                  *
1626                  * @param string $excluded_terms Array of excluded term IDs.
1627                  */
1628                 $excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms );
1629
1630                 if ( ! empty( $excluded_terms ) ) {
1631                         $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( ',', array_map( 'intval', $excluded_terms ) ) . ') )';
1632                 }
1633         }
1634
1635         // 'post_status' clause depends on the current user.
1636         if ( is_user_logged_in() ) {
1637                 $user_id = get_current_user_id();
1638
1639                 $post_type_object = get_post_type_object( $post->post_type );
1640                 if ( empty( $post_type_object ) ) {
1641                         $post_type_cap    = $post->post_type;
1642                         $read_private_cap = 'read_private_' . $post_type_cap . 's';
1643                 } else {
1644                         $read_private_cap = $post_type_object->cap->read_private_posts;
1645                 }
1646
1647                 /*
1648                  * Results should include private posts belonging to the current user, or private posts where the
1649                  * current user has the 'read_private_posts' cap.
1650                  */
1651                 $private_states = get_post_stati( array( 'private' => true ) );
1652                 $where .= " AND ( p.post_status = 'publish'";
1653                 foreach ( (array) $private_states as $state ) {
1654                         if ( current_user_can( $read_private_cap ) ) {
1655                                 $where .= $wpdb->prepare( " OR p.post_status = %s", $state );
1656                         } else {
1657                                 $where .= $wpdb->prepare( " OR (p.post_author = %d AND p.post_status = %s)", $user_id, $state );
1658                         }
1659                 }
1660                 $where .= " )";
1661         } else {
1662                 $where .= " AND p.post_status = 'publish'";
1663         }
1664
1665         $op = $previous ? '<' : '>';
1666         $order = $previous ? 'DESC' : 'ASC';
1667
1668         /**
1669          * Filter the JOIN clause in the SQL for an adjacent post query.
1670          *
1671          * The dynamic portion of the hook name, `$adjacent`, refers to the type
1672          * of adjacency, 'next' or 'previous'.
1673          *
1674          * @since 2.5.0
1675          * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
1676          *
1677          * @param string  $join           The JOIN clause in the SQL.
1678          * @param bool    $in_same_term   Whether post should be in a same taxonomy term.
1679          * @param array   $excluded_terms Array of excluded term IDs.
1680          * @param string  $taxonomy       Taxonomy. Used to identify the term used when `$in_same_term` is true.
1681          * @param WP_Post $post           WP_Post object.
1682          */
1683         $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms, $taxonomy, $post );
1684
1685         /**
1686          * Filter the WHERE clause in the SQL for an adjacent post query.
1687          *
1688          * The dynamic portion of the hook name, `$adjacent`, refers to the type
1689          * of adjacency, 'next' or 'previous'.
1690          *
1691          * @since 2.5.0
1692          * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
1693          *
1694          * @param string $where          The `WHERE` clause in the SQL.
1695          * @param bool   $in_same_term   Whether post should be in a same taxonomy term.
1696          * @param array  $excluded_terms Array of excluded term IDs.
1697          * @param string $taxonomy       Taxonomy. Used to identify the term used when `$in_same_term` is true.
1698          * @param WP_Post $post           WP_Post object.
1699          */
1700         $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 );
1701
1702         /**
1703          * Filter the ORDER BY clause in the SQL for an adjacent post query.
1704          *
1705          * The dynamic portion of the hook name, `$adjacent`, refers to the type
1706          * of adjacency, 'next' or 'previous'.
1707          *
1708          * @since 2.5.0
1709          * @since 4.4.0 Added the `$post` parameter.
1710          *
1711          * @param string $order_by The `ORDER BY` clause in the SQL.
1712          * @param WP_Post $post    WP_Post object.
1713          */
1714         $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post );
1715
1716         $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
1717         $query_key = 'adjacent_post_' . md5( $query );
1718         $result = wp_cache_get( $query_key, 'counts' );
1719         if ( false !== $result ) {
1720                 if ( $result )
1721                         $result = get_post( $result );
1722                 return $result;
1723         }
1724
1725         $result = $wpdb->get_var( $query );
1726         if ( null === $result )
1727                 $result = '';
1728
1729         wp_cache_set( $query_key, $result, 'counts' );
1730
1731         if ( $result )
1732                 $result = get_post( $result );
1733
1734         return $result;
1735 }
1736
1737 /**
1738  * Get adjacent post relational link.
1739  *
1740  * Can either be next or previous post relational link.
1741  *
1742  * @since 2.8.0
1743  *
1744  * @param string       $title          Optional. Link title format.
1745  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1746  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1747  * @param bool         $previous       Optional. Whether to display link to previous or next post. Default true.
1748  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1749  * @return string|void The adjacent post relational link URL.
1750  */
1751 function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1752         if ( $previous && is_attachment() && $post = get_post() )
1753                 $post = get_post( $post->post_parent );
1754         else
1755                 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1756
1757         if ( empty( $post ) )
1758                 return;
1759
1760         $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
1761
1762         if ( empty( $post_title ) )
1763                 $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1764
1765         $date = mysql2date( get_option( 'date_format' ), $post->post_date );
1766
1767         $title = str_replace( '%title', $post_title, $title );
1768         $title = str_replace( '%date', $date, $title );
1769
1770         $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
1771         $link .= esc_attr( $title );
1772         $link .= "' href='" . get_permalink( $post ) . "' />\n";
1773
1774         $adjacent = $previous ? 'previous' : 'next';
1775
1776         /**
1777          * Filter the adjacent post relational link.
1778          *
1779          * The dynamic portion of the hook name, `$adjacent`, refers to the type
1780          * of adjacency, 'next' or 'previous'.
1781          *
1782          * @since 2.8.0
1783          *
1784          * @param string $link The relational link.
1785          */
1786         return apply_filters( "{$adjacent}_post_rel_link", $link );
1787 }
1788
1789 /**
1790  * Display relational links for the posts adjacent to the current post.
1791  *
1792  * @since 2.8.0
1793  *
1794  * @param string       $title          Optional. Link title format.
1795  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1796  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1797  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1798  */
1799 function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1800         echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1801         echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
1802 }
1803
1804 /**
1805  * Display relational links for the posts adjacent to the current post for single post pages.
1806  *
1807  * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
1808  * @since 3.0.0
1809  *
1810  */
1811 function adjacent_posts_rel_link_wp_head() {
1812         if ( ! is_single() || is_attachment() ) {
1813                 return;
1814         }
1815         adjacent_posts_rel_link();
1816 }
1817
1818 /**
1819  * Display relational link for the next 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.
1826  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1827  */
1828 function next_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, false, $taxonomy );
1830 }
1831
1832 /**
1833  * Display relational link for the previous post adjacent to the current post.
1834  *
1835  * @since 2.8.0
1836  *
1837  * @param string       $title          Optional. Link title format.
1838  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1839  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
1840  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1841  */
1842 function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1843         echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
1844 }
1845
1846 /**
1847  * Retrieve boundary post.
1848  *
1849  * Boundary being either the first or last post by publish date within the constraints specified
1850  * by $in_same_term or $excluded_terms.
1851  *
1852  * @since 2.8.0
1853  *
1854  * @param bool         $in_same_term   Optional. Whether returned post should be in a same taxonomy term.
1855  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1856  * @param bool         $start          Optional. Whether to retrieve first or last post.
1857  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1858  * @return null|array Array containing the boundary post object if successful, null otherwise.
1859  */
1860 function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
1861         $post = get_post();
1862         if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
1863                 return null;
1864
1865         $query_args = array(
1866                 'posts_per_page' => 1,
1867                 'order' => $start ? 'ASC' : 'DESC',
1868                 'update_post_term_cache' => false,
1869                 'update_post_meta_cache' => false
1870         );
1871
1872         $term_array = array();
1873
1874         if ( ! is_array( $excluded_terms ) ) {
1875                 if ( ! empty( $excluded_terms ) )
1876                         $excluded_terms = explode( ',', $excluded_terms );
1877                 else
1878                         $excluded_terms = array();
1879         }
1880
1881         if ( $in_same_term || ! empty( $excluded_terms ) ) {
1882                 if ( $in_same_term )
1883                         $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
1884
1885                 if ( ! empty( $excluded_terms ) ) {
1886                         $excluded_terms = array_map( 'intval', $excluded_terms );
1887                         $excluded_terms = array_diff( $excluded_terms, $term_array );
1888
1889                         $inverse_terms = array();
1890                         foreach ( $excluded_terms as $excluded_term )
1891                                 $inverse_terms[] = $excluded_term * -1;
1892                         $excluded_terms = $inverse_terms;
1893                 }
1894
1895                 $query_args[ 'tax_query' ] = array( array(
1896                         'taxonomy' => $taxonomy,
1897                         'terms' => array_merge( $term_array, $excluded_terms )
1898                 ) );
1899         }
1900
1901         return get_posts( $query_args );
1902 }
1903
1904 /**
1905  * Get previous post link that is adjacent to the current post.
1906  *
1907  * @since 3.7.0
1908  *
1909  * @param string       $format         Optional. Link anchor format.
1910  * @param string       $link           Optional. Link permalink format.
1911  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1912  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1913  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1914  * @return string The link URL of the previous post in relation to the current post.
1915  */
1916 function get_previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1917         return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy );
1918 }
1919
1920 /**
1921  * Display previous post link that is adjacent to the current post.
1922  *
1923  * @since 1.5.0
1924  * @see get_previous_post_link()
1925  *
1926  * @param string       $format         Optional. Link anchor format.
1927  * @param string       $link           Optional. Link permalink format.
1928  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1929  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1930  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1931  */
1932 function previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1933         echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1934 }
1935
1936 /**
1937  * Get next post link that is adjacent to the current post.
1938  *
1939  * @since 3.7.0
1940  *
1941  * @param string       $format         Optional. Link anchor format.
1942  * @param string       $link           Optional. Link permalink format.
1943  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1944  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1945  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1946  * @return string The link URL of the next post in relation to the current post.
1947  */
1948 function get_next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1949         return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy );
1950 }
1951
1952 /**
1953  * Display next post link that is adjacent to the current post.
1954  *
1955  * @since 1.5.0
1956  * @see get_next_post_link()
1957  *
1958  * @param string       $format         Optional. Link anchor format.
1959  * @param string       $link           Optional. Link permalink format.
1960  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1961  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
1962  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1963  */
1964 function next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
1965          echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
1966 }
1967
1968 /**
1969  * Get adjacent post link.
1970  *
1971  * Can be either next post link or previous.
1972  *
1973  * @since 3.7.0
1974  *
1975  * @param string       $format         Link anchor format.
1976  * @param string       $link           Link permalink format.
1977  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
1978  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs.
1979  * @param bool         $previous       Optional. Whether to display link to previous or next post. Default true.
1980  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
1981  * @return string The link URL of the previous or next post in relation to the current post.
1982  */
1983 function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
1984         if ( $previous && is_attachment() )
1985                 $post = get_post( get_post()->post_parent );
1986         else
1987                 $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
1988
1989         if ( ! $post ) {
1990                 $output = '';
1991         } else {
1992                 $title = $post->post_title;
1993
1994                 if ( empty( $post->post_title ) )
1995                         $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
1996
1997                 /** This filter is documented in wp-includes/post-template.php */
1998                 $title = apply_filters( 'the_title', $title, $post->ID );
1999
2000                 $date = mysql2date( get_option( 'date_format' ), $post->post_date );
2001                 $rel = $previous ? 'prev' : 'next';
2002
2003                 $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
2004                 $inlink = str_replace( '%title', $title, $link );
2005                 $inlink = str_replace( '%date', $date, $inlink );
2006                 $inlink = $string . $inlink . '</a>';
2007
2008                 $output = str_replace( '%link', $inlink, $format );
2009         }
2010
2011         $adjacent = $previous ? 'previous' : 'next';
2012
2013         /**
2014          * Filter the adjacent post link.
2015          *
2016          * The dynamic portion of the hook name, `$adjacent`, refers to the type
2017          * of adjacency, 'next' or 'previous'.
2018          *
2019          * @since 2.6.0
2020          * @since 4.2.0 Added the `$adjacent` parameter.
2021          *
2022          * @param string  $output   The adjacent post link.
2023          * @param string  $format   Link anchor format.
2024          * @param string  $link     Link permalink format.
2025          * @param WP_Post $post     The adjacent post.
2026          * @param string  $adjacent Whether the post is previous or next.
2027          */
2028         return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post, $adjacent );
2029 }
2030
2031 /**
2032  * Display adjacent post link.
2033  *
2034  * Can be either next post link or previous.
2035  *
2036  * @since 2.5.0
2037  *
2038  * @param string       $format         Link anchor format.
2039  * @param string       $link           Link permalink format.
2040  * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
2041  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs.
2042  * @param bool         $previous       Optional. Whether to display link to previous or next post. Default true.
2043  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
2044  */
2045 function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
2046         echo get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy );
2047 }
2048
2049 /**
2050  * Retrieve links for page numbers.
2051  *
2052  * @since 1.5.0
2053  *
2054  * @global WP_Rewrite $wp_rewrite
2055  *
2056  * @param int  $pagenum Optional. Page ID.
2057  * @param bool $escape  Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
2058  *                          Otherwise, prepares the URL with esc_url_raw().
2059  * @return string The link URL for the given page number.
2060  */
2061 function get_pagenum_link($pagenum = 1, $escape = true ) {
2062         global $wp_rewrite;
2063
2064         $pagenum = (int) $pagenum;
2065
2066         $request = remove_query_arg( 'paged' );
2067
2068         $home_root = parse_url(home_url());
2069         $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
2070         $home_root = preg_quote( $home_root, '|' );
2071
2072         $request = preg_replace('|^'. $home_root . '|i', '', $request);
2073         $request = preg_replace('|^/+|', '', $request);
2074
2075         if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
2076                 $base = trailingslashit( get_bloginfo( 'url' ) );
2077
2078                 if ( $pagenum > 1 ) {
2079                         $result = add_query_arg( 'paged', $pagenum, $base . $request );
2080                 } else {
2081                         $result = $base . $request;
2082                 }
2083         } else {
2084                 $qs_regex = '|\?.*?$|';
2085                 preg_match( $qs_regex, $request, $qs_match );
2086
2087                 if ( !empty( $qs_match[0] ) ) {
2088                         $query_string = $qs_match[0];
2089                         $request = preg_replace( $qs_regex, '', $request );
2090                 } else {
2091                         $query_string = '';
2092                 }
2093
2094                 $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
2095                 $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request);
2096                 $request = ltrim($request, '/');
2097
2098                 $base = trailingslashit( get_bloginfo( 'url' ) );
2099
2100                 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
2101                         $base .= $wp_rewrite->index . '/';
2102
2103                 if ( $pagenum > 1 ) {
2104                         $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
2105                 }
2106
2107                 $result = $base . $request . $query_string;
2108         }
2109
2110         /**
2111          * Filter the page number link for the current request.
2112          *
2113          * @since 2.5.0
2114          *
2115          * @param string $result The page number link.
2116          */
2117         $result = apply_filters( 'get_pagenum_link', $result );
2118
2119         if ( $escape )
2120                 return esc_url( $result );
2121         else
2122                 return esc_url_raw( $result );
2123 }
2124
2125 /**
2126  * Retrieve next posts page link.
2127  *
2128  * Backported from 2.1.3 to 2.0.10.
2129  *
2130  * @since 2.0.10
2131  *
2132  * @global int $paged
2133  *
2134  * @param int $max_page Optional. Max pages.
2135  * @return string|void The link URL for next posts page.
2136  */
2137 function get_next_posts_page_link($max_page = 0) {
2138         global $paged;
2139
2140         if ( !is_single() ) {
2141                 if ( !$paged )
2142                         $paged = 1;
2143                 $nextpage = intval($paged) + 1;
2144                 if ( !$max_page || $max_page >= $nextpage )
2145                         return get_pagenum_link($nextpage);
2146         }
2147 }
2148
2149 /**
2150  * Display or return the next posts page link.
2151  *
2152  * @since 0.71
2153  *
2154  * @param int   $max_page Optional. Max pages.
2155  * @param bool  $echo     Optional. Echo or return;
2156  * @return string|void The link URL for next posts page if `$echo = false`.
2157  */
2158 function next_posts( $max_page = 0, $echo = true ) {
2159         $output = esc_url( get_next_posts_page_link( $max_page ) );
2160
2161         if ( $echo )
2162                 echo $output;
2163         else
2164                 return $output;
2165 }
2166
2167 /**
2168  * Return the next posts page link.
2169  *
2170  * @since 2.7.0
2171  *
2172  * @global int      $paged
2173  * @global WP_Query $wp_query
2174  *
2175  * @param string $label    Content for link text.
2176  * @param int    $max_page Optional. Max pages.
2177  * @return string|void HTML-formatted next posts page link.
2178  */
2179 function get_next_posts_link( $label = null, $max_page = 0 ) {
2180         global $paged, $wp_query;
2181
2182         if ( !$max_page )
2183                 $max_page = $wp_query->max_num_pages;
2184
2185         if ( !$paged )
2186                 $paged = 1;
2187
2188         $nextpage = intval($paged) + 1;
2189
2190         if ( null === $label )
2191                 $label = __( 'Next Page &raquo;' );
2192
2193         if ( !is_single() && ( $nextpage <= $max_page ) ) {
2194                 /**
2195                  * Filter the anchor tag attributes for the next posts page link.
2196                  *
2197                  * @since 2.7.0
2198                  *
2199                  * @param string $attributes Attributes for the anchor tag.
2200                  */
2201                 $attr = apply_filters( 'next_posts_link_attributes', '' );
2202
2203                 return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) . '</a>';
2204         }
2205 }
2206
2207 /**
2208  * Display the next posts page link.
2209  *
2210  * @since 0.71
2211  *
2212  * @param string $label    Content for link text.
2213  * @param int    $max_page Optional. Max pages.
2214  */
2215 function next_posts_link( $label = null, $max_page = 0 ) {
2216         echo get_next_posts_link( $label, $max_page );
2217 }
2218
2219 /**
2220  * Retrieve previous posts page link.
2221  *
2222  * Will only return string, if not on a single page or post.
2223  *
2224  * Backported to 2.0.10 from 2.1.3.
2225  *
2226  * @since 2.0.10
2227  *
2228  * @global int $paged
2229  *
2230  * @return string|void The link for the previous posts page.
2231  */
2232 function get_previous_posts_page_link() {
2233         global $paged;
2234
2235         if ( !is_single() ) {
2236                 $nextpage = intval($paged) - 1;
2237                 if ( $nextpage < 1 )
2238                         $nextpage = 1;
2239                 return get_pagenum_link($nextpage);
2240         }
2241 }
2242
2243 /**
2244  * Display or return the previous posts page link.
2245  *
2246  * @since 0.71
2247  *
2248  * @param bool $echo Optional. Echo or return;
2249  * @return string|void The previous posts page link if `$echo = false`.
2250  */
2251 function previous_posts( $echo = true ) {
2252         $output = esc_url( get_previous_posts_page_link() );
2253
2254         if ( $echo )
2255                 echo $output;
2256         else
2257                 return $output;
2258 }
2259
2260 /**
2261  * Return the previous posts page link.
2262  *
2263  * @since 2.7.0
2264  *
2265  * @global int $paged
2266  *
2267  * @param string $label Optional. Previous page link text.
2268  * @return string|void HTML-formatted previous page link.
2269  */
2270 function get_previous_posts_link( $label = null ) {
2271         global $paged;
2272
2273         if ( null === $label )
2274                 $label = __( '&laquo; Previous Page' );
2275
2276         if ( !is_single() && $paged > 1 ) {
2277                 /**
2278                  * Filter the anchor tag attributes for the previous posts page link.
2279                  *
2280                  * @since 2.7.0
2281                  *
2282                  * @param string $attributes Attributes for the anchor tag.
2283                  */
2284                 $attr = apply_filters( 'previous_posts_link_attributes', '' );
2285                 return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) .'</a>';
2286         }
2287 }
2288
2289 /**
2290  * Display the previous posts page link.
2291  *
2292  * @since 0.71
2293  *
2294  * @param string $label Optional. Previous page link text.
2295  */
2296 function previous_posts_link( $label = null ) {
2297         echo get_previous_posts_link( $label );
2298 }
2299
2300 /**
2301  * Return post pages link navigation for previous and next pages.
2302  *
2303  * @since 2.8.0
2304  *
2305  * @global WP_Query $wp_query
2306  *
2307  * @param string|array $args Optional args.
2308  * @return string The posts link navigation.
2309  */
2310 function get_posts_nav_link( $args = array() ) {
2311         global $wp_query;
2312
2313         $return = '';
2314
2315         if ( !is_singular() ) {
2316                 $defaults = array(
2317                         'sep' => ' &#8212; ',
2318                         'prelabel' => __('&laquo; Previous Page'),
2319                         'nxtlabel' => __('Next Page &raquo;'),
2320                 );
2321                 $args = wp_parse_args( $args, $defaults );
2322
2323                 $max_num_pages = $wp_query->max_num_pages;
2324                 $paged = get_query_var('paged');
2325
2326                 //only have sep if there's both prev and next results
2327                 if ($paged < 2 || $paged >= $max_num_pages) {
2328                         $args['sep'] = '';
2329                 }
2330
2331                 if ( $max_num_pages > 1 ) {
2332                         $return = get_previous_posts_link($args['prelabel']);
2333                         $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $args['sep']);
2334                         $return .= get_next_posts_link($args['nxtlabel']);
2335                 }
2336         }
2337         return $return;
2338
2339 }
2340
2341 /**
2342  * Display post pages link navigation for previous and next pages.
2343  *
2344  * @since 0.71
2345  *
2346  * @param string $sep      Optional. Separator for posts navigation links.
2347  * @param string $prelabel Optional. Label for previous pages.
2348  * @param string $nxtlabel Optional Label for next pages.
2349  */
2350 function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
2351         $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
2352         echo get_posts_nav_link($args);
2353 }
2354
2355 /**
2356  * Return navigation to next/previous post when applicable.
2357  *
2358  * @since 4.1.0
2359  * @since 4.4.0 Introduced the `in_same_term`, `excluded_terms`, and `taxonomy` arguments.
2360  *
2361  * @param array $args {
2362  *     Optional. Default post navigation arguments. Default empty array.
2363  *
2364  *     @type string       $prev_text          Anchor text to display in the previous post link. Default '%title'.
2365  *     @type string       $next_text          Anchor text to display in the next post link. Default '%title'.
2366  *     @type bool         $in_same_term       Whether link should be in a same taxonomy term. Default false.
2367  *     @type array|string $excluded_terms     Array or comma-separated list of excluded term IDs. Default empty.
2368  *     @type string       $taxonomy           Taxonomy, if `$in_same_term` is true. Default 'category'.
2369  *     @type string       $screen_reader_text Screen reader text for nav element. Default 'Post navigation'.
2370  * }
2371  * @return string Markup for post links.
2372  */
2373 function get_the_post_navigation( $args = array() ) {
2374         $args = wp_parse_args( $args, array(
2375                 'prev_text'          => '%title',
2376                 'next_text'          => '%title',
2377                 'in_same_term'       => false,
2378                 'excluded_terms'     => '',
2379                 'taxonomy'           => 'category',
2380                 'screen_reader_text' => __( 'Post navigation' ),
2381         ) );
2382
2383         $navigation = '';
2384
2385         $previous = get_previous_post_link(
2386                 '<div class="nav-previous">%link</div>',
2387                 $args['prev_text'],
2388                 $args['in_same_term'],
2389                 $args['excluded_terms'],
2390                 $args['taxonomy']
2391         );
2392
2393         $next = get_next_post_link(
2394                 '<div class="nav-next">%link</div>',
2395                 $args['next_text'],
2396                 $args['in_same_term'],
2397                 $args['excluded_terms'],
2398                 $args['taxonomy']
2399         );
2400
2401         // Only add markup if there's somewhere to navigate to.
2402         if ( $previous || $next ) {
2403                 $navigation = _navigation_markup( $previous . $next, 'post-navigation', $args['screen_reader_text'] );
2404         }
2405
2406         return $navigation;
2407 }
2408
2409 /**
2410  * Display navigation to next/previous post when applicable.
2411  *
2412  * @since 4.1.0
2413  *
2414  * @param array $args Optional. See {@see get_the_post_navigation()} for available
2415  *                    arguments. Default empty array.
2416  */
2417 function the_post_navigation( $args = array() ) {
2418         echo get_the_post_navigation( $args );
2419 }
2420
2421 /**
2422  * Return navigation to next/previous set of posts when applicable.
2423  *
2424  * @since 4.1.0
2425  *
2426  * @global WP_Query $wp_query WordPress Query object.
2427  *
2428  * @param array $args {
2429  *     Optional. Default posts navigation arguments. Default empty array.
2430  *
2431  *     @type string $prev_text          Anchor text to display in the previous posts link.
2432  *                                      Default 'Older posts'.
2433  *     @type string $next_text          Anchor text to display in the next posts link.
2434  *                                      Default 'Newer posts'.
2435  *     @type string $screen_reader_text Screen reader text for nav element.
2436  *                                      Default 'Posts navigation'.
2437  * }
2438  * @return string Markup for posts links.
2439  */
2440 function get_the_posts_navigation( $args = array() ) {
2441         $navigation = '';
2442
2443         // Don't print empty markup if there's only one page.
2444         if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2445                 $args = wp_parse_args( $args, array(
2446                         'prev_text'          => __( 'Older posts' ),
2447                         'next_text'          => __( 'Newer posts' ),
2448                         'screen_reader_text' => __( 'Posts navigation' ),
2449                 ) );
2450
2451                 $next_link = get_previous_posts_link( $args['next_text'] );
2452                 $prev_link = get_next_posts_link( $args['prev_text'] );
2453
2454                 if ( $prev_link ) {
2455                         $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
2456                 }
2457
2458                 if ( $next_link ) {
2459                         $navigation .= '<div class="nav-next">' . $next_link . '</div>';
2460                 }
2461
2462                 $navigation = _navigation_markup( $navigation, 'posts-navigation', $args['screen_reader_text'] );
2463         }
2464
2465         return $navigation;
2466 }
2467
2468 /**
2469  * Display navigation to next/previous set of posts when applicable.
2470  *
2471  * @since 4.1.0
2472  *
2473  * @param array $args Optional. See {@see get_the_posts_navigation()} for available
2474  *                    arguments. Default empty array.
2475  */
2476 function the_posts_navigation( $args = array() ) {
2477         echo get_the_posts_navigation( $args );
2478 }
2479
2480 /**
2481  * Return a paginated navigation to next/previous set of posts,
2482  * when applicable.
2483  *
2484  * @since 4.1.0
2485  *
2486  * @param array $args {
2487  *     Optional. Default pagination arguments, {@see paginate_links()}.
2488  *
2489  *     @type string $screen_reader_text Screen reader text for navigation element.
2490  *                                      Default 'Posts navigation'.
2491  * }
2492  * @return string Markup for pagination links.
2493  */
2494 function get_the_posts_pagination( $args = array() ) {
2495         $navigation = '';
2496
2497         // Don't print empty markup if there's only one page.
2498         if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2499                 $args = wp_parse_args( $args, array(
2500                         'mid_size'           => 1,
2501                         'prev_text'          => _x( 'Previous', 'previous post' ),
2502                         'next_text'          => _x( 'Next', 'next post' ),
2503                         'screen_reader_text' => __( 'Posts navigation' ),
2504                 ) );
2505
2506                 // Make sure we get a string back. Plain is the next best thing.
2507                 if ( isset( $args['type'] ) && 'array' == $args['type'] ) {
2508                         $args['type'] = 'plain';
2509                 }
2510
2511                 // Set up paginated links.
2512                 $links = paginate_links( $args );
2513
2514                 if ( $links ) {
2515                         $navigation = _navigation_markup( $links, 'pagination', $args['screen_reader_text'] );
2516                 }
2517         }
2518
2519         return $navigation;
2520 }
2521
2522 /**
2523  * Display a paginated navigation to next/previous set of posts,
2524  * when applicable.
2525  *
2526  * @since 4.1.0
2527  *
2528  * @param array $args Optional. See {@see get_the_posts_pagination()} for available arguments.
2529  *                    Default empty array.
2530  */
2531 function the_posts_pagination( $args = array() ) {
2532         echo get_the_posts_pagination( $args );
2533 }
2534
2535 /**
2536  * Wraps passed links in navigational markup.
2537  *
2538  * @since 4.1.0
2539  * @access private
2540  *
2541  * @param string $links              Navigational links.
2542  * @param string $class              Optional. Custom class for nav element. Default: 'posts-navigation'.
2543  * @param string $screen_reader_text Optional. Screen reader text for nav element. Default: 'Posts navigation'.
2544  * @return string Navigation template tag.
2545  */
2546 function _navigation_markup( $links, $class = 'posts-navigation', $screen_reader_text = '' ) {
2547         if ( empty( $screen_reader_text ) ) {
2548                 $screen_reader_text = __( 'Posts navigation' );
2549         }
2550
2551         $template = '
2552         <nav class="navigation %1$s" role="navigation">
2553                 <h2 class="screen-reader-text">%2$s</h2>
2554                 <div class="nav-links">%3$s</div>
2555         </nav>';
2556
2557         /**
2558          * Filter the navigation markup template.
2559          *
2560          * Note: The filtered template HTML must contain specifiers for the navigation
2561          * class (%1$s), the screen-reader-text value (%2$s), and placement of the
2562          * navigation links (%3$s):
2563          *
2564          *     <nav class="navigation %1$s" role="navigation">
2565          *         <h2 class="screen-reader-text">%2$s</h2>
2566          *         <div class="nav-links">%3$s</div>
2567          *     </nav>
2568          *
2569          * @since 4.4.0
2570          *
2571          * @param string $template The default template.
2572          * @param string $class    The class passed by the calling function.
2573          * @return string Navigation template.
2574          */
2575         $template = apply_filters( 'navigation_markup_template', $template, $class );
2576
2577         return sprintf( $template, sanitize_html_class( $class ), esc_html( $screen_reader_text ), $links );
2578 }
2579
2580 /**
2581  * Retrieve comments page number link.
2582  *
2583  * @since 2.7.0
2584  *
2585  * @global WP_Rewrite $wp_rewrite
2586  *
2587  * @param int $pagenum  Optional. Page number.
2588  * @param int $max_page Optional. The maximum number of comment pages.
2589  * @return string The comments page number link URL.
2590  */
2591 function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
2592         global $wp_rewrite;
2593
2594         $pagenum = (int) $pagenum;
2595
2596         $result = get_permalink();
2597
2598         if ( 'newest' == get_option('default_comments_page') ) {
2599                 if ( $pagenum != $max_page ) {
2600                         if ( $wp_rewrite->using_permalinks() )
2601                                 $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged');
2602                         else
2603                                 $result = add_query_arg( 'cpage', $pagenum, $result );
2604                 }
2605         } elseif ( $pagenum > 1 ) {
2606                 if ( $wp_rewrite->using_permalinks() )
2607                         $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged');
2608                 else
2609                         $result = add_query_arg( 'cpage', $pagenum, $result );
2610         }
2611
2612         $result .= '#comments';
2613
2614         /**
2615          * Filter the comments page number link for the current request.
2616          *
2617          * @since 2.7.0
2618          *
2619          * @param string $result The comments page number link.
2620          */
2621         return apply_filters( 'get_comments_pagenum_link', $result );
2622 }
2623
2624 /**
2625  * Return the link to next comments page.
2626  *
2627  * @since 2.7.1
2628  *
2629  * @global WP_Query $wp_query
2630  *
2631  * @param string $label    Optional. Label for link text.
2632  * @param int    $max_page Optional. Max page.
2633  * @return string|void HTML-formatted link for the next page of comments.
2634  */
2635 function get_next_comments_link( $label = '', $max_page = 0 ) {
2636         global $wp_query;
2637
2638         if ( ! is_singular() )
2639                 return;
2640
2641         $page = get_query_var('cpage');
2642
2643         if ( ! $page ) {
2644                 $page = 1;
2645         }
2646
2647         $nextpage = intval($page) + 1;
2648
2649         if ( empty($max_page) )
2650                 $max_page = $wp_query->max_num_comment_pages;
2651
2652         if ( empty($max_page) )
2653                 $max_page = get_comment_pages_count();
2654
2655         if ( $nextpage > $max_page )
2656                 return;
2657
2658         if ( empty($label) )
2659                 $label = __('Newer Comments &raquo;');
2660
2661         /**
2662          * Filter the anchor tag attributes for the next comments page link.
2663          *
2664          * @since 2.7.0
2665          *
2666          * @param string $attributes Attributes for the anchor tag.
2667          */
2668         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>';
2669 }
2670
2671 /**
2672  * Display the link to next comments page.
2673  *
2674  * @since 2.7.0
2675  *
2676  * @param string $label    Optional. Label for link text.
2677  * @param int    $max_page Optional. Max page.
2678  */
2679 function next_comments_link( $label = '', $max_page = 0 ) {
2680         echo get_next_comments_link( $label, $max_page );
2681 }
2682
2683 /**
2684  * Return the previous comments page link.
2685  *
2686  * @since 2.7.1
2687  *
2688  * @param string $label Optional. Label for comments link text.
2689  * @return string|void HTML-formatted link for the previous page of comments.
2690  */
2691 function get_previous_comments_link( $label = '' ) {
2692         if ( ! is_singular() )
2693                 return;
2694
2695         $page = get_query_var('cpage');
2696
2697         if ( intval($page) <= 1 )
2698                 return;
2699
2700         $prevpage = intval($page) - 1;
2701
2702         if ( empty($label) )
2703                 $label = __('&laquo; Older Comments');
2704
2705         /**
2706          * Filter the anchor tag attributes for the previous comments page link.
2707          *
2708          * @since 2.7.0
2709          *
2710          * @param string $attributes Attributes for the anchor tag.
2711          */
2712         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>';
2713 }
2714
2715 /**
2716  * Display the previous comments page link.
2717  *
2718  * @since 2.7.0
2719  *
2720  * @param string $label Optional. Label for comments link text.
2721  */
2722 function previous_comments_link( $label = '' ) {
2723         echo get_previous_comments_link( $label );
2724 }
2725
2726 /**
2727  * Create pagination links for the comments on the current post.
2728  *
2729  * @see paginate_links()
2730  * @since 2.7.0
2731  *
2732  * @global WP_Rewrite $wp_rewrite
2733  *
2734  * @param string|array $args Optional args. See paginate_links().
2735  * @return string|void Markup for pagination links.
2736  */
2737 function paginate_comments_links($args = array()) {
2738         global $wp_rewrite;
2739
2740         if ( ! is_singular() )
2741                 return;
2742
2743         $page = get_query_var('cpage');
2744         if ( !$page )
2745                 $page = 1;
2746         $max_page = get_comment_pages_count();
2747         $defaults = array(
2748                 'base' => add_query_arg( 'cpage', '%#%' ),
2749                 'format' => '',
2750                 'total' => $max_page,
2751                 'current' => $page,
2752                 'echo' => true,
2753                 'add_fragment' => '#comments'
2754         );
2755         if ( $wp_rewrite->using_permalinks() )
2756                 $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged');
2757
2758         $args = wp_parse_args( $args, $defaults );
2759         $page_links = paginate_links( $args );
2760
2761         if ( $args['echo'] )
2762                 echo $page_links;
2763         else
2764                 return $page_links;
2765 }
2766
2767 /**
2768  * Returns navigation to next/previous set of comments when applicable.
2769  *
2770  * @since 4.4.0
2771  *
2772  * @param array $args {
2773  *     Optional. Default comments navigation arguments.
2774  *
2775  *     @type string $prev_text          Anchor text to display in the previous comments link. Default 'Older comments'.
2776  *     @type string $next_text          Anchor text to display in the next comments link. Default 'Newer comments'.
2777  *     @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'.
2778  * }
2779  * @return string Markup for comments links.
2780  */
2781 function get_the_comments_navigation( $args = array() ) {
2782         $navigation = '';
2783
2784         // Are there comments to navigate through?
2785         if ( get_comment_pages_count() > 1 ) {
2786                 $args = wp_parse_args( $args, array(
2787                         'prev_text'          => __( 'Older comments' ),
2788                         'next_text'          => __( 'Newer comments' ),
2789                         'screen_reader_text' => __( 'Comments navigation' ),
2790                 ) );
2791
2792                 $prev_link = get_previous_comments_link( $args['prev_text'] );
2793                 $next_link = get_next_comments_link( $args['next_text'] );
2794
2795                 if ( $prev_link ) {
2796                         $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
2797                 }
2798
2799                 if ( $next_link ) {
2800                         $navigation .= '<div class="nav-next">' . $next_link . '</div>';
2801                 }
2802
2803                 $navigation = _navigation_markup( $navigation, 'comment-navigation', $args['screen_reader_text'] );
2804         }
2805
2806         return $navigation;
2807 }
2808
2809 /**
2810  * Displays navigation to next/previous set of comments when applicable.
2811  *
2812  * @since 4.4.0
2813  *
2814  * @param array $args See {@see get_the_comments_navigation()} for available arguments.
2815  */
2816 function the_comments_navigation( $args = array() ) {
2817         echo get_the_comments_navigation( $args );
2818 }
2819
2820 /**
2821  * Returns a paginated navigation to next/previous set of comments,
2822  * when applicable.
2823  *
2824  * @since 4.4.0
2825  *
2826  * @see paginate_comments_links()
2827  *
2828  * @param array $args {
2829  *     Optional. Default pagination arguments.
2830  *
2831  *     @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'.
2832  * }
2833  * @return string Markup for pagination links.
2834  */
2835 function get_the_comments_pagination( $args = array() ) {
2836         $navigation = '';
2837         $args       = wp_parse_args( $args, array(
2838                 'screen_reader_text' => __( 'Comments navigation' ),
2839         ) );
2840         $args['echo'] = false;
2841
2842         // Make sure we get plain links, so we get a string we can work with.
2843         $args['type'] = 'plain';
2844
2845         $links = paginate_comments_links( $args );
2846
2847         if ( $links ) {
2848                 $navigation = _navigation_markup( $links, 'comments-pagination', $args['screen_reader_text'] );
2849         }
2850
2851         return $navigation;
2852 }
2853
2854 /**
2855  * Displays a paginated navigation to next/previous set of comments,
2856  * when applicable.
2857  *
2858  * @since 4.4.0
2859  *
2860  * @param array $args See {@see get_the_comments_pagination()} for available arguments.
2861  */
2862 function the_comments_pagination( $args = array() ) {
2863         echo get_the_comments_pagination( $args );
2864 }
2865
2866 /**
2867  * Retrieve the Press This bookmarklet link.
2868  *
2869  * Use this in 'a' element 'href' attribute.
2870  *
2871  * @since 2.6.0
2872  *
2873  * @global bool          $is_IE
2874  * @global string        $wp_version
2875  * @global WP_Press_This $wp_press_this
2876  *
2877  * @return string The Press This bookmarklet link URL.
2878  */
2879 function get_shortcut_link() {
2880         global $is_IE, $wp_version;
2881
2882         include_once( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
2883         $bookmarklet_version = $GLOBALS['wp_press_this']->version;
2884         $link = '';
2885
2886         if ( $is_IE ) {
2887                 /**
2888                  * Return the old/shorter bookmarklet code for MSIE 8 and lower,
2889                  * since they only support a max length of ~2000 characters for
2890                  * bookmark[let] URLs, which is way to small for our smarter one.
2891                  * Do update the version number so users do not get the "upgrade your
2892                  * bookmarklet" notice when using PT in those browsers.
2893                  */
2894                 $ua = $_SERVER['HTTP_USER_AGENT'];
2895
2896                 if ( ! empty( $ua ) && preg_match( '/\bMSIE (\d)/', $ua, $matches ) && (int) $matches[1] <= 8 ) {
2897                         $url = wp_json_encode( admin_url( 'press-this.php' ) );
2898
2899                         $link = 'javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,' .
2900                                 's=(e?e():(k)?k():(x?x.createRange().text:0)),f=' . $url . ',l=d.location,e=encodeURIComponent,' .
2901                                 'u=f+"?u="+e(l.href)+"&t="+e(d.title)+"&s="+e(s)+"&v=' . $bookmarklet_version . '";' .
2902                                 'a=function(){if(!w.open(u,"t","toolbar=0,resizable=1,scrollbars=1,status=1,width=600,height=700"))l.href=u;};' .
2903                                 'if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else a();void(0)';
2904                 }
2905         }
2906
2907         if ( empty( $link ) ) {
2908                 $src = @file_get_contents( ABSPATH . 'wp-admin/js/bookmarklet.min.js' );
2909
2910                 if ( $src ) {
2911                         $url = wp_json_encode( admin_url( 'press-this.php' ) . '?v=' . $bookmarklet_version );
2912                         $link = 'javascript:' . str_replace( 'window.pt_url', $url, $src );
2913                 }
2914         }
2915
2916         $link = str_replace( array( "\r", "\n", "\t" ),  '', $link );
2917
2918         /**
2919          * Filter the Press This bookmarklet link.
2920          *
2921          * @since 2.6.0
2922          *
2923          * @param string $link The Press This bookmarklet link.
2924          */
2925         return apply_filters( 'shortcut_link', $link );
2926 }
2927
2928 /**
2929  * Retrieve the URL for the current site where the front end is accessible.
2930  *
2931  * Returns the 'home' option with the appropriate protocol, 'https' if
2932  * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
2933  * `is_ssl()` is overridden.
2934  *
2935  * @since 3.0.0
2936  *
2937  * @param  string      $path   Optional. Path relative to the home url. Default empty.
2938  * @param  string|null $scheme Optional. Scheme to give the home url context. Accepts
2939  *                             'http', 'https', 'relative', 'rest', or null. Default null.
2940  * @return string Home url link with optional path appended.
2941  */
2942 function home_url( $path = '', $scheme = null ) {
2943         return get_home_url( null, $path, $scheme );
2944 }
2945
2946 /**
2947  * Retrieve the URL for a given site where the front end is accessible.
2948  *
2949  * Returns the 'home' option with the appropriate protocol, 'https' if
2950  * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
2951  * `is_ssl()` is
2952  * overridden.
2953  *
2954  * @since 3.0.0
2955  *
2956  * @global string $pagenow
2957  *
2958  * @param  int         $blog_id Optional. Site ID. Default null (current site).
2959  * @param  string      $path    Optional. Path relative to the home URL. Default empty.
2960  * @param  string|null $scheme  Optional. Scheme to give the home URL context. Accepts
2961  *                              'http', 'https', 'relative', 'rest', or null. Default null.
2962  * @return string Home URL link with optional path appended.
2963  */
2964 function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
2965         global $pagenow;
2966
2967         $orig_scheme = $scheme;
2968
2969         if ( empty( $blog_id ) || !is_multisite() ) {
2970                 $url = get_option( 'home' );
2971         } else {
2972                 switch_to_blog( $blog_id );
2973                 $url = get_option( 'home' );
2974                 restore_current_blog();
2975         }
2976
2977         if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
2978                 if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow )
2979                         $scheme = 'https';
2980                 else
2981                         $scheme = parse_url( $url, PHP_URL_SCHEME );
2982         }
2983
2984         $url = set_url_scheme( $url, $scheme );
2985
2986         if ( $path && is_string( $path ) )
2987                 $url .= '/' . ltrim( $path, '/' );
2988
2989         /**
2990          * Filter the home URL.
2991          *
2992          * @since 3.0.0
2993          *
2994          * @param string      $url         The complete home URL including scheme and path.
2995          * @param string      $path        Path relative to the home URL. Blank string if no path is specified.
2996          * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https',
2997          *                                 'relative', 'rest', or null.
2998          * @param int|null    $blog_id     Site ID, or null for the current site.
2999          */
3000         return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
3001 }
3002
3003 /**
3004  * Retrieve the URL for the current site where WordPress application files
3005  * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
3006  *
3007  * Returns the 'site_url' option with the appropriate protocol, 'https' if
3008  * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
3009  * overridden.
3010  *
3011  * @since 3.0.0
3012  *
3013  * @param string $path   Optional. Path relative to the site url.
3014  * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
3015  * @return string Site url link with optional path appended.
3016  */
3017 function site_url( $path = '', $scheme = null ) {
3018         return get_site_url( null, $path, $scheme );
3019 }
3020
3021 /**
3022  * Retrieve the URL for a given site where WordPress application files
3023  * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
3024  *
3025  * Returns the 'site_url' option with the appropriate protocol, 'https' if
3026  * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
3027  * `is_ssl()` is overridden.
3028  *
3029  * @since 3.0.0
3030  *
3031  * @param int    $blog_id Optional. Site ID. Default null (current site).
3032  * @param string $path    Optional. Path relative to the site url. Default empty.
3033  * @param string $scheme  Optional. Scheme to give the site url context. Accepts
3034  *                        'http', 'https', 'login', 'login_post', 'admin', or
3035  *                        'relative'. Default null.
3036  * @return string Site url link with optional path appended.
3037  */
3038 function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
3039         if ( empty( $blog_id ) || !is_multisite() ) {
3040                 $url = get_option( 'siteurl' );
3041         } else {
3042                 switch_to_blog( $blog_id );
3043                 $url = get_option( 'siteurl' );
3044                 restore_current_blog();
3045         }
3046
3047         $url = set_url_scheme( $url, $scheme );
3048
3049         if ( $path && is_string( $path ) )
3050                 $url .= '/' . ltrim( $path, '/' );
3051
3052         /**
3053          * Filter the site URL.
3054          *
3055          * @since 2.7.0
3056          *
3057          * @param string      $url     The complete site URL including scheme and path.
3058          * @param string      $path    Path relative to the site URL. Blank string if no path is specified.
3059          * @param string|null $scheme  Scheme to give the site URL context. Accepts 'http', 'https', 'login',
3060          *                             'login_post', 'admin', 'relative' or null.
3061          * @param int|null    $blog_id Site ID, or null for the current site.
3062          */
3063         return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
3064 }
3065
3066 /**
3067  * Retrieve the url to the admin area for the current site.
3068  *
3069  * @since 2.6.0
3070  *
3071  * @param string $path   Optional path relative to the admin url.
3072  * @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.
3073  * @return string Admin url link with optional path appended.
3074  */
3075 function admin_url( $path = '', $scheme = 'admin' ) {
3076         return get_admin_url( null, $path, $scheme );
3077 }
3078
3079 /**
3080  * Retrieves the url to the admin area for a given site.
3081  *
3082  * @since 3.0.0
3083  *
3084  * @param int    $blog_id Optional. Site ID. Default null (current site).
3085  * @param string $path    Optional. Path relative to the admin url. Default empty.
3086  * @param string $scheme  Optional. The scheme to use. Accepts 'http' or 'https',
3087  *                        to force those schemes. Default 'admin', which obeys
3088  *                        {@see force_ssl_admin()} and {@see is_ssl()}.
3089  * @return string Admin url link with optional path appended.
3090  */
3091 function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
3092         $url = get_site_url($blog_id, 'wp-admin/', $scheme);
3093
3094         if ( $path && is_string( $path ) )
3095                 $url .= ltrim( $path, '/' );
3096
3097         /**
3098          * Filter the admin area URL.
3099          *
3100          * @since 2.8.0
3101          *
3102          * @param string   $url     The complete admin area URL including scheme and path.
3103          * @param string   $path    Path relative to the admin area URL. Blank string if no path is specified.
3104          * @param int|null $blog_id Site ID, or null for the current site.
3105          */
3106         return apply_filters( 'admin_url', $url, $path, $blog_id );
3107 }
3108
3109 /**
3110  * Retrieve the url to the includes directory.
3111  *
3112  * @since 2.6.0
3113  *
3114  * @param string $path   Optional. Path relative to the includes url.
3115  * @param string $scheme Optional. Scheme to give the includes url context.
3116  * @return string Includes url link with optional path appended.
3117  */
3118 function includes_url( $path = '', $scheme = null ) {
3119         $url = site_url( '/' . WPINC . '/', $scheme );
3120
3121         if ( $path && is_string( $path ) )
3122                 $url .= ltrim($path, '/');
3123
3124         /**
3125          * Filter the URL to the includes directory.
3126          *
3127          * @since 2.8.0
3128          *
3129          * @param string $url  The complete URL to the includes directory including scheme and path.
3130          * @param string $path Path relative to the URL to the wp-includes directory. Blank string
3131          *                     if no path is specified.
3132          */
3133         return apply_filters( 'includes_url', $url, $path );
3134 }
3135
3136 /**
3137  * Retrieve the url to the content directory.
3138  *
3139  * @since 2.6.0
3140  *
3141  * @param string $path Optional. Path relative to the content url.
3142  * @return string Content url link with optional path appended.
3143  */
3144 function content_url($path = '') {
3145         $url = set_url_scheme( WP_CONTENT_URL );
3146
3147         if ( $path && is_string( $path ) )
3148                 $url .= '/' . ltrim($path, '/');
3149
3150         /**
3151          * Filter the URL to the content directory.
3152          *
3153          * @since 2.8.0
3154          *
3155          * @param string $url  The complete URL to the content directory including scheme and path.
3156          * @param string $path Path relative to the URL to the content directory. Blank string
3157          *                     if no path is specified.
3158          */
3159         return apply_filters( 'content_url', $url, $path);
3160 }
3161
3162 /**
3163  * Retrieve a URL within the plugins or mu-plugins directory.
3164  *
3165  * Defaults to the plugins directory URL if no arguments are supplied.
3166  *
3167  * @since 2.6.0
3168  *
3169  * @param  string $path   Optional. Extra path appended to the end of the URL, including
3170  *                        the relative directory if $plugin is supplied. Default empty.
3171  * @param  string $plugin Optional. A full path to a file inside a plugin or mu-plugin.
3172  *                        The URL will be relative to its directory. Default empty.
3173  *                        Typically this is done by passing `__FILE__` as the argument.
3174  * @return string Plugins URL link with optional paths appended.
3175  */
3176 function plugins_url( $path = '', $plugin = '' ) {
3177
3178         $path = wp_normalize_path( $path );
3179         $plugin = wp_normalize_path( $plugin );
3180         $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
3181
3182         if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) )
3183                 $url = WPMU_PLUGIN_URL;
3184         else
3185                 $url = WP_PLUGIN_URL;
3186
3187
3188         $url = set_url_scheme( $url );
3189
3190         if ( !empty($plugin) && is_string($plugin) ) {
3191                 $folder = dirname(plugin_basename($plugin));
3192                 if ( '.' != $folder )
3193                         $url .= '/' . ltrim($folder, '/');
3194         }
3195
3196         if ( $path && is_string( $path ) )
3197                 $url .= '/' . ltrim($path, '/');
3198
3199         /**
3200          * Filter the URL to the plugins directory.
3201          *
3202          * @since 2.8.0
3203          *
3204          * @param string $url    The complete URL to the plugins directory including scheme and path.
3205          * @param string $path   Path relative to the URL to the plugins directory. Blank string
3206          *                       if no path is specified.
3207          * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
3208          *                       is specified.
3209          */
3210         return apply_filters( 'plugins_url', $url, $path, $plugin );
3211 }
3212
3213 /**
3214  * Retrieve the site url for the current network.
3215  *
3216  * Returns the site url with the appropriate protocol, 'https' if
3217  * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
3218  * overridden.
3219  *
3220  * @since 3.0.0
3221  *
3222  * @param string $path   Optional. Path relative to the site url.
3223  * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
3224  * @return string Site url link with optional path appended.
3225  */
3226 function network_site_url( $path = '', $scheme = null ) {
3227         if ( ! is_multisite() )
3228                 return site_url($path, $scheme);
3229
3230         $current_site = get_current_site();
3231
3232         if ( 'relative' == $scheme )
3233                 $url = $current_site->path;
3234         else
3235                 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
3236
3237         if ( $path && is_string( $path ) )
3238                 $url .= ltrim( $path, '/' );
3239
3240         /**
3241          * Filter the network site URL.
3242          *
3243          * @since 3.0.0
3244          *
3245          * @param string      $url    The complete network site URL including scheme and path.
3246          * @param string      $path   Path relative to the network site URL. Blank string if
3247          *                            no path is specified.
3248          * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',
3249          *                            'relative' or null.
3250          */
3251         return apply_filters( 'network_site_url', $url, $path, $scheme );
3252 }
3253
3254 /**
3255  * Retrieves the home url for the current network.
3256  *
3257  * Returns the home url with the appropriate protocol, 'https' {@see is_ssl()}
3258  * and 'http' otherwise. If `$scheme` is 'http' or 'https', `is_ssl()` is
3259  * overridden.
3260  *
3261  * @since 3.0.0
3262  *
3263  * @param  string $path   Optional. Path relative to the home url. Default empty.
3264  * @param  string $scheme Optional. Scheme to give the home url context. Accepts
3265  *                        'http', 'https', or 'relative'. Default null.
3266  * @return string Home url link with optional path appended.
3267  */
3268 function network_home_url( $path = '', $scheme = null ) {
3269         if ( ! is_multisite() )
3270                 return home_url($path, $scheme);
3271
3272         $current_site = get_current_site();
3273         $orig_scheme = $scheme;
3274
3275         if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
3276                 $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
3277
3278         if ( 'relative' == $scheme )
3279                 $url = $current_site->path;
3280         else
3281                 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
3282
3283         if ( $path && is_string( $path ) )
3284                 $url .= ltrim( $path, '/' );
3285
3286         /**
3287          * Filter the network home URL.
3288          *
3289          * @since 3.0.0
3290          *
3291          * @param string      $url         The complete network home URL including scheme and path.
3292          * @param string      $path        Path relative to the network home URL. Blank string
3293          *                                 if no path is specified.
3294          * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',
3295          *                                 'relative' or null.
3296          */
3297         return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
3298 }
3299
3300 /**
3301  * Retrieve the url to the admin area for the network.
3302  *
3303  * @since 3.0.0
3304  *
3305  * @param string $path   Optional path relative to the admin url.
3306  * @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.
3307  * @return string Admin url link with optional path appended.
3308  */
3309 function network_admin_url( $path = '', $scheme = 'admin' ) {
3310         if ( ! is_multisite() )
3311                 return admin_url( $path, $scheme );
3312
3313         $url = network_site_url('wp-admin/network/', $scheme);
3314
3315         if ( $path && is_string( $path ) )
3316                 $url .= ltrim($path, '/');
3317
3318         /**
3319          * Filter the network admin URL.
3320          *
3321          * @since 3.0.0
3322          *
3323          * @param string $url  The complete network admin URL including scheme and path.
3324          * @param string $path Path relative to the network admin URL. Blank string if
3325          *                     no path is specified.
3326          */
3327         return apply_filters( 'network_admin_url', $url, $path );
3328 }
3329
3330 /**
3331  * Retrieve the url to the admin area for the current user.
3332  *
3333  * @since 3.0.0
3334  *
3335  * @param string $path   Optional path relative to the admin url.
3336  * @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.
3337  * @return string Admin url link with optional path appended.
3338  */
3339 function user_admin_url( $path = '', $scheme = 'admin' ) {
3340         $url = network_site_url('wp-admin/user/', $scheme);
3341
3342         if ( $path && is_string( $path ) )
3343                 $url .= ltrim($path, '/');
3344
3345         /**
3346          * Filter the user admin URL for the current user.
3347          *
3348          * @since 3.1.0
3349          *
3350          * @param string $url  The complete URL including scheme and path.
3351          * @param string $path Path relative to the URL. Blank string if
3352          *                     no path is specified.
3353          */
3354         return apply_filters( 'user_admin_url', $url, $path );
3355 }
3356
3357 /**
3358  * Retrieve the url to the admin area for either the current site or the network depending on context.
3359  *
3360  * @since 3.1.0
3361  *
3362  * @param string $path   Optional path relative to the admin url.
3363  * @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.
3364  * @return string Admin url link with optional path appended.
3365  */
3366 function self_admin_url($path = '', $scheme = 'admin') {
3367         if ( is_network_admin() )
3368                 return network_admin_url($path, $scheme);
3369         elseif ( is_user_admin() )
3370                 return user_admin_url($path, $scheme);
3371         else
3372                 return admin_url($path, $scheme);
3373 }
3374
3375 /**
3376  * Sets the scheme for a URL.
3377  *
3378  * @since 3.4.0
3379  * @since 4.4.0 The 'rest' scheme was added.
3380  *
3381  * @param string      $url    Absolute url that includes a scheme
3382  * @param string|null $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login',
3383  *                            'login_post', 'admin', 'relative', 'rest', 'rpc', or null. Default null.
3384  * @return string $url URL with chosen scheme.
3385  */
3386 function set_url_scheme( $url, $scheme = null ) {
3387         $orig_scheme = $scheme;
3388
3389         if ( ! $scheme ) {
3390                 $scheme = is_ssl() ? 'https' : 'http';
3391         } elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) {
3392                 $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
3393         } elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) {
3394                 $scheme = is_ssl() ? 'https' : 'http';
3395         }
3396
3397         $url = trim( $url );
3398         if ( substr( $url, 0, 2 ) === '//' )
3399                 $url = 'http:' . $url;
3400
3401         if ( 'relative' == $scheme ) {
3402                 $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
3403                 if ( $url !== '' && $url[0] === '/' )
3404                         $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" );
3405         } else {
3406                 $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
3407         }
3408
3409         /**
3410          * Filter the resulting URL after setting the scheme.
3411          *
3412          * @since 3.4.0
3413          *
3414          * @param string      $url         The complete URL including scheme and path.
3415          * @param string      $scheme      Scheme applied to the URL. One of 'http', 'https', or 'relative'.
3416          * @param string|null $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
3417          *                                 'login_post', 'admin', 'relative', 'rest', 'rpc', or null.
3418          */
3419         return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
3420 }
3421
3422 /**
3423  * Get the URL to the user's dashboard.
3424  *
3425  * If a user does not belong to any site, the global user dashboard is used. If the user belongs to the current site,
3426  * the dashboard for the current site is returned. If the user cannot edit the current site, the dashboard to the user's
3427  * primary site is returned.
3428  *
3429  * @since 3.1.0
3430  *
3431  * @param int    $user_id Optional. User ID. Defaults to current user.
3432  * @param string $path    Optional path relative to the dashboard. Use only paths known to both site and user admins.
3433  * @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.
3434  * @return string Dashboard url link with optional path appended.
3435  */
3436 function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
3437         $user_id = $user_id ? (int) $user_id : get_current_user_id();
3438
3439         $blogs = get_blogs_of_user( $user_id );
3440         if ( ! is_super_admin() && empty($blogs) ) {
3441                 $url = user_admin_url( $path, $scheme );
3442         } elseif ( ! is_multisite() ) {
3443                 $url = admin_url( $path, $scheme );
3444         } else {
3445                 $current_blog = get_current_blog_id();
3446                 if ( $current_blog  && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
3447                         $url = admin_url( $path, $scheme );
3448                 } else {
3449                         $active = get_active_blog_for_user( $user_id );
3450                         if ( $active )
3451                                 $url = get_admin_url( $active->blog_id, $path, $scheme );
3452                         else
3453                                 $url = user_admin_url( $path, $scheme );
3454                 }
3455         }
3456
3457         /**
3458          * Filter the dashboard URL for a user.
3459          *
3460          * @since 3.1.0
3461          *
3462          * @param string $url     The complete URL including scheme and path.
3463          * @param int    $user_id The user ID.
3464          * @param string $path    Path relative to the URL. Blank string if no path is specified.
3465          * @param string $scheme  Scheme to give the URL context. Accepts 'http', 'https', 'login',
3466          *                        'login_post', 'admin', 'relative' or null.
3467          */
3468         return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
3469 }
3470
3471 /**
3472  * Get the URL to the user's profile editor.
3473  *
3474  * @since 3.1.0
3475  *
3476  * @param int    $user_id Optional. User ID. Defaults to current user.
3477  * @param string $scheme  The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
3478  *                        'http' or 'https' can be passed to force those schemes.
3479  * @return string Dashboard url link with optional path appended.
3480  */
3481 function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) {
3482         $user_id = $user_id ? (int) $user_id : get_current_user_id();
3483
3484         if ( is_user_admin() )
3485                 $url = user_admin_url( 'profile.php', $scheme );
3486         elseif ( is_network_admin() )
3487                 $url = network_admin_url( 'profile.php', $scheme );
3488         else
3489                 $url = get_dashboard_url( $user_id, 'profile.php', $scheme );
3490
3491         /**
3492          * Filter the URL for a user's profile editor.
3493          *
3494          * @since 3.1.0
3495          *
3496          * @param string $url     The complete URL including scheme and path.
3497          * @param int    $user_id The user ID.
3498          * @param string $scheme  Scheme to give the URL context. Accepts 'http', 'https', 'login',
3499          *                        'login_post', 'admin', 'relative' or null.
3500          */
3501         return apply_filters( 'edit_profile_url', $url, $user_id, $scheme);
3502 }
3503
3504 /**
3505  * Output rel=canonical for singular queries.
3506  *
3507  * @since 2.9.0
3508  */
3509 function rel_canonical() {
3510         if ( ! is_singular() ) {
3511                 return;
3512         }
3513
3514         if ( ! $id = get_queried_object_id() ) {
3515                 return;
3516         }
3517
3518         $url = get_permalink( $id );
3519
3520         $page = get_query_var( 'page' );
3521         if ( $page >= 2 ) {
3522                 if ( '' == get_option( 'permalink_structure' ) ) {
3523                         $url = add_query_arg( 'page', $page, $url );
3524                 } else {
3525                         $url = trailingslashit( $url ) . user_trailingslashit( $page, 'single_paged' );
3526                 }
3527         }
3528
3529         $cpage = get_query_var( 'cpage' );
3530         if ( $cpage ) {
3531                 $url = get_comments_pagenum_link( $cpage );
3532         }
3533         echo '<link rel="canonical" href="' . esc_url( $url ) . "\" />\n";
3534 }
3535
3536 /**
3537  * Return a shortlink for a post, page, attachment, or a site.
3538  *
3539  * This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to
3540  * provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts.
3541  * Plugins can short-circuit this function via the pre_get_shortlink filter or filter the output
3542  * via the get_shortlink filter.
3543  *
3544  * @since 3.0.0.
3545  *
3546  * @param int    $id          A post or site id. Default is 0, which means the current post or site.
3547  * @param string $context     Whether the id is a 'site' id, 'post' id, or 'media' id.
3548  *                            If 'post', the post_type of the post is consulted.
3549  *                            If 'query', the current query is consulted to determine the id and context.
3550  *                            Default is 'post'.
3551  * @param bool   $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
3552  * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
3553  */
3554 function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
3555         /**
3556          * Filter whether to preempt generating a shortlink for the given post.
3557          *
3558          * Passing a truthy value to the filter will effectively short-circuit the
3559          * shortlink-generation process, returning that value instead.
3560          *
3561          * @since 3.0.0
3562          *
3563          * @param bool|string $return      Short-circuit return value. Either false or a URL string.
3564          * @param int         $id          Post ID, or 0 for the current post.
3565          * @param string      $context     The context for the link. One of 'post' or 'query',
3566          * @param bool        $allow_slugs Whether to allow post slugs in the shortlink.
3567          */
3568         $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );
3569
3570         if ( false !== $shortlink ) {
3571                 return $shortlink;
3572         }
3573
3574         $post_id = 0;
3575         if ( 'query' == $context && is_singular() ) {
3576                 $post_id = get_queried_object_id();
3577                 $post = get_post( $post_id );
3578         } elseif ( 'post' == $context ) {
3579                 $post = get_post( $id );
3580                 if ( ! empty( $post->ID ) )
3581                         $post_id = $post->ID;
3582         }
3583
3584         $shortlink = '';
3585
3586         // Return p= link for all public post types.
3587         if ( ! empty( $post_id ) ) {
3588                 $post_type = get_post_type_object( $post->post_type );
3589
3590                 if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) {
3591                         $shortlink = home_url( '/' );
3592                 } elseif ( $post_type->public ) {
3593                         $shortlink = home_url( '?p=' . $post_id );
3594                 }
3595         }
3596
3597         /**
3598          * Filter the shortlink for a post.
3599          *
3600          * @since 3.0.0
3601          *
3602          * @param string $shortlink   Shortlink URL.
3603          * @param int    $id          Post ID, or 0 for the current post.
3604          * @param string $context     The context for the link. One of 'post' or 'query',
3605          * @param bool   $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
3606          */
3607         return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
3608 }
3609
3610 /**
3611  *  Inject rel=shortlink into head if a shortlink is defined for the current page.
3612  *
3613  *  Attached to the wp_head action.
3614  *
3615  * @since 3.0.0
3616  */
3617 function wp_shortlink_wp_head() {
3618         $shortlink = wp_get_shortlink( 0, 'query' );
3619
3620         if ( empty( $shortlink ) )
3621                 return;
3622
3623         echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
3624 }
3625
3626 /**
3627  * Send a Link: rel=shortlink header if a shortlink is defined for the current page.
3628  *
3629  * Attached to the wp action.
3630  *
3631  * @since 3.0.0
3632  */
3633 function wp_shortlink_header() {
3634         if ( headers_sent() )
3635                 return;
3636
3637         $shortlink = wp_get_shortlink(0, 'query');
3638
3639         if ( empty($shortlink) )
3640                 return;
3641
3642         header('Link: <' . $shortlink . '>; rel=shortlink', false);
3643 }
3644
3645 /**
3646  * Display the Short Link for a Post
3647  *
3648  * Must be called from inside "The Loop"
3649  *
3650  * Call like the_shortlink(__('Shortlinkage FTW'))
3651  *
3652  * @since 3.0.0
3653  *
3654  * @param string $text   Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
3655  * @param string $title  Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
3656  * @param string $before Optional HTML to display before the link.
3657  * @param string $after  Optional HTML to display after the link.
3658  */
3659 function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
3660         $post = get_post();
3661
3662         if ( empty( $text ) )
3663                 $text = __('This is the short link.');
3664
3665         if ( empty( $title ) )
3666                 $title = the_title_attribute( array( 'echo' => false ) );
3667
3668         $shortlink = wp_get_shortlink( $post->ID );
3669
3670         if ( !empty( $shortlink ) ) {
3671                 $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
3672
3673                 /**
3674                  * Filter the shortlink anchor tag for a post.
3675                  *
3676                  * @since 3.0.0
3677                  *
3678                  * @param string $link      Shortlink anchor tag.
3679                  * @param string $shortlink Shortlink URL.
3680                  * @param string $text      Shortlink's text.
3681                  * @param string $title     Shortlink's title attribute.
3682                  */
3683                 $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
3684                 echo $before, $link, $after;
3685         }
3686 }
3687
3688
3689 /**
3690  * Retrieve the avatar URL.
3691  *
3692  * @since 4.2.0
3693  *
3694  * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,
3695  *                           user email, WP_User object, WP_Post object, or WP_Comment object.
3696  * @param array $args {
3697  *     Optional. Arguments to return instead of the default arguments.
3698  *
3699  *     @type int    $size           Height and width of the avatar in pixels. Default 96.
3700  *     @type string $default        URL for the default image or a default type. Accepts '404' (return
3701  *                                  a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
3702  *                                  'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
3703  *                                  or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or
3704  *                                  'gravatar_default' (the Gravatar logo). Default is the value of the
3705  *                                  'avatar_default' option, with a fallback of 'mystery'.
3706  *     @type bool   $force_default  Whether to always show the default image, never the Gravatar. Default false.
3707  *     @type string $rating         What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
3708  *                                  judged in that order. Default is the value of the 'avatar_rating' option.
3709  *     @type string $scheme         URL scheme to use. See set_url_scheme() for accepted values.
3710  *                                  Default null.
3711  *     @type array  $processed_args When the function returns, the value will be the processed/sanitized $args
3712  *                                  plus a "found_avatar" guess. Pass as a reference. Default null.
3713  * }
3714  * @return false|string The URL of the avatar we found, or false if we couldn't find an avatar.
3715  */
3716 function get_avatar_url( $id_or_email, $args = null ) {
3717         $args = get_avatar_data( $id_or_email, $args );
3718         return $args['url'];
3719 }
3720
3721 /**
3722  * Retrieve default data about the avatar.
3723  *
3724  * @since 4.2.0
3725  *
3726  * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
3727  *                            user email, WP_User object, WP_Post object, or WP_Comment object.
3728  * @param array $args {
3729  *     Optional. Arguments to return instead of the default arguments.
3730  *
3731  *     @type int    $size           Height and width of the avatar image file in pixels. Default 96.
3732  *     @type int    $height         Display height of the avatar in pixels. Defaults to $size.
3733  *     @type int    $width          Display width of the avatar in pixels. Defaults to $size.
3734  *     @type string $default        URL for the default image or a default type. Accepts '404' (return
3735  *                                  a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
3736  *                                  'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
3737  *                                  or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or
3738  *                                  'gravatar_default' (the Gravatar logo). Default is the value of the
3739  *                                  'avatar_default' option, with a fallback of 'mystery'.
3740  *     @type bool   $force_default  Whether to always show the default image, never the Gravatar. Default false.
3741  *     @type string $rating         What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
3742  *                                  judged in that order. Default is the value of the 'avatar_rating' option.
3743  *     @type string $scheme         URL scheme to use. See set_url_scheme() for accepted values.
3744  *                                  Default null.
3745  *     @type array  $processed_args When the function returns, the value will be the processed/sanitized $args
3746  *                                  plus a "found_avatar" guess. Pass as a reference. Default null.
3747  *     @type string $extra_attr     HTML attributes to insert in the IMG element. Is not sanitized. Default empty.
3748  * }
3749  * @return array $processed_args {
3750  *     Along with the arguments passed in `$args`, this will contain a couple of extra arguments.
3751  *
3752  *     @type bool   $found_avatar True if we were able to find an avatar for this user,
3753  *                                false or not set if we couldn't.
3754  *     @type string $url          The URL of the avatar we found.
3755  * }
3756  */
3757 function get_avatar_data( $id_or_email, $args = null ) {
3758         $args = wp_parse_args( $args, array(
3759                 'size'           => 96,
3760                 'height'         => null,
3761                 'width'          => null,
3762                 'default'        => get_option( 'avatar_default', 'mystery' ),
3763                 'force_default'  => false,
3764                 'rating'         => get_option( 'avatar_rating' ),
3765                 'scheme'         => null,
3766                 'processed_args' => null, // if used, should be a reference
3767                 'extra_attr'     => '',
3768         ) );
3769
3770         if ( is_numeric( $args['size'] ) ) {
3771                 $args['size'] = absint( $args['size'] );
3772                 if ( ! $args['size'] ) {
3773                         $args['size'] = 96;
3774                 }
3775         } else {
3776                 $args['size'] = 96;
3777         }
3778
3779         if ( is_numeric( $args['height'] ) ) {
3780                 $args['height'] = absint( $args['height'] );
3781                 if ( ! $args['height'] ) {
3782                         $args['height'] = $args['size'];
3783                 }
3784         } else {
3785                 $args['height'] = $args['size'];
3786         }
3787
3788         if ( is_numeric( $args['width'] ) ) {
3789                 $args['width'] = absint( $args['width'] );
3790                 if ( ! $args['width'] ) {
3791                         $args['width'] = $args['size'];
3792                 }
3793         } else {
3794                 $args['width'] = $args['size'];
3795         }
3796
3797         if ( empty( $args['default'] ) ) {
3798                 $args['default'] = get_option( 'avatar_default', 'mystery' );
3799         }
3800
3801         switch ( $args['default'] ) {
3802                 case 'mm' :
3803                 case 'mystery' :
3804                 case 'mysteryman' :
3805                         $args['default'] = 'mm';
3806                         break;
3807                 case 'gravatar_default' :
3808                         $args['default'] = false;
3809                         break;
3810         }
3811
3812         $args['force_default'] = (bool) $args['force_default'];
3813
3814         $args['rating'] = strtolower( $args['rating'] );
3815
3816         $args['found_avatar'] = false;
3817
3818         /**
3819          * Filter whether to retrieve the avatar URL early.
3820          *
3821          * Passing a non-null value in the 'url' member of the return array will
3822          * effectively short circuit get_avatar_data(), passing the value through
3823          * the {@see 'get_avatar_data'} filter and returning early.
3824          *
3825          * @since 4.2.0
3826          *
3827          * @param array  $args        Arguments passed to get_avatar_data(), after processing.
3828          * @param mixed  $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
3829          *                            user email, WP_User object, WP_Post object, or WP_Comment object.
3830          */
3831         $args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email );
3832
3833         if ( isset( $args['url'] ) && ! is_null( $args['url'] ) ) {
3834                 /** This filter is documented in wp-includes/link-template.php */
3835                 return apply_filters( 'get_avatar_data', $args, $id_or_email );
3836         }
3837
3838         $email_hash = '';
3839         $user = $email = false;
3840
3841         if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
3842                 $id_or_email = get_comment( $id_or_email );
3843         }
3844
3845         // Process the user identifier.
3846         if ( is_numeric( $id_or_email ) ) {
3847                 $user = get_user_by( 'id', absint( $id_or_email ) );
3848         } elseif ( is_string( $id_or_email ) ) {
3849                 if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
3850                         // md5 hash
3851                         list( $email_hash ) = explode( '@', $id_or_email );
3852                 } else {
3853                         // email address
3854                         $email = $id_or_email;
3855                 }
3856         } elseif ( $id_or_email instanceof WP_User ) {
3857                 // User Object
3858                 $user = $id_or_email;
3859         } elseif ( $id_or_email instanceof WP_Post ) {
3860                 // Post Object
3861                 $user = get_user_by( 'id', (int) $id_or_email->post_author );
3862         } elseif ( $id_or_email instanceof WP_Comment ) {
3863                 /**
3864                  * Filter the list of allowed comment types for retrieving avatars.
3865                  *
3866                  * @since 3.0.0
3867                  *
3868                  * @param array $types An array of content types. Default only contains 'comment'.
3869                  */
3870                 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
3871                 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) {
3872                         $args['url'] = false;
3873                         /** This filter is documented in wp-includes/link-template.php */
3874                         return apply_filters( 'get_avatar_data', $args, $id_or_email );
3875                 }
3876
3877                 if ( ! empty( $id_or_email->user_id ) ) {
3878                         $user = get_user_by( 'id', (int) $id_or_email->user_id );
3879                 }
3880                 if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) {
3881                         $email = $id_or_email->comment_author_email;
3882                 }
3883         }
3884
3885         if ( ! $email_hash ) {
3886                 if ( $user ) {
3887                         $email = $user->user_email;
3888                 }
3889
3890                 if ( $email ) {
3891                         $email_hash = md5( strtolower( trim( $email ) ) );
3892                 }
3893         }
3894
3895         if ( $email_hash ) {
3896                 $args['found_avatar'] = true;
3897                 $gravatar_server = hexdec( $email_hash[0] ) % 3;
3898         } else {
3899                 $gravatar_server = rand( 0, 2 );
3900         }
3901
3902         $url_args = array(
3903                 's' => $args['size'],
3904                 'd' => $args['default'],
3905                 'f' => $args['force_default'] ? 'y' : false,
3906                 'r' => $args['rating'],
3907         );
3908
3909         if ( is_ssl() ) {
3910                 $url = 'https://secure.gravatar.com/avatar/' . $email_hash;
3911         } else {
3912                 $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash );
3913         }
3914
3915         $url = add_query_arg(
3916                 rawurlencode_deep( array_filter( $url_args ) ),
3917                 set_url_scheme( $url, $args['scheme'] )
3918         );
3919
3920         /**
3921          * Filter the avatar URL.
3922          *
3923          * @since 4.2.0
3924          *
3925          * @param string $url         The URL of the avatar.
3926          * @param mixed  $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
3927          *                            user email, WP_User object, WP_Post object, or WP_Comment object.
3928          * @param array  $args        Arguments passed to get_avatar_data(), after processing.
3929          */
3930         $args['url'] = apply_filters( 'get_avatar_url', $url, $id_or_email, $args );
3931
3932         /**
3933          * Filter the avatar data.
3934          *
3935          * @since 4.2.0
3936          *
3937          * @param array  $args        Arguments passed to get_avatar_data(), after processing.
3938          * @param mixed  $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
3939          *                            user email, WP_User object, WP_Post object, or WP_Comment object.
3940          */
3941         return apply_filters( 'get_avatar_data', $args, $id_or_email );
3942 }