[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * WordPress Post Template Functions.
   4   *
   5   * Gets content for the current post in the loop.
   6   *
   7   * @package WordPress
   8   * @subpackage Template
   9   */
  10  
  11  /**
  12   * Displays the ID of the current item in the WordPress Loop.
  13   *
  14   * @since 0.71
  15   */
  16  function the_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  17      echo get_the_ID();
  18  }
  19  
  20  /**
  21   * Retrieves the ID of the current item in the WordPress Loop.
  22   *
  23   * @since 2.1.0
  24   *
  25   * @return int|false The ID of the current item in the WordPress Loop. False if $post is not set.
  26   */
  27  function get_the_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  28      $post = get_post();
  29      return ! empty( $post ) ? $post->ID : false;
  30  }
  31  
  32  /**
  33   * Displays or retrieves the current post title with optional markup.
  34   *
  35   * @since 0.71
  36   *
  37   * @param string $before  Optional. Markup to prepend to the title. Default empty.
  38   * @param string $after   Optional. Markup to append to the title. Default empty.
  39   * @param bool   $display Optional. Whether to echo or return the title. Default true for echo.
  40   * @return void|string Void if `$display` argument is true or the title is empty,
  41   *                     current post title if `$display` is false.
  42   */
  43  function the_title( $before = '', $after = '', $display = true ) {
  44      $title = get_the_title();
  45  
  46      if ( strlen( $title ) === 0 ) {
  47          return;
  48      }
  49  
  50      $title = $before . $title . $after;
  51  
  52      if ( $display ) {
  53          echo $title;
  54      } else {
  55          return $title;
  56      }
  57  }
  58  
  59  /**
  60   * Sanitizes the current title when retrieving or displaying.
  61   *
  62   * Works like the_title(), except the parameters can be in a string or
  63   * an array. See the function for what can be override in the $args parameter.
  64   *
  65   * The title before it is displayed will have the tags stripped and esc_attr()
  66   * before it is passed to the user or displayed. The default as with the_title(),
  67   * is to display the title.
  68   *
  69   * @since 2.3.0
  70   *
  71   * @param string|array $args {
  72   *     Title attribute arguments. Optional.
  73   *
  74   *     @type string  $before Markup to prepend to the title. Default empty.
  75   *     @type string  $after  Markup to append to the title. Default empty.
  76   *     @type bool    $echo   Whether to echo or return the title. Default true for echo.
  77   *     @type WP_Post $post   Current post object to retrieve the title for.
  78   * }
  79   * @return void|string Void if 'echo' argument is true, the title attribute if 'echo' is false.
  80   */
  81  function the_title_attribute( $args = '' ) {
  82      $defaults    = array(
  83          'before' => '',
  84          'after'  => '',
  85          'echo'   => true,
  86          'post'   => get_post(),
  87      );
  88      $parsed_args = wp_parse_args( $args, $defaults );
  89  
  90      $title = get_the_title( $parsed_args['post'] );
  91  
  92      if ( strlen( $title ) === 0 ) {
  93          return;
  94      }
  95  
  96      $title = $parsed_args['before'] . $title . $parsed_args['after'];
  97      $title = esc_attr( strip_tags( $title ) );
  98  
  99      if ( $parsed_args['echo'] ) {
 100          echo $title;
 101      } else {
 102          return $title;
 103      }
 104  }
 105  
 106  /**
 107   * Retrieves the post title.
 108   *
 109   * If the post is protected and the visitor is not an admin, then "Protected"
 110   * will be inserted before the post title. If the post is private, then
 111   * "Private" will be inserted before the post title.
 112   *
 113   * @since 0.71
 114   *
 115   * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
 116   * @return string
 117   */
 118  function get_the_title( $post = 0 ) {
 119      $post = get_post( $post );
 120  
 121      $post_title = $post->post_title ?? '';
 122      $post_id    = $post->ID ?? 0;
 123  
 124      if ( ! is_admin() ) {
 125          if ( ! empty( $post->post_password ) ) {
 126  
 127              /* translators: %s: Protected post title. */
 128              $prepend = __( 'Protected: %s' );
 129  
 130              /**
 131               * Filters the text prepended to the post title for protected posts.
 132               *
 133               * The filter is only applied on the front end.
 134               *
 135               * @since 2.8.0
 136               *
 137               * @param string  $prepend Text displayed before the post title.
 138               *                         Default 'Protected: %s'.
 139               * @param WP_Post $post    Current post object.
 140               */
 141              $protected_title_format = apply_filters( 'protected_title_format', $prepend, $post );
 142  
 143              $post_title = sprintf( $protected_title_format, $post_title );
 144          } elseif ( isset( $post->post_status ) && 'private' === $post->post_status ) {
 145  
 146              /* translators: %s: Private post title. */
 147              $prepend = __( 'Private: %s' );
 148  
 149              /**
 150               * Filters the text prepended to the post title of private posts.
 151               *
 152               * The filter is only applied on the front end.
 153               *
 154               * @since 2.8.0
 155               *
 156               * @param string  $prepend Text displayed before the post title.
 157               *                         Default 'Private: %s'.
 158               * @param WP_Post $post    Current post object.
 159               */
 160              $private_title_format = apply_filters( 'private_title_format', $prepend, $post );
 161  
 162              $post_title = sprintf( $private_title_format, $post_title );
 163          }
 164      }
 165  
 166      /**
 167       * Filters the post title.
 168       *
 169       * @since 0.71
 170       *
 171       * @param string $post_title The post title.
 172       * @param int    $post_id    The post ID.
 173       */
 174      return apply_filters( 'the_title', $post_title, $post_id );
 175  }
 176  
 177  /**
 178   * Displays the Post Global Unique Identifier (guid).
 179   *
 180   * The guid will appear to be a link, but should not be used as a link to the
 181   * post. The reason you should not use it as a link, is because of moving the
 182   * blog across domains.
 183   *
 184   * URL is escaped to make it XML-safe.
 185   *
 186   * @since 1.5.0
 187   *
 188   * @param int|WP_Post $post Optional. Post ID or post object. Default is global $post.
 189   */
 190  function the_guid( $post = 0 ) {
 191      $post = get_post( $post );
 192  
 193      $post_guid = isset( $post->guid ) ? get_the_guid( $post ) : '';
 194      $post_id   = $post->ID ?? 0;
 195  
 196      /**
 197       * Filters the escaped Global Unique Identifier (guid) of the post.
 198       *
 199       * @since 4.2.0
 200       *
 201       * @see get_the_guid()
 202       *
 203       * @param string $post_guid Escaped Global Unique Identifier (guid) of the post.
 204       * @param int    $post_id   The post ID.
 205       */
 206      echo apply_filters( 'the_guid', $post_guid, $post_id );
 207  }
 208  
 209  /**
 210   * Retrieves the Post Global Unique Identifier (guid).
 211   *
 212   * The guid will appear to be a link, but should not be used as an link to the
 213   * post. The reason you should not use it as a link, is because of moving the
 214   * blog across domains.
 215   *
 216   * @since 1.5.0
 217   *
 218   * @param int|WP_Post $post Optional. Post ID or post object. Default is global $post.
 219   * @return string
 220   */
 221  function get_the_guid( $post = 0 ) {
 222      $post = get_post( $post );
 223  
 224      $post_guid = $post->guid ?? '';
 225      $post_id   = $post->ID ?? 0;
 226  
 227      /**
 228       * Filters the Global Unique Identifier (guid) of the post.
 229       *
 230       * @since 1.5.0
 231       *
 232       * @param string $post_guid Global Unique Identifier (guid) of the post.
 233       * @param int    $post_id   The post ID.
 234       */
 235      return apply_filters( 'get_the_guid', $post_guid, $post_id );
 236  }
 237  
 238  /**
 239   * Displays the post content.
 240   *
 241   * @since 0.71
 242   *
 243   * @param string $more_link_text Optional. Content for when there is more text.
 244   * @param bool   $strip_teaser   Optional. Strip teaser content before the more text. Default false.
 245   */
 246  function the_content( $more_link_text = null, $strip_teaser = false ) {
 247      $content = get_the_content( $more_link_text, $strip_teaser );
 248  
 249      /**
 250       * Filters the post content.
 251       *
 252       * @since 0.71
 253       *
 254       * @param string $content Content of the current post.
 255       */
 256      $content = apply_filters( 'the_content', $content );
 257      $content = str_replace( ']]>', ']]&gt;', $content );
 258      echo $content;
 259  }
 260  
 261  /**
 262   * Retrieves the post content.
 263   *
 264   * @since 0.71
 265   * @since 5.2.0 Added the `$post` parameter.
 266   *
 267   * @global int   $page      Page number of a single post/page.
 268   * @global int   $more      Boolean indicator for whether single post/page is being viewed.
 269   * @global bool  $preview   Whether post/page is in preview mode.
 270   * @global array $pages     Array of all pages in post/page. Each array element contains
 271   *                          part of the content separated by the `<!--nextpage-->` tag.
 272   * @global int   $multipage Boolean indicator for whether multiple pages are in play.
 273   *
 274   * @param string             $more_link_text Optional. Content for when there is more text.
 275   * @param bool               $strip_teaser   Optional. Strip teaser content before the more text. Default false.
 276   * @param WP_Post|object|int $post           Optional. WP_Post instance or Post ID/object. Default null.
 277   * @return string
 278   */
 279  function get_the_content( $more_link_text = null, $strip_teaser = false, $post = null ) {
 280      global $page, $more, $preview, $pages, $multipage;
 281  
 282      $_post = get_post( $post );
 283  
 284      if ( ! ( $_post instanceof WP_Post ) ) {
 285          return '';
 286      }
 287  
 288      /*
 289       * Use the globals if the $post parameter was not specified,
 290       * but only after they have been set up in setup_postdata().
 291       */
 292      if ( null === $post && did_action( 'the_post' ) ) {
 293          $elements = compact( 'page', 'more', 'preview', 'pages', 'multipage' );
 294      } else {
 295          $elements = generate_postdata( $_post );
 296      }
 297  
 298      if ( null === $more_link_text ) {
 299          $more_link_text = sprintf(
 300              '<span aria-label="%1$s">%2$s</span>',
 301              sprintf(
 302                  /* translators: %s: Post title. */
 303                  __( 'Continue reading %s' ),
 304                  the_title_attribute(
 305                      array(
 306                          'echo' => false,
 307                          'post' => $_post,
 308                      )
 309                  )
 310              ),
 311              __( '(more&hellip;)' )
 312          );
 313      }
 314  
 315      $output     = '';
 316      $has_teaser = false;
 317  
 318      // If post password required and it doesn't match the cookie.
 319      if ( post_password_required( $_post ) ) {
 320          return get_the_password_form( $_post );
 321      }
 322  
 323      // If the requested page doesn't exist.
 324      if ( $elements['page'] > count( $elements['pages'] ) ) {
 325          // Give them the highest numbered page that DOES exist.
 326          $elements['page'] = count( $elements['pages'] );
 327      }
 328  
 329      $page_no = $elements['page'];
 330      $content = $elements['pages'][ $page_no - 1 ];
 331      if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
 332          if ( has_block( 'more', $content ) ) {
 333              // Remove the core/more block delimiters. They will be left over after $content is split up.
 334              $content = preg_replace( '/<!-- \/?wp:more(.*?) -->/', '', $content );
 335          }
 336  
 337          $content = explode( $matches[0], $content, 2 );
 338  
 339          if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) ) {
 340              $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
 341          }
 342  
 343          $has_teaser = true;
 344      } else {
 345          $content = array( $content );
 346      }
 347  
 348      if ( str_contains( $_post->post_content, '<!--noteaser-->' )
 349          && ( ! $elements['multipage'] || 1 === $elements['page'] )
 350      ) {
 351          $strip_teaser = true;
 352      }
 353  
 354      $teaser = $content[0];
 355  
 356      if ( $elements['more'] && $strip_teaser && $has_teaser ) {
 357          $teaser = '';
 358      }
 359  
 360      $output .= $teaser;
 361  
 362      if ( count( $content ) > 1 ) {
 363          if ( $elements['more'] ) {
 364              $output .= '<span id="more-' . $_post->ID . '"></span>' . $content[1];
 365          } else {
 366              if ( ! empty( $more_link_text ) ) {
 367  
 368                  /**
 369                   * Filters the Read More link text.
 370                   *
 371                   * @since 2.8.0
 372                   *
 373                   * @param string $more_link_element Read More link element.
 374                   * @param string $more_link_text    Read More text.
 375                   */
 376                  $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink( $_post ) . "#more-{$_post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );
 377              }
 378              $output = force_balance_tags( $output );
 379          }
 380      }
 381  
 382      return $output;
 383  }
 384  
 385  /**
 386   * Displays the post excerpt.
 387   *
 388   * @since 0.71
 389   */
 390  function the_excerpt() {
 391  
 392      /**
 393       * Filters the displayed post excerpt.
 394       *
 395       * @since 0.71
 396       *
 397       * @see get_the_excerpt()
 398       *
 399       * @param string $post_excerpt The post excerpt.
 400       */
 401      echo apply_filters( 'the_excerpt', get_the_excerpt() );
 402  }
 403  
 404  /**
 405   * Retrieves the post excerpt.
 406   *
 407   * @since 0.71
 408   * @since 4.5.0 Introduced the `$post` parameter.
 409   *
 410   * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
 411   * @return string Post excerpt.
 412   */
 413  function get_the_excerpt( $post = null ) {
 414      if ( is_bool( $post ) ) {
 415          _deprecated_argument( __FUNCTION__, '2.3.0' );
 416      }
 417  
 418      $post = get_post( $post );
 419      if ( empty( $post ) ) {
 420          return '';
 421      }
 422  
 423      if ( post_password_required( $post ) ) {
 424          return __( 'There is no excerpt because this is a protected post.' );
 425      }
 426  
 427      /**
 428       * Filters the retrieved post excerpt.
 429       *
 430       * @since 1.2.0
 431       * @since 4.5.0 Introduced the `$post` parameter.
 432       *
 433       * @param string  $post_excerpt The post excerpt.
 434       * @param WP_Post $post         Post object.
 435       */
 436      return apply_filters( 'get_the_excerpt', $post->post_excerpt, $post );
 437  }
 438  
 439  /**
 440   * Determines whether the post has a custom excerpt.
 441   *
 442   * For more information on this and similar theme functions, check out
 443   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 444   * Conditional Tags} article in the Theme Developer Handbook.
 445   *
 446   * @since 2.3.0
 447   *
 448   * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
 449   * @return bool True if the post has a custom excerpt, false otherwise.
 450   */
 451  function has_excerpt( $post = 0 ) {
 452      $post = get_post( $post );
 453      return ( ! empty( $post->post_excerpt ) );
 454  }
 455  
 456  /**
 457   * Displays the classes for the post container element.
 458   *
 459   * @since 2.7.0
 460   *
 461   * @param string|string[] $css_class Optional. One or more classes to add to the class list.
 462   *                                   Default empty.
 463   * @param int|WP_Post     $post      Optional. Post ID or post object. Defaults to the global `$post`.
 464   */
 465  function post_class( $css_class = '', $post = null ) {
 466      // Separates classes with a single space, collates classes for post DIV.
 467      echo 'class="' . esc_attr( implode( ' ', get_post_class( $css_class, $post ) ) ) . '"';
 468  }
 469  
 470  /**
 471   * Retrieves an array of the class names for the post container element.
 472   *
 473   * The class names are many:
 474   *
 475   *  - If the post has a post thumbnail, `has-post-thumbnail` is added as a class.
 476   *  - If the post is sticky, then the `sticky` class name is added.
 477   *  - The class `hentry` is always added to each post.
 478   *  - For each taxonomy that the post belongs to, a class will be added of the format
 479   *    `{$taxonomy}-{$slug}`, e.g. `category-foo` or `my_custom_taxonomy-bar`.
 480   *    The `post_tag` taxonomy is a special case; the class has the `tag-` prefix
 481   *    instead of `post_tag-`.
 482   *
 483   * All class names are passed through the filter, {@see 'post_class'}, followed by
 484   * `$css_class` parameter value, with the post ID as the last parameter.
 485   *
 486   * @since 2.7.0
 487   * @since 4.2.0 Custom taxonomy class names were added.
 488   *
 489   * @param string|string[] $css_class Optional. Space-separated string or array of class names
 490   *                                   to add to the class list. Default empty.
 491   * @param int|WP_Post     $post      Optional. Post ID or post object.
 492   * @return string[] Array of class names.
 493   */
 494  function get_post_class( $css_class = '', $post = null ) {
 495      $post = get_post( $post );
 496  
 497      $classes = array();
 498  
 499      if ( $css_class ) {
 500          if ( ! is_array( $css_class ) ) {
 501              $css_class = preg_split( '#\s+#', $css_class );
 502          }
 503          $classes = array_map( 'esc_attr', $css_class );
 504      } else {
 505          // Ensure that we always coerce class to being an array.
 506          $css_class = array();
 507      }
 508  
 509      if ( ! $post ) {
 510          return $classes;
 511      }
 512  
 513      $classes[] = 'post-' . $post->ID;
 514      if ( ! is_admin() ) {
 515          $classes[] = $post->post_type;
 516      }
 517      $classes[] = 'type-' . $post->post_type;
 518      $classes[] = 'status-' . $post->post_status;
 519  
 520      // Post Format.
 521      if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
 522          $post_format = get_post_format( $post->ID );
 523  
 524          if ( $post_format && ! is_wp_error( $post_format ) ) {
 525              $classes[] = 'format-' . sanitize_html_class( $post_format );
 526          } else {
 527              $classes[] = 'format-standard';
 528          }
 529      }
 530  
 531      $post_password_required = post_password_required( $post->ID );
 532  
 533      // Post requires password.
 534      if ( $post_password_required ) {
 535          $classes[] = 'post-password-required';
 536      } elseif ( ! empty( $post->post_password ) ) {
 537          $classes[] = 'post-password-protected';
 538      }
 539  
 540      // Post thumbnails.
 541      if ( current_theme_supports( 'post-thumbnails' ) && has_post_thumbnail( $post->ID ) && ! is_attachment( $post ) && ! $post_password_required ) {
 542          $classes[] = 'has-post-thumbnail';
 543      }
 544  
 545      // Sticky for Sticky Posts.
 546      if ( is_sticky( $post->ID ) ) {
 547          if ( is_home() && ! is_paged() ) {
 548              $classes[] = 'sticky';
 549          } elseif ( is_admin() ) {
 550              $classes[] = 'status-sticky';
 551          }
 552      }
 553  
 554      // hentry for hAtom compliance.
 555      $classes[] = 'hentry';
 556  
 557      // All public taxonomies.
 558      $taxonomies = get_taxonomies( array( 'public' => true ) );
 559  
 560      /**
 561       * Filters the taxonomies to generate classes for each individual term.
 562       *
 563       * Default is all public taxonomies registered to the post type.
 564       *
 565       * @since 6.1.0
 566       *
 567       * @param string[] $taxonomies List of all taxonomy names to generate classes for.
 568       * @param int      $post_id    The post ID.
 569       * @param string[] $classes    An array of post class names.
 570       * @param string[] $css_class  An array of additional class names added to the post.
 571      */
 572      $taxonomies = apply_filters( 'post_class_taxonomies', $taxonomies, $post->ID, $classes, $css_class );
 573  
 574      foreach ( (array) $taxonomies as $taxonomy ) {
 575          if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
 576              foreach ( (array) get_the_terms( $post->ID, $taxonomy ) as $term ) {
 577                  if ( empty( $term->slug ) ) {
 578                      continue;
 579                  }
 580  
 581                  $term_class = sanitize_html_class( $term->slug, $term->term_id );
 582                  if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
 583                      $term_class = $term->term_id;
 584                  }
 585  
 586                  // 'post_tag' uses the 'tag' prefix for backward compatibility.
 587                  if ( 'post_tag' === $taxonomy ) {
 588                      $classes[] = 'tag-' . $term_class;
 589                  } else {
 590                      $classes[] = sanitize_html_class( $taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id );
 591                  }
 592              }
 593          }
 594      }
 595  
 596      $classes = array_map( 'esc_attr', $classes );
 597  
 598      /**
 599       * Filters the list of CSS class names for the current post.
 600       *
 601       * @since 2.7.0
 602       *
 603       * @param string[] $classes   An array of post class names.
 604       * @param string[] $css_class An array of additional class names added to the post.
 605       * @param int      $post_id   The post ID.
 606       */
 607      $classes = apply_filters( 'post_class', $classes, $css_class, $post->ID );
 608  
 609      $classes = array_unique( $classes );
 610      $classes = array_values( $classes );
 611  
 612      return $classes;
 613  }
 614  
 615  /**
 616   * Displays the class names for the body element.
 617   *
 618   * @since 2.8.0
 619   *
 620   * @param string|string[] $css_class Optional. Space-separated string or array of class names
 621   *                                   to add to the class list. Default empty.
 622   */
 623  function body_class( $css_class = '' ) {
 624      // Separates class names with a single space, collates class names for body element.
 625      echo 'class="' . esc_attr( implode( ' ', get_body_class( $css_class ) ) ) . '"';
 626  }
 627  
 628  /**
 629   * Retrieves an array of the class names for the body element.
 630   *
 631   * @since 2.8.0
 632   *
 633   * @global WP_Query $wp_query WordPress Query object.
 634   *
 635   * @param string|string[] $css_class Optional. Space-separated string or array of class names
 636   *                                   to add to the class list. Default empty.
 637   * @return string[] Array of class names.
 638   */
 639  function get_body_class( $css_class = '' ) {
 640      global $wp_query;
 641  
 642      $classes = array();
 643  
 644      if ( is_rtl() ) {
 645          $classes[] = 'rtl';
 646      }
 647  
 648      if ( is_front_page() ) {
 649          $classes[] = 'home';
 650      }
 651      if ( is_home() ) {
 652          $classes[] = 'blog';
 653      }
 654      if ( is_privacy_policy() ) {
 655          $classes[] = 'privacy-policy';
 656      }
 657      if ( is_archive() ) {
 658          $classes[] = 'archive';
 659      }
 660      if ( is_date() ) {
 661          $classes[] = 'date';
 662      }
 663      if ( is_search() ) {
 664          $classes[] = 'search';
 665          $classes[] = $wp_query->posts ? 'search-results' : 'search-no-results';
 666      }
 667      if ( is_paged() ) {
 668          $classes[] = 'paged';
 669      }
 670      if ( is_attachment() ) {
 671          $classes[] = 'attachment';
 672      }
 673      if ( is_404() ) {
 674          $classes[] = 'error404';
 675      }
 676  
 677      if ( is_singular() ) {
 678          $post      = $wp_query->get_queried_object();
 679          $post_id   = $post->ID;
 680          $post_type = $post->post_type;
 681  
 682          $classes[] = 'wp-singular';
 683  
 684          if ( is_page_template() ) {
 685              $classes[] = "{$post_type}-template";
 686  
 687              $template_slug  = get_page_template_slug( $post_id );
 688              $template_parts = explode( '/', $template_slug );
 689  
 690              foreach ( $template_parts as $part ) {
 691                  $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) );
 692              }
 693              $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( '.', '-', $template_slug ) );
 694          } else {
 695              $classes[] = "{$post_type}-template-default";
 696          }
 697  
 698          if ( is_single() ) {
 699              $classes[] = 'single';
 700              if ( isset( $post->post_type ) ) {
 701                  $classes[] = 'single-' . sanitize_html_class( $post->post_type, $post_id );
 702                  $classes[] = 'postid-' . $post_id;
 703  
 704                  // Post Format.
 705                  if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
 706                      $post_format = get_post_format( $post->ID );
 707  
 708                      if ( $post_format && ! is_wp_error( $post_format ) ) {
 709                          $classes[] = 'single-format-' . sanitize_html_class( $post_format );
 710                      } else {
 711                          $classes[] = 'single-format-standard';
 712                      }
 713                  }
 714              }
 715          }
 716  
 717          if ( is_attachment() ) {
 718              $mime_type   = get_post_mime_type( $post_id );
 719              $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
 720              $classes[]   = 'attachmentid-' . $post_id;
 721              $classes[]   = 'attachment-' . str_replace( $mime_prefix, '', $mime_type );
 722          } elseif ( is_page() ) {
 723              $classes[] = 'page';
 724              $classes[] = 'page-id-' . $post_id;
 725  
 726              if ( get_pages(
 727                  array(
 728                      'parent' => $post_id,
 729                      'number' => 1,
 730                  )
 731              ) ) {
 732                  $classes[] = 'page-parent';
 733              }
 734  
 735              if ( $post->post_parent ) {
 736                  $classes[] = 'page-child';
 737                  $classes[] = 'parent-pageid-' . $post->post_parent;
 738              }
 739          }
 740      } elseif ( is_archive() ) {
 741          if ( is_post_type_archive() ) {
 742              $classes[] = 'post-type-archive';
 743              $post_type = get_query_var( 'post_type' );
 744              if ( is_array( $post_type ) ) {
 745                  $post_type = reset( $post_type );
 746              }
 747              $classes[] = 'post-type-archive-' . sanitize_html_class( $post_type );
 748          } elseif ( is_author() ) {
 749              $author    = $wp_query->get_queried_object();
 750              $classes[] = 'author';
 751              if ( isset( $author->user_nicename ) ) {
 752                  $classes[] = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID );
 753                  $classes[] = 'author-' . $author->ID;
 754              }
 755          } elseif ( is_category() ) {
 756              $cat       = $wp_query->get_queried_object();
 757              $classes[] = 'category';
 758              if ( isset( $cat->term_id ) ) {
 759                  $cat_class = sanitize_html_class( $cat->slug, $cat->term_id );
 760                  if ( is_numeric( $cat_class ) || ! trim( $cat_class, '-' ) ) {
 761                      $cat_class = $cat->term_id;
 762                  }
 763  
 764                  $classes[] = 'category-' . $cat_class;
 765                  $classes[] = 'category-' . $cat->term_id;
 766              }
 767          } elseif ( is_tag() ) {
 768              $tag       = $wp_query->get_queried_object();
 769              $classes[] = 'tag';
 770              if ( isset( $tag->term_id ) ) {
 771                  $tag_class = sanitize_html_class( $tag->slug, $tag->term_id );
 772                  if ( is_numeric( $tag_class ) || ! trim( $tag_class, '-' ) ) {
 773                      $tag_class = $tag->term_id;
 774                  }
 775  
 776                  $classes[] = 'tag-' . $tag_class;
 777                  $classes[] = 'tag-' . $tag->term_id;
 778              }
 779          } elseif ( is_tax() ) {
 780              $term = $wp_query->get_queried_object();
 781              if ( isset( $term->term_id ) ) {
 782                  $term_class = sanitize_html_class( $term->slug, $term->term_id );
 783                  if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
 784                      $term_class = $term->term_id;
 785                  }
 786  
 787                  $classes[] = 'tax-' . sanitize_html_class( $term->taxonomy );
 788                  $classes[] = 'term-' . $term_class;
 789                  $classes[] = 'term-' . $term->term_id;
 790              }
 791          }
 792      }
 793  
 794      if ( is_user_logged_in() ) {
 795          $classes[] = 'logged-in';
 796      }
 797  
 798      if ( is_admin_bar_showing() ) {
 799          $classes[] = 'admin-bar';
 800          $classes[] = 'no-customize-support';
 801      }
 802  
 803      if ( current_theme_supports( 'custom-background' )
 804          && ( get_background_color() !== get_theme_support( 'custom-background', 'default-color' ) || get_background_image() ) ) {
 805          $classes[] = 'custom-background';
 806      }
 807  
 808      if ( has_custom_logo() ) {
 809          $classes[] = 'wp-custom-logo';
 810      }
 811  
 812      if ( current_theme_supports( 'responsive-embeds' ) ) {
 813          $classes[] = 'wp-embed-responsive';
 814      }
 815  
 816      $page = $wp_query->get( 'page' );
 817  
 818      if ( ! $page || $page < 2 ) {
 819          $page = $wp_query->get( 'paged' );
 820      }
 821  
 822      if ( $page && $page > 1 && ! is_404() ) {
 823          $classes[] = 'paged-' . $page;
 824  
 825          if ( is_single() ) {
 826              $classes[] = 'single-paged-' . $page;
 827          } elseif ( is_page() ) {
 828              $classes[] = 'page-paged-' . $page;
 829          } elseif ( is_category() ) {
 830              $classes[] = 'category-paged-' . $page;
 831          } elseif ( is_tag() ) {
 832              $classes[] = 'tag-paged-' . $page;
 833          } elseif ( is_date() ) {
 834              $classes[] = 'date-paged-' . $page;
 835          } elseif ( is_author() ) {
 836              $classes[] = 'author-paged-' . $page;
 837          } elseif ( is_search() ) {
 838              $classes[] = 'search-paged-' . $page;
 839          } elseif ( is_post_type_archive() ) {
 840              $classes[] = 'post-type-paged-' . $page;
 841          }
 842      }
 843  
 844      $classes[] = 'wp-theme-' . sanitize_html_class( get_template() );
 845      if ( is_child_theme() ) {
 846          $classes[] = 'wp-child-theme-' . sanitize_html_class( get_stylesheet() );
 847      }
 848  
 849      if ( ! empty( $css_class ) ) {
 850          if ( ! is_array( $css_class ) ) {
 851              $css_class = preg_split( '#\s+#', $css_class );
 852          }
 853          $classes = array_merge( $classes, $css_class );
 854      } else {
 855          // Ensure that we always coerce class to being an array.
 856          $css_class = array();
 857      }
 858  
 859      $classes = array_map( 'esc_attr', $classes );
 860  
 861      /**
 862       * Filters the list of CSS body class names for the current post or page.
 863       *
 864       * @since 2.8.0
 865       *
 866       * @param string[] $classes   An array of body class names.
 867       * @param string[] $css_class An array of additional class names added to the body.
 868       */
 869      $classes = apply_filters( 'body_class', $classes, $css_class );
 870  
 871      return array_unique( $classes );
 872  }
 873  
 874  /**
 875   * Determines whether the post requires password and whether a correct password has been provided.
 876   *
 877   * @since 2.7.0
 878   *
 879   * @param int|WP_Post|null $post An optional post. Global $post used if not provided.
 880   * @return bool false if a password is not required or the correct password cookie is present, true otherwise.
 881   */
 882  function post_password_required( $post = null ) {
 883      $post = get_post( $post );
 884  
 885      if ( empty( $post->post_password ) ) {
 886          /** This filter is documented in wp-includes/post-template.php */
 887          return apply_filters( 'post_password_required', false, $post );
 888      }
 889  
 890      if ( ! isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) {
 891          /** This filter is documented in wp-includes/post-template.php */
 892          return apply_filters( 'post_password_required', true, $post );
 893      }
 894  
 895      require_once  ABSPATH . WPINC . '/class-phpass.php';
 896      $hasher = new PasswordHash( 8, true );
 897  
 898      $hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
 899      if ( ! str_starts_with( $hash, '$P$B' ) ) {
 900          $required = true;
 901      } else {
 902          $required = ! $hasher->CheckPassword( $post->post_password, $hash );
 903      }
 904  
 905      /**
 906       * Filters whether a post requires the user to supply a password.
 907       *
 908       * @since 4.7.0
 909       *
 910       * @param bool    $required Whether the user needs to supply a password. True if password has not been
 911       *                          provided or is incorrect, false if password has been supplied or is not required.
 912       * @param WP_Post $post     Post object.
 913       */
 914      return apply_filters( 'post_password_required', $required, $post );
 915  }
 916  
 917  //
 918  // Page Template Functions for usage in Themes.
 919  //
 920  
 921  /**
 922   * The formatted output of a list of pages.
 923   *
 924   * Displays page links for paginated posts (i.e. including the `<!--nextpage-->`
 925   * Quicktag one or more times). This tag must be within The Loop.
 926   *
 927   * @since 1.2.0
 928   * @since 5.1.0 Added the `aria_current` argument.
 929   *
 930   * @global int $page
 931   * @global int $numpages
 932   * @global int $multipage
 933   * @global int $more
 934   *
 935   * @param string|array $args {
 936   *     Optional. Array or string of default arguments.
 937   *
 938   *     @type string       $before           HTML or text to prepend to each link. Default is `<p> Pages:`.
 939   *     @type string       $after            HTML or text to append to each link. Default is `</p>`.
 940   *     @type string       $link_before      HTML or text to prepend to each link, inside the `<a>` tag.
 941   *                                          Also prepended to the current item, which is not linked. Default empty.
 942   *     @type string       $link_after       HTML or text to append to each Pages link inside the `<a>` tag.
 943   *                                          Also appended to the current item, which is not linked. Default empty.
 944   *     @type string       $aria_current     The value for the aria-current attribute. Possible values are 'page',
 945   *                                          'step', 'location', 'date', 'time', 'true', 'false'. Default is 'page'.
 946   *     @type string       $next_or_number   Indicates whether page numbers should be used. Valid values are number
 947   *                                          and next. Default is 'number'.
 948   *     @type string       $separator        Text between pagination links. Default is ' '.
 949   *     @type string       $nextpagelink     Link text for the next page link, if available. Default is 'Next Page'.
 950   *     @type string       $previouspagelink Link text for the previous page link, if available. Default is 'Previous Page'.
 951   *     @type string       $pagelink         Format string for page numbers. The % in the parameter string will be
 952   *                                          replaced with the page number, so 'Page %' generates "Page 1", "Page 2", etc.
 953   *                                          Defaults to '%', just the page number.
 954   *     @type int|bool     $echo             Whether to echo or not. Accepts 1|true or 0|false. Default 1|true.
 955   * }
 956   * @return string Formatted output in HTML.
 957   */
 958  function wp_link_pages( $args = '' ) {
 959      global $page, $numpages, $multipage, $more;
 960  
 961      $defaults = array(
 962          'before'           => '<p class="post-nav-links">' . __( 'Pages:' ),
 963          'after'            => '</p>',
 964          'link_before'      => '',
 965          'link_after'       => '',
 966          'aria_current'     => 'page',
 967          'next_or_number'   => 'number',
 968          'separator'        => ' ',
 969          'nextpagelink'     => __( 'Next page' ),
 970          'previouspagelink' => __( 'Previous page' ),
 971          'pagelink'         => '%',
 972          'echo'             => 1,
 973      );
 974  
 975      $parsed_args = wp_parse_args( $args, $defaults );
 976  
 977      /**
 978       * Filters the arguments used in retrieving page links for paginated posts.
 979       *
 980       * @since 3.0.0
 981       *
 982       * @param array $parsed_args An array of page link arguments. See wp_link_pages()
 983       *                           for information on accepted arguments.
 984       */
 985      $parsed_args = apply_filters( 'wp_link_pages_args', $parsed_args );
 986  
 987      $output = '';
 988      if ( $multipage ) {
 989          if ( 'number' === $parsed_args['next_or_number'] ) {
 990              $output .= $parsed_args['before'];
 991              for ( $i = 1; $i <= $numpages; $i++ ) {
 992                  $link = $parsed_args['link_before'] . str_replace( '%', $i, $parsed_args['pagelink'] ) . $parsed_args['link_after'];
 993  
 994                  if ( $i !== $page || ! $more && 1 === $page ) {
 995                      $link = _wp_link_page( $i ) . $link . '</a>';
 996                  } elseif ( $i === $page ) {
 997                      $link = '<span class="post-page-numbers current" aria-current="' . esc_attr( $parsed_args['aria_current'] ) . '">' . $link . '</span>';
 998                  }
 999  
1000                  /**
1001                   * Filters the HTML output of individual page number links.
1002                   *
1003                   * @since 3.6.0
1004                   *
1005                   * @param string $link The page number HTML output.
1006                   * @param int    $i    Page number for paginated posts' page links.
1007                   */
1008                  $link = apply_filters( 'wp_link_pages_link', $link, $i );
1009  
1010                  // Use the custom links separator beginning with the second link.
1011                  $output .= ( 1 === $i ) ? ' ' : $parsed_args['separator'];
1012                  $output .= $link;
1013              }
1014              $output .= $parsed_args['after'];
1015          } elseif ( $more ) {
1016              $output .= $parsed_args['before'];
1017              $prev    = $page - 1;
1018              if ( $prev > 0 ) {
1019                  $link = _wp_link_page( $prev ) . $parsed_args['link_before'] . $parsed_args['previouspagelink'] . $parsed_args['link_after'] . '</a>';
1020  
1021                  /** This filter is documented in wp-includes/post-template.php */
1022                  $output .= apply_filters( 'wp_link_pages_link', $link, $prev );
1023              }
1024              $next = $page + 1;
1025              if ( $next <= $numpages ) {
1026                  if ( $prev ) {
1027                      $output .= $parsed_args['separator'];
1028                  }
1029                  $link = _wp_link_page( $next ) . $parsed_args['link_before'] . $parsed_args['nextpagelink'] . $parsed_args['link_after'] . '</a>';
1030  
1031                  /** This filter is documented in wp-includes/post-template.php */
1032                  $output .= apply_filters( 'wp_link_pages_link', $link, $next );
1033              }
1034              $output .= $parsed_args['after'];
1035          }
1036      }
1037  
1038      /**
1039       * Filters the HTML output of page links for paginated posts.
1040       *
1041       * @since 3.6.0
1042       *
1043       * @param string       $output HTML output of paginated posts' page links.
1044       * @param array|string $args   An array or query string of arguments. See wp_link_pages()
1045       *                             for information on accepted arguments.
1046       */
1047      $html = apply_filters( 'wp_link_pages', $output, $args );
1048  
1049      if ( $parsed_args['echo'] ) {
1050          echo $html;
1051      }
1052      return $html;
1053  }
1054  
1055  /**
1056   * Helper function for wp_link_pages().
1057   *
1058   * @since 3.1.0
1059   * @access private
1060   *
1061   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
1062   *
1063   * @param int $i Page number.
1064   * @return string Link.
1065   */
1066  function _wp_link_page( $i ) {
1067      global $wp_rewrite;
1068      $post       = get_post();
1069      $query_args = array();
1070  
1071      if ( 1 === $i ) {
1072          $url = get_permalink();
1073      } else {
1074          if ( ! get_option( 'permalink_structure' ) || in_array( $post->post_status, array( 'draft', 'pending' ), true ) ) {
1075              $url = add_query_arg( 'page', $i, get_permalink() );
1076          } elseif ( 'page' === get_option( 'show_on_front' ) && (int) get_option( 'page_on_front' ) === $post->ID ) {
1077              $url = trailingslashit( get_permalink() ) . user_trailingslashit( "$wp_rewrite->pagination_base/" . $i, 'single_paged' );
1078          } else {
1079              $url = trailingslashit( get_permalink() ) . user_trailingslashit( $i, 'single_paged' );
1080          }
1081      }
1082  
1083      if ( is_preview() ) {
1084  
1085          if ( ( 'draft' !== $post->post_status ) && isset( $_GET['preview_id'], $_GET['preview_nonce'] ) ) {
1086              $query_args['preview_id']    = wp_unslash( $_GET['preview_id'] );
1087              $query_args['preview_nonce'] = wp_unslash( $_GET['preview_nonce'] );
1088          }
1089  
1090          $url = get_preview_post_link( $post, $query_args, $url );
1091      }
1092  
1093      return '<a href="' . esc_url( $url ) . '" class="post-page-numbers">';
1094  }
1095  
1096  //
1097  // Post-meta: Custom per-post fields.
1098  //
1099  
1100  /**
1101   * Retrieves post custom meta data field.
1102   *
1103   * @since 1.5.0
1104   *
1105   * @param string $key Meta data key name.
1106   * @return array|string|false Array of values, or single value if only one element exists.
1107   *                            False if the key does not exist.
1108   */
1109  function post_custom( $key = '' ) {
1110      $custom = get_post_custom();
1111  
1112      if ( ! isset( $custom[ $key ] ) ) {
1113          return false;
1114      } elseif ( 1 === count( $custom[ $key ] ) ) {
1115          return $custom[ $key ][0];
1116      } else {
1117          return $custom[ $key ];
1118      }
1119  }
1120  
1121  /**
1122   * Displays a list of post custom fields.
1123   *
1124   * @since 1.2.0
1125   *
1126   * @deprecated 6.0.2 Use get_post_meta() to retrieve post meta and render manually.
1127   */
1128  function the_meta() {
1129      _deprecated_function( __FUNCTION__, '6.0.2', 'get_post_meta()' );
1130      $keys = get_post_custom_keys();
1131      if ( $keys ) {
1132          $li_html = '';
1133          foreach ( (array) $keys as $key ) {
1134              $keyt = trim( $key );
1135              if ( is_protected_meta( $keyt, 'post' ) ) {
1136                  continue;
1137              }
1138  
1139              $values = array_map( 'trim', get_post_custom_values( $key ) );
1140              $value  = implode( ', ', $values );
1141  
1142              $html = sprintf(
1143                  "<li><span class='post-meta-key'>%s</span> %s</li>\n",
1144                  /* translators: %s: Post custom field name. */
1145                  esc_html( sprintf( _x( '%s:', 'Post custom field name' ), $key ) ),
1146                  esc_html( $value )
1147              );
1148  
1149              /**
1150               * Filters the HTML output of the li element in the post custom fields list.
1151               *
1152               * @since 2.2.0
1153               *
1154               * @param string $html  The HTML output for the li element.
1155               * @param string $key   Meta key.
1156               * @param string $value Meta value.
1157               */
1158              $li_html .= apply_filters( 'the_meta_key', $html, $key, $value );
1159          }
1160  
1161          if ( $li_html ) {
1162              echo "<ul class='post-meta'>\n{$li_html}</ul>\n";
1163          }
1164      }
1165  }
1166  
1167  //
1168  // Pages.
1169  //
1170  
1171  /**
1172   * Retrieves or displays a list of pages as a dropdown (select list).
1173   *
1174   * @since 2.1.0
1175   * @since 4.2.0 The `$value_field` argument was added.
1176   * @since 4.3.0 The `$class` argument was added.
1177   *
1178   * @see get_pages()
1179   *
1180   * @param array|string $args {
1181   *     Optional. Array or string of arguments to generate a page dropdown. See get_pages() for additional arguments.
1182   *
1183   *     @type int          $depth                 Maximum depth. Default 0.
1184   *     @type int          $child_of              Page ID to retrieve child pages of. Default 0.
1185   *     @type int|string   $selected              Value of the option that should be selected. Default 0.
1186   *     @type bool|int     $echo                  Whether to echo or return the generated markup. Accepts 0, 1,
1187   *                                               or their bool equivalents. Default 1.
1188   *     @type string       $name                  Value for the 'name' attribute of the select element.
1189   *                                               Default 'page_id'.
1190   *     @type string       $id                    Value for the 'id' attribute of the select element.
1191   *     @type string       $class                 Value for the 'class' attribute of the select element. Default: none.
1192   *                                               Defaults to the value of `$name`.
1193   *     @type string       $show_option_none      Text to display for showing no pages. Default empty (does not display).
1194   *     @type string       $show_option_no_change Text to display for "no change" option. Default empty (does not display).
1195   *     @type string       $option_none_value     Value to use when no page is selected. Default empty.
1196   *     @type string       $value_field           Post field used to populate the 'value' attribute of the option
1197   *                                               elements. Accepts any valid post field. Default 'ID'.
1198   * }
1199   * @return string HTML dropdown list of pages.
1200   */
1201  function wp_dropdown_pages( $args = '' ) {
1202      $defaults = array(
1203          'depth'                 => 0,
1204          'child_of'              => 0,
1205          'selected'              => 0,
1206          'echo'                  => 1,
1207          'name'                  => 'page_id',
1208          'id'                    => '',
1209          'class'                 => '',
1210          'show_option_none'      => '',
1211          'show_option_no_change' => '',
1212          'option_none_value'     => '',
1213          'value_field'           => 'ID',
1214      );
1215  
1216      $parsed_args = wp_parse_args( $args, $defaults );
1217  
1218      $pages  = get_pages( $parsed_args );
1219      $output = '';
1220      // Back-compat with old system where both id and name were based on $name argument.
1221      if ( empty( $parsed_args['id'] ) ) {
1222          $parsed_args['id'] = $parsed_args['name'];
1223      }
1224  
1225      if ( ! empty( $pages ) ) {
1226          $class = '';
1227          if ( ! empty( $parsed_args['class'] ) ) {
1228              $class = " class='" . esc_attr( $parsed_args['class'] ) . "'";
1229          }
1230  
1231          $output = "<select name='" . esc_attr( $parsed_args['name'] ) . "'" . $class . " id='" . esc_attr( $parsed_args['id'] ) . "'>\n";
1232          if ( $parsed_args['show_option_no_change'] ) {
1233              $output .= "\t<option value=\"-1\">" . $parsed_args['show_option_no_change'] . "</option>\n";
1234          }
1235          if ( $parsed_args['show_option_none'] ) {
1236              $output .= "\t<option value=\"" . esc_attr( $parsed_args['option_none_value'] ) . '">' . $parsed_args['show_option_none'] . "</option>\n";
1237          }
1238          $output .= walk_page_dropdown_tree( $pages, $parsed_args['depth'], $parsed_args );
1239          $output .= "</select>\n";
1240      }
1241  
1242      /**
1243       * Filters the HTML output of a list of pages as a dropdown.
1244       *
1245       * @since 2.1.0
1246       * @since 4.4.0 `$parsed_args` and `$pages` added as arguments.
1247       *
1248       * @param string    $output      HTML output for dropdown list of pages.
1249       * @param array     $parsed_args The parsed arguments array. See wp_dropdown_pages()
1250       *                               for information on accepted arguments.
1251       * @param WP_Post[] $pages       Array of the page objects.
1252       */
1253      $html = apply_filters( 'wp_dropdown_pages', $output, $parsed_args, $pages );
1254  
1255      if ( $parsed_args['echo'] ) {
1256          echo $html;
1257      }
1258  
1259      return $html;
1260  }
1261  
1262  /**
1263   * Retrieves or displays a list of pages (or hierarchical post type items) in list (li) format.
1264   *
1265   * @since 1.5.0
1266   * @since 4.7.0 Added the `item_spacing` argument.
1267   *
1268   * @see get_pages()
1269   *
1270   * @global WP_Query $wp_query WordPress Query object.
1271   *
1272   * @param array|string $args {
1273   *     Optional. Array or string of arguments to generate a list of pages. See get_pages() for additional arguments.
1274   *
1275   *     @type int          $child_of     Display only the sub-pages of a single page by ID. Default 0 (all pages).
1276   *     @type string       $authors      Comma-separated list of author IDs. Default empty (all authors).
1277   *     @type string       $date_format  PHP date format to use for the listed pages. Relies on the 'show_date' parameter.
1278   *                                      Default is the value of 'date_format' option.
1279   *     @type int          $depth        Number of levels in the hierarchy of pages to include in the generated list.
1280   *                                      Accepts -1 (any depth), 0 (all pages), 1 (top-level pages only), and n (pages to
1281   *                                      the given n depth). Default 0.
1282   *     @type bool         $echo         Whether or not to echo the list of pages. Default true.
1283   *     @type string       $exclude      Comma-separated list of page IDs to exclude. Default empty.
1284   *     @type array        $include      Comma-separated list of page IDs to include. Default empty.
1285   *     @type string       $link_after   Text or HTML to follow the page link label. Default null.
1286   *     @type string       $link_before  Text or HTML to precede the page link label. Default null.
1287   *     @type string       $post_type    Post type to query for. Default 'page'.
1288   *     @type string|array $post_status  Comma-separated list or array of post statuses to include. Default 'publish'.
1289   *     @type string       $show_date    Whether to display the page publish or modified date for each page. Accepts
1290   *                                      'modified' or any other value. An empty value hides the date. Default empty.
1291   *     @type string       $sort_column  Comma-separated list of column names to sort the pages by. Accepts 'post_author',
1292   *                                      'post_date', 'post_title', 'post_name', 'post_modified', 'post_modified_gmt',
1293   *                                      'menu_order', 'post_parent', 'ID', 'rand', or 'comment_count'. Default 'post_title'.
1294   *     @type string       $title_li     List heading. Passing a null or empty value will result in no heading, and the list
1295   *                                      will not be wrapped with unordered list `<ul>` tags. Default 'Pages'.
1296   *     @type string       $item_spacing Whether to preserve whitespace within the menu's HTML. Accepts 'preserve' or 'discard'.
1297   *                                      Default 'preserve'.
1298   *     @type Walker       $walker       Walker instance to use for listing pages. Default empty which results in a
1299   *                                      Walker_Page instance being used.
1300   * }
1301   * @return void|string Void if 'echo' argument is true, HTML list of pages if 'echo' is false.
1302   */
1303  function wp_list_pages( $args = '' ) {
1304      $defaults = array(
1305          'depth'        => 0,
1306          'show_date'    => '',
1307          'date_format'  => get_option( 'date_format' ),
1308          'child_of'     => 0,
1309          'exclude'      => '',
1310          'title_li'     => __( 'Pages' ),
1311          'echo'         => 1,
1312          'authors'      => '',
1313          'sort_column'  => 'menu_order, post_title',
1314          'link_before'  => '',
1315          'link_after'   => '',
1316          'item_spacing' => 'preserve',
1317          'walker'       => '',
1318      );
1319  
1320      $parsed_args = wp_parse_args( $args, $defaults );
1321  
1322      if ( ! in_array( $parsed_args['item_spacing'], array( 'preserve', 'discard' ), true ) ) {
1323          // Invalid value, fall back to default.
1324          $parsed_args['item_spacing'] = $defaults['item_spacing'];
1325      }
1326  
1327      $output       = '';
1328      $current_page = 0;
1329  
1330      // Sanitize, mostly to keep spaces out.
1331      $parsed_args['exclude'] = preg_replace( '/[^0-9,]/', '', $parsed_args['exclude'] );
1332  
1333      // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array).
1334      $exclude_array = ( $parsed_args['exclude'] ) ? explode( ',', $parsed_args['exclude'] ) : array();
1335  
1336      /**
1337       * Filters the array of pages to exclude from the pages list.
1338       *
1339       * @since 2.1.0
1340       *
1341       * @param string[] $exclude_array An array of page IDs to exclude.
1342       */
1343      $parsed_args['exclude'] = implode( ',', apply_filters( 'wp_list_pages_excludes', $exclude_array ) );
1344  
1345      $parsed_args['hierarchical'] = 0;
1346  
1347      // Query pages.
1348      $pages = get_pages( $parsed_args );
1349  
1350      if ( ! empty( $pages ) ) {
1351          if ( $parsed_args['title_li'] ) {
1352              $output .= '<li class="pagenav">' . $parsed_args['title_li'] . '<ul>';
1353          }
1354          global $wp_query;
1355          if ( is_page() || is_attachment() || $wp_query->is_posts_page ) {
1356              $current_page = get_queried_object_id();
1357          } elseif ( is_singular() ) {
1358              $queried_object = get_queried_object();
1359              if ( is_post_type_hierarchical( $queried_object->post_type ) ) {
1360                  $current_page = $queried_object->ID;
1361              }
1362          }
1363  
1364          $output .= walk_page_tree( $pages, $parsed_args['depth'], $current_page, $parsed_args );
1365  
1366          if ( $parsed_args['title_li'] ) {
1367              $output .= '</ul></li>';
1368          }
1369      }
1370  
1371      /**
1372       * Filters the HTML output of the pages to list.
1373       *
1374       * @since 1.5.1
1375       * @since 4.4.0 `$pages` added as arguments.
1376       *
1377       * @see wp_list_pages()
1378       *
1379       * @param string    $output      HTML output of the pages list.
1380       * @param array     $parsed_args An array of page-listing arguments. See wp_list_pages()
1381       *                               for information on accepted arguments.
1382       * @param WP_Post[] $pages       Array of the page objects.
1383       */
1384      $html = apply_filters( 'wp_list_pages', $output, $parsed_args, $pages );
1385  
1386      if ( $parsed_args['echo'] ) {
1387          echo $html;
1388      } else {
1389          return $html;
1390      }
1391  }
1392  
1393  /**
1394   * Displays or retrieves a list of pages with an optional home link.
1395   *
1396   * The arguments are listed below and part of the arguments are for wp_list_pages() function.
1397   * Check that function for more info on those arguments.
1398   *
1399   * @since 2.7.0
1400   * @since 4.4.0 Added `menu_id`, `container`, `before`, `after`, and `walker` arguments.
1401   * @since 4.7.0 Added the `item_spacing` argument.
1402   *
1403   * @param array|string $args {
1404   *     Optional. Array or string of arguments to generate a page menu. See wp_list_pages() for additional arguments.
1405   *
1406   *     @type string          $sort_column  How to sort the list of pages. Accepts post column names.
1407   *                                         Default 'menu_order, post_title'.
1408   *     @type string          $menu_id      ID for the div containing the page list. Default is empty string.
1409   *     @type string          $menu_class   Class to use for the element containing the page list. Default 'menu'.
1410   *     @type string          $container    Element to use for the element containing the page list. Default 'div'.
1411   *     @type bool            $echo         Whether to echo the list or return it. Accepts true (echo) or false (return).
1412   *                                         Default true.
1413   *     @type int|bool|string $show_home    Whether to display the link to the home page. Can just enter the text
1414   *                                         you'd like shown for the home link. 1|true defaults to 'Home'.
1415   *     @type string          $link_before  The HTML or text to prepend to $show_home text. Default empty.
1416   *     @type string          $link_after   The HTML or text to append to $show_home text. Default empty.
1417   *     @type string          $before       The HTML or text to prepend to the menu. Default is '<ul>'.
1418   *     @type string          $after        The HTML or text to append to the menu. Default is '</ul>'.
1419   *     @type string          $item_spacing Whether to preserve whitespace within the menu's HTML. Accepts 'preserve'
1420   *                                         or 'discard'. Default 'discard'.
1421   *     @type Walker          $walker       Walker instance to use for listing pages. Default empty which results in a
1422   *                                         Walker_Page instance being used.
1423   * }
1424   * @return void|string Void if 'echo' argument is true, HTML menu if 'echo' is false.
1425   */
1426  function wp_page_menu( $args = array() ) {
1427      $defaults = array(
1428          'sort_column'  => 'menu_order, post_title',
1429          'menu_id'      => '',
1430          'menu_class'   => 'menu',
1431          'container'    => 'div',
1432          'echo'         => true,
1433          'link_before'  => '',
1434          'link_after'   => '',
1435          'before'       => '<ul>',
1436          'after'        => '</ul>',
1437          'item_spacing' => 'discard',
1438          'walker'       => '',
1439      );
1440      $args     = wp_parse_args( $args, $defaults );
1441  
1442      if ( ! in_array( $args['item_spacing'], array( 'preserve', 'discard' ), true ) ) {
1443          // Invalid value, fall back to default.
1444          $args['item_spacing'] = $defaults['item_spacing'];
1445      }
1446  
1447      if ( 'preserve' === $args['item_spacing'] ) {
1448          $t = "\t";
1449          $n = "\n";
1450      } else {
1451          $t = '';
1452          $n = '';
1453      }
1454  
1455      /**
1456       * Filters the arguments used to generate a page-based menu.
1457       *
1458       * @since 2.7.0
1459       *
1460       * @see wp_page_menu()
1461       *
1462       * @param array $args An array of page menu arguments. See wp_page_menu()
1463       *                    for information on accepted arguments.
1464       */
1465      $args = apply_filters( 'wp_page_menu_args', $args );
1466  
1467      $menu = '';
1468  
1469      $list_args = $args;
1470  
1471      // Show Home in the menu.
1472      if ( ! empty( $args['show_home'] ) ) {
1473          if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] ) {
1474              $text = __( 'Home' );
1475          } else {
1476              $text = $args['show_home'];
1477          }
1478          $class = '';
1479          if ( is_front_page() && ! is_paged() ) {
1480              $class = 'class="current_page_item"';
1481          }
1482          $menu .= '<li ' . $class . '><a href="' . esc_url( home_url( '/' ) ) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>';
1483          // If the front page is a page, add it to the exclude list.
1484          if ( 'page' === get_option( 'show_on_front' ) ) {
1485              if ( ! empty( $list_args['exclude'] ) ) {
1486                  $list_args['exclude'] .= ',';
1487              } else {
1488                  $list_args['exclude'] = '';
1489              }
1490              $list_args['exclude'] .= get_option( 'page_on_front' );
1491          }
1492      }
1493  
1494      $list_args['echo']     = false;
1495      $list_args['title_li'] = '';
1496      $menu                 .= wp_list_pages( $list_args );
1497  
1498      $container = sanitize_text_field( $args['container'] );
1499  
1500      // Fallback in case `wp_nav_menu()` was called without a container.
1501      if ( empty( $container ) ) {
1502          $container = 'div';
1503      }
1504  
1505      if ( $menu ) {
1506  
1507          // wp_nav_menu() doesn't set before and after.
1508          if ( isset( $args['fallback_cb'] ) &&
1509              'wp_page_menu' === $args['fallback_cb'] &&
1510              'ul' !== $container ) {
1511              $args['before'] = "<ul>{$n}";
1512              $args['after']  = '</ul>';
1513          }
1514  
1515          $menu = $args['before'] . $menu . $args['after'];
1516      }
1517  
1518      $attrs = '';
1519      if ( ! empty( $args['menu_id'] ) ) {
1520          $attrs .= ' id="' . esc_attr( $args['menu_id'] ) . '"';
1521      }
1522  
1523      if ( ! empty( $args['menu_class'] ) ) {
1524          $attrs .= ' class="' . esc_attr( $args['menu_class'] ) . '"';
1525      }
1526  
1527      $menu = "<{$container}{$attrs}>" . $menu . "</{$container}>{$n}";
1528  
1529      /**
1530       * Filters the HTML output of a page-based menu.
1531       *
1532       * @since 2.7.0
1533       *
1534       * @see wp_page_menu()
1535       *
1536       * @param string $menu The HTML output.
1537       * @param array  $args An array of arguments. See wp_page_menu()
1538       *                     for information on accepted arguments.
1539       */
1540      $menu = apply_filters( 'wp_page_menu', $menu, $args );
1541  
1542      if ( $args['echo'] ) {
1543          echo $menu;
1544      } else {
1545          return $menu;
1546      }
1547  }
1548  
1549  //
1550  // Page helpers.
1551  //
1552  
1553  /**
1554   * Retrieves HTML list content for page list.
1555   *
1556   * @uses Walker_Page to create HTML list content.
1557   * @since 2.1.0
1558   *
1559   * @param array $pages
1560   * @param int   $depth
1561   * @param int   $current_page
1562   * @param array $args
1563   * @return string
1564   */
1565  function walk_page_tree( $pages, $depth, $current_page, $args ) {
1566      if ( empty( $args['walker'] ) ) {
1567          $walker = new Walker_Page();
1568      } else {
1569          /**
1570           * @var Walker $walker
1571           */
1572          $walker = $args['walker'];
1573      }
1574  
1575      foreach ( (array) $pages as $page ) {
1576          if ( $page->post_parent ) {
1577              $args['pages_with_children'][ $page->post_parent ] = true;
1578          }
1579      }
1580  
1581      return $walker->walk( $pages, $depth, $args, $current_page );
1582  }
1583  
1584  /**
1585   * Retrieves HTML dropdown (select) content for page list.
1586   *
1587   * @since 2.1.0
1588   * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
1589   *              to the function signature.
1590   *
1591   * @uses Walker_PageDropdown to create HTML dropdown content.
1592   * @see Walker_PageDropdown::walk() for parameters and return description.
1593   *
1594   * @param mixed ...$args Elements array, maximum hierarchical depth and optional additional arguments.
1595   * @return string
1596   */
1597  function walk_page_dropdown_tree( ...$args ) {
1598      if ( empty( $args[2]['walker'] ) ) { // The user's options are the third parameter.
1599          $walker = new Walker_PageDropdown();
1600      } else {
1601          /**
1602           * @var Walker $walker
1603           */
1604          $walker = $args[2]['walker'];
1605      }
1606  
1607      return $walker->walk( ...$args );
1608  }
1609  
1610  //
1611  // Attachments.
1612  //
1613  
1614  /**
1615   * Displays an attachment page link using an image or icon.
1616   *
1617   * @since 2.0.0
1618   *
1619   * @param int|WP_Post $post       Optional. Post ID or post object.
1620   * @param bool        $fullsize   Optional. Whether to use full size. Default false.
1621   * @param bool        $deprecated Deprecated. Not used.
1622   * @param bool        $permalink Optional. Whether to include permalink. Default false.
1623   */
1624  function the_attachment_link( $post = 0, $fullsize = false, $deprecated = false, $permalink = false ) {
1625      if ( ! empty( $deprecated ) ) {
1626          _deprecated_argument( __FUNCTION__, '2.5.0' );
1627      }
1628  
1629      if ( $fullsize ) {
1630          echo wp_get_attachment_link( $post, 'full', $permalink );
1631      } else {
1632          echo wp_get_attachment_link( $post, 'thumbnail', $permalink );
1633      }
1634  }
1635  
1636  /**
1637   * Retrieves an attachment page link using an image or icon, if possible.
1638   *
1639   * @since 2.5.0
1640   * @since 4.4.0 The `$post` parameter can now accept either a post ID or `WP_Post` object.
1641   *
1642   * @param int|WP_Post  $post      Optional. Post ID or post object.
1643   * @param string|int[] $size      Optional. Image size. Accepts any registered image size name, or an array
1644   *                                of width and height values in pixels (in that order). Default 'thumbnail'.
1645   * @param bool         $permalink Optional. Whether to add permalink to image. Default false.
1646   * @param bool         $icon      Optional. Whether the attachment is an icon. Default false.
1647   * @param string|false $text      Optional. Link text to use. Activated by passing a string, false otherwise.
1648   *                                Default false.
1649   * @param array|string $attr      Optional. Array or string of attributes. Default empty.
1650   * @return string HTML content.
1651   */
1652  function wp_get_attachment_link( $post = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) {
1653      $_post = get_post( $post );
1654  
1655      if ( empty( $_post ) || ( 'attachment' !== $_post->post_type ) || ! wp_get_attachment_url( $_post->ID ) ) {
1656          return __( 'Missing Attachment' );
1657      }
1658  
1659      $url = wp_get_attachment_url( $_post->ID );
1660  
1661      if ( $permalink ) {
1662          $url = get_attachment_link( $_post->ID );
1663      }
1664  
1665      if ( $text ) {
1666          $link_text = $text;
1667      } elseif ( $size && 'none' !== $size ) {
1668          $link_text = wp_get_attachment_image( $_post->ID, $size, $icon, $attr );
1669      } else {
1670          $link_text = '';
1671      }
1672  
1673      if ( '' === trim( $link_text ) ) {
1674          $link_text = $_post->post_title;
1675      }
1676  
1677      if ( '' === trim( $link_text ) ) {
1678          $link_text = esc_html( pathinfo( get_attached_file( $_post->ID ), PATHINFO_FILENAME ) );
1679      }
1680  
1681      /**
1682       * Filters the list of attachment link attributes.
1683       *
1684       * @since 6.2.0
1685       *
1686       * @param array $attributes An array of attributes for the link markup,
1687       *                          keyed on the attribute name.
1688       * @param int   $id         Post ID.
1689       */
1690      $attributes = apply_filters( 'wp_get_attachment_link_attributes', array( 'href' => $url ), $_post->ID );
1691  
1692      $link_attributes = '';
1693      foreach ( $attributes as $name => $value ) {
1694          $value            = 'href' === $name ? esc_url( $value ) : esc_attr( $value );
1695          $link_attributes .= ' ' . esc_attr( $name ) . "='" . $value . "'";
1696      }
1697  
1698      $link_html = "<a$link_attributes>$link_text</a>";
1699  
1700      /**
1701       * Filters a retrieved attachment page link.
1702       *
1703       * @since 2.7.0
1704       * @since 5.1.0 Added the `$attr` parameter.
1705       *
1706       * @param string       $link_html The page link HTML output.
1707       * @param int|WP_Post  $post      Post ID or object. Can be 0 for the current global post.
1708       * @param string|int[] $size      Requested image size. Can be any registered image size name, or
1709       *                                an array of width and height values in pixels (in that order).
1710       * @param bool         $permalink Whether to add permalink to image. Default false.
1711       * @param bool         $icon      Whether to include an icon.
1712       * @param string|false $text      If string, will be link text.
1713       * @param array|string $attr      Array or string of attributes.
1714       */
1715      return apply_filters( 'wp_get_attachment_link', $link_html, $post, $size, $permalink, $icon, $text, $attr );
1716  }
1717  
1718  /**
1719   * Wraps attachment in paragraph tag before content.
1720   *
1721   * @since 2.0.0
1722   *
1723   * @param string $content
1724   * @return string
1725   */
1726  function prepend_attachment( $content ) {
1727      $post = get_post();
1728  
1729      if ( empty( $post->post_type ) || 'attachment' !== $post->post_type ) {
1730          return $content;
1731      }
1732  
1733      if ( wp_attachment_is( 'video', $post ) ) {
1734          $meta = wp_get_attachment_metadata( get_the_ID() );
1735          $atts = array( 'src' => wp_get_attachment_url() );
1736          if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) {
1737              $atts['width']  = (int) $meta['width'];
1738              $atts['height'] = (int) $meta['height'];
1739          }
1740          if ( has_post_thumbnail() ) {
1741              $atts['poster'] = wp_get_attachment_url( get_post_thumbnail_id() );
1742          }
1743          $p = wp_video_shortcode( $atts );
1744      } elseif ( wp_attachment_is( 'audio', $post ) ) {
1745          $p = wp_audio_shortcode( array( 'src' => wp_get_attachment_url() ) );
1746      } else {
1747          $p = '<p class="attachment">';
1748          // Show the medium sized image representation of the attachment if available, and link to the raw file.
1749          $p .= wp_get_attachment_link( 0, 'medium', false );
1750          $p .= '</p>';
1751      }
1752  
1753      /**
1754       * Filters the attachment markup to be prepended to the post content.
1755       *
1756       * @since 2.0.0
1757       *
1758       * @see prepend_attachment()
1759       *
1760       * @param string $p The attachment HTML output.
1761       */
1762      $p = apply_filters( 'prepend_attachment', $p );
1763  
1764      return "$p\n$content";
1765  }
1766  
1767  //
1768  // Misc.
1769  //
1770  
1771  /**
1772   * Retrieves protected post password form content.
1773   *
1774   * @since 1.0.0
1775   *
1776   * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
1777   * @return string HTML content for password form for password-protected post.
1778   */
1779  function get_the_password_form( $post = 0 ) {
1780      $post                  = get_post( $post );
1781      $field_id              = 'pwbox-' . ( empty( $post->ID ) ? wp_rand() : $post->ID );
1782      $invalid_password      = '';
1783      $invalid_password_html = '';
1784      $aria                  = '';
1785      $class                 = '';
1786      $redirect_field        = '';
1787  
1788      // If the referrer is the same as the current request, the user has entered an invalid password.
1789      if ( ! empty( $post->ID ) && wp_get_raw_referer() === get_permalink( $post->ID ) && isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) {
1790          /**
1791           * Filters the invalid password message shown on password-protected posts.
1792           * The filter is only applied if the post is password-protected.
1793           *
1794           * @since 6.8.0
1795           *
1796           * @param string  $text The message shown to users when entering an invalid password.
1797           * @param WP_Post $post Post object.
1798           */
1799          $invalid_password      = apply_filters( 'the_password_form_incorrect_password', __( 'Invalid password.' ), $post );
1800          $invalid_password_html = '<div class="post-password-form-invalid-password" role="alert"><p id="error-' . $field_id . '">' . $invalid_password . '</p></div>';
1801          $aria                  = ' aria-describedby="error-' . $field_id . '"';
1802          $class                 = ' password-form-error';
1803      }
1804  
1805      if ( ! empty( $post->ID ) ) {
1806          $redirect_field = sprintf(
1807              '<input type="hidden" name="redirect_to" value="%s" />',
1808              esc_attr( get_permalink( $post->ID ) )
1809          );
1810      }
1811  
1812      $output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form' . $class . '" method="post">' . $redirect_field . $invalid_password_html . '
1813      <p>' . __( 'This content is password-protected. To view it, please enter the password below.' ) . '</p>
1814      <p><label for="' . $field_id . '">' . __( 'Password:' ) . ' <input name="post_password" id="' . $field_id . '" type="password" spellcheck="false" required size="20"' . $aria . ' /></label> <input type="submit" name="Submit" value="' . esc_attr_x( 'Enter', 'post password form' ) . '" /></p></form>
1815      ';
1816  
1817      /**
1818       * Filters the HTML output for the protected post password form.
1819       *
1820       * If modifying the password field, please note that the WordPress database schema
1821       * limits the password field to 255 characters regardless of the value of the
1822       * `minlength` or `maxlength` attributes or other validation that may be added to
1823       * the input.
1824       *
1825       * @since 2.7.0
1826       * @since 5.8.0 Added the `$post` parameter.
1827       * @since 6.8.0 Added the `$invalid_password` parameter.
1828       *
1829       * @param string  $output           The password form HTML output.
1830       * @param WP_Post $post             Post object.
1831       * @param string  $invalid_password The invalid password message.
1832       */
1833      return apply_filters( 'the_password_form', $output, $post, $invalid_password );
1834  }
1835  
1836  /**
1837   * Determines whether the current post uses a page template.
1838   *
1839   * This template tag allows you to determine if you are in a page template.
1840   * You can optionally provide a template filename or array of template filenames
1841   * and then the check will be specific to that template.
1842   *
1843   * For more information on this and similar theme functions, check out
1844   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
1845   * Conditional Tags} article in the Theme Developer Handbook.
1846   *
1847   * @since 2.5.0
1848   * @since 4.2.0 The `$template` parameter was changed to also accept an array of page templates.
1849   * @since 4.7.0 Now works with any post type, not just pages.
1850   *
1851   * @param string|string[] $template The specific template filename or array of templates to match.
1852   * @return bool True on success, false on failure.
1853   */
1854  function is_page_template( $template = '' ) {
1855      if ( ! is_singular() ) {
1856          return false;
1857      }
1858  
1859      $page_template = get_page_template_slug( get_queried_object_id() );
1860  
1861      if ( empty( $template ) ) {
1862          return (bool) $page_template;
1863      }
1864  
1865      if ( $template === $page_template ) {
1866          return true;
1867      }
1868  
1869      if ( is_array( $template ) ) {
1870          if ( ( in_array( 'default', $template, true ) && ! $page_template )
1871              || in_array( $page_template, $template, true )
1872          ) {
1873              return true;
1874          }
1875      }
1876  
1877      return ( 'default' === $template && ! $page_template );
1878  }
1879  
1880  /**
1881   * Gets the specific template filename for a given post.
1882   *
1883   * @since 3.4.0
1884   * @since 4.7.0 Now works with any post type, not just pages.
1885   *
1886   * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
1887   * @return string|false Page template filename. Returns an empty string when the default page template
1888   *                      is in use. Returns false if the post does not exist.
1889   */
1890  function get_page_template_slug( $post = null ) {
1891      $post = get_post( $post );
1892  
1893      if ( ! $post ) {
1894          return false;
1895      }
1896  
1897      $template = get_post_meta( $post->ID, '_wp_page_template', true );
1898  
1899      if ( ! $template || 'default' === $template ) {
1900          return '';
1901      }
1902  
1903      return $template;
1904  }
1905  
1906  /**
1907   * Retrieves formatted date timestamp of a revision (linked to that revisions's page).
1908   *
1909   * @since 2.6.0
1910   *
1911   * @param int|WP_Post $revision Revision ID or revision object.
1912   * @param bool        $link     Optional. Whether to link to revision's page. Default true.
1913   * @return string|false i18n formatted datetimestamp or localized 'Current Revision'.
1914   */
1915  function wp_post_revision_title( $revision, $link = true ) {
1916      $revision = get_post( $revision );
1917  
1918      if ( ! $revision ) {
1919          return $revision;
1920      }
1921  
1922      if ( ! in_array( $revision->post_type, array( 'post', 'page', 'revision' ), true ) ) {
1923          return false;
1924      }
1925  
1926      /* translators: Revision date format, see https://www.php.net/manual/datetime.format.php */
1927      $datef = _x( 'F j, Y @ H:i:s', 'revision date format' );
1928      /* translators: %s: Revision date. */
1929      $autosavef = __( '%s [Autosave]' );
1930      /* translators: %s: Revision date. */
1931      $currentf = __( '%s [Current Revision]' );
1932  
1933      $date      = date_i18n( $datef, strtotime( $revision->post_modified ) );
1934      $edit_link = get_edit_post_link( $revision->ID );
1935      if ( $link && current_user_can( 'edit_post', $revision->ID ) && $edit_link ) {
1936          $date = "<a href='$edit_link'>$date</a>";
1937      }
1938  
1939      if ( ! wp_is_post_revision( $revision ) ) {
1940          $date = sprintf( $currentf, $date );
1941      } elseif ( wp_is_post_autosave( $revision ) ) {
1942          $date = sprintf( $autosavef, $date );
1943      }
1944  
1945      return $date;
1946  }
1947  
1948  /**
1949   * Retrieves formatted date timestamp of a revision (linked to that revisions's page).
1950   *
1951   * @since 3.6.0
1952   *
1953   * @param int|WP_Post $revision Revision ID or revision object.
1954   * @param bool        $link     Optional. Whether to link to revision's page. Default true.
1955   * @return string|false gravatar, user, i18n formatted datetimestamp or localized 'Current Revision'.
1956   */
1957  function wp_post_revision_title_expanded( $revision, $link = true ) {
1958      $revision = get_post( $revision );
1959  
1960      if ( ! $revision ) {
1961          return $revision;
1962      }
1963  
1964      if ( ! in_array( $revision->post_type, array( 'post', 'page', 'revision' ), true ) ) {
1965          return false;
1966      }
1967  
1968      $author = get_the_author_meta( 'display_name', $revision->post_author );
1969      /* translators: Revision date format, see https://www.php.net/manual/datetime.format.php */
1970      $datef = _x( 'F j, Y @ H:i:s', 'revision date format' );
1971  
1972      $gravatar = get_avatar( $revision->post_author, 24 );
1973  
1974      $date      = date_i18n( $datef, strtotime( $revision->post_modified ) );
1975      $edit_link = get_edit_post_link( $revision->ID );
1976      if ( $link && current_user_can( 'edit_post', $revision->ID ) && $edit_link ) {
1977          $date = "<a href='$edit_link'>$date</a>";
1978      }
1979  
1980      $revision_date_author = sprintf(
1981          /* translators: Post revision title. 1: Author avatar, 2: Author name, 3: Time ago, 4: Date. */
1982          __( '%1$s %2$s, %3$s ago (%4$s)' ),
1983          $gravatar,
1984          $author,
1985          human_time_diff( strtotime( $revision->post_modified_gmt ) ),
1986          $date
1987      );
1988  
1989      /* translators: %s: Revision date with author avatar. */
1990      $autosavef = __( '%s [Autosave]' );
1991      /* translators: %s: Revision date with author avatar. */
1992      $currentf = __( '%s [Current Revision]' );
1993  
1994      if ( ! wp_is_post_revision( $revision ) ) {
1995          $revision_date_author = sprintf( $currentf, $revision_date_author );
1996      } elseif ( wp_is_post_autosave( $revision ) ) {
1997          $revision_date_author = sprintf( $autosavef, $revision_date_author );
1998      }
1999  
2000      /**
2001       * Filters the formatted author and date for a revision.
2002       *
2003       * @since 4.4.0
2004       *
2005       * @param string  $revision_date_author The formatted string.
2006       * @param WP_Post $revision             The revision object.
2007       * @param bool    $link                 Whether to link to the revisions page, as passed into
2008       *                                      wp_post_revision_title_expanded().
2009       */
2010      return apply_filters( 'wp_post_revision_title_expanded', $revision_date_author, $revision, $link );
2011  }
2012  
2013  /**
2014   * Displays a list of a post's revisions.
2015   *
2016   * Can output either a UL with edit links or a TABLE with diff interface, and
2017   * restore action links.
2018   *
2019   * @since 2.6.0
2020   *
2021   * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
2022   * @param string      $type 'all' (default), 'revision' or 'autosave'
2023   */
2024  function wp_list_post_revisions( $post = 0, $type = 'all' ) {
2025      $post = get_post( $post );
2026  
2027      if ( ! $post ) {
2028          return;
2029      }
2030  
2031      // $args array with (parent, format, right, left, type) deprecated since 3.6.
2032      if ( is_array( $type ) ) {
2033          $type = ! empty( $type['type'] ) ? $type['type'] : $type;
2034          _deprecated_argument( __FUNCTION__, '3.6.0' );
2035      }
2036  
2037      $revisions = wp_get_post_revisions( $post->ID );
2038  
2039      if ( ! $revisions ) {
2040          return;
2041      }
2042  
2043      $rows = '';
2044      foreach ( $revisions as $revision ) {
2045          if ( ! current_user_can( 'read_post', $revision->ID ) ) {
2046              continue;
2047          }
2048  
2049          $is_autosave = wp_is_post_autosave( $revision );
2050          if ( ( 'revision' === $type && $is_autosave ) || ( 'autosave' === $type && ! $is_autosave ) ) {
2051              continue;
2052          }
2053  
2054          $rows .= "\t<li>" . wp_post_revision_title_expanded( $revision ) . "</li>\n";
2055      }
2056  
2057      echo "<div class='hide-if-js'><p>" . __( 'JavaScript must be enabled to use this feature.' ) . "</p></div>\n";
2058  
2059      echo "<ul class='post-revisions hide-if-no-js'>\n";
2060      echo $rows;
2061      echo '</ul>';
2062  }
2063  
2064  /**
2065   * Retrieves the parent post object for the given post.
2066   *
2067   * @since 5.7.0
2068   *
2069   * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global $post.
2070   * @return WP_Post|null Parent post object, or null if there isn't one.
2071   */
2072  function get_post_parent( $post = null ) {
2073      $wp_post = get_post( $post );
2074      return ! empty( $wp_post->post_parent ) ? get_post( $wp_post->post_parent ) : null;
2075  }
2076  
2077  /**
2078   * Returns whether the given post has a parent post.
2079   *
2080   * @since 5.7.0
2081   *
2082   * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global $post.
2083   * @return bool Whether the post has a parent post.
2084   */
2085  function has_post_parent( $post = null ) {
2086      return (bool) get_post_parent( $post );
2087  }


Generated : Mon May 4 08:20:14 2026 Cross-referenced by PHPXref