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


Generated : Mon Oct 12 08:20:33 2026 Cross-referenced by PHPXref