[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7