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