[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> link-template.php (source)

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


Generated : Fri Aug 19 08:20:02 2022 Cross-referenced by PHPXref