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


Generated : Tue Sep 22 08:20:31 2026 Cross-referenced by PHPXref