Advertisement
Guest User

Untitled

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