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