Changeset 42343 for trunk/src/wp-includes/revision.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/revision.php
r42228 r42343 32 32 // Allow these to be versioned 33 33 $fields = array( 34 'post_title' => __( 'Title' ),34 'post_title' => __( 'Title' ), 35 35 'post_content' => __( 'Content' ), 36 36 'post_excerpt' => __( 'Excerpt' ), … … 60 60 unset( $fields[ $protect ] ); 61 61 } 62 63 62 64 63 return $fields; … … 111 110 */ 112 111 function wp_save_post_revision( $post_id ) { 113 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 112 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { 114 113 return; 115 116 if ( ! $post = get_post( $post_id ) ) 114 } 115 116 if ( ! $post = get_post( $post_id ) ) { 117 117 return; 118 119 if ( ! post_type_supports( $post->post_type, 'revisions' ) ) 118 } 119 120 if ( ! post_type_supports( $post->post_type, 'revisions' ) ) { 120 121 return; 121 122 if ( 'auto-draft' == $post->post_status ) 122 } 123 124 if ( 'auto-draft' == $post->post_status ) { 123 125 return; 124 125 if ( ! wp_revisions_enabled( $post ) ) 126 } 127 128 if ( ! wp_revisions_enabled( $post ) ) { 126 129 return; 130 } 127 131 128 132 // Compare the proposed update with the last stored revision verifying that … … 150 154 * @param WP_Post $last_revision The last revision post object. 151 155 * @param WP_Post $post The post object. 152 *153 156 */ 154 157 if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', $check_for_changes = true, $last_revision, $post ) ) { … … 173 176 * @param WP_Post $last_revision The last revision post object. 174 177 * @param WP_Post $post The post object. 175 *176 178 */ 177 179 $post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $last_revision, $post ); … … 190 192 $revisions_to_keep = wp_revisions_to_keep( $post ); 191 193 192 if ( $revisions_to_keep < 0 ) 194 if ( $revisions_to_keep < 0 ) { 193 195 return $return; 196 } 194 197 195 198 $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) ); 196 199 197 $delete = count( $revisions) - $revisions_to_keep;198 199 if ( $delete < 1 ) 200 $delete = count( $revisions ) - $revisions_to_keep; 201 202 if ( $delete < 1 ) { 200 203 return $return; 204 } 201 205 202 206 $revisions = array_slice( $revisions, 0, $delete ); 203 207 204 for ( $i = 0; isset( $revisions[ $i] ); $i++ ) {205 if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) ) 208 for ( $i = 0; isset( $revisions[ $i ] ); $i++ ) { 209 if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) ) { 206 210 continue; 211 } 207 212 208 213 wp_delete_post_revision( $revisions[ $i ]->ID ); … … 230 235 foreach ( $revisions as $revision ) { 231 236 if ( false !== strpos( $revision->post_name, "{$post_id}-autosave" ) ) { 232 if ( $user_id && $user_id != $revision->post_author ) 237 if ( $user_id && $user_id != $revision->post_author ) { 233 238 continue; 239 } 234 240 235 241 return $revision; … … 249 255 */ 250 256 function wp_is_post_revision( $post ) { 251 if ( ! $post = wp_get_post_revision( $post ) )257 if ( ! $post = wp_get_post_revision( $post ) ) { 252 258 return false; 259 } 253 260 254 261 return (int) $post->post_parent; … … 264 271 */ 265 272 function wp_is_post_autosave( $post ) { 266 if ( ! $post = wp_get_post_revision( $post ) )273 if ( ! $post = wp_get_post_revision( $post ) ) { 267 274 return false; 268 269 if ( false !== strpos( $post->post_name, "{$post->post_parent}-autosave" ) ) 275 } 276 277 if ( false !== strpos( $post->post_name, "{$post->post_parent}-autosave" ) ) { 270 278 return (int) $post->post_parent; 279 } 271 280 272 281 return false; … … 284 293 */ 285 294 function _wp_put_post_revision( $post = null, $autosave = false ) { 286 if ( is_object( $post) )295 if ( is_object( $post ) ) { 287 296 $post = get_object_vars( $post ); 288 elseif ( !is_array($post) ) 289 $post = get_post($post, ARRAY_A); 290 291 if ( ! $post || empty($post['ID']) ) 297 } elseif ( ! is_array( $post ) ) { 298 $post = get_post( $post, ARRAY_A ); 299 } 300 301 if ( ! $post || empty( $post['ID'] ) ) { 292 302 return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); 293 294 if ( isset($post['post_type']) && 'revision' == $post['post_type'] ) 303 } 304 305 if ( isset( $post['post_type'] ) && 'revision' == $post['post_type'] ) { 295 306 return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) ); 307 } 296 308 297 309 $post = _wp_post_revision_data( $post, $autosave ); 298 $post = wp_slash( $post); //since data is from db310 $post = wp_slash( $post ); //since data is from db 299 311 300 312 $revision_id = wp_insert_post( $post ); 301 if ( is_wp_error( $revision_id) )313 if ( is_wp_error( $revision_id ) ) { 302 314 return $revision_id; 315 } 303 316 304 317 if ( $revision_id ) { … … 327 340 * @return WP_Post|array|null WP_Post (or array) on success, or null on failure. 328 341 */ 329 function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw') {330 if ( ! $revision = get_post( $post, OBJECT, $filter ) )342 function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) { 343 if ( ! $revision = get_post( $post, OBJECT, $filter ) ) { 331 344 return $revision; 332 if ( 'revision' !== $revision->post_type ) 345 } 346 if ( 'revision' !== $revision->post_type ) { 333 347 return null; 348 } 334 349 335 350 if ( $output == OBJECT ) { 336 351 return $revision; 337 352 } elseif ( $output == ARRAY_A ) { 338 $_revision = get_object_vars( $revision);353 $_revision = get_object_vars( $revision ); 339 354 return $_revision; 340 355 } elseif ( $output == ARRAY_N ) { 341 $_revision = array_values( get_object_vars($revision));356 $_revision = array_values( get_object_vars( $revision ) ); 342 357 return $_revision; 343 358 } … … 358 373 */ 359 374 function wp_restore_post_revision( $revision_id, $fields = null ) { 360 if ( ! $revision = wp_get_post_revision( $revision_id, ARRAY_A ) )375 if ( ! $revision = wp_get_post_revision( $revision_id, ARRAY_A ) ) { 361 376 return $revision; 362 363 if ( !is_array( $fields ) ) 377 } 378 379 if ( ! is_array( $fields ) ) { 364 380 $fields = array_keys( _wp_post_revision_fields( $revision ) ); 381 } 365 382 366 383 $update = array(); 367 384 foreach ( array_intersect( array_keys( $revision ), $fields ) as $field ) { 368 $update[ $field] = $revision[$field];369 } 370 371 if ( ! $update )385 $update[ $field ] = $revision[ $field ]; 386 } 387 388 if ( ! $update ) { 372 389 return false; 390 } 373 391 374 392 $update['ID'] = $revision['post_parent']; … … 377 395 378 396 $post_id = wp_update_post( $update ); 379 if ( ! $post_id || is_wp_error( $post_id ) ) 397 if ( ! $post_id || is_wp_error( $post_id ) ) { 380 398 return $post_id; 399 } 381 400 382 401 // Update last edit user … … 440 459 function wp_get_post_revisions( $post_id = 0, $args = null ) { 441 460 $post = get_post( $post_id ); 442 if ( ! $post || empty( $post->ID ) ) 461 if ( ! $post || empty( $post->ID ) ) { 443 462 return array(); 444 445 $defaults = array( 'order' => 'DESC', 'orderby' => 'date ID', 'check_enabled' => true ); 446 $args = wp_parse_args( $args, $defaults ); 447 448 if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) ) 463 } 464 465 $defaults = array( 466 'order' => 'DESC', 467 'orderby' => 'date ID', 468 'check_enabled' => true, 469 ); 470 $args = wp_parse_args( $args, $defaults ); 471 472 if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) ) { 449 473 return array(); 450 451 $args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) ); 452 453 if ( ! $revisions = get_children( $args ) ) 474 } 475 476 $args = array_merge( 477 $args, array( 478 'post_parent' => $post->ID, 479 'post_type' => 'revision', 480 'post_status' => 'inherit', 481 ) 482 ); 483 484 if ( ! $revisions = get_children( $args ) ) { 454 485 return array(); 486 } 455 487 456 488 return $revisions; … … 485 517 $num = WP_POST_REVISIONS; 486 518 487 if ( true === $num ) 519 if ( true === $num ) { 488 520 $num = -1; 489 else521 } else { 490 522 $num = intval( $num ); 491 492 if ( ! post_type_supports( $post->post_type, 'revisions' ) ) 523 } 524 525 if ( ! post_type_supports( $post->post_type, 'revisions' ) ) { 493 526 $num = 0; 527 } 494 528 495 529 /** … … 528 562 529 563 $post->post_content = $preview->post_content; 530 $post->post_title = $preview->post_title;564 $post->post_title = $preview->post_title; 531 565 $post->post_excerpt = $preview->post_excerpt; 532 566 … … 544 578 */ 545 579 function _show_post_preview() { 546 if ( isset( $_GET['preview_id']) && isset($_GET['preview_nonce']) ) {580 if ( isset( $_GET['preview_id'] ) && isset( $_GET['preview_nonce'] ) ) { 547 581 $id = (int) $_GET['preview_id']; 548 582 549 if ( false === wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) ) 550 wp_die( __('Sorry, you are not allowed to preview drafts.') ); 551 552 add_filter('the_preview', '_set_preview'); 583 if ( false === wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) ) { 584 wp_die( __( 'Sorry, you are not allowed to preview drafts.' ) ); 585 } 586 587 add_filter( 'the_preview', '_set_preview' ); 553 588 } 554 589 } … … 566 601 */ 567 602 function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) { 568 if ( ! $post = get_post() ) 603 if ( ! $post = get_post() ) { 569 604 return $terms; 570 571 if ( empty( $_REQUEST['post_format'] ) || $post->ID != $post_id || 'post_format' != $taxonomy || 'revision' == $post->post_type ) 605 } 606 607 if ( empty( $_REQUEST['post_format'] ) || $post->ID != $post_id || 'post_format' != $taxonomy || 'revision' == $post->post_type ) { 572 608 return $terms; 573 574 if ( 'standard' == $_REQUEST['post_format'] ) 609 } 610 611 if ( 'standard' == $_REQUEST['post_format'] ) { 575 612 $terms = array(); 576 elseif ( $term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' ) )613 } elseif ( $term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' ) ) { 577 614 $terms = array( $term ); // Can only have one post format 615 } 578 616 579 617 return $terms; … … 624 662 */ 625 663 function _wp_get_post_revision_version( $revision ) { 626 if ( is_object( $revision ) ) 664 if ( is_object( $revision ) ) { 627 665 $revision = get_object_vars( $revision ); 628 elseif ( !is_array( $revision ) )666 } elseif ( ! is_array( $revision ) ) { 629 667 return false; 630 631 if ( preg_match( '/^\d+-(?:autosave|revision)-v(\d+)$/', $revision['post_name'], $matches ) ) 668 } 669 670 if ( preg_match( '/^\d+-(?:autosave|revision)-v(\d+)$/', $revision['post_name'], $matches ) ) { 632 671 return (int) $matches[1]; 672 } 633 673 634 674 return 0; … … 651 691 652 692 // Add post option exclusively 653 $lock = "revision-upgrade-{$post->ID}";654 $now = time();693 $lock = "revision-upgrade-{$post->ID}"; 694 $now = time(); 655 695 $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now ) ); 656 696 if ( ! $result ) { … … 684 724 685 725 // Something terrible happened 686 if ( false === $this_revision_version ) 726 if ( false === $this_revision_version ) { 687 727 continue; 728 } 688 729 689 730 // 1 is the latest revision version, so we're already up to date. … … 706 747 707 748 // If the previous revision is already up to date, it no longer has the information we need :( 708 if ( $prev_revision_version < 1 ) 749 if ( $prev_revision_version < 1 ) { 709 750 $update['post_author'] = $prev_revision->post_author; 751 } 710 752 } 711 753 … … 713 755 $result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) ); 714 756 715 if ( $result ) 757 if ( $result ) { 716 758 wp_cache_delete( $this_revision->ID, 'posts' ); 717 759 } 718 760 } while ( $prev_revision ); 719 761 … … 721 763 722 764 // Add a copy of the post as latest revision. 723 if ( $add_last ) 765 if ( $add_last ) { 724 766 wp_save_post_revision( $post->ID ); 767 } 725 768 726 769 return true;
Note: See TracChangeset
for help on using the changeset viewer.