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


Generated: Tue Jan 28 01:00:03 2020 Cross-referenced by PHPXref 0.7.1