[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * WordPress Link Template Functions
   4   *
   5   * @package WordPress
   6   * @subpackage Template
   7   */
   8  
   9  /**
  10   * Displays the permalink for the current post.
  11   *
  12   * @since 1.2.0
  13   * @since 4.4.0 Added the `$post` parameter.
  14   *
  15   * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`.
  16   */
  17  function the_permalink( $post = 0 ) {
  18      /**
  19       * Filters the display of the permalink for the current post.
  20       *
  21       * @since 1.5.0
  22       * @since 4.4.0 Added the `$post` parameter.
  23       *
  24       * @param string      $permalink The permalink for the current post.
  25       * @param int|WP_Post $post      Post ID, WP_Post object, or 0. Default 0.
  26       */
  27      echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) );
  28  }
  29  
  30  /**
  31   * Retrieves a trailing-slashed string if the site is set for adding trailing slashes.
  32   *
  33   * Conditionally adds a trailing slash if the permalink structure has a trailing
  34   * slash, strips the trailing slash if not. The string is passed through the
  35   * {@see 'user_trailingslashit'} filter. Will remove trailing slash from string, if
  36   * site is not set to have them.
  37   *
  38   * @since 2.2.0
  39   *
  40   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  41   *
  42   * @param string $string      URL with or without a trailing slash.
  43   * @param string $type_of_url Optional. The type of URL being considered (e.g. single, category, etc)
  44   *                            for use in the filter. Default empty string.
  45   * @return string The URL with the trailing slash appended or stripped.
  46   */
  47  function user_trailingslashit( $string, $type_of_url = '' ) {
  48      global $wp_rewrite;
  49      if ( $wp_rewrite->use_trailing_slashes ) {
  50          $string = trailingslashit( $string );
  51      } else {
  52          $string = untrailingslashit( $string );
  53      }
  54  
  55      /**
  56       * Filters the trailing-slashed string, depending on whether the site is set to use trailing slashes.
  57       *
  58       * @since 2.2.0
  59       *
  60       * @param string $string      URL with or without a trailing slash.
  61       * @param string $type_of_url The type of URL being considered. Accepts 'single', 'single_trackback',
  62       *                            'single_feed', 'single_paged', 'commentpaged', 'paged', 'home', 'feed',
  63       *                            'category', 'page', 'year', 'month', 'day', 'post_type_archive'.
  64       */
  65      return apply_filters( 'user_trailingslashit', $string, $type_of_url );
  66  }
  67  
  68  /**
  69   * Displays the permalink anchor for the current post.
  70   *
  71   * The permalink mode title will use the post title for the 'a' element 'id'
  72   * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute.
  73   *
  74   * @since 0.71
  75   *
  76   * @param string $mode Optional. Permalink mode. Accepts 'title' or 'id'. Default 'id'.
  77   */
  78  function permalink_anchor( $mode = 'id' ) {
  79      $post = get_post();
  80      switch ( strtolower( $mode ) ) {
  81          case 'title':
  82              $title = sanitize_title( $post->post_title ) . '-' . $post->ID;
  83              echo '<a id="' . $title . '"></a>';
  84              break;
  85          case 'id':
  86          default:
  87              echo '<a id="post-' . $post->ID . '"></a>';
  88              break;
  89      }
  90  }
  91  
  92  /**
  93   * Retrieves the full permalink for the current post or post ID.
  94   *
  95   * This function is an alias for get_permalink().
  96   *
  97   * @since 3.9.0
  98   *
  99   * @see get_permalink()
 100   *
 101   * @param int|WP_Post $post      Optional. Post ID or post object. Default is the global `$post`.
 102   * @param bool        $leavename Optional. Whether to keep post name or page name. Default false.
 103   *
 104   * @return string|false The permalink URL or false if post does not exist.
 105   */
 106  function get_the_permalink( $post = 0, $leavename = false ) {
 107      return get_permalink( $post, $leavename );
 108  }
 109  
 110  /**
 111   * Retrieves the full permalink for the current post or post ID.
 112   *
 113   * @since 1.0.0
 114   *
 115   * @param int|WP_Post $post      Optional. Post ID or post object. Default is the global `$post`.
 116   * @param bool        $leavename Optional. Whether to keep post name or page name. Default false.
 117   * @return string|false The permalink URL or false if post does not exist.
 118   */
 119  function get_permalink( $post = 0, $leavename = false ) {
 120      $rewritecode = array(
 121          '%year%',
 122          '%monthnum%',
 123          '%day%',
 124          '%hour%',
 125          '%minute%',
 126          '%second%',
 127          $leavename ? '' : '%postname%',
 128          '%post_id%',
 129          '%category%',
 130          '%author%',
 131          $leavename ? '' : '%pagename%',
 132      );
 133  
 134      if ( is_object( $post ) && isset( $post->filter ) && 'sample' == $post->filter ) {
 135          $sample = true;
 136      } else {
 137          $post   = get_post( $post );
 138          $sample = false;
 139      }
 140  
 141      if ( empty( $post->ID ) ) {
 142          return false;
 143      }
 144  
 145      if ( $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. 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` argument 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. 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. 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   *
2548   * @param array $args {
2549   *     Optional. Default post navigation arguments. Default empty array.
2550   *
2551   *     @type string       $prev_text          Anchor text to display in the previous post link. Default '%title'.
2552   *     @type string       $next_text          Anchor text to display in the next post link. Default '%title'.
2553   *     @type bool         $in_same_term       Whether link should be in a same taxonomy term. Default false.
2554   *     @type array|string $excluded_terms     Array or comma-separated list of excluded term IDs. Default empty.
2555   *     @type string       $taxonomy           Taxonomy, if `$in_same_term` is true. Default 'category'.
2556   *     @type string       $screen_reader_text Screen reader text for nav element. Default 'Post navigation'.
2557   * }
2558   * @return string Markup for post links.
2559   */
2560  function get_the_post_navigation( $args = array() ) {
2561      $args = wp_parse_args(
2562          $args,
2563          array(
2564              'prev_text'          => '%title',
2565              'next_text'          => '%title',
2566              'in_same_term'       => false,
2567              'excluded_terms'     => '',
2568              'taxonomy'           => 'category',
2569              'screen_reader_text' => __( 'Post navigation' ),
2570          )
2571      );
2572  
2573      $navigation = '';
2574  
2575      $previous = get_previous_post_link(
2576          '<div class="nav-previous">%link</div>',
2577          $args['prev_text'],
2578          $args['in_same_term'],
2579          $args['excluded_terms'],
2580          $args['taxonomy']
2581      );
2582  
2583      $next = get_next_post_link(
2584          '<div class="nav-next">%link</div>',
2585          $args['next_text'],
2586          $args['in_same_term'],
2587          $args['excluded_terms'],
2588          $args['taxonomy']
2589      );
2590  
2591      // Only add markup if there's somewhere to navigate to.
2592      if ( $previous || $next ) {
2593          $navigation = _navigation_markup( $previous . $next, 'post-navigation', $args['screen_reader_text'] );
2594      }
2595  
2596      return $navigation;
2597  }
2598  
2599  /**
2600   * Displays the navigation to next/previous post, when applicable.
2601   *
2602   * @since 4.1.0
2603   *
2604   * @param array $args Optional. See get_the_post_navigation() for available arguments.
2605   *                    Default empty array.
2606   */
2607  function the_post_navigation( $args = array() ) {
2608      echo get_the_post_navigation( $args );
2609  }
2610  
2611  /**
2612   * Returns the navigation to next/previous set of posts, when applicable.
2613   *
2614   * @since 4.1.0
2615   *
2616   * @global WP_Query $wp_query WordPress Query object.
2617   *
2618   * @param array $args {
2619   *     Optional. Default posts navigation arguments. Default empty array.
2620   *
2621   *     @type string $prev_text          Anchor text to display in the previous posts link.
2622   *                                      Default 'Older posts'.
2623   *     @type string $next_text          Anchor text to display in the next posts link.
2624   *                                      Default 'Newer posts'.
2625   *     @type string $screen_reader_text Screen reader text for nav element.
2626   *                                      Default 'Posts navigation'.
2627   * }
2628   * @return string Markup for posts links.
2629   */
2630  function get_the_posts_navigation( $args = array() ) {
2631      $navigation = '';
2632  
2633      // Don't print empty markup if there's only one page.
2634      if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2635          $args = wp_parse_args(
2636              $args,
2637              array(
2638                  'prev_text'          => __( 'Older posts' ),
2639                  'next_text'          => __( 'Newer posts' ),
2640                  'screen_reader_text' => __( 'Posts navigation' ),
2641              )
2642          );
2643  
2644          $next_link = get_previous_posts_link( $args['next_text'] );
2645          $prev_link = get_next_posts_link( $args['prev_text'] );
2646  
2647          if ( $prev_link ) {
2648              $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
2649          }
2650  
2651          if ( $next_link ) {
2652              $navigation .= '<div class="nav-next">' . $next_link . '</div>';
2653          }
2654  
2655          $navigation = _navigation_markup( $navigation, 'posts-navigation', $args['screen_reader_text'] );
2656      }
2657  
2658      return $navigation;
2659  }
2660  
2661  /**
2662   * Displays the navigation to next/previous set of posts, when applicable.
2663   *
2664   * @since 4.1.0
2665   *
2666   * @param array $args Optional. See get_the_posts_navigation() for available arguments.
2667   *                    Default empty array.
2668   */
2669  function the_posts_navigation( $args = array() ) {
2670      echo get_the_posts_navigation( $args );
2671  }
2672  
2673  /**
2674   * Retrieves a paginated navigation to next/previous set of posts, when applicable.
2675   *
2676   * @since 4.1.0
2677   *
2678   * @param array $args {
2679   *     Optional. Default pagination arguments, see paginate_links().
2680   *
2681   *     @type string $screen_reader_text Screen reader text for navigation element.
2682   *                                      Default 'Posts navigation'.
2683   * }
2684   * @return string Markup for pagination links.
2685   */
2686  function get_the_posts_pagination( $args = array() ) {
2687      $navigation = '';
2688  
2689      // Don't print empty markup if there's only one page.
2690      if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
2691          $args = wp_parse_args(
2692              $args,
2693              array(
2694                  'mid_size'           => 1,
2695                  'prev_text'          => _x( 'Previous', 'previous set of posts' ),
2696                  'next_text'          => _x( 'Next', 'next set of posts' ),
2697                  'screen_reader_text' => __( 'Posts navigation' ),
2698              )
2699          );
2700  
2701          // Make sure we get a string back. Plain is the next best thing.
2702          if ( isset( $args['type'] ) && 'array' == $args['type'] ) {
2703              $args['type'] = 'plain';
2704          }
2705  
2706          // Set up paginated links.
2707          $links = paginate_links( $args );
2708  
2709          if ( $links ) {
2710              $navigation = _navigation_markup( $links, 'pagination', $args['screen_reader_text'] );
2711          }
2712      }
2713  
2714      return $navigation;
2715  }
2716  
2717  /**
2718   * Displays a paginated navigation to next/previous set of posts, when applicable.
2719   *
2720   * @since 4.1.0
2721   *
2722   * @param array $args Optional. See get_the_posts_pagination() for available arguments.
2723   *                    Default empty array.
2724   */
2725  function the_posts_pagination( $args = array() ) {
2726      echo get_the_posts_pagination( $args );
2727  }
2728  
2729  /**
2730   * Wraps passed links in navigational markup.
2731   *
2732   * @since 4.1.0
2733   * @access private
2734   *
2735   * @param string $links              Navigational links.
2736   * @param string $class              Optional. Custom class for nav element. Default: 'posts-navigation'.
2737   * @param string $screen_reader_text Optional. Screen reader text for nav element. Default: 'Posts navigation'.
2738   * @return string Navigation template tag.
2739   */
2740  function _navigation_markup( $links, $class = 'posts-navigation', $screen_reader_text = '' ) {
2741      if ( empty( $screen_reader_text ) ) {
2742          $screen_reader_text = __( 'Posts navigation' );
2743      }
2744  
2745      $template = '
2746      <nav class="navigation %1$s" role="navigation">
2747          <h2 class="screen-reader-text">%2$s</h2>
2748          <div class="nav-links">%3$s</div>
2749      </nav>';
2750  
2751      /**
2752       * Filters the navigation markup template.
2753       *
2754       * Note: The filtered template HTML must contain specifiers for the navigation
2755       * class (%1$s), the screen-reader-text value (%2$s), and placement of the
2756       * navigation links (%3$s):
2757       *
2758       *     <nav class="navigation %1$s" role="navigation">
2759       *         <h2 class="screen-reader-text">%2$s</h2>
2760       *         <div class="nav-links">%3$s</div>
2761       *     </nav>
2762       *
2763       * @since 4.4.0
2764       *
2765       * @param string $template The default template.
2766       * @param string $class    The class passed by the calling function.
2767       * @return string Navigation template.
2768       */
2769      $template = apply_filters( 'navigation_markup_template', $template, $class );
2770  
2771      return sprintf( $template, sanitize_html_class( $class ), esc_html( $screen_reader_text ), $links );
2772  }
2773  
2774  /**
2775   * Retrieves the comments page number link.
2776   *
2777   * @since 2.7.0
2778   *
2779   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
2780   *
2781   * @param int $pagenum  Optional. Page number. Default 1.
2782   * @param int $max_page Optional. The maximum number of comment pages. Default 0.
2783   * @return string The comments page number link URL.
2784   */
2785  function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
2786      global $wp_rewrite;
2787  
2788      $pagenum = (int) $pagenum;
2789  
2790      $result = get_permalink();
2791  
2792      if ( 'newest' == get_option( 'default_comments_page' ) ) {
2793          if ( $pagenum != $max_page ) {
2794              if ( $wp_rewrite->using_permalinks() ) {
2795                  $result = user_trailingslashit( trailingslashit( $result ) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged' );
2796              } else {
2797                  $result = add_query_arg( 'cpage', $pagenum, $result );
2798              }
2799          }
2800      } elseif ( $pagenum > 1 ) {
2801          if ( $wp_rewrite->using_permalinks() ) {
2802              $result = user_trailingslashit( trailingslashit( $result ) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged' );
2803          } else {
2804              $result = add_query_arg( 'cpage', $pagenum, $result );
2805          }
2806      }
2807  
2808      $result .= '#comments';
2809  
2810      /**
2811       * Filters the comments page number link for the current request.
2812       *
2813       * @since 2.7.0
2814       *
2815       * @param string $result The comments page number link.
2816       */
2817      return apply_filters( 'get_comments_pagenum_link', $result );
2818  }
2819  
2820  /**
2821   * Retrieves the link to the next comments page.
2822   *
2823   * @since 2.7.1
2824   *
2825   * @global WP_Query $wp_query WordPress Query object.
2826   *
2827   * @param string $label    Optional. Label for link text. Default empty.
2828   * @param int    $max_page Optional. Max page. Default 0.
2829   * @return string|void HTML-formatted link for the next page of comments.
2830   */
2831  function get_next_comments_link( $label = '', $max_page = 0 ) {
2832      global $wp_query;
2833  
2834      if ( ! is_singular() ) {
2835          return;
2836      }
2837  
2838      $page = get_query_var( 'cpage' );
2839  
2840      if ( ! $page ) {
2841          $page = 1;
2842      }
2843  
2844      $nextpage = intval( $page ) + 1;
2845  
2846      if ( empty( $max_page ) ) {
2847          $max_page = $wp_query->max_num_comment_pages;
2848      }
2849  
2850      if ( empty( $max_page ) ) {
2851          $max_page = get_comment_pages_count();
2852      }
2853  
2854      if ( $nextpage > $max_page ) {
2855          return;
2856      }
2857  
2858      if ( empty( $label ) ) {
2859          $label = __( 'Newer Comments &raquo;' );
2860      }
2861  
2862      /**
2863       * Filters the anchor tag attributes for the next comments page link.
2864       *
2865       * @since 2.7.0
2866       *
2867       * @param string $attributes Attributes for the anchor tag.
2868       */
2869      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>';
2870  }
2871  
2872  /**
2873   * Displays the link to the next comments page.
2874   *
2875   * @since 2.7.0
2876   *
2877   * @param string $label    Optional. Label for link text. Default empty.
2878   * @param int    $max_page Optional. Max page. Default 0.
2879   */
2880  function next_comments_link( $label = '', $max_page = 0 ) {
2881      echo get_next_comments_link( $label, $max_page );
2882  }
2883  
2884  /**
2885   * Retrieves the link to the previous comments page.
2886   *
2887   * @since 2.7.1
2888   *
2889   * @param string $label Optional. Label for comments link text. Default empty.
2890   * @return string|void HTML-formatted link for the previous page of comments.
2891   */
2892  function get_previous_comments_link( $label = '' ) {
2893      if ( ! is_singular() ) {
2894          return;
2895      }
2896  
2897      $page = get_query_var( 'cpage' );
2898  
2899      if ( intval( $page ) <= 1 ) {
2900          return;
2901      }
2902  
2903      $prevpage = intval( $page ) - 1;
2904  
2905      if ( empty( $label ) ) {
2906          $label = __( '&laquo; Older Comments' );
2907      }
2908  
2909      /**
2910       * Filters the anchor tag attributes for the previous comments page link.
2911       *
2912       * @since 2.7.0
2913       *
2914       * @param string $attributes Attributes for the anchor tag.
2915       */
2916      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>';
2917  }
2918  
2919  /**
2920   * Displays the link to the previous comments page.
2921   *
2922   * @since 2.7.0
2923   *
2924   * @param string $label Optional. Label for comments link text. Default empty.
2925   */
2926  function previous_comments_link( $label = '' ) {
2927      echo get_previous_comments_link( $label );
2928  }
2929  
2930  /**
2931   * Displays or retrieves pagination links for the comments on the current post.
2932   *
2933   * @see paginate_links()
2934   * @since 2.7.0
2935   *
2936   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
2937   *
2938   * @param string|array $args Optional args. See paginate_links(). Default empty array.
2939   * @return string|array|void Markup for comment page links or array of comment page links.
2940   */
2941  function paginate_comments_links( $args = array() ) {
2942      global $wp_rewrite;
2943  
2944      if ( ! is_singular() ) {
2945          return;
2946      }
2947  
2948      $page = get_query_var( 'cpage' );
2949      if ( ! $page ) {
2950          $page = 1;
2951      }
2952      $max_page = get_comment_pages_count();
2953      $defaults = array(
2954          'base'         => add_query_arg( 'cpage', '%#%' ),
2955          'format'       => '',
2956          'total'        => $max_page,
2957          'current'      => $page,
2958          'echo'         => true,
2959          'type'         => 'plain',
2960          'add_fragment' => '#comments',
2961      );
2962      if ( $wp_rewrite->using_permalinks() ) {
2963          $defaults['base'] = user_trailingslashit( trailingslashit( get_permalink() ) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged' );
2964      }
2965  
2966      $args       = wp_parse_args( $args, $defaults );
2967      $page_links = paginate_links( $args );
2968  
2969      if ( $args['echo'] && 'array' !== $args['type'] ) {
2970          echo $page_links;
2971      } else {
2972          return $page_links;
2973      }
2974  }
2975  
2976  /**
2977   * Retrieves navigation to next/previous set of comments, when applicable.
2978   *
2979   * @since 4.4.0
2980   *
2981   * @param array $args {
2982   *     Optional. Default comments navigation arguments.
2983   *
2984   *     @type string $prev_text          Anchor text to display in the previous comments link.
2985   *                                      Default 'Older comments'.
2986   *     @type string $next_text          Anchor text to display in the next comments link.
2987   *                                      Default 'Newer comments'.
2988   *     @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'.
2989   * }
2990   * @return string Markup for comments links.
2991   */
2992  function get_the_comments_navigation( $args = array() ) {
2993      $navigation = '';
2994  
2995      // Are there comments to navigate through?
2996      if ( get_comment_pages_count() > 1 ) {
2997          $args = wp_parse_args(
2998              $args,
2999              array(
3000                  'prev_text'          => __( 'Older comments' ),
3001                  'next_text'          => __( 'Newer comments' ),
3002                  'screen_reader_text' => __( 'Comments navigation' ),
3003              )
3004          );
3005  
3006          $prev_link = get_previous_comments_link( $args['prev_text'] );
3007          $next_link = get_next_comments_link( $args['next_text'] );
3008  
3009          if ( $prev_link ) {
3010              $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
3011          }
3012  
3013          if ( $next_link ) {
3014              $navigation .= '<div class="nav-next">' . $next_link . '</div>';
3015          }
3016  
3017          $navigation = _navigation_markup( $navigation, 'comment-navigation', $args['screen_reader_text'] );
3018      }
3019  
3020      return $navigation;
3021  }
3022  
3023  /**
3024   * Displays navigation to next/previous set of comments, when applicable.
3025   *
3026   * @since 4.4.0
3027   *
3028   * @param array $args See get_the_comments_navigation() for available arguments. Default empty array.
3029   */
3030  function the_comments_navigation( $args = array() ) {
3031      echo get_the_comments_navigation( $args );
3032  }
3033  
3034  /**
3035   * Retrieves a paginated navigation to next/previous set of comments, when applicable.
3036   *
3037   * @since 4.4.0
3038   *
3039   * @see paginate_comments_links()
3040   *
3041   * @param array $args {
3042   *     Optional. Default pagination arguments.
3043   *
3044   *     @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'.
3045   * }
3046   * @return string Markup for pagination links.
3047   */
3048  function get_the_comments_pagination( $args = array() ) {
3049      $navigation   = '';
3050      $args         = wp_parse_args(
3051          $args,
3052          array(
3053              'screen_reader_text' => __( 'Comments navigation' ),
3054          )
3055      );
3056      $args['echo'] = false;
3057  
3058      // Make sure we get a string back. Plain is the next best thing.
3059      if ( isset( $args['type'] ) && 'array' == $args['type'] ) {
3060          $args['type'] = 'plain';
3061      }
3062  
3063      $links = paginate_comments_links( $args );
3064  
3065      if ( $links ) {
3066          $navigation = _navigation_markup( $links, 'comments-pagination', $args['screen_reader_text'] );
3067      }
3068  
3069      return $navigation;
3070  }
3071  
3072  /**
3073   * Displays a paginated navigation to next/previous set of comments, when applicable.
3074   *
3075   * @since 4.4.0
3076   *
3077   * @param array $args See get_the_comments_pagination() for available arguments. Default empty array.
3078   */
3079  function the_comments_pagination( $args = array() ) {
3080      echo get_the_comments_pagination( $args );
3081  }
3082  
3083  /**
3084   * Retrieves the URL for the current site where the front end is accessible.
3085   *
3086   * Returns the 'home' option with the appropriate protocol. The protocol will be 'https'
3087   * if is_ssl() evaluates to true; otherwise, it will be the same as the 'home' option.
3088   * If `$scheme` is 'http' or 'https', is_ssl() is overridden.
3089   *
3090   * @since 3.0.0
3091   *
3092   * @param  string      $path   Optional. Path relative to the home URL. Default empty.
3093   * @param  string|null $scheme Optional. Scheme to give the home URL context. Accepts
3094   *                             'http', 'https', 'relative', 'rest', or null. Default null.
3095   * @return string Home URL link with optional path appended.
3096   */
3097  function home_url( $path = '', $scheme = null ) {
3098      return get_home_url( null, $path, $scheme );
3099  }
3100  
3101  /**
3102   * Retrieves the URL for a given site where the front end is accessible.
3103   *
3104   * Returns the 'home' option with the appropriate protocol. The protocol will be 'https'
3105   * if is_ssl() evaluates to true; otherwise, it will be the same as the 'home' option.
3106   * If `$scheme` is 'http' or 'https', is_ssl() is overridden.
3107   *
3108   * @since 3.0.0
3109   *
3110   * @global string $pagenow
3111   *
3112   * @param  int         $blog_id Optional. Site ID. Default null (current site).
3113   * @param  string      $path    Optional. Path relative to the home URL. Default empty.
3114   * @param  string|null $scheme  Optional. Scheme to give the home URL context. Accepts
3115   *                              'http', 'https', 'relative', 'rest', or null. Default null.
3116   * @return string Home URL link with optional path appended.
3117   */
3118  function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
3119      global $pagenow;
3120  
3121      $orig_scheme = $scheme;
3122  
3123      if ( empty( $blog_id ) || ! is_multisite() ) {
3124          $url = get_option( 'home' );
3125      } else {
3126          switch_to_blog( $blog_id );
3127          $url = get_option( 'home' );
3128          restore_current_blog();
3129      }
3130  
3131      if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
3132          if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow ) {
3133              $scheme = 'https';
3134          } else {
3135              $scheme = parse_url( $url, PHP_URL_SCHEME );
3136          }
3137      }
3138  
3139      $url = set_url_scheme( $url, $scheme );
3140  
3141      if ( $path && is_string( $path ) ) {
3142          $url .= '/' . ltrim( $path, '/' );
3143      }
3144  
3145      /**
3146       * Filters the home URL.
3147       *
3148       * @since 3.0.0
3149       *
3150       * @param string      $url         The complete home URL including scheme and path.
3151       * @param string      $path        Path relative to the home URL. Blank string if no path is specified.
3152       * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https',
3153       *                                 'relative', 'rest', or null.
3154       * @param int|null    $blog_id     Site ID, or null for the current site.
3155       */
3156      return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
3157  }
3158  
3159  /**
3160   * Retrieves the URL for the current site where WordPress application files
3161   * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
3162   *
3163   * Returns the 'site_url' option with the appropriate protocol, 'https' if
3164   * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
3165   * overridden.
3166   *
3167   * @since 3.0.0
3168   *
3169   * @param string $path   Optional. Path relative to the site URL. Default empty.
3170   * @param string $scheme Optional. Scheme to give the site URL context. See set_url_scheme().
3171   * @return string Site URL link with optional path appended.
3172   */
3173  function site_url( $path = '', $scheme = null ) {
3174      return get_site_url( null, $path, $scheme );
3175  }
3176  
3177  /**
3178   * Retrieves the URL for a given site where WordPress application files
3179   * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
3180   *
3181   * Returns the 'site_url' option with the appropriate protocol, 'https' if
3182   * is_ssl() and 'http' otherwise. If `$scheme` is 'http' or 'https',
3183   * `is_ssl()` is overridden.
3184   *
3185   * @since 3.0.0
3186   *
3187   * @param int    $blog_id Optional. Site ID. Default null (current site).
3188   * @param string $path    Optional. Path relative to the site URL. Default empty.
3189   * @param string $scheme  Optional. Scheme to give the site URL context. Accepts
3190   *                        'http', 'https', 'login', 'login_post', 'admin', or
3191   *                        'relative'. Default null.
3192   * @return string Site URL link with optional path appended.
3193   */
3194  function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
3195      if ( empty( $blog_id ) || ! is_multisite() ) {
3196          $url = get_option( 'siteurl' );
3197      } else {
3198          switch_to_blog( $blog_id );
3199          $url = get_option( 'siteurl' );
3200          restore_current_blog();
3201      }
3202  
3203      $url = set_url_scheme( $url, $scheme );
3204  
3205      if ( $path && is_string( $path ) ) {
3206          $url .= '/' . ltrim( $path, '/' );
3207      }
3208  
3209      /**
3210       * Filters the site URL.
3211       *
3212       * @since 2.7.0
3213       *
3214       * @param string      $url     The complete site URL including scheme and path.
3215       * @param string      $path    Path relative to the site URL. Blank string if no path is specified.
3216       * @param string|null $scheme  Scheme to give the site URL context. Accepts 'http', 'https', 'login',
3217       *                             'login_post', 'admin', 'relative' or null.
3218       * @param int|null    $blog_id Site ID, or null for the current site.
3219       */
3220      return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
3221  }
3222  
3223  /**
3224   * Retrieves the URL to the admin area for the current site.
3225   *
3226   * @since 2.6.0
3227   *
3228   * @param string $path   Optional path relative to the admin URL.
3229   * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
3230   *                       'http' or 'https' can be passed to force those schemes.
3231   * @return string Admin URL link with optional path appended.
3232   */
3233  function admin_url( $path = '', $scheme = 'admin' ) {
3234      return get_admin_url( null, $path, $scheme );
3235  }
3236  
3237  /**
3238   * Retrieves the URL to the admin area for a given site.
3239   *
3240   * @since 3.0.0
3241   *
3242   * @param int    $blog_id Optional. Site ID. Default null (current site).
3243   * @param string $path    Optional. Path relative to the admin URL. Default empty.
3244   * @param string $scheme  Optional. The scheme to use. Accepts 'http' or 'https',
3245   *                        to force those schemes. Default 'admin', which obeys
3246   *                        force_ssl_admin() and is_ssl().
3247   * @return string Admin URL link with optional path appended.
3248   */
3249  function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
3250      $url = get_site_url( $blog_id, 'wp-admin/', $scheme );
3251  
3252      if ( $path && is_string( $path ) ) {
3253          $url .= ltrim( $path, '/' );
3254      }
3255  
3256      /**
3257       * Filters the admin area URL.
3258       *
3259       * @since 2.8.0
3260       *
3261       * @param string   $url     The complete admin area URL including scheme and path.
3262       * @param string   $path    Path relative to the admin area URL. Blank string if no path is specified.
3263       * @param int|null $blog_id Site ID, or null for the current site.
3264       */
3265      return apply_filters( 'admin_url', $url, $path, $blog_id );
3266  }
3267  
3268  /**
3269   * Retrieves the URL to the includes directory.
3270   *
3271   * @since 2.6.0
3272   *
3273   * @param string $path   Optional. Path relative to the includes URL. Default empty.
3274   * @param string $scheme Optional. Scheme to give the includes URL context. Accepts
3275   *                       'http', 'https', or 'relative'. Default null.
3276   * @return string Includes URL link with optional path appended.
3277   */
3278  function includes_url( $path = '', $scheme = null ) {
3279      $url = site_url( '/' . WPINC . '/', $scheme );
3280  
3281      if ( $path && is_string( $path ) ) {
3282          $url .= ltrim( $path, '/' );
3283      }
3284  
3285      /**
3286       * Filters the URL to the includes directory.
3287       *
3288       * @since 2.8.0
3289       *
3290       * @param string $url  The complete URL to the includes directory including scheme and path.
3291       * @param string $path Path relative to the URL to the wp-includes directory. Blank string
3292       *                     if no path is specified.
3293       */
3294      return apply_filters( 'includes_url', $url, $path );
3295  }
3296  
3297  /**
3298   * Retrieves the URL to the content directory.
3299   *
3300   * @since 2.6.0
3301   *
3302   * @param string $path Optional. Path relative to the content URL. Default empty.
3303   * @return string Content URL link with optional path appended.
3304   */
3305  function content_url( $path = '' ) {
3306      $url = set_url_scheme( WP_CONTENT_URL );
3307  
3308      if ( $path && is_string( $path ) ) {
3309          $url .= '/' . ltrim( $path, '/' );
3310      }
3311  
3312      /**
3313       * Filters the URL to the content directory.
3314       *
3315       * @since 2.8.0
3316       *
3317       * @param string $url  The complete URL to the content directory including scheme and path.
3318       * @param string $path Path relative to the URL to the content directory. Blank string
3319       *                     if no path is specified.
3320       */
3321      return apply_filters( 'content_url', $url, $path );
3322  }
3323  
3324  /**
3325   * Retrieves a URL within the plugins or mu-plugins directory.
3326   *
3327   * Defaults to the plugins directory URL if no arguments are supplied.
3328   *
3329   * @since 2.6.0
3330   *
3331   * @param  string $path   Optional. Extra path appended to the end of the URL, including
3332   *                        the relative directory if $plugin is supplied. Default empty.
3333   * @param  string $plugin Optional. A full path to a file inside a plugin or mu-plugin.
3334   *                        The URL will be relative to its directory. Default empty.
3335   *                        Typically this is done by passing `__FILE__` as the argument.
3336   * @return string Plugins URL link with optional paths appended.
3337   */
3338  function plugins_url( $path = '', $plugin = '' ) {
3339  
3340      $path          = wp_normalize_path( $path );
3341      $plugin        = wp_normalize_path( $plugin );
3342      $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
3343  
3344      if ( ! empty( $plugin ) && 0 === strpos( $plugin, $mu_plugin_dir ) ) {
3345          $url = WPMU_PLUGIN_URL;
3346      } else {
3347          $url = WP_PLUGIN_URL;
3348      }
3349  
3350      $url = set_url_scheme( $url );
3351  
3352      if ( ! empty( $plugin ) && is_string( $plugin ) ) {
3353          $folder = dirname( plugin_basename( $plugin ) );
3354          if ( '.' != $folder ) {
3355              $url .= '/' . ltrim( $folder, '/' );
3356          }
3357      }
3358  
3359      if ( $path && is_string( $path ) ) {
3360          $url .= '/' . ltrim( $path, '/' );
3361      }
3362  
3363      /**
3364       * Filters the URL to the plugins directory.
3365       *
3366       * @since 2.8.0
3367       *
3368       * @param string $url    The complete URL to the plugins directory including scheme and path.
3369       * @param string $path   Path relative to the URL to the plugins directory. Blank string
3370       *                       if no path is specified.
3371       * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
3372       *                       is specified.
3373       */
3374      return apply_filters( 'plugins_url', $url, $path, $plugin );
3375  }
3376  
3377  /**
3378   * Retrieves the site URL for the current network.
3379   *
3380   * Returns the site URL with the appropriate protocol, 'https' if
3381   * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
3382   * overridden.
3383   *
3384   * @since 3.0.0
3385   *
3386   * @see set_url_scheme()
3387   *
3388   * @param string $path   Optional. Path relative to the site URL. Default empty.
3389   * @param string $scheme Optional. Scheme to give the site URL context. Accepts
3390   *                       'http', 'https', or 'relative'. Default null.
3391   * @return string Site URL link with optional path appended.
3392   */
3393  function network_site_url( $path = '', $scheme = null ) {
3394      if ( ! is_multisite() ) {
3395          return site_url( $path, $scheme );
3396      }
3397  
3398      $current_network = get_network();
3399  
3400      if ( 'relative' == $scheme ) {
3401          $url = $current_network->path;
3402      } else {
3403          $url = set_url_scheme( 'http://' . $current_network->domain . $current_network->path, $scheme );
3404      }
3405  
3406      if ( $path && is_string( $path ) ) {
3407          $url .= ltrim( $path, '/' );
3408      }
3409  
3410      /**
3411       * Filters the network site URL.
3412       *
3413       * @since 3.0.0
3414       *
3415       * @param string      $url    The complete network site URL including scheme and path.
3416       * @param string      $path   Path relative to the network site URL. Blank string if
3417       *                            no path is specified.
3418       * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',
3419       *                            'relative' or null.
3420       */
3421      return apply_filters( 'network_site_url', $url, $path, $scheme );
3422  }
3423  
3424  /**
3425   * Retrieves the home URL for the current network.
3426   *
3427   * Returns the home URL with the appropriate protocol, 'https' is_ssl()
3428   * and 'http' otherwise. If `$scheme` is 'http' or 'https', `is_ssl()` is
3429   * overridden.
3430   *
3431   * @since 3.0.0
3432   *
3433   * @param  string $path   Optional. Path relative to the home URL. Default empty.
3434   * @param  string $scheme Optional. Scheme to give the home URL context. Accepts
3435   *                        'http', 'https', or 'relative'. Default null.
3436   * @return string Home URL link with optional path appended.
3437   */
3438  function network_home_url( $path = '', $scheme = null ) {
3439      if ( ! is_multisite() ) {
3440          return home_url( $path, $scheme );
3441      }
3442  
3443      $current_network = get_network();
3444      $orig_scheme     = $scheme;
3445  
3446      if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
3447          $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
3448      }
3449  
3450      if ( 'relative' == $scheme ) {
3451          $url = $current_network->path;
3452      } else {
3453          $url = set_url_scheme( 'http://' . $current_network->domain . $current_network->path, $scheme );
3454      }
3455  
3456      if ( $path && is_string( $path ) ) {
3457          $url .= ltrim( $path, '/' );
3458      }
3459  
3460      /**
3461       * Filters the network home URL.
3462       *
3463       * @since 3.0.0
3464       *
3465       * @param string      $url         The complete network home URL including scheme and path.
3466       * @param string      $path        Path relative to the network home URL. Blank string
3467       *                                 if no path is specified.
3468       * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',
3469       *                                 'relative' or null.
3470       */
3471      return apply_filters( 'network_home_url', $url, $path, $orig_scheme );
3472  }
3473  
3474  /**
3475   * Retrieves the URL to the admin area for the network.
3476   *
3477   * @since 3.0.0
3478   *
3479   * @param string $path   Optional path relative to the admin URL. Default empty.
3480   * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
3481   *                       and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3482   * @return string Admin URL link with optional path appended.
3483   */
3484  function network_admin_url( $path = '', $scheme = 'admin' ) {
3485      if ( ! is_multisite() ) {
3486          return admin_url( $path, $scheme );
3487      }
3488  
3489      $url = network_site_url( 'wp-admin/network/', $scheme );
3490  
3491      if ( $path && is_string( $path ) ) {
3492          $url .= ltrim( $path, '/' );
3493      }
3494  
3495      /**
3496       * Filters the network admin URL.
3497       *
3498       * @since 3.0.0
3499       *
3500       * @param string $url  The complete network admin URL including scheme and path.
3501       * @param string $path Path relative to the network admin URL. Blank string if
3502       *                     no path is specified.
3503       */
3504      return apply_filters( 'network_admin_url', $url, $path );
3505  }
3506  
3507  /**
3508   * Retrieves the URL to the admin area for the current user.
3509   *
3510   * @since 3.0.0
3511   *
3512   * @param string $path   Optional. Path relative to the admin URL. Default empty.
3513   * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
3514   *                       and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3515   * @return string Admin URL link with optional path appended.
3516   */
3517  function user_admin_url( $path = '', $scheme = 'admin' ) {
3518      $url = network_site_url( 'wp-admin/user/', $scheme );
3519  
3520      if ( $path && is_string( $path ) ) {
3521          $url .= ltrim( $path, '/' );
3522      }
3523  
3524      /**
3525       * Filters the user admin URL for the current user.
3526       *
3527       * @since 3.1.0
3528       *
3529       * @param string $url  The complete URL including scheme and path.
3530       * @param string $path Path relative to the URL. Blank string if
3531       *                     no path is specified.
3532       */
3533      return apply_filters( 'user_admin_url', $url, $path );
3534  }
3535  
3536  /**
3537   * Retrieves the URL to the admin area for either the current site or the network depending on context.
3538   *
3539   * @since 3.1.0
3540   *
3541   * @param string $path   Optional. Path relative to the admin URL. Default empty.
3542   * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin()
3543   *                       and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3544   * @return string Admin URL link with optional path appended.
3545   */
3546  function self_admin_url( $path = '', $scheme = 'admin' ) {
3547      if ( is_network_admin() ) {
3548          $url = network_admin_url( $path, $scheme );
3549      } elseif ( is_user_admin() ) {
3550          $url = user_admin_url( $path, $scheme );
3551      } else {
3552          $url = admin_url( $path, $scheme );
3553      }
3554  
3555      /**
3556       * Filters the admin URL for the current site or network depending on context.
3557       *
3558       * @since 4.9.0
3559       *
3560       * @param string $url    The complete URL including scheme and path.
3561       * @param string $path   Path relative to the URL. Blank string if no path is specified.
3562       * @param string $scheme The scheme to use.
3563       */
3564      return apply_filters( 'self_admin_url', $url, $path, $scheme );
3565  }
3566  
3567  /**
3568   * Sets the scheme for a URL.
3569   *
3570   * @since 3.4.0
3571   * @since 4.4.0 The 'rest' scheme was added.
3572   *
3573   * @param string      $url    Absolute URL that includes a scheme
3574   * @param string|null $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login',
3575   *                            'login_post', 'admin', 'relative', 'rest', 'rpc', or null. Default null.
3576   * @return string $url URL with chosen scheme.
3577   */
3578  function set_url_scheme( $url, $scheme = null ) {
3579      $orig_scheme = $scheme;
3580  
3581      if ( ! $scheme ) {
3582          $scheme = is_ssl() ? 'https' : 'http';
3583      } elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) {
3584          $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
3585      } elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) {
3586          $scheme = is_ssl() ? 'https' : 'http';
3587      }
3588  
3589      $url = trim( $url );
3590      if ( substr( $url, 0, 2 ) === '//' ) {
3591          $url = 'http:' . $url;
3592      }
3593  
3594      if ( 'relative' == $scheme ) {
3595          $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
3596          if ( $url !== '' && $url[0] === '/' ) {
3597              $url = '/' . ltrim( $url, "/ \t\n\r\0\x0B" );
3598          }
3599      } else {
3600          $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
3601      }
3602  
3603      /**
3604       * Filters the resulting URL after setting the scheme.
3605       *
3606       * @since 3.4.0
3607       *
3608       * @param string      $url         The complete URL including scheme and path.
3609       * @param string      $scheme      Scheme applied to the URL. One of 'http', 'https', or 'relative'.
3610       * @param string|null $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
3611       *                                 'login_post', 'admin', 'relative', 'rest', 'rpc', or null.
3612       */
3613      return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
3614  }
3615  
3616  /**
3617   * Retrieves the URL to the user's dashboard.
3618   *
3619   * If a user does not belong to any site, the global user dashboard is used. If the user
3620   * belongs to the current site, the dashboard for the current site is returned. If the user
3621   * cannot edit the current site, the dashboard to the user's primary site is returned.
3622   *
3623   * @since 3.1.0
3624   *
3625   * @param int    $user_id Optional. User ID. Defaults to current user.
3626   * @param string $path    Optional path relative to the dashboard. Use only paths known to
3627   *                        both site and user admins. Default empty.
3628   * @param string $scheme  The scheme to use. Default is 'admin', which obeys force_ssl_admin()
3629   *                        and is_ssl(). 'http' or 'https' can be passed to force those schemes.
3630   * @return string Dashboard URL link with optional path appended.
3631   */
3632  function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
3633      $user_id = $user_id ? (int) $user_id : get_current_user_id();
3634  
3635      $blogs = get_blogs_of_user( $user_id );
3636      if ( is_multisite() && ! user_can( $user_id, 'manage_network' ) && empty( $blogs ) ) {
3637          $url = user_admin_url( $path, $scheme );
3638      } elseif ( ! is_multisite() ) {
3639          $url = admin_url( $path, $scheme );
3640      } else {
3641          $current_blog = get_current_blog_id();
3642          if ( $current_blog && ( user_can( $user_id, 'manage_network' ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
3643              $url = admin_url( $path, $scheme );
3644          } else {
3645              $active = get_active_blog_for_user( $user_id );
3646              if ( $active ) {
3647                  $url = get_admin_url( $active->blog_id, $path, $scheme );
3648              } else {
3649                  $url = user_admin_url( $path, $scheme );
3650              }
3651          }
3652      }
3653  
3654      /**
3655       * Filters the dashboard URL for a user.
3656       *
3657       * @since 3.1.0
3658       *
3659       * @param string $url     The complete URL including scheme and path.
3660       * @param int    $user_id The user ID.
3661       * @param string $path    Path relative to the URL. Blank string if no path is specified.
3662       * @param string $scheme  Scheme to give the URL context. Accepts 'http', 'https', 'login',
3663       *                        'login_post', 'admin', 'relative' or null.
3664       */
3665      return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme );
3666  }
3667  
3668  /**
3669   * Retrieves the URL to the user's profile editor.
3670   *
3671   * @since 3.1.0
3672   *
3673   * @param int    $user_id Optional. User ID. Defaults to current user.
3674   * @param string $scheme  Optional. 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_edit_profile_url( $user_id = 0, $scheme = 'admin' ) {
3679      $user_id = $user_id ? (int) $user_id : get_current_user_id();
3680  
3681      if ( is_user_admin() ) {
3682          $url = user_admin_url( 'profile.php', $scheme );
3683      } elseif ( is_network_admin() ) {
3684          $url = network_admin_url( 'profile.php', $scheme );
3685      } else {
3686          $url = get_dashboard_url( $user_id, 'profile.php', $scheme );
3687      }
3688  
3689      /**
3690       * Filters the URL for a user's profile editor.
3691       *
3692       * @since 3.1.0
3693       *
3694       * @param string $url     The complete URL including scheme and path.
3695       * @param int    $user_id The user ID.
3696       * @param string $scheme  Scheme to give the URL context. Accepts 'http', 'https', 'login',
3697       *                        'login_post', 'admin', 'relative' or null.
3698       */
3699      return apply_filters( 'edit_profile_url', $url, $user_id, $scheme );
3700  }
3701  
3702  /**
3703   * Returns the canonical URL for a post.
3704   *
3705   * When the post is the same as the current requested page the function will handle the
3706   * pagination arguments too.
3707   *
3708   * @since 4.6.0
3709   *
3710   * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`.
3711   * @return string|false The canonical URL, or false if the post does not exist or has not
3712   *                      been published yet.
3713   */
3714  function wp_get_canonical_url( $post = null ) {
3715      $post = get_post( $post );
3716  
3717      if ( ! $post ) {
3718          return false;
3719      }
3720  
3721      if ( 'publish' !== $post->post_status ) {
3722          return false;
3723      }
3724  
3725      $canonical_url = get_permalink( $post );
3726  
3727      // If a canonical is being generated for the current page, make sure it has pagination if needed.
3728      if ( $post->ID === get_queried_object_id() ) {
3729          $page = get_query_var( 'page', 0 );
3730          if ( $page >= 2 ) {
3731              if ( '' == get_option( 'permalink_structure' ) ) {
3732                  $canonical_url = add_query_arg( 'page', $page, $canonical_url );
3733              } else {
3734                  $canonical_url = trailingslashit( $canonical_url ) . user_trailingslashit( $page, 'single_paged' );
3735              }
3736          }
3737  
3738          $cpage = get_query_var( 'cpage', 0 );
3739          if ( $cpage ) {
3740              $canonical_url = get_comments_pagenum_link( $cpage );
3741          }
3742      }
3743  
3744      /**
3745       * Filters the canonical URL for a post.
3746       *
3747       * @since 4.6.0
3748       *
3749       * @param string  $canonical_url The post's canonical URL.
3750       * @param WP_Post $post          Post object.
3751       */
3752      return apply_filters( 'get_canonical_url', $canonical_url, $post );
3753  }
3754  
3755  /**
3756   * Outputs rel=canonical for singular queries.
3757   *
3758   * @since 2.9.0
3759   * @since 4.6.0 Adjusted to use `wp_get_canonical_url()`.
3760   */
3761  function rel_canonical() {
3762      if ( ! is_singular() ) {
3763          return;
3764      }
3765  
3766      $id = get_queried_object_id();
3767  
3768      if ( 0 === $id ) {
3769          return;
3770      }
3771  
3772      $url = wp_get_canonical_url( $id );
3773  
3774      if ( ! empty( $url ) ) {
3775          echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n";
3776      }
3777  }
3778  
3779  /**
3780   * Returns a shortlink for a post, page, attachment, or site.
3781   *
3782   * This function exists to provide a shortlink tag that all themes and plugins can target.
3783   * A plugin must hook in to provide the actual shortlinks. Default shortlink support is
3784   * limited to providing ?p= style links for posts. Plugins can short-circuit this function
3785   * via the {@see 'pre_get_shortlink'} filter or filter the output via the {@see 'get_shortlink'}
3786   * filter.
3787   *
3788   * @since 3.0.0
3789   *
3790   * @param int    $id          Optional. A post or site id. Default is 0, which means the current post or site.
3791   * @param string $context     Optional. Whether the id is a 'site' id, 'post' id, or 'media' id. If 'post',
3792   *                            the post_type of the post is consulted. If 'query', the current query is consulted
3793   *                            to determine the id and context. Default 'post'.
3794   * @param bool   $allow_slugs Optional. Whether to allow post slugs in the shortlink. It is up to the plugin how
3795   *                            and whether to honor this. Default true.
3796   * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks
3797   *                are not enabled.
3798   */
3799  function wp_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) {
3800      /**
3801       * Filters whether to preempt generating a shortlink for the given post.
3802       *
3803       * Passing a truthy value to the filter will effectively short-circuit the
3804       * shortlink-generation process, returning that value instead.
3805       *
3806       * @since 3.0.0
3807       *
3808       * @param bool|string $return      Short-circuit return value. Either false or a URL string.
3809       * @param int         $id          Post ID, or 0 for the current post.
3810       * @param string      $context     The context for the link. One of 'post' or 'query',
3811       * @param bool        $allow_slugs Whether to allow post slugs in the shortlink.
3812       */
3813      $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );
3814  
3815      if ( false !== $shortlink ) {
3816          return $shortlink;
3817      }
3818  
3819      $post_id = 0;
3820      if ( 'query' == $context && is_singular() ) {
3821          $post_id = get_queried_object_id();
3822          $post    = get_post( $post_id );
3823      } elseif ( 'post' == $context ) {
3824          $post = get_post( $id );
3825          if ( ! empty( $post->ID ) ) {
3826              $post_id = $post->ID;
3827          }
3828      }
3829  
3830      $shortlink = '';
3831  
3832      // Return p= link for all public post types.
3833      if ( ! empty( $post_id ) ) {
3834          $post_type = get_post_type_object( $post->post_type );
3835  
3836          if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) {
3837              $shortlink = home_url( '/' );
3838          } elseif ( $post_type->public ) {
3839              $shortlink = home_url( '?p=' . $post_id );
3840          }
3841      }
3842  
3843      /**
3844       * Filters the shortlink for a post.
3845       *
3846       * @since 3.0.0
3847       *
3848       * @param string $shortlink   Shortlink URL.
3849       * @param int    $id          Post ID, or 0 for the current post.
3850       * @param string $context     The context for the link. One of 'post' or 'query',
3851       * @param bool   $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
3852       */
3853      return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
3854  }
3855  
3856  /**
3857   * Injects rel=shortlink into the head if a shortlink is defined for the current page.
3858   *
3859   * Attached to the {@see 'wp_head'} action.
3860   *
3861   * @since 3.0.0
3862   */
3863  function wp_shortlink_wp_head() {
3864      $shortlink = wp_get_shortlink( 0, 'query' );
3865  
3866      if ( empty( $shortlink ) ) {
3867          return;
3868      }
3869  
3870      echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
3871  }
3872  
3873  /**
3874   * Sends a Link: rel=shortlink header if a shortlink is defined for the current page.
3875   *
3876   * Attached to the {@see 'wp'} action.
3877   *
3878   * @since 3.0.0
3879   */
3880  function wp_shortlink_header() {
3881      if ( headers_sent() ) {
3882          return;
3883      }
3884  
3885      $shortlink = wp_get_shortlink( 0, 'query' );
3886  
3887      if ( empty( $shortlink ) ) {
3888          return;
3889      }
3890  
3891      header( 'Link: <' . $shortlink . '>; rel=shortlink', false );
3892  }
3893  
3894  /**
3895   * Displays the shortlink for a post.
3896   *
3897   * Must be called from inside "The Loop"
3898   *
3899   * Call like the_shortlink( __( 'Shortlinkage FTW' ) )
3900   *
3901   * @since 3.0.0
3902   *
3903   * @param string $text   Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
3904   * @param string $title  Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
3905   * @param string $before Optional HTML to display before the link. Default empty.
3906   * @param string $after  Optional HTML to display after the link. Default empty.
3907   */
3908  function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
3909      $post = get_post();
3910  
3911      if ( empty( $text ) ) {
3912          $text = __( 'This is the short link.' );
3913      }
3914  
3915      if ( empty( $title ) ) {
3916          $title = the_title_attribute( array( 'echo' => false ) );
3917      }
3918  
3919      $shortlink = wp_get_shortlink( $post->ID );
3920  
3921      if ( ! empty( $shortlink ) ) {
3922          $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
3923  
3924          /**
3925           * Filters the short link anchor tag for a post.
3926           *
3927           * @since 3.0.0
3928           *
3929           * @param string $link      Shortlink anchor tag.
3930           * @param string $shortlink Shortlink URL.
3931           * @param string $text      Shortlink's text.
3932           * @param string $title     Shortlink's title attribute.
3933           */
3934          $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
3935          echo $before, $link, $after;
3936      }
3937  }
3938  
3939  
3940  /**
3941   * Retrieves the avatar URL.
3942   *
3943   * @since 4.2.0
3944   *
3945   * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,
3946   *                           user email, WP_User object, WP_Post object, or WP_Comment object.
3947   * @param array $args {
3948   *     Optional. Arguments to return instead of the default arguments.
3949   *
3950   *     @type int    $size           Height and width of the avatar in pixels. Default 96.
3951   *     @type string $default        URL for the default image or a default type. Accepts '404' (return
3952   *                                  a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
3953   *                                  'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
3954   *                                  or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or
3955   *                                  'gravatar_default' (the Gravatar logo). Default is the value of the
3956   *                                  'avatar_default' option, with a fallback of 'mystery'.
3957   *     @type bool   $force_default  Whether to always show the default image, never the Gravatar. Default false.
3958   *     @type string $rating         What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
3959   *                                  judged in that order. Default is the value of the 'avatar_rating' option.
3960   *     @type string $scheme         URL scheme to use. See set_url_scheme() for accepted values.
3961   *                                  Default null.
3962   *     @type array  $processed_args When the function returns, the value will be the processed/sanitized $args
3963   *                                  plus a "found_avatar" guess. Pass as a reference. Default null.
3964   * }
3965   * @return false|string The URL of the avatar we found, or false if we couldn't find an avatar.
3966   */
3967  function get_avatar_url( $id_or_email, $args = null ) {
3968      $args = get_avatar_data( $id_or_email, $args );
3969      return $args['url'];
3970  }
3971  
3972  
3973  /**
3974   * Check if this comment type allows avatars to be retrieved.
3975   *
3976   * @since 5.1.0
3977   *
3978   * @param string $comment_type Comment type to check.
3979   * @return bool Whether the comment type is allowed for retrieving avatars.
3980   */
3981  function is_avatar_comment_type( $comment_type ) {
3982      /**
3983       * Filters the list of allowed comment types for retrieving avatars.
3984       *
3985       * @since 3.0.0
3986       *
3987       * @param array $types An array of content types. Default only contains 'comment'.
3988       */
3989      $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
3990  
3991      return in_array( $comment_type, (array) $allowed_comment_types, true );
3992  }
3993  
3994  
3995  /**
3996   * Retrieves default data about the avatar.
3997   *
3998   * @since 4.2.0
3999   *
4000   * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
4001   *                            user email, WP_User object, WP_Post object, or WP_Comment object.
4002   * @param array $args {
4003   *     Optional. Arguments to return instead of the default arguments.
4004   *
4005   *     @type int    $size           Height and width of the avatar image file in pixels. Default 96.
4006   *     @type int    $height         Display height of the avatar in pixels. Defaults to $size.
4007   *     @type int    $width          Display width of the avatar in pixels. Defaults to $size.
4008   *     @type string $default        URL for the default image or a default type. Accepts '404' (return
4009   *                                  a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
4010   *                                  'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
4011   *                                  or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or
4012   *                                  'gravatar_default' (the Gravatar logo). Default is the value of the
4013   *                                  'avatar_default' option, with a fallback of 'mystery'.
4014   *     @type bool   $force_default  Whether to always show the default image, never the Gravatar. Default false.
4015   *     @type string $rating         What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
4016   *                                  judged in that order. Default is the value of the 'avatar_rating' option.
4017   *     @type string $scheme         URL scheme to use. See set_url_scheme() for accepted values.
4018   *                                  Default null.
4019   *     @type array  $processed_args When the function returns, the value will be the processed/sanitized $args
4020   *                                  plus a "found_avatar" guess. Pass as a reference. Default null.
4021   *     @type string $extra_attr     HTML attributes to insert in the IMG element. Is not sanitized. Default empty.
4022   * }
4023   * @return array $processed_args {
4024   *     Along with the arguments passed in `$args`, this will contain a couple of extra arguments.
4025   *
4026   *     @type bool   $found_avatar True if we were able to find an avatar for this user,
4027   *                                false or not set if we couldn't.
4028   *     @type string $url          The URL of the avatar we found.
4029   * }
4030   */
4031  function get_avatar_data( $id_or_email, $args = null ) {
4032      $args = wp_parse_args(
4033          $args,
4034          array(
4035              'size'           => 96,
4036              'height'         => null,
4037              'width'          => null,
4038              'default'        => get_option( 'avatar_default', 'mystery' ),
4039              'force_default'  => false,
4040              'rating'         => get_option( 'avatar_rating' ),
4041              'scheme'         => null,
4042              'processed_args' => null, // if used, should be a reference
4043              'extra_attr'     => '',
4044          )
4045      );
4046  
4047      if ( is_numeric( $args['size'] ) ) {
4048          $args['size'] = absint( $args['size'] );
4049          if ( ! $args['size'] ) {
4050              $args['size'] = 96;
4051          }
4052      } else {
4053          $args['size'] = 96;
4054      }
4055  
4056      if ( is_numeric( $args['height'] ) ) {
4057          $args['height'] = absint( $args['height'] );
4058          if ( ! $args['height'] ) {
4059              $args['height'] = $args['size'];
4060          }
4061      } else {
4062          $args['height'] = $args['size'];
4063      }
4064  
4065      if ( is_numeric( $args['width'] ) ) {
4066          $args['width'] = absint( $args['width'] );
4067          if ( ! $args['width'] ) {
4068              $args['width'] = $args['size'];
4069          }
4070      } else {
4071          $args['width'] = $args['size'];
4072      }
4073  
4074      if ( empty( $args['default'] ) ) {
4075          $args['default'] = get_option( 'avatar_default', 'mystery' );
4076      }
4077  
4078      switch ( $args['default'] ) {
4079          case 'mm':
4080          case 'mystery':
4081          case 'mysteryman':
4082              $args['default'] = 'mm';
4083              break;
4084          case 'gravatar_default':
4085              $args['default'] = false;
4086              break;
4087      }
4088  
4089      $args['force_default'] = (bool) $args['force_default'];
4090  
4091      $args['rating'] = strtolower( $args['rating'] );
4092  
4093      $args['found_avatar'] = false;
4094  
4095      /**
4096       * Filters whether to retrieve the avatar URL early.
4097       *
4098       * Passing a non-null value in the 'url' member of the return array will
4099       * effectively short circuit get_avatar_data(), passing the value through
4100       * the {@see 'get_avatar_data'} filter and returning early.
4101       *
4102       * @since 4.2.0
4103       *
4104       * @param array  $args        Arguments passed to get_avatar_data(), after processing.
4105       * @param mixed  $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
4106       *                            user email, WP_User object, WP_Post object, or WP_Comment object.
4107       */
4108      $args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email );
4109  
4110      if ( isset( $args['url'] ) ) {
4111          /** This filter is documented in wp-includes/link-template.php */
4112          return apply_filters( 'get_avatar_data', $args, $id_or_email );
4113      }
4114  
4115      $email_hash = '';
4116      $user       = false;
4117      $email      = false;
4118  
4119      if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
4120          $id_or_email = get_comment( $id_or_email );
4121      }
4122  
4123      // Process the user identifier.
4124      if ( is_numeric( $id_or_email ) ) {
4125          $user = get_user_by( 'id', absint( $id_or_email ) );
4126      } elseif ( is_string( $id_or_email ) ) {
4127          if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
4128              // md5 hash
4129              list( $email_hash ) = explode( '@', $id_or_email );
4130          } else {
4131              // email address
4132              $email = $id_or_email;
4133          }
4134      } elseif ( $id_or_email instanceof WP_User ) {
4135          // User Object
4136          $user = $id_or_email;
4137      } elseif ( $id_or_email instanceof WP_Post ) {
4138          // Post Object
4139          $user = get_user_by( 'id', (int) $id_or_email->post_author );
4140      } elseif ( $id_or_email instanceof WP_Comment ) {
4141          if ( ! is_avatar_comment_type( get_comment_type( $id_or_email ) ) ) {
4142              $args['url'] = false;
4143              /** This filter is documented in wp-includes/link-template.php */
4144              return apply_filters( 'get_avatar_data', $args, $id_or_email );
4145          }
4146  
4147          if ( ! empty( $id_or_email->user_id ) ) {
4148              $user = get_user_by( 'id', (int) $id_or_email->user_id );
4149          }
4150          if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) {
4151              $email = $id_or_email->comment_author_email;
4152          }
4153      }
4154  
4155      if ( ! $email_hash ) {
4156          if ( $user ) {
4157              $email = $user->user_email;
4158          }
4159  
4160          if ( $email ) {
4161              $email_hash = md5( strtolower( trim( $email ) ) );
4162          }
4163      }
4164  
4165      if ( $email_hash ) {
4166          $args['found_avatar'] = true;
4167          $gravatar_server      = hexdec( $email_hash[0] ) % 3;
4168      } else {
4169          $gravatar_server = rand( 0, 2 );
4170      }
4171  
4172      $url_args = array(
4173          's' => $args['size'],
4174          'd' => $args['default'],
4175          'f' => $args['force_default'] ? 'y' : false,
4176          'r' => $args['rating'],
4177      );
4178  
4179      if ( is_ssl() ) {
4180          $url = 'https://secure.gravatar.com/avatar/' . $email_hash;
4181      } else {
4182          $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash );
4183      }
4184  
4185      $url = add_query_arg(
4186          rawurlencode_deep( array_filter( $url_args ) ),
4187          set_url_scheme( $url, $args['scheme'] )
4188      );
4189  
4190      /**
4191       * Filters the avatar URL.
4192       *
4193       * @since 4.2.0
4194       *
4195       * @param string $url         The URL of the avatar.
4196       * @param mixed  $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
4197       *                            user email, WP_User object, WP_Post object, or WP_Comment object.
4198       * @param array  $args        Arguments passed to get_avatar_data(), after processing.
4199       */
4200      $args['url'] = apply_filters( 'get_avatar_url', $url, $id_or_email, $args );
4201  
4202      /**
4203       * Filters the avatar data.
4204       *
4205       * @since 4.2.0
4206       *
4207       * @param array  $args        Arguments passed to get_avatar_data(), after processing.
4208       * @param mixed  $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
4209       *                            user email, WP_User object, WP_Post object, or WP_Comment object.
4210       */
4211      return apply_filters( 'get_avatar_data', $args, $id_or_email );
4212  }
4213  
4214  /**
4215   * Retrieves the URL of a file in the theme.
4216   *
4217   * Searches in the stylesheet directory before the template directory so themes
4218   * which inherit from a parent theme can just override one file.
4219   *
4220   * @since 4.7.0
4221   *
4222   * @param string $file Optional. File to search for in the stylesheet directory.
4223   * @return string The URL of the file.
4224   */
4225  function get_theme_file_uri( $file = '' ) {
4226      $file = ltrim( $file, '/' );
4227  
4228      if ( empty( $file ) ) {
4229          $url = get_stylesheet_directory_uri();
4230      } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
4231          $url = get_stylesheet_directory_uri() . '/' . $file;
4232      } else {
4233          $url = get_template_directory_uri() . '/' . $file;
4234      }
4235  
4236      /**
4237       * Filters the URL to a file in the theme.
4238       *
4239       * @since 4.7.0
4240       *
4241       * @param string $url  The file URL.
4242       * @param string $file The requested file to search for.
4243       */
4244      return apply_filters( 'theme_file_uri', $url, $file );
4245  }
4246  
4247  /**
4248   * Retrieves the URL of a file in the parent theme.
4249   *
4250   * @since 4.7.0
4251   *
4252   * @param string $file Optional. File to return the URL for in the template directory.
4253   * @return string The URL of the file.
4254   */
4255  function get_parent_theme_file_uri( $file = '' ) {
4256      $file = ltrim( $file, '/' );
4257  
4258      if ( empty( $file ) ) {
4259          $url = get_template_directory_uri();
4260      } else {
4261          $url = get_template_directory_uri() . '/' . $file;
4262      }
4263  
4264      /**
4265       * Filters the URL to a file in the parent theme.
4266       *
4267       * @since 4.7.0
4268       *
4269       * @param string $url  The file URL.
4270       * @param string $file The requested file to search for.
4271       */
4272      return apply_filters( 'parent_theme_file_uri', $url, $file );
4273  }
4274  
4275  /**
4276   * Retrieves the path of a file in the theme.
4277   *
4278   * Searches in the stylesheet directory before the template directory so themes
4279   * which inherit from a parent theme can just override one file.
4280   *
4281   * @since 4.7.0
4282   *
4283   * @param string $file Optional. File to search for in the stylesheet directory.
4284   * @return string The path of the file.
4285   */
4286  function get_theme_file_path( $file = '' ) {
4287      $file = ltrim( $file, '/' );
4288  
4289      if ( empty( $file ) ) {
4290          $path = get_stylesheet_directory();
4291      } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
4292          $path = get_stylesheet_directory() . '/' . $file;
4293      } else {
4294          $path = get_template_directory() . '/' . $file;
4295      }
4296  
4297      /**
4298       * Filters the path to a file in the theme.
4299       *
4300       * @since 4.7.0
4301       *
4302       * @param string $path The file path.
4303       * @param string $file The requested file to search for.
4304       */
4305      return apply_filters( 'theme_file_path', $path, $file );
4306  }
4307  
4308  /**
4309   * Retrieves the path of a file in the parent theme.
4310   *
4311   * @since 4.7.0
4312   *
4313   * @param string $file Optional. File to return the path for in the template directory.
4314   * @return string The path of the file.
4315   */
4316  function get_parent_theme_file_path( $file = '' ) {
4317      $file = ltrim( $file, '/' );
4318  
4319      if ( empty( $file ) ) {
4320          $path = get_template_directory();
4321      } else {
4322          $path = get_template_directory() . '/' . $file;
4323      }
4324  
4325      /**
4326       * Filters the path to a file in the parent theme.
4327       *
4328       * @since 4.7.0
4329       *
4330       * @param string $path The file path.
4331       * @param string $file The requested file to search for.
4332       */
4333      return apply_filters( 'parent_theme_file_path', $path, $file );
4334  }
4335  
4336  /**
4337   * Retrieves the URL to the privacy policy page.
4338   *
4339   * @since 4.9.6
4340   *
4341   * @return string The URL to the privacy policy page. Empty string if it doesn't exist.
4342   */
4343  function get_privacy_policy_url() {
4344      $url            = '';
4345      $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
4346  
4347      if ( ! empty( $policy_page_id ) && get_post_status( $policy_page_id ) === 'publish' ) {
4348          $url = (string) get_permalink( $policy_page_id );
4349      }
4350  
4351      /**
4352       * Filters the URL of the privacy policy page.
4353       *
4354       * @since 4.9.6
4355       *
4356       * @param string $url            The URL to the privacy policy page. Empty string
4357       *                               if it doesn't exist.
4358       * @param int    $policy_page_id The ID of privacy policy page.
4359       */
4360      return apply_filters( 'privacy_policy_url', $url, $policy_page_id );
4361  }
4362  
4363  /**
4364   * Displays the privacy policy link with formatting, when applicable.
4365   *
4366   * @since 4.9.6
4367   *
4368   * @param string $before Optional. Display before privacy policy link. Default empty.
4369   * @param string $after  Optional. Display after privacy policy link. Default empty.
4370   */
4371  function the_privacy_policy_link( $before = '', $after = '' ) {
4372      echo get_the_privacy_policy_link( $before, $after );
4373  }
4374  
4375  /**
4376   * Returns the privacy policy link with formatting, when applicable.
4377   *
4378   * @since 4.9.6
4379   *
4380   * @param string $before Optional. Display before privacy policy link. Default empty.
4381   * @param string $after  Optional. Display after privacy policy link. Default empty.
4382   *
4383   * @return string Markup for the link and surrounding elements. Empty string if it
4384   *                doesn't exist.
4385   */
4386  function get_the_privacy_policy_link( $before = '', $after = '' ) {
4387      $link               = '';
4388      $privacy_policy_url = get_privacy_policy_url();
4389      $policy_page_id     = (int) get_option( 'wp_page_for_privacy_policy' );
4390      $page_title         = ( $policy_page_id ) ? get_the_title( $policy_page_id ) : '';
4391  
4392      if ( $privacy_policy_url && $page_title ) {
4393          $link = sprintf(
4394              '<a class="privacy-policy-link" href="%s">%s</a>',
4395              esc_url( $privacy_policy_url ),
4396              esc_html( $page_title )
4397          );
4398      }
4399  
4400      /**
4401       * Filters the privacy policy link.
4402       *
4403       * @since 4.9.6
4404       *
4405       * @param string $link               The privacy policy link. Empty string if it
4406       *                                   doesn't exist.
4407       * @param string $privacy_policy_url The URL of the privacy policy. Empty string
4408       *                                   if it doesn't exist.
4409       */
4410      $link = apply_filters( 'the_privacy_policy_link', $link, $privacy_policy_url );
4411  
4412      if ( $link ) {
4413          return $before . $link . $after;
4414      }
4415  
4416      return '';
4417  }


Generated: Tue Sep 17 01:00:03 2019 Cross-referenced by PHPXref 0.7.1