| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Post revision functions. 4 * 5 * @package WordPress 6 * @subpackage Post_Revisions 7 */ 8 9 /** 10 * Determines which fields of posts are to be saved in revisions. 11 * 12 * @since 2.6.0 13 * @since 4.5.0 A `WP_Post` object can now be passed to the `$post` parameter. 14 * @since 4.5.0 The optional `$autosave` parameter was deprecated and renamed to `$deprecated`. 15 * @access private 16 * 17 * @param array|WP_Post $post Optional. A post array or a WP_Post object being processed 18 * for insertion as a post revision. Default empty array. 19 * @param bool $deprecated Not used. 20 * @return string[] Array of fields that can be versioned. 21 * 22 * @phpstan-param false $deprecated 23 */ 24 function _wp_post_revision_fields( $post = array(), $deprecated = false ) { 25 static $fields = null; 26 27 if ( ! is_array( $post ) ) { 28 $post = get_post( $post, ARRAY_A ); 29 } 30 31 if ( is_null( $fields ) ) { 32 // Allow these to be versioned. 33 $fields = array( 34 'post_title' => __( 'Title' ), 35 'post_content' => __( 'Content' ), 36 'post_excerpt' => __( 'Excerpt' ), 37 ); 38 } 39 40 /** 41 * Filters the list of fields saved in post revisions. 42 * 43 * Included by default: 'post_title', 'post_content' and 'post_excerpt'. 44 * 45 * Disallowed fields: 'ID', 'post_name', 'post_parent', 'post_date', 46 * 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 47 * and 'post_author'. 48 * 49 * @since 2.6.0 50 * @since 4.5.0 The `$post` parameter was added. 51 * 52 * @param string[] $fields List of fields to revision. Contains 'post_title', 53 * 'post_content', and 'post_excerpt' by default. 54 * @param array $post A post array being processed for insertion as a post revision. 55 */ 56 $fields = apply_filters( '_wp_post_revision_fields', $fields, $post ); 57 58 // WP uses these internally either in versioning or elsewhere - they cannot be versioned. 59 foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect ) { 60 unset( $fields[ $protect ] ); 61 } 62 63 return $fields; 64 } 65 66 /** 67 * Returns a post array ready to be inserted into the posts table as a post revision. 68 * 69 * @since 4.5.0 70 * @access private 71 * 72 * @param array|WP_Post $post Optional. A post array or a WP_Post object to be processed 73 * for insertion as a post revision. Default empty array. 74 * @param bool $autosave Optional. Is the revision an autosave? Default false. 75 * @return array Post array ready to be inserted as a post revision. 76 */ 77 function _wp_post_revision_data( $post = array(), $autosave = false ) { 78 if ( ! is_array( $post ) ) { 79 $post = get_post( $post, ARRAY_A ); 80 } 81 82 $fields = _wp_post_revision_fields( $post ); 83 84 $revision_data = array(); 85 86 foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field ) { 87 $revision_data[ $field ] = $post[ $field ]; 88 } 89 90 $revision_data['post_parent'] = $post['ID']; 91 $revision_data['post_status'] = 'inherit'; 92 $revision_data['post_type'] = 'revision'; 93 $revision_data['post_name'] = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version. 94 $revision_data['post_date'] = $post['post_modified'] ?? ''; 95 $revision_data['post_date_gmt'] = $post['post_modified_gmt'] ?? ''; 96 97 return $revision_data; 98 } 99 100 /** 101 * Saves revisions for a post after all changes have been made. 102 * 103 * @since 6.4.0 104 * 105 * @param int $post_id The post id that was inserted. 106 * @param WP_Post $post The post object that was inserted. 107 * @param bool $update Whether this insert is updating an existing post. 108 */ 109 function wp_save_post_revision_on_insert( $post_id, $post, $update ) { 110 if ( ! $update ) { 111 return; 112 } 113 114 if ( ! has_action( 'post_updated', 'wp_save_post_revision' ) ) { 115 return; 116 } 117 118 wp_save_post_revision( $post_id ); 119 } 120 121 /** 122 * Creates a revision for the current version of a post. 123 * 124 * Typically used immediately after a post update, as every update is a revision, 125 * and the most recent revision always matches the current post. 126 * 127 * @since 2.6.0 128 * 129 * @param int $post_id The ID of the post to save as a revision. 130 * @return int|WP_Error|null Null or 0 if error, new revision ID, if success. 131 */ 132 function wp_save_post_revision( $post_id ) { 133 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { 134 return null; 135 } 136 137 // Prevent saving post revisions if revisions should be saved on wp_after_insert_post. 138 if ( doing_action( 'post_updated' ) && has_action( 'wp_after_insert_post', 'wp_save_post_revision_on_insert' ) ) { 139 return null; 140 } 141 142 $post = get_post( $post_id ); 143 144 if ( ! $post ) { 145 return null; 146 } 147 148 if ( ! post_type_supports( $post->post_type, 'revisions' ) ) { 149 return null; 150 } 151 152 if ( 'auto-draft' === $post->post_status ) { 153 return null; 154 } 155 156 if ( ! wp_revisions_enabled( $post ) ) { 157 return null; 158 } 159 160 /* 161 * Compare the proposed update with the last stored revision verifying that 162 * they are different, unless a plugin tells us to always save regardless. 163 * If no previous revisions, save one. 164 */ 165 $revisions = wp_get_post_revisions( $post_id ); 166 if ( $revisions ) { 167 // Grab the latest revision, but not an autosave. 168 foreach ( $revisions as $revision ) { 169 if ( str_contains( $revision->post_name, "{$revision->post_parent}-revision" ) ) { 170 $latest_revision = $revision; 171 break; 172 } 173 } 174 175 /** 176 * Filters whether the post has changed since the latest revision. 177 * 178 * By default a revision is saved only if one of the revisioned fields has changed. 179 * This filter can override that so a revision is saved even if nothing has changed. 180 * 181 * @since 3.6.0 182 * 183 * @param bool $check_for_changes Whether to check for changes before saving a new revision. 184 * Default true. 185 * @param WP_Post $latest_revision The latest revision post object. 186 * @param WP_Post $post The post object. 187 */ 188 if ( isset( $latest_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', true, $latest_revision, $post ) ) { 189 $post_has_changed = false; 190 191 foreach ( array_keys( _wp_post_revision_fields( $post ) ) as $field ) { 192 if ( normalize_whitespace( maybe_serialize( $post->$field ) ) !== normalize_whitespace( maybe_serialize( $latest_revision->$field ) ) ) { 193 $post_has_changed = true; 194 break; 195 } 196 } 197 198 /** 199 * Filters whether a post has changed. 200 * 201 * By default a revision is saved only if one of the revisioned fields has changed. 202 * This filter allows for additional checks to determine if there were changes. 203 * 204 * @since 4.1.0 205 * 206 * @param bool $post_has_changed Whether the post has changed. 207 * @param WP_Post $latest_revision The latest revision post object. 208 * @param WP_Post $post The post object. 209 */ 210 $post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $latest_revision, $post ); 211 212 // Don't save revision if post unchanged. 213 if ( ! $post_has_changed ) { 214 return null; 215 } 216 } 217 } 218 219 $return = _wp_put_post_revision( $post ); 220 221 /* 222 * If a limit for the number of revisions to keep has been set, 223 * delete the oldest ones. 224 */ 225 $revisions_to_keep = wp_revisions_to_keep( $post ); 226 227 if ( $revisions_to_keep < 0 ) { 228 return $return; 229 } 230 231 $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) ); 232 233 /** 234 * Filters the revisions to be considered for deletion. 235 * 236 * @since 6.2.0 237 * 238 * @param WP_Post[] $revisions Array of revisions, or an empty array if none. 239 * @param int $post_id The ID of the post to save as a revision. 240 */ 241 $revisions = apply_filters( 242 'wp_save_post_revision_revisions_before_deletion', 243 $revisions, 244 $post_id 245 ); 246 247 $delete = count( $revisions ) - $revisions_to_keep; 248 249 if ( $delete < 1 ) { 250 return $return; 251 } 252 253 $revisions = array_slice( $revisions, 0, $delete ); 254 255 for ( $i = 0; isset( $revisions[ $i ] ); $i++ ) { 256 if ( str_contains( $revisions[ $i ]->post_name, 'autosave' ) ) { 257 continue; 258 } 259 260 wp_delete_post_revision( $revisions[ $i ]->ID ); 261 } 262 263 return $return; 264 } 265 266 /** 267 * Retrieves the autosaved data of the specified post. 268 * 269 * Returns a post object with the information that was autosaved for the specified post. 270 * If the optional $user_id is passed, returns the autosave for that user, otherwise 271 * returns the latest autosave. 272 * 273 * @since 2.6.0 274 * 275 * @param int $post_id The post ID. 276 * @param int $user_id Optional. The post author ID. Default 0. 277 * @return WP_Post|false The autosaved data or false on failure or when no autosave exists. 278 */ 279 function wp_get_post_autosave( $post_id, $user_id = 0 ) { 280 $args = array( 281 'post_type' => 'revision', 282 'post_status' => 'inherit', 283 'post_parent' => $post_id, 284 'name' => $post_id . '-autosave-v1', 285 'posts_per_page' => 1, 286 'orderby' => 'date', 287 'order' => 'DESC', 288 'fields' => 'ids', 289 'no_found_rows' => true, 290 ); 291 292 if ( 0 !== $user_id ) { 293 $args['author'] = $user_id; 294 } 295 296 $query = new WP_Query( $args ); 297 298 if ( ! $query->have_posts() ) { 299 return false; 300 } 301 302 return get_post( $query->posts[0] ); 303 } 304 305 /** 306 * Determines if the specified post is a revision. 307 * 308 * @since 2.6.0 309 * 310 * @param int|WP_Post $post Post ID or post object. 311 * @return int|false ID of revision's parent on success, false if not a revision. 312 * 313 * @phpstan-return ( $post is int<min, -1> ? false : false|int<0, max> ) 314 */ 315 function wp_is_post_revision( $post ) { 316 $post = wp_get_post_revision( $post ); 317 318 if ( ! $post ) { 319 return false; 320 } 321 322 return (int) $post->post_parent; 323 } 324 325 /** 326 * Determines if the specified post is an autosave. 327 * 328 * @since 2.6.0 329 * 330 * @param int|WP_Post $post Post ID or post object. 331 * @return int|false ID of autosave's parent on success, false if not a revision. 332 */ 333 function wp_is_post_autosave( $post ) { 334 $post = wp_get_post_revision( $post ); 335 336 if ( ! $post ) { 337 return false; 338 } 339 340 if ( str_contains( $post->post_name, "{$post->post_parent}-autosave" ) ) { 341 return (int) $post->post_parent; 342 } 343 344 return false; 345 } 346 347 /** 348 * Inserts post data into the posts table as a post revision. 349 * 350 * @since 2.6.0 351 * @access private 352 * 353 * @param int|WP_Post|array|null $post Post ID, post object OR post array. 354 * @param bool $autosave Optional. Whether the revision is an autosave or not. 355 * Default false. 356 * @return int|WP_Error WP_Error or 0 if error, new revision ID if success. 357 */ 358 function _wp_put_post_revision( $post = null, $autosave = false ) { 359 if ( is_object( $post ) ) { 360 $post = get_object_vars( $post ); 361 } elseif ( ! is_array( $post ) ) { 362 $post = get_post( $post, ARRAY_A ); 363 } 364 365 if ( ! $post || empty( $post['ID'] ) ) { 366 return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); 367 } 368 369 if ( isset( $post['post_type'] ) && 'revision' === $post['post_type'] ) { 370 return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) ); 371 } 372 373 $post = _wp_post_revision_data( $post, $autosave ); 374 $post = wp_slash( $post ); // Since data is from DB. 375 376 $revision_id = wp_insert_post( $post, true ); 377 if ( is_wp_error( $revision_id ) ) { 378 return $revision_id; 379 } 380 381 if ( $revision_id ) { 382 /** 383 * Fires once a revision has been saved. 384 * 385 * @since 2.6.0 386 * @since 6.4.0 The `$post_id` parameter was added. 387 * 388 * @param int $revision_id Post revision ID. 389 * @param int $post_id Post ID. 390 */ 391 do_action( '_wp_put_post_revision', $revision_id, $post['post_parent'] ); 392 } 393 394 return $revision_id; 395 } 396 397 398 /** 399 * Save the revisioned meta fields. 400 * 401 * @since 6.4.0 402 * 403 * @param int $revision_id The ID of the revision to save the meta to. 404 * @param int $post_id The ID of the post the revision is associated with. 405 */ 406 function wp_save_revisioned_meta_fields( $revision_id, $post_id ) { 407 $post_type = get_post_type( $post_id ); 408 if ( ! $post_type ) { 409 return; 410 } 411 412 foreach ( wp_post_revision_meta_keys( $post_type ) as $meta_key ) { 413 if ( metadata_exists( 'post', $post_id, $meta_key ) ) { 414 _wp_copy_post_meta( $post_id, $revision_id, $meta_key ); 415 } 416 } 417 } 418 419 /** 420 * Gets a post revision. 421 * 422 * @since 2.6.0 423 * 424 * @param int|WP_Post $post Post ID or post object. 425 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which 426 * correspond to a WP_Post object, an associative array, or a numeric array, 427 * respectively. Default OBJECT. 428 * @param string $filter Optional sanitization filter. See sanitize_post(). Default 'raw'. 429 * @return WP_Post|array|null WP_Post (or array) on success, or null on failure. 430 * 431 * @phpstan-param int|WP_Post $post 432 * @phpstan-param 'OBJECT'|'ARRAY_A'|'ARRAY_N' $output 433 * @phpstan-param 'raw'|'edit'|'db'|'display' $filter 434 * @phpstan-return ( 435 * $output is 'ARRAY_A' ? non-empty-array<string, mixed>|null : ( 436 * $output is 'ARRAY_N' ? non-empty-array<int, mixed>|null : ( 437 * WP_Post|null 438 * ) 439 * ) 440 * ) 441 */ 442 function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) { 443 $revision = get_post( $post, OBJECT, $filter ); 444 445 if ( ! $revision ) { 446 return $revision; 447 } 448 449 if ( 'revision' !== $revision->post_type ) { 450 return null; 451 } 452 453 if ( OBJECT === $output ) { 454 return $revision; 455 } elseif ( ARRAY_A === $output ) { 456 /** @var non-empty-array<string, mixed> $_revision */ 457 $_revision = get_object_vars( $revision ); 458 return $_revision; 459 } elseif ( ARRAY_N === $output ) { 460 /** @var non-empty-array<string, mixed> $vars */ 461 $vars = get_object_vars( $revision ); 462 $_revision = array_values( $vars ); 463 return $_revision; 464 } 465 466 return $revision; 467 } 468 469 /** 470 * Restores a post to the specified revision. 471 * 472 * Can restore a past revision using all fields of the post revision, or only selected fields. 473 * 474 * @since 2.6.0 475 * 476 * @param int|WP_Post $revision Revision ID or revision object. 477 * @param array $fields Optional. What fields to restore from. Defaults to all. 478 * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success. 479 */ 480 function wp_restore_post_revision( $revision, $fields = null ) { 481 $revision = wp_get_post_revision( $revision, ARRAY_A ); 482 483 if ( ! $revision ) { 484 return $revision; 485 } 486 487 if ( ! is_array( $fields ) ) { 488 $fields = array_keys( _wp_post_revision_fields( $revision ) ); 489 } 490 491 $update = array(); 492 foreach ( array_intersect( array_keys( $revision ), $fields ) as $field ) { 493 $update[ $field ] = $revision[ $field ]; 494 } 495 496 if ( ! $update ) { 497 return false; 498 } 499 500 $update['ID'] = $revision['post_parent']; 501 502 $update = wp_slash( $update ); // Since data is from DB. 503 504 $post_id = wp_update_post( $update ); 505 506 if ( ! $post_id || is_wp_error( $post_id ) ) { 507 return $post_id; 508 } 509 510 // Update last edit user. 511 update_post_meta( $post_id, '_edit_last', get_current_user_id() ); 512 513 /** 514 * Fires after a post revision has been restored. 515 * 516 * @since 2.6.0 517 * 518 * @param int $post_id Post ID. 519 * @param int $revision_id Post revision ID. 520 */ 521 do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] ); 522 523 return $post_id; 524 } 525 526 /** 527 * Restore the revisioned meta values for a post. 528 * 529 * @since 6.4.0 530 * 531 * @param int $post_id The ID of the post to restore the meta to. 532 * @param int $revision_id The ID of the revision to restore the meta from. 533 */ 534 function wp_restore_post_revision_meta( $post_id, $revision_id ) { 535 $post_type = get_post_type( $post_id ); 536 if ( ! $post_type ) { 537 return; 538 } 539 540 // Restore revisioned meta fields. 541 foreach ( wp_post_revision_meta_keys( $post_type ) as $meta_key ) { 542 543 // Clear any existing meta. 544 delete_post_meta( $post_id, $meta_key ); 545 546 _wp_copy_post_meta( $revision_id, $post_id, $meta_key ); 547 } 548 } 549 550 /** 551 * Copy post meta for the given key from one post to another. 552 * 553 * @since 6.4.0 554 * 555 * @param int $source_post_id Post ID to copy meta value(s) from. 556 * @param int $target_post_id Post ID to copy meta value(s) to. 557 * @param string $meta_key Meta key to copy. 558 */ 559 function _wp_copy_post_meta( $source_post_id, $target_post_id, $meta_key ) { 560 561 foreach ( get_post_meta( $source_post_id, $meta_key ) as $meta_value ) { 562 /** 563 * We use add_metadata() function vs add_post_meta() here 564 * to allow for a revision post target OR regular post. 565 */ 566 add_metadata( 'post', $target_post_id, $meta_key, wp_slash( $meta_value ) ); 567 } 568 } 569 570 /** 571 * Determine which post meta fields should be revisioned. 572 * 573 * @since 6.4.0 574 * 575 * @param string $post_type The post type being revisioned. 576 * @return array An array of meta keys to be revisioned. 577 */ 578 function wp_post_revision_meta_keys( $post_type ) { 579 $registered_meta = array_merge( 580 get_registered_meta_keys( 'post' ), 581 get_registered_meta_keys( 'post', $post_type ) 582 ); 583 584 $wp_revisioned_meta_keys = array(); 585 586 foreach ( $registered_meta as $name => $args ) { 587 if ( $args['revisions_enabled'] ) { 588 $wp_revisioned_meta_keys[ $name ] = true; 589 } 590 } 591 592 $wp_revisioned_meta_keys = array_keys( $wp_revisioned_meta_keys ); 593 594 /** 595 * Filter the list of post meta keys to be revisioned. 596 * 597 * @since 6.4.0 598 * 599 * @param array $keys An array of meta fields to be revisioned. 600 * @param string $post_type The post type being revisioned. 601 */ 602 return apply_filters( 'wp_post_revision_meta_keys', $wp_revisioned_meta_keys, $post_type ); 603 } 604 605 /** 606 * Check whether revisioned post meta fields have changed. 607 * 608 * @since 6.4.0 609 * 610 * @param bool $post_has_changed Whether the post has changed. 611 * @param WP_Post $last_revision The last revision post object. 612 * @param WP_Post $post The post object. 613 * @return bool Whether the post has changed. 614 */ 615 function wp_check_revisioned_meta_fields_have_changed( $post_has_changed, WP_Post $last_revision, WP_Post $post ) { 616 foreach ( wp_post_revision_meta_keys( $post->post_type ) as $meta_key ) { 617 if ( get_post_meta( $post->ID, $meta_key ) !== get_post_meta( $last_revision->ID, $meta_key ) ) { 618 $post_has_changed = true; 619 break; 620 } 621 } 622 return $post_has_changed; 623 } 624 625 /** 626 * Deletes a revision. 627 * 628 * Deletes the row from the posts table corresponding to the specified revision. 629 * 630 * @since 2.6.0 631 * 632 * @param int|WP_Post $revision Revision ID or revision object. 633 * @return WP_Post|false|null Null or false if error, deleted post object if success. 634 */ 635 function wp_delete_post_revision( $revision ) { 636 $revision = wp_get_post_revision( $revision ); 637 638 if ( ! $revision ) { 639 return $revision; 640 } 641 642 $delete = wp_delete_post( $revision->ID ); 643 644 if ( $delete ) { 645 /** 646 * Fires once a post revision has been deleted. 647 * 648 * @since 2.6.0 649 * 650 * @param int $revision_id Post revision ID. 651 * @param WP_Post $revision Post revision object. 652 */ 653 do_action( 'wp_delete_post_revision', $revision->ID, $revision ); 654 } 655 656 return $delete; 657 } 658 659 /** 660 * Returns all revisions of specified post. 661 * 662 * @since 2.6.0 663 * 664 * @see get_children() 665 * 666 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. 667 * @param array|null $args Optional. Arguments for retrieving post revisions. Default null. 668 * @return WP_Post[]|int[] Array of revision objects or IDs, or an empty array if none. 669 */ 670 function wp_get_post_revisions( $post = 0, $args = null ) { 671 $post = get_post( $post ); 672 673 if ( ! $post || empty( $post->ID ) ) { 674 return array(); 675 } 676 677 $defaults = array( 678 'order' => 'DESC', 679 'orderby' => 'date ID', 680 'check_enabled' => true, 681 ); 682 $args = wp_parse_args( $args, $defaults ); 683 684 if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) ) { 685 return array(); 686 } 687 688 $args = array_merge( 689 $args, 690 array( 691 'post_parent' => $post->ID, 692 'post_type' => 'revision', 693 'post_status' => 'inherit', 694 ) 695 ); 696 697 $revisions = get_children( $args ); 698 699 if ( ! $revisions ) { 700 return array(); 701 } 702 703 return $revisions; 704 } 705 706 /** 707 * Returns the latest revision ID and count of revisions for a post. 708 * 709 * @since 6.1.0 710 * 711 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 712 * @return array|WP_Error { 713 * Returns associative array with latest revision ID and total count, 714 * or a WP_Error if the post does not exist or revisions are not enabled. 715 * 716 * @type int $latest_id The latest revision post ID or 0 if no revisions exist. 717 * @type int $count The total count of revisions for the given post. 718 * } 719 * 720 * @phpstan-return array{ latest_id: int, count: int<0, max> }|WP_Error 721 */ 722 function wp_get_latest_revision_id_and_total_count( $post = 0 ) { 723 $post = get_post( $post ); 724 725 if ( ! $post ) { 726 return new WP_Error( 'invalid_post', __( 'Invalid post.' ) ); 727 } 728 729 if ( ! wp_revisions_enabled( $post ) ) { 730 return new WP_Error( 'revisions_not_enabled', __( 'Revisions not enabled.' ) ); 731 } 732 733 $args = array( 734 'post_parent' => $post->ID, 735 'fields' => 'ids', 736 'post_type' => 'revision', 737 'post_status' => 'inherit', 738 'order' => 'DESC', 739 'orderby' => 'date ID', 740 'posts_per_page' => 1, 741 'ignore_sticky_posts' => true, 742 ); 743 744 $revision_query = new WP_Query(); 745 $revisions = $revision_query->query( $args ); 746 747 if ( ! $revisions ) { 748 return array( 749 'latest_id' => 0, 750 'count' => 0, 751 ); 752 } 753 754 return array( 755 'latest_id' => $revisions[0], 756 'count' => $revision_query->found_posts, 757 ); 758 } 759 760 /** 761 * Returns the url for viewing and potentially restoring revisions of a given post. 762 * 763 * @since 5.9.0 764 * 765 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. 766 * @return string|null The URL for editing revisions on the given post, otherwise null. 767 */ 768 function wp_get_post_revisions_url( $post = 0 ) { 769 $post = get_post( $post ); 770 771 if ( ! $post instanceof WP_Post ) { 772 return null; 773 } 774 775 // If the post is a revision, return early. 776 if ( 'revision' === $post->post_type ) { 777 return get_edit_post_link( $post ); 778 } 779 780 if ( ! wp_revisions_enabled( $post ) ) { 781 return null; 782 } 783 784 $revisions = wp_get_latest_revision_id_and_total_count( $post->ID ); 785 786 if ( is_wp_error( $revisions ) || 0 === $revisions['count'] ) { 787 return null; 788 } 789 790 return get_edit_post_link( $revisions['latest_id'] ); 791 } 792 793 /** 794 * Determines whether revisions are enabled for a given post. 795 * 796 * @since 3.6.0 797 * 798 * @param WP_Post $post The post object. 799 * @return bool True if number of revisions to keep isn't zero, false otherwise. 800 */ 801 function wp_revisions_enabled( $post ) { 802 return wp_revisions_to_keep( $post ) !== 0; 803 } 804 805 /** 806 * Determines how many revisions to retain for a given post. 807 * 808 * By default, an infinite number of revisions are kept. 809 * 810 * The constant WP_POST_REVISIONS can be set in wp-config to specify the limit 811 * of revisions to keep. 812 * 813 * @since 3.6.0 814 * 815 * @param WP_Post $post The post object. 816 * @return int The number of revisions to keep. 817 */ 818 function wp_revisions_to_keep( $post ) { 819 $num = WP_POST_REVISIONS; 820 821 if ( true === $num ) { 822 $num = -1; 823 } else { 824 $num = (int) $num; 825 } 826 827 if ( ! post_type_supports( $post->post_type, 'revisions' ) ) { 828 $num = 0; 829 } 830 831 /** 832 * Filters the number of revisions to save for the given post. 833 * 834 * Overrides the value of WP_POST_REVISIONS. 835 * 836 * @since 3.6.0 837 * 838 * @param int $num Number of revisions to store. 839 * @param WP_Post $post Post object. 840 */ 841 $num = apply_filters( 'wp_revisions_to_keep', $num, $post ); 842 843 /** 844 * Filters the number of revisions to save for the given post by its post type. 845 * 846 * Overrides both the value of WP_POST_REVISIONS and the {@see 'wp_revisions_to_keep'} filter. 847 * 848 * The dynamic portion of the hook name, `$post->post_type`, refers to 849 * the post type slug. 850 * 851 * Possible hook names include: 852 * 853 * - `wp_post_revisions_to_keep` 854 * - `wp_page_revisions_to_keep` 855 * 856 * @since 5.8.0 857 * 858 * @param int $num Number of revisions to store. 859 * @param WP_Post $post Post object. 860 */ 861 $num = apply_filters( "wp_{$post->post_type}_revisions_to_keep", $num, $post ); 862 863 return (int) $num; 864 } 865 866 /** 867 * Sets up the post object for preview based on the post autosave. 868 * 869 * @since 2.7.0 870 * @access private 871 * 872 * @param WP_Post $post 873 * @return WP_Post 874 */ 875 function _set_preview( $post ) { 876 $preview = wp_get_post_autosave( $post->ID ); 877 878 if ( is_object( $preview ) ) { 879 $preview = sanitize_post( $preview ); 880 881 $post->post_content = $preview->post_content; 882 $post->post_title = $preview->post_title; 883 $post->post_excerpt = $preview->post_excerpt; 884 } 885 886 add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 ); 887 add_filter( 'get_post_metadata', '_wp_preview_post_thumbnail_filter', 10, 3 ); 888 add_filter( 'get_post_metadata', '_wp_preview_meta_filter', 10, 4 ); 889 890 return $post; 891 } 892 893 /** 894 * Filters the latest content for preview from the post autosave. 895 * 896 * @since 2.7.0 897 * @access private 898 */ 899 function _show_post_preview() { 900 if ( isset( $_GET['preview_id'] ) && isset( $_GET['preview_nonce'] ) ) { 901 $id = (int) $_GET['preview_id']; 902 903 if ( false === wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) ) { 904 wp_die( __( 'Sorry, you are not allowed to preview drafts.' ), 403 ); 905 } 906 907 add_filter( 'the_preview', '_set_preview' ); 908 } 909 } 910 911 /** 912 * Filters terms lookup to set the post format. 913 * 914 * @since 3.6.0 915 * @access private 916 * 917 * @param array $terms 918 * @param int $post_id 919 * @param string $taxonomy 920 * @return array 921 */ 922 function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) { 923 $post = get_post(); 924 925 if ( ! $post ) { 926 return $terms; 927 } 928 929 if ( empty( $_REQUEST['post_format'] ) || $post->ID !== $post_id 930 || 'post_format' !== $taxonomy || 'revision' === $post->post_type 931 ) { 932 return $terms; 933 } 934 935 if ( 'standard' === $_REQUEST['post_format'] ) { 936 $terms = array(); 937 } else { 938 $term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' ); 939 940 if ( $term ) { 941 $terms = array( $term ); // Can only have one post format. 942 } 943 } 944 945 return $terms; 946 } 947 948 /** 949 * Filters post thumbnail lookup to set the post thumbnail. 950 * 951 * @since 4.6.0 952 * @access private 953 * 954 * @param null|array|string $value The value to return - a single metadata value, or an array of values. 955 * @param int $post_id Post ID. 956 * @param string $meta_key Meta key. 957 * @return null|array The default return value or the post thumbnail meta array. 958 */ 959 function _wp_preview_post_thumbnail_filter( $value, $post_id, $meta_key ) { 960 $post = get_post(); 961 962 if ( ! $post ) { 963 return $value; 964 } 965 966 if ( empty( $_REQUEST['_thumbnail_id'] ) || empty( $_REQUEST['preview_id'] ) 967 || $post->ID !== $post_id || $post_id !== (int) $_REQUEST['preview_id'] 968 || '_thumbnail_id' !== $meta_key || 'revision' === $post->post_type 969 ) { 970 return $value; 971 } 972 973 $thumbnail_id = (int) $_REQUEST['_thumbnail_id']; 974 975 if ( $thumbnail_id <= 0 ) { 976 return ''; 977 } 978 979 return (string) $thumbnail_id; 980 } 981 982 /** 983 * Gets the post revision version. 984 * 985 * @since 3.6.0 986 * @access private 987 * 988 * @param WP_Post $revision 989 * @return int|false 990 */ 991 function _wp_get_post_revision_version( $revision ) { 992 if ( is_object( $revision ) ) { 993 $revision = get_object_vars( $revision ); 994 } elseif ( ! is_array( $revision ) ) { 995 return false; 996 } 997 998 if ( preg_match( '/^\d+-(?:autosave|revision)-v(\d+)$/', $revision['post_name'], $matches ) ) { 999 return (int) $matches[1]; 1000 } 1001 1002 return 0; 1003 } 1004 1005 /** 1006 * Upgrades the revisions author, adds the current post as a revision and sets the revisions version to 1. 1007 * 1008 * @since 3.6.0 1009 * @access private 1010 * 1011 * @global wpdb $wpdb WordPress database abstraction object. 1012 * 1013 * @param WP_Post $post Post object. 1014 * @param array $revisions Current revisions of the post. 1015 * @return bool true if the revisions were upgraded, false if problems. 1016 */ 1017 function _wp_upgrade_revisions_of_post( $post, $revisions ) { 1018 global $wpdb; 1019 1020 // Add post option exclusively. 1021 $lock = "revision-upgrade-{$post->ID}"; 1022 $now = time(); 1023 $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'off') /* LOCK */", $lock, $now ) ); 1024 1025 if ( ! $result ) { 1026 // If we couldn't get a lock, see how old the previous lock is. 1027 $locked = get_option( $lock ); 1028 1029 if ( ! $locked ) { 1030 /* 1031 * Can't write to the lock, and can't read the lock. 1032 * Something broken has happened. 1033 */ 1034 return false; 1035 } 1036 1037 if ( $locked > $now - HOUR_IN_SECONDS ) { 1038 // Lock is not too old: some other process may be upgrading this post. Bail. 1039 return false; 1040 } 1041 1042 // Lock is too old - update it (below) and continue. 1043 } 1044 1045 // If we could get a lock, re-"add" the option to fire all the correct filters. 1046 update_option( $lock, $now ); 1047 1048 reset( $revisions ); 1049 $add_last = true; 1050 1051 do { 1052 $this_revision = current( $revisions ); 1053 $prev_revision = next( $revisions ); 1054 1055 $this_revision_version = _wp_get_post_revision_version( $this_revision ); 1056 1057 // Something terrible happened. 1058 if ( false === $this_revision_version ) { 1059 continue; 1060 } 1061 1062 /* 1063 * 1 is the latest revision version, so we're already up to date. 1064 * No need to add a copy of the post as latest revision. 1065 */ 1066 if ( 0 < $this_revision_version ) { 1067 $add_last = false; 1068 continue; 1069 } 1070 1071 // Always update the revision version. 1072 $update = array( 1073 'post_name' => preg_replace( '/^(\d+-(?:autosave|revision))[\d-]*$/', '$1-v1', $this_revision->post_name ), 1074 ); 1075 1076 /* 1077 * If this revision is the oldest revision of the post, i.e. no $prev_revision, 1078 * the correct post_author is probably $post->post_author, but that's only a good guess. 1079 * Update the revision version only and Leave the author as-is. 1080 */ 1081 if ( $prev_revision ) { 1082 $prev_revision_version = _wp_get_post_revision_version( $prev_revision ); 1083 1084 // If the previous revision is already up to date, it no longer has the information we need :( 1085 if ( $prev_revision_version < 1 ) { 1086 $update['post_author'] = $prev_revision->post_author; 1087 } 1088 } 1089 1090 // Upgrade this revision. 1091 $result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) ); 1092 1093 if ( $result ) { 1094 wp_cache_delete( $this_revision->ID, 'posts' ); 1095 } 1096 } while ( $prev_revision ); 1097 1098 delete_option( $lock ); 1099 1100 // Add a copy of the post as latest revision. 1101 if ( $add_last ) { 1102 wp_save_post_revision( $post->ID ); 1103 } 1104 1105 return true; 1106 } 1107 1108 /** 1109 * Filters preview post meta retrieval to get values from the autosave. 1110 * 1111 * Filters revisioned meta keys only. 1112 * 1113 * @since 6.4.0 1114 * 1115 * @param mixed $value Meta value to filter. 1116 * @param int $object_id Object ID. 1117 * @param string $meta_key Meta key to filter a value for. 1118 * @param bool $single Whether to return a single value. 1119 * @return mixed Original meta value if the meta key isn't revisioned, the object doesn't exist, 1120 * the post type is a revision or the post ID doesn't match the object ID. 1121 * Otherwise, the revisioned meta value is returned for the preview. 1122 */ 1123 function _wp_preview_meta_filter( $value, $object_id, $meta_key, $single ) { 1124 $post = get_post(); 1125 1126 if ( empty( $post ) 1127 || $post->ID !== $object_id 1128 || ! in_array( $meta_key, wp_post_revision_meta_keys( $post->post_type ), true ) 1129 || 'revision' === $post->post_type 1130 ) { 1131 return $value; 1132 } 1133 1134 $preview = wp_get_post_autosave( $post->ID ); 1135 1136 if ( false === $preview ) { 1137 return $value; 1138 } 1139 1140 return get_post_meta( $preview->ID, $meta_key, $single ); 1141 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sun Nov 22 08:20:01 2026 | Cross-referenced by PHPXref |