Ticket #20564: 20564-11.diff
File 20564-11.diff, 11.0 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 1364 // Save the post format as part of the autosave if different. 1365 if ( isset( $_POST['post_format'] ) && get_post_meta( $new_autosave['ID'], '_revision_post_format', true ) != $_POST['post_format'] ) { 1366 delete_metadata( 'post', $new_autosave['ID'], '_revision_post_format' ); 1367 if ( ! empty( $_POST['post_format'] ) ) 1368 update_metadata( 'post', $new_autosave['ID'], '_revision_post_format', $_POST['post_format'] ); 1369 } 1370 1353 1371 // If the new autosave is the same content as the post, delete the old autosave. 1354 1372 $post = get_post( $post_id ); 1355 1373 $autosave_is_different = false; -
src/wp-includes/js/autosave.js
328 328 (function($){ 329 329 // Returns the data for saving in both localStorage and autosaves to the server 330 330 wp.autosave.getPostData = function() { 331 var ed = typeof tinymce != 'undefined' ? tinymce.activeEditor : null, post_name, parent_id, cats = [],331 var ed = typeof tinymce != 'undefined' ? tinymce.activeEditor : null, post_name, parent_id, post_format, cats = [], 332 332 data = { 333 333 action: 'autosave', 334 334 autosave: true, … … 387 387 if ( $('#auto_draft').val() == '1' ) 388 388 data.auto_draft = '1'; 389 389 390 post_format = $('#post_format').val() || ''; 391 data['post_format'] = post_format == 'standard' ? '' : post_format; 392 393 // Add the post format selected on the edit screen. 394 $('#post-formats-select').find('input[name="post_format"]').each( function(i, field) { 395 data[ field.name ] = $(field).is(':checked') || ''; 396 }); 397 390 398 return data; 391 399 }; 392 400 -
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.6.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.8.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.8.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 // Check whether the post format has changed 153 if ( get_post_format( $post->ID ) != get_post_meta( $last_revision->ID, '_revision_post_format', true ) ) 154 $post_has_changed = true; 155 156 // Don't save revision if post unchanged 112 157 if( ! $post_has_changed ) 113 158 return; 114 159 } … … 240 285 if ( $revision_id ) 241 286 do_action( '_wp_put_post_revision', $revision_id ); 242 287 243 return $revision_id; 244 } 288 // Save revisioned meta fields. 289 foreach ( _wp_post_revision_meta_keys() as $meta_key ) { 290 $meta_value = get_post_meta( $post_id, $meta_key, true ); 291 if ( empty( $meta_value ) ) 292 continue; 293 // Use the underlying add_metadata vs add_post_meta to make sure 294 // metadata is added to the revision post and not its parent. 295 add_metadata( 'post', $revision_id, $meta_key, wp_slash( $meta_value ) ); 296 } 297 // Save the post format 298 if ( $post_format = get_post_format( $post_id ) ) 299 add_metadata( 'post', $revision_id, '_revision_post_format', $post_format ); 245 300 301 return $revision_id; 302 } 303 246 304 /** 247 305 * Gets a post revision. 248 306 * … … 308 366 309 367 $update['ID'] = $revision['post_parent']; 310 368 369 // Restore revisioned meta fields. 370 foreach ( _wp_post_revision_meta_keys() as $meta_key ) { 371 $meta_value = get_post_meta( $revision['ID'], $meta_key, true ); 372 if ( empty( $meta_value ) ) 373 $meta_value = ''; 374 // Add slashes to data pulled from the db 375 update_post_meta( $update['ID'], $meta_key, wp_slash( $meta_value ) ); 376 } 377 311 378 $update = wp_slash( $update ); //since data is from db 379 // Restore post format 380 set_post_format( $update['ID'], get_post_meta( $revision['ID'], '_revision_post_format', true ) ); 312 381 382 // Restore revisioned meta fields. 383 foreach ( _wp_post_revision_meta_keys() as $meta_key ) { 384 delete_post_meta( $update['ID'], $meta_key ); 385 $meta_values = get_post_meta( $revision['ID'], $meta_key ); 386 if ( false === $meta_values ) 387 continue; 388 389 foreach ( $meta_values as $meta_value ) 390 add_post_meta( $update['ID'], $meta_key, $meta_value ); 391 } 392 313 393 $post_id = wp_update_post( $update ); 314 394 if ( ! $post_id || is_wp_error( $post_id ) ) 315 395 return $post_id; … … 449 529 $post->post_excerpt = $preview->post_excerpt; 450 530 451 531 add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 ); 532 add_filter( 'get_post_metadata', '_wp_preview_meta_filter', 10, 4 ); 452 533 453 534 return $post; 454 535 } … … 472 553 } 473 554 474 555 /** 556 * Filters post meta retrieval to get values from the actual autosave post, 557 * and not its parent. Filters revisioned meta keys only. 558 * 559 * @since 3.6.0 560 * @access private 561 */ 562 function _wp_preview_meta_filter( $value, $object_id, $meta_key, $single ) { 563 $post = get_post(); 564 if ( empty( $post ) || $post->ID != $object_id || ! in_array( $meta_key, _wp_post_revision_meta_keys() ) || 'revision' == $post->post_type ) 565 return $value; 566 $preview = wp_get_post_autosave( $post->ID ); 567 if ( ! is_object( $preview ) ) 568 return $value; 569 return get_post_meta( $preview->ID, $meta_key, $single ); 570 } 571 572 /** 475 573 * Filters terms lookup to set the post format. 476 574 * 477 575 * @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 & post format 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 revision matches the current post) 376 $last_revision = 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 see if they are revisioned 384 */ 385 386 // Custom post met 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 416 $last_revision = array_pop( $revisions ); 417 $last_revision = array_pop( $revisions ); 418 wp_restore_post_revision( $last_revision->ID ); 419 420 $this->assertEquals( 'update1', get_post_meta( $post_id, 'meta_revision_test', true ) ); 421 422 // Cleanup! 423 wp_delete_post( $original_post_id ); 424 425 426 } 341 427 }