Revisions:

- Store the post format as meta on revisions (including autosaves).
- Add post formats data (post meta) when autosaving.
- Only add non-empty post formats data to revisions.
- Correct the post format when previewing a published post.
Props kovshenin, see #19570, see #20564.


git-svn-id: http://core.svn.wordpress.org/trunk@23928 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Ozz 2013-04-06 23:18:52 +00:00
parent 98fc1a774a
commit 33dd4b3d69
3 changed files with 68 additions and 20 deletions

View File

@ -1279,15 +1279,24 @@ function wp_create_post_autosave( $post_id ) {
$new_autosave['ID'] = $old_autosave->ID; $new_autosave['ID'] = $old_autosave->ID;
$new_autosave['post_author'] = $post_author; $new_autosave['post_author'] = $post_author;
// Auto-save revisioned meta fields too. // Auto-save revisioned meta fields.
foreach ( _wp_post_revision_meta_keys() as $meta_key ) { foreach ( _wp_post_revision_meta_keys() as $meta_key ) {
if ( ! isset( $_POST[ $meta_key ] ) ) if ( isset( $_POST[ $meta_key ] ) && get_post_meta( $new_autosave['ID'], $meta_key, true ) != $_POST[ $meta_key ] ) {
continue; // Use the underlying delete_metadata and add_metadata vs delete_post_meta
// and add_post_meta to make sure we're working with the actual revision meta.
delete_metadata( 'post', $new_autosave['ID'], $meta_key );
// Use the underlying delete_metadata and add_metadata vs delete_post_meta if ( ! empty( $_POST[ $meta_key ] ) )
// and add_post_meta to make sure we're working with the actual revision meta. add_metadata( 'post', $new_autosave['ID'], $meta_key, $_POST[ $meta_key ] );
delete_metadata( 'post', $new_autosave['ID'], $meta_key ); }
add_metadata( 'post', $new_autosave['ID'], $meta_key, $_POST[ $meta_key ] ); }
// Save the post format if different
if ( isset( $_POST['post_format'] ) && get_post_meta( $new_autosave['ID'], '_revision_post_format', true ) != $_POST['post_format'] ) {
delete_metadata( 'post', $new_autosave['ID'], '_revision_post_format' );
if ( ! empty( $_POST['post_format'] ) )
add_metadata( 'post', $new_autosave['ID'], '_revision_post_format', $_POST['post_format'] );
} }
return wp_update_post( $new_autosave ); return wp_update_post( $new_autosave );

View File

@ -305,7 +305,7 @@ wp.autosave = wp.autosave || {};
(function($){ (function($){
// Returns the data for saving in both localStorage and autosaves to the server // Returns the data for saving in both localStorage and autosaves to the server
wp.autosave.getPostData = function() { wp.autosave.getPostData = function() {
var ed = typeof tinymce != 'undefined' ? tinymce.activeEditor : null, post_name, parent_id, cats = [], var ed = typeof tinymce != 'undefined' ? tinymce.activeEditor : null, post_name, parent_id, post_format, cats = [],
data = { data = {
action: 'autosave', action: 'autosave',
autosave: true, autosave: true,
@ -364,6 +364,13 @@ wp.autosave.getPostData = function() {
if ( $('#auto_draft').val() == '1' ) if ( $('#auto_draft').val() == '1' )
data['auto_draft'] = '1'; data['auto_draft'] = '1';
post_format = $('#post_format').val() || '';
data['post_format'] = post_format == 'standard' ? '' : post_format;
$('.post-formats-fields').find('input[name^="_wp_format_"], textarea[name^="_wp_format_"]').each( function(i, field) {
data[ field.name ] = field.value || '';
});
return data; return data;
} }

View File

@ -134,12 +134,16 @@ function wp_save_post_revision( $post_id ) {
// Check whether revisioned meta fields have changed. // Check whether revisioned meta fields have changed.
foreach ( _wp_post_revision_meta_keys() as $meta_key ) { foreach ( _wp_post_revision_meta_keys() as $meta_key ) {
if ( get_post_meta( $post->ID, $meta_key ) != get_post_meta( $last_revision->ID, $meta_key ) ) { if ( get_post_meta( $post->ID, $meta_key, true ) != get_post_meta( $last_revision->ID, $meta_key, true ) ) {
$post_has_changed = true; $post_has_changed = true;
break; break;
} }
} }
// Check whether the post format has changed
if ( get_post_format( $post->ID ) != get_post_meta( $last_revision->ID, '_revision_post_format', true ) )
$post_has_changed = true;
//don't save revision if post unchanged //don't save revision if post unchanged
if( ! $post_has_changed ) if( ! $post_has_changed )
return; return;
@ -280,16 +284,19 @@ function _wp_put_post_revision( $post = null, $autosave = false ) {
// Save revisioned meta fields. // Save revisioned meta fields.
foreach ( _wp_post_revision_meta_keys() as $meta_key ) { foreach ( _wp_post_revision_meta_keys() as $meta_key ) {
$meta_values = get_post_meta( $post_id, $meta_key ); $meta_value = get_post_meta( $post_id, $meta_key, true );
if ( false === $meta_values ) if ( empty( $meta_value ) )
continue; continue;
// Use the underlying add_metadata vs add_post_meta to make sure // Use the underlying add_metadata vs add_post_meta to make sure
// metadata is added to the revision post and not its parent. // metadata is added to the revision post and not its parent.
foreach ( $meta_values as $meta_value ) add_metadata( 'post', $revision_id, $meta_key, wp_slash( $meta_value ) );
add_metadata( 'post', $revision_id, $meta_key, $meta_value );
} }
// Save the post format
if ( $post_format = get_post_format( $post_id ) )
add_metadata( 'post', $revision_id, '_revision_post_format', $post_format );
return $revision_id; return $revision_id;
} }
@ -366,15 +373,16 @@ function wp_restore_post_revision( $revision_id, $fields = null ) {
// Restore revisioned meta fields. // Restore revisioned meta fields.
foreach ( _wp_post_revision_meta_keys() as $meta_key ) { foreach ( _wp_post_revision_meta_keys() as $meta_key ) {
delete_post_meta( $update['ID'], $meta_key ); $meta_value = get_post_meta( $revision['ID'], $meta_key, true );
$meta_values = get_post_meta( $revision['ID'], $meta_key ); if ( empty( $meta_value ) )
if ( false === $meta_values ) $meta_value = '';
continue; // Add slashes to data pulled from the db
update_post_meta( $update['ID'], $meta_key, wp_slash( $meta_value ) );
foreach ( $meta_values as $meta_value )
add_post_meta( $update['ID'], $meta_key, $meta_value );
} }
// Restore post format
set_post_format( $update['ID'], get_post_meta( $revision['ID'], '_revision_post_format', true ) );
$post_id = wp_update_post( $update ); $post_id = wp_update_post( $update );
if ( is_wp_error( $post_id ) ) if ( is_wp_error( $post_id ) )
return $post_id; return $post_id;
@ -505,6 +513,7 @@ function _set_preview($post) {
$post->post_excerpt = $preview->post_excerpt; $post->post_excerpt = $preview->post_excerpt;
add_filter( 'get_post_metadata', '_wp_preview_meta_filter', 10, 4 ); add_filter( 'get_post_metadata', '_wp_preview_meta_filter', 10, 4 );
add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 );
return $post; return $post;
} }
@ -541,6 +550,29 @@ function _wp_preview_meta_filter( $value, $object_id, $meta_key, $single ) {
return get_post_meta( $preview->ID, $meta_key, $single ); return get_post_meta( $preview->ID, $meta_key, $single );
} }
/**
* Filters terms lookup to get the post format saved with the preview revision.
*
* @since 2.6
* @access private
*/
function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) {
$post = get_post();
if ( $post->ID != $post_id || 'post_format' != $taxonomy || 'revision' == $post->post_type )
return $terms;
if ( ! $preview = wp_get_post_autosave( $post->ID ) )
return $terms;
if ( $post_format = get_post_meta( $preview->ID, '_revision_post_format', true ) ) {
if ( $term = get_term_by( 'slug', 'post-format-' . sanitize_key( $post_format ), 'post_format' ) )
$terms = array( $term ); // Can only have one post format
}
return $terms;
}
function _wp_get_post_revision_version( $revision ) { function _wp_get_post_revision_version( $revision ) {
if ( is_object( $revision ) ) if ( is_object( $revision ) )
$revision = get_object_vars( $revision ); $revision = get_object_vars( $revision );