Ticket #20564: 20564-14.diff
File 20564-14.diff, 9.1 KB (added by , 11 years ago) |
---|
-
src/wp-admin/includes/post.php
1350 1350 $new_autosave['ID'] = $old_autosave->ID; 1351 1351 $new_autosave['post_author'] = $post_author; 1352 1352 1353 // Auto-save revisioned meta fields. 1354 foreach ( _wp_post_revision_meta_keys() as $meta_key ) { 1355 if ( isset( $_POST[ $meta_key ] ) && get_post_meta( $new_autosave['ID'], $meta_key, true ) != $_POST[ $meta_key ] ) { 1356 // Use the underlying delete_metadata and add_metadata vs delete_post_meta 1357 // and add_post_meta to make sure we're working with the actual revision meta. 1358 delete_metadata( 'post', $new_autosave['ID'], $meta_key ); 1359 if ( ! empty( $_POST[ $meta_key ] ) ) 1360 add_metadata( 'post', $new_autosave['ID'], $meta_key, $_POST[ $meta_key ] ); 1361 } 1362 } 1363 1353 1364 // If the new autosave is the same content as the post, delete the old autosave. 1354 1365 $post = get_post( $post_id ); 1355 1366 $autosave_is_different = false; -
src/wp-includes/revision.php
28 28 if ( !$fields ) { 29 29 // Allow these to be versioned 30 30 $fields = array( 31 'post_title' => __( 'Title' ),31 'post_title' => __( 'Title' ), 32 32 'post_content' => __( 'Content' ), 33 33 'post_excerpt' => __( 'Excerpt' ), 34 34 ); 35 35 36 // Runs only once 37 $fields = apply_filters( '_wp_post_revision_fields', $fields ); 36 /** 37 * Filter the list of post fields that are revisioned. 38 * 39 * @since 3.9.0 40 * 41 * @param array $fields An array of fields to be be revisioned. 42 * 43 */ 44 $fields = apply_filters( '_wp_post_revision_fields', $fields ); // Runs only once 38 45 39 46 // WP uses these internally either in versioning or elsewhere - they cannot be versioned 40 47 foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect ) … … 59 66 } 60 67 61 68 /** 69 * Determines which post meta fields are revisioned. 70 * 71 * @since 3.9.0 72 * 73 * @access private 74 * @return array An array of meta keys that should be revisioned. 75 * 76 */ 77 function _wp_post_revision_meta_keys() { 78 // Default meta keys to be revisioned 79 $keys = array(); 80 81 /** 82 * Filter the list of post meta keys that are revisioned. 83 * 84 * @since 3.9.0 85 * 86 * @param array $keys An array of fields to be be revisioned. 87 * 88 */ 89 return apply_filters( 'wp_post_revision_meta_keys', $keys ); 90 } 91 92 /** 62 93 * Saves an already existing post as a post revision. 63 94 * 64 95 * Typically used immediately after post updates. … … 102 133 if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', true, $last_revision, $post ) ) { 103 134 $post_has_changed = false; 104 135 136 // Check whether revisioned fields have been changed 105 137 foreach ( array_keys( _wp_post_revision_fields() ) as $field ) { 106 138 if ( normalize_whitespace( $post->$field ) != normalize_whitespace( $last_revision->$field ) ) { 107 139 $post_has_changed = true; 108 140 break; 109 141 } 110 142 } 111 //don't save revision if post unchanged 143 144 // Check whether revisioned meta fields have changed 145 foreach ( _wp_post_revision_meta_keys() as $meta_key ) { 146 if ( get_post_meta( $post->ID, $meta_key ) != get_post_meta( $last_revision->ID, $meta_key ) ) { 147 $post_has_changed = true; 148 break; 149 } 150 } 151 152 // Don't save revision if post unchanged 112 153 if( ! $post_has_changed ) 113 154 return; 114 155 } … … 240 281 if ( $revision_id ) 241 282 do_action( '_wp_put_post_revision', $revision_id ); 242 283 243 return $revision_id; 244 } 284 // Save revisioned meta fields. 285 foreach ( _wp_post_revision_meta_keys() as $meta_key ) { 286 $meta_value = get_post_meta( $post_id, $meta_key, true ); 287 if ( empty( $meta_value ) ) 288 continue; 289 // Use the underlying add_metadata vs add_post_meta to make sure 290 // metadata is added to the revision post and not its parent. 291 add_metadata( 'post', $revision_id, $meta_key, wp_slash( $meta_value ) ); 292 } 245 293 294 return $revision_id; 295 } 296 246 297 /** 247 298 * Gets a post revision. 248 299 * … … 308 359 309 360 $update['ID'] = $revision['post_parent']; 310 361 362 // Restore revisioned meta fields. 363 foreach ( _wp_post_revision_meta_keys() as $meta_key ) { 364 $meta_value = get_post_meta( $revision['ID'], $meta_key, true ); 365 if ( empty( $meta_value ) ) 366 $meta_value = ''; 367 // Add slashes to data pulled from the db 368 update_post_meta( $update['ID'], $meta_key, wp_slash( $meta_value ) ); 369 } 370 311 371 $update = wp_slash( $update ); //since data is from db 312 372 373 // Restore revisioned meta fields. 374 foreach ( _wp_post_revision_meta_keys() as $meta_key ) { 375 delete_post_meta( $update['ID'], $meta_key ); 376 $meta_values = get_post_meta( $revision['ID'], $meta_key ); 377 if ( false === $meta_values ) 378 continue; 379 380 foreach ( $meta_values as $meta_value ) 381 add_post_meta( $update['ID'], $meta_key, $meta_value ); 382 } 383 313 384 $post_id = wp_update_post( $update ); 314 385 if ( ! $post_id || is_wp_error( $post_id ) ) 315 386 return $post_id; … … 449 520 $post->post_excerpt = $preview->post_excerpt; 450 521 451 522 add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 ); 523 add_filter( 'get_post_metadata', '_wp_preview_meta_filter', 10, 4 ); 452 524 453 525 return $post; 454 526 } … … 472 544 } 473 545 474 546 /** 547 * Filters post meta retrieval to get values from the actual autosave post, 548 * and not its parent. Filters revisioned meta keys only. 549 * 550 * @since 3.9.0 551 * @access private 552 */ 553 function _wp_preview_meta_filter( $value, $object_id, $meta_key, $single ) { 554 $post = get_post(); 555 if ( empty( $post ) || $post->ID != $object_id || ! in_array( $meta_key, _wp_post_revision_meta_keys() ) || 'revision' == $post->post_type ) 556 return $value; 557 $preview = wp_get_post_autosave( $post->ID ); 558 if ( ! is_object( $preview ) ) 559 return $value; 560 return get_post_meta( $preview->ID, $meta_key, $single ); 561 } 562 563 /** 475 564 * Filters terms lookup to set the post format. 476 565 * 477 566 * @since 3.6.0 -
tests/phpunit/tests/post/revisions.php
338 338 $this->assertTrue( user_can( $author_user_id, 'read_post', $revision->ID ) ); 339 339 } 340 340 } 341 342 /** 343 * Test the revisions system for storage of meta values 344 * @ticket 20564 345 */ 346 function test_revisions_stores_meta_values() { 347 // Set up a new post 348 $original_post_id = $post_id = $this->factory->post->create(); 349 // And update to store an initial revision 350 wp_update_post( array( 'post_content' => 'some initial content', 'ID' => $post_id ) ); 351 352 /** 353 * First set up a meta value 354 */ 355 356 // Store a custom meta value, which is not revisioned by default 357 update_post_meta( $post_id, 'meta_revision_test', 'original' ); 358 359 // Update the post, storing a revision 360 wp_update_post( array( 'post_content' => 'some more content', 'ID' => $post_id ) ); 361 362 // Next, store some updated meta values for the same key 363 update_post_meta( $post_id, 'meta_revision_test', 'update1' ); 364 365 // Save the post, changing content to force a revision 366 wp_update_post( array( 'post_content' => 'some updated content', 'ID' => $post_id ) ); 367 368 /** 369 * Now restore the original revision 370 */ 371 372 // Get all the revisions 373 $revisions = (Array)wp_get_post_revisions( $post_id ); 374 375 // Go back two revisions (the 1st 'previous' revision matches the current post) 376 array_pop( $revisions ); 377 $last_revision = array_pop( $revisions ); 378 379 // Restore! 380 wp_restore_post_revision( $last_revision->ID ); 381 382 /** 383 * Check the meta values to verify they are NOT revisioned - they are not revisioned by default 384 */ 385 386 // Custom post meta should NOT be restored, orignal value should not be restored, value still 'update1' 387 $this->assertEquals( 'update1', get_post_meta( $post_id, 'meta_revision_test', true ) ); 388 389 /* 390 * Now test the revisioning of custom meta when enabled by the wp_post_revision_meta_keys filter 391 */ 392 393 // Add the custom field to be revised via the wp_post_revision_meta_keys filter 394 add_filter( 'wp_post_revision_meta_keys', function( $keys ) { 395 $keys[] = 'meta_revision_test'; 396 return $keys; 397 }); 398 399 /** 400 * Save new meta in the revisioned field 401 */ 402 403 // Save the post again, custom meta should now be revisioned - note no change in content, revision still saved 404 wp_update_post( array( 'ID' => $post_id ) ); 405 406 // Store custom meta values, which should now be revisioned 407 update_post_meta( $post_id, 'meta_revision_test', 'update2' ); 408 409 // Save the post again 410 wp_update_post( array( 'ID' => $post_id ) ); 411 412 // Retore the previous revision 413 $revisions = wp_get_post_revisions( $post_id ); 414 415 // Go back two to load the previous revision 416 array_pop( $revisions ); 417 $last_revision = array_pop( $revisions ); 418 wp_restore_post_revision( $last_revision->ID ); 419 420 // Verify that previous post meta is restored 421 $this->assertEquals( 'update1', get_post_meta( $post_id, 'meta_revision_test', true ) ); 422 423 // Cleanup! 424 wp_delete_post( $original_post_id ); 425 426 } 341 427 }