home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / link-template.php < prev    next >
Encoding:
PHP Script  |  2017-09-24  |  130.7 KB  |  4,107 lines

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