[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 = isset( $post->post_title ) ? $post->post_title : ''; 122 $post_id = isset( $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 = isset( $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 = isset( $post->guid ) ? $post->guid : ''; 225 $post_id = isset( $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( ']]>', ']]>', $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…)' ) 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 return array_unique( $classes ); 610 } 611 612 /** 613 * Displays the class names for the body element. 614 * 615 * @since 2.8.0 616 * 617 * @param string|string[] $css_class Optional. Space-separated string or array of class names 618 * to add to the class list. Default empty. 619 */ 620 function body_class( $css_class = '' ) { 621 // Separates class names with a single space, collates class names for body element. 622 echo 'class="' . esc_attr( implode( ' ', get_body_class( $css_class ) ) ) . '"'; 623 } 624 625 /** 626 * Retrieves an array of the class names for the body element. 627 * 628 * @since 2.8.0 629 * 630 * @global WP_Query $wp_query WordPress Query object. 631 * 632 * @param string|string[] $css_class Optional. Space-separated string or array of class names 633 * to add to the class list. Default empty. 634 * @return string[] Array of class names. 635 */ 636 function get_body_class( $css_class = '' ) { 637 global $wp_query; 638 639 $classes = array(); 640 641 if ( is_rtl() ) { 642 $classes[] = 'rtl'; 643 } 644 645 if ( is_front_page() ) { 646 $classes[] = 'home'; 647 } 648 if ( is_home() ) { 649 $classes[] = 'blog'; 650 } 651 if ( is_privacy_policy() ) { 652 $classes[] = 'privacy-policy'; 653 } 654 if ( is_archive() ) { 655 $classes[] = 'archive'; 656 } 657 if ( is_date() ) { 658 $classes[] = 'date'; 659 } 660 if ( is_search() ) { 661 $classes[] = 'search'; 662 $classes[] = $wp_query->posts ? 'search-results' : 'search-no-results'; 663 } 664 if ( is_paged() ) { 665 $classes[] = 'paged'; 666 } 667 if ( is_attachment() ) { 668 $classes[] = 'attachment'; 669 } 670 if ( is_404() ) { 671 $classes[] = 'error404'; 672 } 673 674 if ( is_singular() ) { 675 $post = $wp_query->get_queried_object(); 676 $post_id = $post->ID; 677 $post_type = $post->post_type; 678 679 $classes[] = 'wp-singular'; 680 681 if ( is_page_template() ) { 682 $classes[] = "{$post_type}-template"; 683 684 $template_slug = get_page_template_slug( $post_id ); 685 $template_parts = explode( '/', $template_slug ); 686 687 foreach ( $template_parts as $part ) { 688 $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) ); 689 } 690 $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( '.', '-', $template_slug ) ); 691 } else { 692 $classes[] = "{$post_type}-template-default"; 693 } 694 695 if ( is_single() ) { 696 $classes[] = 'single'; 697 if ( isset( $post->post_type ) ) { 698 $classes[] = 'single-' . sanitize_html_class( $post->post_type, $post_id ); 699 $classes[] = 'postid-' . $post_id; 700 701 // Post Format. 702 if ( post_type_supports( $post->post_type, 'post-formats' ) ) { 703 $post_format = get_post_format( $post->ID ); 704 705 if ( $post_format && ! is_wp_error( $post_format ) ) { 706 $classes[] = 'single-format-' . sanitize_html_class( $post_format ); 707 } else { 708 $classes[] = 'single-format-standard'; 709 } 710 } 711 } 712 } 713 714 if ( is_attachment() ) { 715 $mime_type = get_post_mime_type( $post_id ); 716 $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' ); 717 $classes[] = 'attachmentid-' . $post_id; 718 $classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type ); 719 } elseif ( is_page() ) { 720 $classes[] = 'page'; 721 $classes[] = 'page-id-' . $post_id; 722 723 if ( get_pages( 724 array( 725 'parent' => $post_id, 726 'number' => 1, 727 ) 728 ) ) { 729 $classes[] = 'page-parent'; 730 } 731 732 if ( $post->post_parent ) { 733 $classes[] = 'page-child'; 734 $classes[] = 'parent-pageid-' . $post->post_parent; 735 } 736 } 737 } elseif ( is_archive() ) { 738 if ( is_post_type_archive() ) { 739 $classes[] = 'post-type-archive'; 740 $post_type = get_query_var( 'post_type' ); 741 if ( is_array( $post_type ) ) { 742 $post_type = reset( $post_type ); 743 } 744 $classes[] = 'post-type-archive-' . sanitize_html_class( $post_type ); 745 } elseif ( is_author() ) { 746 $author = $wp_query->get_queried_object(); 747 $classes[] = 'author'; 748 if ( isset( $author->user_nicename ) ) { 749 $classes[] = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID ); 750 $classes[] = 'author-' . $author->ID; 751 } 752 } elseif ( is_category() ) { 753 $cat = $wp_query->get_queried_object(); 754 $classes[] = 'category'; 755 if ( isset( $cat->term_id ) ) { 756 $cat_class = sanitize_html_class( $cat->slug, $cat->term_id ); 757 if ( is_numeric( $cat_class ) || ! trim( $cat_class, '-' ) ) { 758 $cat_class = $cat->term_id; 759 } 760 761 $classes[] = 'category-' . $cat_class; 762 $classes[] = 'category-' . $cat->term_id; 763 } 764 } elseif ( is_tag() ) { 765 $tag = $wp_query->get_queried_object(); 766 $classes[] = 'tag'; 767 if ( isset( $tag->term_id ) ) { 768 $tag_class = sanitize_html_class( $tag->slug, $tag->term_id ); 769 if ( is_numeric( $tag_class ) || ! trim( $tag_class, '-' ) ) { 770 $tag_class = $tag->term_id; 771 } 772 773 $classes[] = 'tag-' . $tag_class; 774 $classes[] = 'tag-' . $tag->term_id; 775 } 776 } elseif ( is_tax() ) { 777 $term = $wp_query->get_queried_object(); 778 if ( isset( $term->term_id ) ) { 779 $term_class = sanitize_html_class( $term->slug, $term->term_id ); 780 if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) { 781 $term_class = $term->term_id; 782 } 783 784 $classes[] = 'tax-' . sanitize_html_class( $term->taxonomy ); 785 $classes[] = 'term-' . $term_class; 786 $classes[] = 'term-' . $term->term_id; 787 } 788 } 789 } 790 791 if ( is_user_logged_in() ) { 792 $classes[] = 'logged-in'; 793 } 794 795 if ( is_admin_bar_showing() ) { 796 $classes[] = 'admin-bar'; 797 $classes[] = 'no-customize-support'; 798 } 799 800 if ( current_theme_supports( 'custom-background' ) 801 && ( get_background_color() !== get_theme_support( 'custom-background', 'default-color' ) || get_background_image() ) ) { 802 $classes[] = 'custom-background'; 803 } 804 805 if ( has_custom_logo() ) { 806 $classes[] = 'wp-custom-logo'; 807 } 808 809 if ( current_theme_supports( 'responsive-embeds' ) ) { 810 $classes[] = 'wp-embed-responsive'; 811 } 812 813 $page = $wp_query->get( 'page' ); 814 815 if ( ! $page || $page < 2 ) { 816 $page = $wp_query->get( 'paged' ); 817 } 818 819 if ( $page && $page > 1 && ! is_404() ) { 820 $classes[] = 'paged-' . $page; 821 822 if ( is_single() ) { 823 $classes[] = 'single-paged-' . $page; 824 } elseif ( is_page() ) { 825 $classes[] = 'page-paged-' . $page; 826 } elseif ( is_category() ) { 827 $classes[] = 'category-paged-' . $page; 828 } elseif ( is_tag() ) { 829 $classes[] = 'tag-paged-' . $page; 830 } elseif ( is_date() ) { 831 $classes[] = 'date-paged-' . $page; 832 } elseif ( is_author() ) { 833 $classes[] = 'author-paged-' . $page; 834 } elseif ( is_search() ) { 835 $classes[] = 'search-paged-' . $page; 836 } elseif ( is_post_type_archive() ) { 837 $classes[] = 'post-type-paged-' . $page; 838 } 839 } 840 841 $classes[] = 'wp-theme-' . sanitize_html_class( get_template() ); 842 if ( is_child_theme() ) { 843 $classes[] = 'wp-child-theme-' . sanitize_html_class( get_stylesheet() ); 844 } 845 846 if ( ! empty( $css_class ) ) { 847 if ( ! is_array( $css_class ) ) { 848 $css_class = preg_split( '#\s+#', $css_class ); 849 } 850 $classes = array_merge( $classes, $css_class ); 851 } else { 852 // Ensure that we always coerce class to being an array. 853 $css_class = array(); 854 } 855 856 $classes = array_map( 'esc_attr', $classes ); 857 858 /** 859 * Filters the list of CSS body class names for the current post or page. 860 * 861 * @since 2.8.0 862 * 863 * @param string[] $classes An array of body class names. 864 * @param string[] $css_class An array of additional class names added to the body. 865 */ 866 $classes = apply_filters( 'body_class', $classes, $css_class ); 867 868 return array_unique( $classes ); 869 } 870 871 /** 872 * Determines whether the post requires password and whether a correct password has been provided. 873 * 874 * @since 2.7.0 875 * 876 * @param int|WP_Post|null $post An optional post. Global $post used if not provided. 877 * @return bool false if a password is not required or the correct password cookie is present, true otherwise. 878 */ 879 function post_password_required( $post = null ) { 880 $post = get_post( $post ); 881 882 if ( empty( $post->post_password ) ) { 883 /** This filter is documented in wp-includes/post-template.php */ 884 return apply_filters( 'post_password_required', false, $post ); 885 } 886 887 if ( ! isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) { 888 /** This filter is documented in wp-includes/post-template.php */ 889 return apply_filters( 'post_password_required', true, $post ); 890 } 891 892 require_once ABSPATH . WPINC . '/class-phpass.php'; 893 $hasher = new PasswordHash( 8, true ); 894 895 $hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ); 896 if ( ! str_starts_with( $hash, '$P$B' ) ) { 897 $required = true; 898 } else { 899 $required = ! $hasher->CheckPassword( $post->post_password, $hash ); 900 } 901 902 /** 903 * Filters whether a post requires the user to supply a password. 904 * 905 * @since 4.7.0 906 * 907 * @param bool $required Whether the user needs to supply a password. True if password has not been 908 * provided or is incorrect, false if password has been supplied or is not required. 909 * @param WP_Post $post Post object. 910 */ 911 return apply_filters( 'post_password_required', $required, $post ); 912 } 913 914 // 915 // Page Template Functions for usage in Themes. 916 // 917 918 /** 919 * The formatted output of a list of pages. 920 * 921 * Displays page links for paginated posts (i.e. including the `<!--nextpage-->` 922 * Quicktag one or more times). This tag must be within The Loop. 923 * 924 * @since 1.2.0 925 * @since 5.1.0 Added the `aria_current` argument. 926 * 927 * @global int $page 928 * @global int $numpages 929 * @global int $multipage 930 * @global int $more 931 * 932 * @param string|array $args { 933 * Optional. Array or string of default arguments. 934 * 935 * @type string $before HTML or text to prepend to each link. Default is `<p> Pages:`. 936 * @type string $after HTML or text to append to each link. Default is `</p>`. 937 * @type string $link_before HTML or text to prepend to each link, inside the `<a>` tag. 938 * Also prepended to the current item, which is not linked. Default empty. 939 * @type string $link_after HTML or text to append to each Pages link inside the `<a>` tag. 940 * Also appended to the current item, which is not linked. Default empty. 941 * @type string $aria_current The value for the aria-current attribute. Possible values are 'page', 942 * 'step', 'location', 'date', 'time', 'true', 'false'. Default is 'page'. 943 * @type string $next_or_number Indicates whether page numbers should be used. Valid values are number 944 * and next. Default is 'number'. 945 * @type string $separator Text between pagination links. Default is ' '. 946 * @type string $nextpagelink Link text for the next page link, if available. Default is 'Next Page'. 947 * @type string $previouspagelink Link text for the previous page link, if available. Default is 'Previous Page'. 948 * @type string $pagelink Format string for page numbers. The % in the parameter string will be 949 * replaced with the page number, so 'Page %' generates "Page 1", "Page 2", etc. 950 * Defaults to '%', just the page number. 951 * @type int|bool $echo Whether to echo or not. Accepts 1|true or 0|false. Default 1|true. 952 * } 953 * @return string Formatted output in HTML. 954 */ 955 function wp_link_pages( $args = '' ) { 956 global $page, $numpages, $multipage, $more; 957 958 $defaults = array( 959 'before' => '<p class="post-nav-links">' . __( 'Pages:' ), 960 'after' => '</p>', 961 'link_before' => '', 962 'link_after' => '', 963 'aria_current' => 'page', 964 'next_or_number' => 'number', 965 'separator' => ' ', 966 'nextpagelink' => __( 'Next page' ), 967 'previouspagelink' => __( 'Previous page' ), 968 'pagelink' => '%', 969 'echo' => 1, 970 ); 971 972 $parsed_args = wp_parse_args( $args, $defaults ); 973 974 /** 975 * Filters the arguments used in retrieving page links for paginated posts. 976 * 977 * @since 3.0.0 978 * 979 * @param array $parsed_args An array of page link arguments. See wp_link_pages() 980 * for information on accepted arguments. 981 */ 982 $parsed_args = apply_filters( 'wp_link_pages_args', $parsed_args ); 983 984 $output = ''; 985 if ( $multipage ) { 986 if ( 'number' === $parsed_args['next_or_number'] ) { 987 $output .= $parsed_args['before']; 988 for ( $i = 1; $i <= $numpages; $i++ ) { 989 $link = $parsed_args['link_before'] . str_replace( '%', $i, $parsed_args['pagelink'] ) . $parsed_args['link_after']; 990 991 if ( $i !== $page || ! $more && 1 === $page ) { 992 $link = _wp_link_page( $i ) . $link . '</a>'; 993 } elseif ( $i === $page ) { 994 $link = '<span class="post-page-numbers current" aria-current="' . esc_attr( $parsed_args['aria_current'] ) . '">' . $link . '</span>'; 995 } 996 997 /** 998 * Filters the HTML output of individual page number links. 999 * 1000 * @since 3.6.0 1001 * 1002 * @param string $link The page number HTML output. 1003 * @param int $i Page number for paginated posts' page links. 1004 */ 1005 $link = apply_filters( 'wp_link_pages_link', $link, $i ); 1006 1007 // Use the custom links separator beginning with the second link. 1008 $output .= ( 1 === $i ) ? ' ' : $parsed_args['separator']; 1009 $output .= $link; 1010 } 1011 $output .= $parsed_args['after']; 1012 } elseif ( $more ) { 1013 $output .= $parsed_args['before']; 1014 $prev = $page - 1; 1015 if ( $prev > 0 ) { 1016 $link = _wp_link_page( $prev ) . $parsed_args['link_before'] . $parsed_args['previouspagelink'] . $parsed_args['link_after'] . '</a>'; 1017 1018 /** This filter is documented in wp-includes/post-template.php */ 1019 $output .= apply_filters( 'wp_link_pages_link', $link, $prev ); 1020 } 1021 $next = $page + 1; 1022 if ( $next <= $numpages ) { 1023 if ( $prev ) { 1024 $output .= $parsed_args['separator']; 1025 } 1026 $link = _wp_link_page( $next ) . $parsed_args['link_before'] . $parsed_args['nextpagelink'] . $parsed_args['link_after'] . '</a>'; 1027 1028 /** This filter is documented in wp-includes/post-template.php */ 1029 $output .= apply_filters( 'wp_link_pages_link', $link, $next ); 1030 } 1031 $output .= $parsed_args['after']; 1032 } 1033 } 1034 1035 /** 1036 * Filters the HTML output of page links for paginated posts. 1037 * 1038 * @since 3.6.0 1039 * 1040 * @param string $output HTML output of paginated posts' page links. 1041 * @param array|string $args An array or query string of arguments. See wp_link_pages() 1042 * for information on accepted arguments. 1043 */ 1044 $html = apply_filters( 'wp_link_pages', $output, $args ); 1045 1046 if ( $parsed_args['echo'] ) { 1047 echo $html; 1048 } 1049 return $html; 1050 } 1051 1052 /** 1053 * Helper function for wp_link_pages(). 1054 * 1055 * @since 3.1.0 1056 * @access private 1057 * 1058 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 1059 * 1060 * @param int $i Page number. 1061 * @return string Link. 1062 */ 1063 function _wp_link_page( $i ) { 1064 global $wp_rewrite; 1065 $post = get_post(); 1066 $query_args = array(); 1067 1068 if ( 1 === $i ) { 1069 $url = get_permalink(); 1070 } else { 1071 if ( ! get_option( 'permalink_structure' ) || in_array( $post->post_status, array( 'draft', 'pending' ), true ) ) { 1072 $url = add_query_arg( 'page', $i, get_permalink() ); 1073 } elseif ( 'page' === get_option( 'show_on_front' ) && (int) get_option( 'page_on_front' ) === $post->ID ) { 1074 $url = trailingslashit( get_permalink() ) . user_trailingslashit( "$wp_rewrite->pagination_base/" . $i, 'single_paged' ); 1075 } else { 1076 $url = trailingslashit( get_permalink() ) . user_trailingslashit( $i, 'single_paged' ); 1077 } 1078 } 1079 1080 if ( is_preview() ) { 1081 1082 if ( ( 'draft' !== $post->post_status ) && isset( $_GET['preview_id'], $_GET['preview_nonce'] ) ) { 1083 $query_args['preview_id'] = wp_unslash( $_GET['preview_id'] ); 1084 $query_args['preview_nonce'] = wp_unslash( $_GET['preview_nonce'] ); 1085 } 1086 1087 $url = get_preview_post_link( $post, $query_args, $url ); 1088 } 1089 1090 return '<a href="' . esc_url( $url ) . '" class="post-page-numbers">'; 1091 } 1092 1093 // 1094 // Post-meta: Custom per-post fields. 1095 // 1096 1097 /** 1098 * Retrieves post custom meta data field. 1099 * 1100 * @since 1.5.0 1101 * 1102 * @param string $key Meta data key name. 1103 * @return array|string|false Array of values, or single value if only one element exists. 1104 * False if the key does not exist. 1105 */ 1106 function post_custom( $key = '' ) { 1107 $custom = get_post_custom(); 1108 1109 if ( ! isset( $custom[ $key ] ) ) { 1110 return false; 1111 } elseif ( 1 === count( $custom[ $key ] ) ) { 1112 return $custom[ $key ][0]; 1113 } else { 1114 return $custom[ $key ]; 1115 } 1116 } 1117 1118 /** 1119 * Displays a list of post custom fields. 1120 * 1121 * @since 1.2.0 1122 * 1123 * @deprecated 6.0.2 Use get_post_meta() to retrieve post meta and render manually. 1124 */ 1125 function the_meta() { 1126 _deprecated_function( __FUNCTION__, '6.0.2', 'get_post_meta()' ); 1127 $keys = get_post_custom_keys(); 1128 if ( $keys ) { 1129 $li_html = ''; 1130 foreach ( (array) $keys as $key ) { 1131 $keyt = trim( $key ); 1132 if ( is_protected_meta( $keyt, 'post' ) ) { 1133 continue; 1134 } 1135 1136 $values = array_map( 'trim', get_post_custom_values( $key ) ); 1137 $value = implode( ', ', $values ); 1138 1139 $html = sprintf( 1140 "<li><span class='post-meta-key'>%s</span> %s</li>\n", 1141 /* translators: %s: Post custom field name. */ 1142 esc_html( sprintf( _x( '%s:', 'Post custom field name' ), $key ) ), 1143 esc_html( $value ) 1144 ); 1145 1146 /** 1147 * Filters the HTML output of the li element in the post custom fields list. 1148 * 1149 * @since 2.2.0 1150 * 1151 * @param string $html The HTML output for the li element. 1152 * @param string $key Meta key. 1153 * @param string $value Meta value. 1154 */ 1155 $li_html .= apply_filters( 'the_meta_key', $html, $key, $value ); 1156 } 1157 1158 if ( $li_html ) { 1159 echo "<ul class='post-meta'>\n{$li_html}</ul>\n"; 1160 } 1161 } 1162 } 1163 1164 // 1165 // Pages. 1166 // 1167 1168 /** 1169 * Retrieves or displays a list of pages as a dropdown (select list). 1170 * 1171 * @since 2.1.0 1172 * @since 4.2.0 The `$value_field` argument was added. 1173 * @since 4.3.0 The `$class` argument was added. 1174 * 1175 * @see get_pages() 1176 * 1177 * @param array|string $args { 1178 * Optional. Array or string of arguments to generate a page dropdown. See get_pages() for additional arguments. 1179 * 1180 * @type int $depth Maximum depth. Default 0. 1181 * @type int $child_of Page ID to retrieve child pages of. Default 0. 1182 * @type int|string $selected Value of the option that should be selected. Default 0. 1183 * @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1, 1184 * or their bool equivalents. Default 1. 1185 * @type string $name Value for the 'name' attribute of the select element. 1186 * Default 'page_id'. 1187 * @type string $id Value for the 'id' attribute of the select element. 1188 * @type string $class Value for the 'class' attribute of the select element. Default: none. 1189 * Defaults to the value of `$name`. 1190 * @type string $show_option_none Text to display for showing no pages. Default empty (does not display). 1191 * @type string $show_option_no_change Text to display for "no change" option. Default empty (does not display). 1192 * @type string $option_none_value Value to use when no page is selected. Default empty. 1193 * @type string $value_field Post field used to populate the 'value' attribute of the option 1194 * elements. Accepts any valid post field. Default 'ID'. 1195 * } 1196 * @return string HTML dropdown list of pages. 1197 */ 1198 function wp_dropdown_pages( $args = '' ) { 1199 $defaults = array( 1200 'depth' => 0, 1201 'child_of' => 0, 1202 'selected' => 0, 1203 'echo' => 1, 1204 'name' => 'page_id', 1205 'id' => '', 1206 'class' => '', 1207 'show_option_none' => '', 1208 'show_option_no_change' => '', 1209 'option_none_value' => '', 1210 'value_field' => 'ID', 1211 ); 1212 1213 $parsed_args = wp_parse_args( $args, $defaults ); 1214 1215 $pages = get_pages( $parsed_args ); 1216 $output = ''; 1217 // Back-compat with old system where both id and name were based on $name argument. 1218 if ( empty( $parsed_args['id'] ) ) { 1219 $parsed_args['id'] = $parsed_args['name']; 1220 } 1221 1222 if ( ! empty( $pages ) ) { 1223 $class = ''; 1224 if ( ! empty( $parsed_args['class'] ) ) { 1225 $class = " class='" . esc_attr( $parsed_args['class'] ) . "'"; 1226 } 1227 1228 $output = "<select name='" . esc_attr( $parsed_args['name'] ) . "'" . $class . " id='" . esc_attr( $parsed_args['id'] ) . "'>\n"; 1229 if ( $parsed_args['show_option_no_change'] ) { 1230 $output .= "\t<option value=\"-1\">" . $parsed_args['show_option_no_change'] . "</option>\n"; 1231 } 1232 if ( $parsed_args['show_option_none'] ) { 1233 $output .= "\t<option value=\"" . esc_attr( $parsed_args['option_none_value'] ) . '">' . $parsed_args['show_option_none'] . "</option>\n"; 1234 } 1235 $output .= walk_page_dropdown_tree( $pages, $parsed_args['depth'], $parsed_args ); 1236 $output .= "</select>\n"; 1237 } 1238 1239 /** 1240 * Filters the HTML output of a list of pages as a dropdown. 1241 * 1242 * @since 2.1.0 1243 * @since 4.4.0 `$parsed_args` and `$pages` added as arguments. 1244 * 1245 * @param string $output HTML output for dropdown list of pages. 1246 * @param array $parsed_args The parsed arguments array. See wp_dropdown_pages() 1247 * for information on accepted arguments. 1248 * @param WP_Post[] $pages Array of the page objects. 1249 */ 1250 $html = apply_filters( 'wp_dropdown_pages', $output, $parsed_args, $pages ); 1251 1252 if ( $parsed_args['echo'] ) { 1253 echo $html; 1254 } 1255 1256 return $html; 1257 } 1258 1259 /** 1260 * Retrieves or displays a list of pages (or hierarchical post type items) in list (li) format. 1261 * 1262 * @since 1.5.0 1263 * @since 4.7.0 Added the `item_spacing` argument. 1264 * 1265 * @see get_pages() 1266 * 1267 * @global WP_Query $wp_query WordPress Query object. 1268 * 1269 * @param array|string $args { 1270 * Optional. Array or string of arguments to generate a list of pages. See get_pages() for additional arguments. 1271 * 1272 * @type int $child_of Display only the sub-pages of a single page by ID. Default 0 (all pages). 1273 * @type string $authors Comma-separated list of author IDs. Default empty (all authors). 1274 * @type string $date_format PHP date format to use for the listed pages. Relies on the 'show_date' parameter. 1275 * Default is the value of 'date_format' option. 1276 * @type int $depth Number of levels in the hierarchy of pages to include in the generated list. 1277 * Accepts -1 (any depth), 0 (all pages), 1 (top-level pages only), and n (pages to 1278 * the given n depth). Default 0. 1279 * @type bool $echo Whether or not to echo the list of pages. Default true. 1280 * @type string $exclude Comma-separated list of page IDs to exclude. Default empty. 1281 * @type array $include Comma-separated list of page IDs to include. Default empty. 1282 * @type string $link_after Text or HTML to follow the page link label. Default null. 1283 * @type string $link_before Text or HTML to precede the page link label. Default null. 1284 * @type string $post_type Post type to query for. Default 'page'. 1285 * @type string|array $post_status Comma-separated list or array of post statuses to include. Default 'publish'. 1286 * @type string $show_date Whether to display the page publish or modified date for each page. Accepts 1287 * 'modified' or any other value. An empty value hides the date. Default empty. 1288 * @type string $sort_column Comma-separated list of column names to sort the pages by. Accepts 'post_author', 1289 * 'post_date', 'post_title', 'post_name', 'post_modified', 'post_modified_gmt', 1290 * 'menu_order', 'post_parent', 'ID', 'rand', or 'comment_count'. Default 'post_title'. 1291 * @type string $title_li List heading. Passing a null or empty value will result in no heading, and the list 1292 * will not be wrapped with unordered list `<ul>` tags. Default 'Pages'. 1293 * @type string $item_spacing Whether to preserve whitespace within the menu's HTML. Accepts 'preserve' or 'discard'. 1294 * Default 'preserve'. 1295 * @type Walker $walker Walker instance to use for listing pages. Default empty which results in a 1296 * Walker_Page instance being used. 1297 * } 1298 * @return void|string Void if 'echo' argument is true, HTML list of pages if 'echo' is false. 1299 */ 1300 function wp_list_pages( $args = '' ) { 1301 $defaults = array( 1302 'depth' => 0, 1303 'show_date' => '', 1304 'date_format' => get_option( 'date_format' ), 1305 'child_of' => 0, 1306 'exclude' => '', 1307 'title_li' => __( 'Pages' ), 1308 'echo' => 1, 1309 'authors' => '', 1310 'sort_column' => 'menu_order, post_title', 1311 'link_before' => '', 1312 'link_after' => '', 1313 'item_spacing' => 'preserve', 1314 'walker' => '', 1315 ); 1316 1317 $parsed_args = wp_parse_args( $args, $defaults ); 1318 1319 if ( ! in_array( $parsed_args['item_spacing'], array( 'preserve', 'discard' ), true ) ) { 1320 // Invalid value, fall back to default. 1321 $parsed_args['item_spacing'] = $defaults['item_spacing']; 1322 } 1323 1324 $output = ''; 1325 $current_page = 0; 1326 1327 // Sanitize, mostly to keep spaces out. 1328 $parsed_args['exclude'] = preg_replace( '/[^0-9,]/', '', $parsed_args['exclude'] ); 1329 1330 // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array). 1331 $exclude_array = ( $parsed_args['exclude'] ) ? explode( ',', $parsed_args['exclude'] ) : array(); 1332 1333 /** 1334 * Filters the array of pages to exclude from the pages list. 1335 * 1336 * @since 2.1.0 1337 * 1338 * @param string[] $exclude_array An array of page IDs to exclude. 1339 */ 1340 $parsed_args['exclude'] = implode( ',', apply_filters( 'wp_list_pages_excludes', $exclude_array ) ); 1341 1342 $parsed_args['hierarchical'] = 0; 1343 1344 // Query pages. 1345 $pages = get_pages( $parsed_args ); 1346 1347 if ( ! empty( $pages ) ) { 1348 if ( $parsed_args['title_li'] ) { 1349 $output .= '<li class="pagenav">' . $parsed_args['title_li'] . '<ul>'; 1350 } 1351 global $wp_query; 1352 if ( is_page() || is_attachment() || $wp_query->is_posts_page ) { 1353 $current_page = get_queried_object_id(); 1354 } elseif ( is_singular() ) { 1355 $queried_object = get_queried_object(); 1356 if ( is_post_type_hierarchical( $queried_object->post_type ) ) { 1357 $current_page = $queried_object->ID; 1358 } 1359 } 1360 1361 $output .= walk_page_tree( $pages, $parsed_args['depth'], $current_page, $parsed_args ); 1362 1363 if ( $parsed_args['title_li'] ) { 1364 $output .= '</ul></li>'; 1365 } 1366 } 1367 1368 /** 1369 * Filters the HTML output of the pages to list. 1370 * 1371 * @since 1.5.1 1372 * @since 4.4.0 `$pages` added as arguments. 1373 * 1374 * @see wp_list_pages() 1375 * 1376 * @param string $output HTML output of the pages list. 1377 * @param array $parsed_args An array of page-listing arguments. See wp_list_pages() 1378 * for information on accepted arguments. 1379 * @param WP_Post[] $pages Array of the page objects. 1380 */ 1381 $html = apply_filters( 'wp_list_pages', $output, $parsed_args, $pages ); 1382 1383 if ( $parsed_args['echo'] ) { 1384 echo $html; 1385 } else { 1386 return $html; 1387 } 1388 } 1389 1390 /** 1391 * Displays or retrieves a list of pages with an optional home link. 1392 * 1393 * The arguments are listed below and part of the arguments are for wp_list_pages() function. 1394 * Check that function for more info on those arguments. 1395 * 1396 * @since 2.7.0 1397 * @since 4.4.0 Added `menu_id`, `container`, `before`, `after`, and `walker` arguments. 1398 * @since 4.7.0 Added the `item_spacing` argument. 1399 * 1400 * @param array|string $args { 1401 * Optional. Array or string of arguments to generate a page menu. See wp_list_pages() for additional arguments. 1402 * 1403 * @type string $sort_column How to sort the list of pages. Accepts post column names. 1404 * Default 'menu_order, post_title'. 1405 * @type string $menu_id ID for the div containing the page list. Default is empty string. 1406 * @type string $menu_class Class to use for the element containing the page list. Default 'menu'. 1407 * @type string $container Element to use for the element containing the page list. Default 'div'. 1408 * @type bool $echo Whether to echo the list or return it. Accepts true (echo) or false (return). 1409 * Default true. 1410 * @type int|bool|string $show_home Whether to display the link to the home page. Can just enter the text 1411 * you'd like shown for the home link. 1|true defaults to 'Home'. 1412 * @type string $link_before The HTML or text to prepend to $show_home text. Default empty. 1413 * @type string $link_after The HTML or text to append to $show_home text. Default empty. 1414 * @type string $before The HTML or text to prepend to the menu. Default is '<ul>'. 1415 * @type string $after The HTML or text to append to the menu. Default is '</ul>'. 1416 * @type string $item_spacing Whether to preserve whitespace within the menu's HTML. Accepts 'preserve' 1417 * or 'discard'. Default 'discard'. 1418 * @type Walker $walker Walker instance to use for listing pages. Default empty which results in a 1419 * Walker_Page instance being used. 1420 * } 1421 * @return void|string Void if 'echo' argument is true, HTML menu if 'echo' is false. 1422 */ 1423 function wp_page_menu( $args = array() ) { 1424 $defaults = array( 1425 'sort_column' => 'menu_order, post_title', 1426 'menu_id' => '', 1427 'menu_class' => 'menu', 1428 'container' => 'div', 1429 'echo' => true, 1430 'link_before' => '', 1431 'link_after' => '', 1432 'before' => '<ul>', 1433 'after' => '</ul>', 1434 'item_spacing' => 'discard', 1435 'walker' => '', 1436 ); 1437 $args = wp_parse_args( $args, $defaults ); 1438 1439 if ( ! in_array( $args['item_spacing'], array( 'preserve', 'discard' ), true ) ) { 1440 // Invalid value, fall back to default. 1441 $args['item_spacing'] = $defaults['item_spacing']; 1442 } 1443 1444 if ( 'preserve' === $args['item_spacing'] ) { 1445 $t = "\t"; 1446 $n = "\n"; 1447 } else { 1448 $t = ''; 1449 $n = ''; 1450 } 1451 1452 /** 1453 * Filters the arguments used to generate a page-based menu. 1454 * 1455 * @since 2.7.0 1456 * 1457 * @see wp_page_menu() 1458 * 1459 * @param array $args An array of page menu arguments. See wp_page_menu() 1460 * for information on accepted arguments. 1461 */ 1462 $args = apply_filters( 'wp_page_menu_args', $args ); 1463 1464 $menu = ''; 1465 1466 $list_args = $args; 1467 1468 // Show Home in the menu. 1469 if ( ! empty( $args['show_home'] ) ) { 1470 if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] ) { 1471 $text = __( 'Home' ); 1472 } else { 1473 $text = $args['show_home']; 1474 } 1475 $class = ''; 1476 if ( is_front_page() && ! is_paged() ) { 1477 $class = 'class="current_page_item"'; 1478 } 1479 $menu .= '<li ' . $class . '><a href="' . esc_url( home_url( '/' ) ) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>'; 1480 // If the front page is a page, add it to the exclude list. 1481 if ( 'page' === get_option( 'show_on_front' ) ) { 1482 if ( ! empty( $list_args['exclude'] ) ) { 1483 $list_args['exclude'] .= ','; 1484 } else { 1485 $list_args['exclude'] = ''; 1486 } 1487 $list_args['exclude'] .= get_option( 'page_on_front' ); 1488 } 1489 } 1490 1491 $list_args['echo'] = false; 1492 $list_args['title_li'] = ''; 1493 $menu .= wp_list_pages( $list_args ); 1494 1495 $container = sanitize_text_field( $args['container'] ); 1496 1497 // Fallback in case `wp_nav_menu()` was called without a container. 1498 if ( empty( $container ) ) { 1499 $container = 'div'; 1500 } 1501 1502 if ( $menu ) { 1503 1504 // wp_nav_menu() doesn't set before and after. 1505 if ( isset( $args['fallback_cb'] ) && 1506 'wp_page_menu' === $args['fallback_cb'] && 1507 'ul' !== $container ) { 1508 $args['before'] = "<ul>{$n}"; 1509 $args['after'] = '</ul>'; 1510 } 1511 1512 $menu = $args['before'] . $menu . $args['after']; 1513 } 1514 1515 $attrs = ''; 1516 if ( ! empty( $args['menu_id'] ) ) { 1517 $attrs .= ' id="' . esc_attr( $args['menu_id'] ) . '"'; 1518 } 1519 1520 if ( ! empty( $args['menu_class'] ) ) { 1521 $attrs .= ' class="' . esc_attr( $args['menu_class'] ) . '"'; 1522 } 1523 1524 $menu = "<{$container}{$attrs}>" . $menu . "</{$container}>{$n}"; 1525 1526 /** 1527 * Filters the HTML output of a page-based menu. 1528 * 1529 * @since 2.7.0 1530 * 1531 * @see wp_page_menu() 1532 * 1533 * @param string $menu The HTML output. 1534 * @param array $args An array of arguments. See wp_page_menu() 1535 * for information on accepted arguments. 1536 */ 1537 $menu = apply_filters( 'wp_page_menu', $menu, $args ); 1538 1539 if ( $args['echo'] ) { 1540 echo $menu; 1541 } else { 1542 return $menu; 1543 } 1544 } 1545 1546 // 1547 // Page helpers. 1548 // 1549 1550 /** 1551 * Retrieves HTML list content for page list. 1552 * 1553 * @uses Walker_Page to create HTML list content. 1554 * @since 2.1.0 1555 * 1556 * @param array $pages 1557 * @param int $depth 1558 * @param int $current_page 1559 * @param array $args 1560 * @return string 1561 */ 1562 function walk_page_tree( $pages, $depth, $current_page, $args ) { 1563 if ( empty( $args['walker'] ) ) { 1564 $walker = new Walker_Page(); 1565 } else { 1566 /** 1567 * @var Walker $walker 1568 */ 1569 $walker = $args['walker']; 1570 } 1571 1572 foreach ( (array) $pages as $page ) { 1573 if ( $page->post_parent ) { 1574 $args['pages_with_children'][ $page->post_parent ] = true; 1575 } 1576 } 1577 1578 return $walker->walk( $pages, $depth, $args, $current_page ); 1579 } 1580 1581 /** 1582 * Retrieves HTML dropdown (select) content for page list. 1583 * 1584 * @since 2.1.0 1585 * @since 5.3.0 Formalized the existing `...$args` parameter by adding it 1586 * to the function signature. 1587 * 1588 * @uses Walker_PageDropdown to create HTML dropdown content. 1589 * @see Walker_PageDropdown::walk() for parameters and return description. 1590 * 1591 * @param mixed ...$args Elements array, maximum hierarchical depth and optional additional arguments. 1592 * @return string 1593 */ 1594 function walk_page_dropdown_tree( ...$args ) { 1595 if ( empty( $args[2]['walker'] ) ) { // The user's options are the third parameter. 1596 $walker = new Walker_PageDropdown(); 1597 } else { 1598 /** 1599 * @var Walker $walker 1600 */ 1601 $walker = $args[2]['walker']; 1602 } 1603 1604 return $walker->walk( ...$args ); 1605 } 1606 1607 // 1608 // Attachments. 1609 // 1610 1611 /** 1612 * Displays an attachment page link using an image or icon. 1613 * 1614 * @since 2.0.0 1615 * 1616 * @param int|WP_Post $post Optional. Post ID or post object. 1617 * @param bool $fullsize Optional. Whether to use full size. Default false. 1618 * @param bool $deprecated Deprecated. Not used. 1619 * @param bool $permalink Optional. Whether to include permalink. Default false. 1620 */ 1621 function the_attachment_link( $post = 0, $fullsize = false, $deprecated = false, $permalink = false ) { 1622 if ( ! empty( $deprecated ) ) { 1623 _deprecated_argument( __FUNCTION__, '2.5.0' ); 1624 } 1625 1626 if ( $fullsize ) { 1627 echo wp_get_attachment_link( $post, 'full', $permalink ); 1628 } else { 1629 echo wp_get_attachment_link( $post, 'thumbnail', $permalink ); 1630 } 1631 } 1632 1633 /** 1634 * Retrieves an attachment page link using an image or icon, if possible. 1635 * 1636 * @since 2.5.0 1637 * @since 4.4.0 The `$post` parameter can now accept either a post ID or `WP_Post` object. 1638 * 1639 * @param int|WP_Post $post Optional. Post ID or post object. 1640 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array 1641 * of width and height values in pixels (in that order). Default 'thumbnail'. 1642 * @param bool $permalink Optional. Whether to add permalink to image. Default false. 1643 * @param bool $icon Optional. Whether the attachment is an icon. Default false. 1644 * @param string|false $text Optional. Link text to use. Activated by passing a string, false otherwise. 1645 * Default false. 1646 * @param array|string $attr Optional. Array or string of attributes. Default empty. 1647 * @return string HTML content. 1648 */ 1649 function wp_get_attachment_link( $post = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) { 1650 $_post = get_post( $post ); 1651 1652 if ( empty( $_post ) || ( 'attachment' !== $_post->post_type ) || ! wp_get_attachment_url( $_post->ID ) ) { 1653 return __( 'Missing Attachment' ); 1654 } 1655 1656 $url = wp_get_attachment_url( $_post->ID ); 1657 1658 if ( $permalink ) { 1659 $url = get_attachment_link( $_post->ID ); 1660 } 1661 1662 if ( $text ) { 1663 $link_text = $text; 1664 } elseif ( $size && 'none' !== $size ) { 1665 $link_text = wp_get_attachment_image( $_post->ID, $size, $icon, $attr ); 1666 } else { 1667 $link_text = ''; 1668 } 1669 1670 if ( '' === trim( $link_text ) ) { 1671 $link_text = $_post->post_title; 1672 } 1673 1674 if ( '' === trim( $link_text ) ) { 1675 $link_text = esc_html( pathinfo( get_attached_file( $_post->ID ), PATHINFO_FILENAME ) ); 1676 } 1677 1678 /** 1679 * Filters the list of attachment link attributes. 1680 * 1681 * @since 6.2.0 1682 * 1683 * @param array $attributes An array of attributes for the link markup, 1684 * keyed on the attribute name. 1685 * @param int $id Post ID. 1686 */ 1687 $attributes = apply_filters( 'wp_get_attachment_link_attributes', array( 'href' => $url ), $_post->ID ); 1688 1689 $link_attributes = ''; 1690 foreach ( $attributes as $name => $value ) { 1691 $value = 'href' === $name ? esc_url( $value ) : esc_attr( $value ); 1692 $link_attributes .= ' ' . esc_attr( $name ) . "='" . $value . "'"; 1693 } 1694 1695 $link_html = "<a$link_attributes>$link_text</a>"; 1696 1697 /** 1698 * Filters a retrieved attachment page link. 1699 * 1700 * @since 2.7.0 1701 * @since 5.1.0 Added the `$attr` parameter. 1702 * 1703 * @param string $link_html The page link HTML output. 1704 * @param int|WP_Post $post Post ID or object. Can be 0 for the current global post. 1705 * @param string|int[] $size Requested image size. Can be any registered image size name, or 1706 * an array of width and height values in pixels (in that order). 1707 * @param bool $permalink Whether to add permalink to image. Default false. 1708 * @param bool $icon Whether to include an icon. 1709 * @param string|false $text If string, will be link text. 1710 * @param array|string $attr Array or string of attributes. 1711 */ 1712 return apply_filters( 'wp_get_attachment_link', $link_html, $post, $size, $permalink, $icon, $text, $attr ); 1713 } 1714 1715 /** 1716 * Wraps attachment in paragraph tag before content. 1717 * 1718 * @since 2.0.0 1719 * 1720 * @param string $content 1721 * @return string 1722 */ 1723 function prepend_attachment( $content ) { 1724 $post = get_post(); 1725 1726 if ( empty( $post->post_type ) || 'attachment' !== $post->post_type ) { 1727 return $content; 1728 } 1729 1730 if ( wp_attachment_is( 'video', $post ) ) { 1731 $meta = wp_get_attachment_metadata( get_the_ID() ); 1732 $atts = array( 'src' => wp_get_attachment_url() ); 1733 if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) { 1734 $atts['width'] = (int) $meta['width']; 1735 $atts['height'] = (int) $meta['height']; 1736 } 1737 if ( has_post_thumbnail() ) { 1738 $atts['poster'] = wp_get_attachment_url( get_post_thumbnail_id() ); 1739 } 1740 $p = wp_video_shortcode( $atts ); 1741 } elseif ( wp_attachment_is( 'audio', $post ) ) { 1742 $p = wp_audio_shortcode( array( 'src' => wp_get_attachment_url() ) ); 1743 } else { 1744 $p = '<p class="attachment">'; 1745 // Show the medium sized image representation of the attachment if available, and link to the raw file. 1746 $p .= wp_get_attachment_link( 0, 'medium', false ); 1747 $p .= '</p>'; 1748 } 1749 1750 /** 1751 * Filters the attachment markup to be prepended to the post content. 1752 * 1753 * @since 2.0.0 1754 * 1755 * @see prepend_attachment() 1756 * 1757 * @param string $p The attachment HTML output. 1758 */ 1759 $p = apply_filters( 'prepend_attachment', $p ); 1760 1761 return "$p\n$content"; 1762 } 1763 1764 // 1765 // Misc. 1766 // 1767 1768 /** 1769 * Retrieves protected post password form content. 1770 * 1771 * @since 1.0.0 1772 * 1773 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 1774 * @return string HTML content for password form for password protected post. 1775 */ 1776 function get_the_password_form( $post = 0 ) { 1777 $post = get_post( $post ); 1778 $field_id = 'pwbox-' . ( empty( $post->ID ) ? wp_rand() : $post->ID ); 1779 $invalid_password = ''; 1780 $invalid_password_html = ''; 1781 $aria = ''; 1782 $class = ''; 1783 $redirect_field = ''; 1784 1785 // If the referrer is the same as the current request, the user has entered an invalid password. 1786 if ( ! empty( $post->ID ) && wp_get_raw_referer() === get_permalink( $post->ID ) && isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) { 1787 /** 1788 * Filters the invalid password message shown on password-protected posts. 1789 * The filter is only applied if the post is password protected. 1790 * 1791 * @since 6.8.0 1792 * 1793 * @param string $text The message shown to users when entering an invalid password. 1794 * @param WP_Post $post Post object. 1795 */ 1796 $invalid_password = apply_filters( 'the_password_form_incorrect_password', __( 'Invalid password.' ), $post ); 1797 $invalid_password_html = '<div class="post-password-form-invalid-password" role="alert"><p id="error-' . $field_id . '">' . $invalid_password . '</p></div>'; 1798 $aria = ' aria-describedby="error-' . $field_id . '"'; 1799 $class = ' password-form-error'; 1800 } 1801 1802 if ( ! empty( $post->ID ) ) { 1803 $redirect_field = sprintf( 1804 '<input type="hidden" name="redirect_to" value="%s" />', 1805 esc_attr( get_permalink( $post->ID ) ) 1806 ); 1807 } 1808 1809 $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 . ' 1810 <p>' . __( 'This content is password protected. To view it please enter your password below:' ) . '</p> 1811 <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> 1812 '; 1813 1814 /** 1815 * Filters the HTML output for the protected post password form. 1816 * 1817 * If modifying the password field, please note that the WordPress database schema 1818 * limits the password field to 255 characters regardless of the value of the 1819 * `minlength` or `maxlength` attributes or other validation that may be added to 1820 * the input. 1821 * 1822 * @since 2.7.0 1823 * @since 5.8.0 Added the `$post` parameter. 1824 * @since 6.8.0 Added the `$invalid_password` parameter. 1825 * 1826 * @param string $output The password form HTML output. 1827 * @param WP_Post $post Post object. 1828 * @param string $invalid_password The invalid password message. 1829 */ 1830 return apply_filters( 'the_password_form', $output, $post, $invalid_password ); 1831 } 1832 1833 /** 1834 * Determines whether the current post uses a page template. 1835 * 1836 * This template tag allows you to determine if you are in a page template. 1837 * You can optionally provide a template filename or array of template filenames 1838 * and then the check will be specific to that template. 1839 * 1840 * For more information on this and similar theme functions, check out 1841 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 1842 * Conditional Tags} article in the Theme Developer Handbook. 1843 * 1844 * @since 2.5.0 1845 * @since 4.2.0 The `$template` parameter was changed to also accept an array of page templates. 1846 * @since 4.7.0 Now works with any post type, not just pages. 1847 * 1848 * @param string|string[] $template The specific template filename or array of templates to match. 1849 * @return bool True on success, false on failure. 1850 */ 1851 function is_page_template( $template = '' ) { 1852 if ( ! is_singular() ) { 1853 return false; 1854 } 1855 1856 $page_template = get_page_template_slug( get_queried_object_id() ); 1857 1858 if ( empty( $template ) ) { 1859 return (bool) $page_template; 1860 } 1861 1862 if ( $template === $page_template ) { 1863 return true; 1864 } 1865 1866 if ( is_array( $template ) ) { 1867 if ( ( in_array( 'default', $template, true ) && ! $page_template ) 1868 || in_array( $page_template, $template, true ) 1869 ) { 1870 return true; 1871 } 1872 } 1873 1874 return ( 'default' === $template && ! $page_template ); 1875 } 1876 1877 /** 1878 * Gets the specific template filename for a given post. 1879 * 1880 * @since 3.4.0 1881 * @since 4.7.0 Now works with any post type, not just pages. 1882 * 1883 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 1884 * @return string|false Page template filename. Returns an empty string when the default page template 1885 * is in use. Returns false if the post does not exist. 1886 */ 1887 function get_page_template_slug( $post = null ) { 1888 $post = get_post( $post ); 1889 1890 if ( ! $post ) { 1891 return false; 1892 } 1893 1894 $template = get_post_meta( $post->ID, '_wp_page_template', true ); 1895 1896 if ( ! $template || 'default' === $template ) { 1897 return ''; 1898 } 1899 1900 return $template; 1901 } 1902 1903 /** 1904 * Retrieves formatted date timestamp of a revision (linked to that revisions's page). 1905 * 1906 * @since 2.6.0 1907 * 1908 * @param int|WP_Post $revision Revision ID or revision object. 1909 * @param bool $link Optional. Whether to link to revision's page. Default true. 1910 * @return string|false i18n formatted datetimestamp or localized 'Current Revision'. 1911 */ 1912 function wp_post_revision_title( $revision, $link = true ) { 1913 $revision = get_post( $revision ); 1914 1915 if ( ! $revision ) { 1916 return $revision; 1917 } 1918 1919 if ( ! in_array( $revision->post_type, array( 'post', 'page', 'revision' ), true ) ) { 1920 return false; 1921 } 1922 1923 /* translators: Revision date format, see https://www.php.net/manual/datetime.format.php */ 1924 $datef = _x( 'F j, Y @ H:i:s', 'revision date format' ); 1925 /* translators: %s: Revision date. */ 1926 $autosavef = __( '%s [Autosave]' ); 1927 /* translators: %s: Revision date. */ 1928 $currentf = __( '%s [Current Revision]' ); 1929 1930 $date = date_i18n( $datef, strtotime( $revision->post_modified ) ); 1931 $edit_link = get_edit_post_link( $revision->ID ); 1932 if ( $link && current_user_can( 'edit_post', $revision->ID ) && $edit_link ) { 1933 $date = "<a href='$edit_link'>$date</a>"; 1934 } 1935 1936 if ( ! wp_is_post_revision( $revision ) ) { 1937 $date = sprintf( $currentf, $date ); 1938 } elseif ( wp_is_post_autosave( $revision ) ) { 1939 $date = sprintf( $autosavef, $date ); 1940 } 1941 1942 return $date; 1943 } 1944 1945 /** 1946 * Retrieves formatted date timestamp of a revision (linked to that revisions's page). 1947 * 1948 * @since 3.6.0 1949 * 1950 * @param int|WP_Post $revision Revision ID or revision object. 1951 * @param bool $link Optional. Whether to link to revision's page. Default true. 1952 * @return string|false gravatar, user, i18n formatted datetimestamp or localized 'Current Revision'. 1953 */ 1954 function wp_post_revision_title_expanded( $revision, $link = true ) { 1955 $revision = get_post( $revision ); 1956 1957 if ( ! $revision ) { 1958 return $revision; 1959 } 1960 1961 if ( ! in_array( $revision->post_type, array( 'post', 'page', 'revision' ), true ) ) { 1962 return false; 1963 } 1964 1965 $author = get_the_author_meta( 'display_name', $revision->post_author ); 1966 /* translators: Revision date format, see https://www.php.net/manual/datetime.format.php */ 1967 $datef = _x( 'F j, Y @ H:i:s', 'revision date format' ); 1968 1969 $gravatar = get_avatar( $revision->post_author, 24 ); 1970 1971 $date = date_i18n( $datef, strtotime( $revision->post_modified ) ); 1972 $edit_link = get_edit_post_link( $revision->ID ); 1973 if ( $link && current_user_can( 'edit_post', $revision->ID ) && $edit_link ) { 1974 $date = "<a href='$edit_link'>$date</a>"; 1975 } 1976 1977 $revision_date_author = sprintf( 1978 /* translators: Post revision title. 1: Author avatar, 2: Author name, 3: Time ago, 4: Date. */ 1979 __( '%1$s %2$s, %3$s ago (%4$s)' ), 1980 $gravatar, 1981 $author, 1982 human_time_diff( strtotime( $revision->post_modified_gmt ) ), 1983 $date 1984 ); 1985 1986 /* translators: %s: Revision date with author avatar. */ 1987 $autosavef = __( '%s [Autosave]' ); 1988 /* translators: %s: Revision date with author avatar. */ 1989 $currentf = __( '%s [Current Revision]' ); 1990 1991 if ( ! wp_is_post_revision( $revision ) ) { 1992 $revision_date_author = sprintf( $currentf, $revision_date_author ); 1993 } elseif ( wp_is_post_autosave( $revision ) ) { 1994 $revision_date_author = sprintf( $autosavef, $revision_date_author ); 1995 } 1996 1997 /** 1998 * Filters the formatted author and date for a revision. 1999 * 2000 * @since 4.4.0 2001 * 2002 * @param string $revision_date_author The formatted string. 2003 * @param WP_Post $revision The revision object. 2004 * @param bool $link Whether to link to the revisions page, as passed into 2005 * wp_post_revision_title_expanded(). 2006 */ 2007 return apply_filters( 'wp_post_revision_title_expanded', $revision_date_author, $revision, $link ); 2008 } 2009 2010 /** 2011 * Displays a list of a post's revisions. 2012 * 2013 * Can output either a UL with edit links or a TABLE with diff interface, and 2014 * restore action links. 2015 * 2016 * @since 2.6.0 2017 * 2018 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 2019 * @param string $type 'all' (default), 'revision' or 'autosave' 2020 */ 2021 function wp_list_post_revisions( $post = 0, $type = 'all' ) { 2022 $post = get_post( $post ); 2023 2024 if ( ! $post ) { 2025 return; 2026 } 2027 2028 // $args array with (parent, format, right, left, type) deprecated since 3.6. 2029 if ( is_array( $type ) ) { 2030 $type = ! empty( $type['type'] ) ? $type['type'] : $type; 2031 _deprecated_argument( __FUNCTION__, '3.6.0' ); 2032 } 2033 2034 $revisions = wp_get_post_revisions( $post->ID ); 2035 2036 if ( ! $revisions ) { 2037 return; 2038 } 2039 2040 $rows = ''; 2041 foreach ( $revisions as $revision ) { 2042 if ( ! current_user_can( 'read_post', $revision->ID ) ) { 2043 continue; 2044 } 2045 2046 $is_autosave = wp_is_post_autosave( $revision ); 2047 if ( ( 'revision' === $type && $is_autosave ) || ( 'autosave' === $type && ! $is_autosave ) ) { 2048 continue; 2049 } 2050 2051 $rows .= "\t<li>" . wp_post_revision_title_expanded( $revision ) . "</li>\n"; 2052 } 2053 2054 echo "<div class='hide-if-js'><p>" . __( 'JavaScript must be enabled to use this feature.' ) . "</p></div>\n"; 2055 2056 echo "<ul class='post-revisions hide-if-no-js'>\n"; 2057 echo $rows; 2058 echo '</ul>'; 2059 } 2060 2061 /** 2062 * Retrieves the parent post object for the given post. 2063 * 2064 * @since 5.7.0 2065 * 2066 * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global $post. 2067 * @return WP_Post|null Parent post object, or null if there isn't one. 2068 */ 2069 function get_post_parent( $post = null ) { 2070 $wp_post = get_post( $post ); 2071 return ! empty( $wp_post->post_parent ) ? get_post( $wp_post->post_parent ) : null; 2072 } 2073 2074 /** 2075 * Returns whether the given post has a parent post. 2076 * 2077 * @since 5.7.0 2078 * 2079 * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global $post. 2080 * @return bool Whether the post has a parent post. 2081 */ 2082 function has_post_parent( $post = null ) { 2083 return (bool) get_post_parent( $post ); 2084 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sun Mar 9 08:20:01 2025 | Cross-referenced by PHPXref |