[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

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


Generated: Tue Apr 7 01:00:07 2020 Cross-referenced by PHPXref 0.7.1