WordPress.org

Make WordPress Core

Changeset 23928


Ignore:
Timestamp:
04/06/13 23:18:52 (13 months ago)
Author:
azaozz
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/post.php

    r23862 r23928  
    12801280        $new_autosave['post_author'] = $post_author; 
    12811281 
    1282         // Auto-save revisioned meta fields too. 
     1282        // Auto-save revisioned meta fields. 
    12831283        foreach ( _wp_post_revision_meta_keys() as $meta_key ) { 
    1284             if ( ! isset( $_POST[ $meta_key ] ) ) 
    1285                 continue; 
    1286  
    1287             // Use the underlying delete_metadata and add_metadata vs delete_post_meta 
    1288             // and add_post_meta to make sure we're working with the actual revision meta. 
    1289             delete_metadata( 'post', $new_autosave['ID'], $meta_key ); 
    1290             add_metadata( 'post', $new_autosave['ID'], $meta_key, $_POST[ $meta_key ] ); 
     1284            if ( isset( $_POST[ $meta_key ] ) && get_post_meta( $new_autosave['ID'], $meta_key, true ) != $_POST[ $meta_key ] ) { 
     1285                // Use the underlying delete_metadata and add_metadata vs delete_post_meta 
     1286                // and add_post_meta to make sure we're working with the actual revision meta. 
     1287                delete_metadata( 'post', $new_autosave['ID'], $meta_key ); 
     1288 
     1289                if ( ! empty( $_POST[ $meta_key ] ) ) 
     1290                    add_metadata( 'post', $new_autosave['ID'], $meta_key, $_POST[ $meta_key ] ); 
     1291            } 
     1292        } 
     1293 
     1294        // Save the post format if different 
     1295        if ( isset( $_POST['post_format'] ) && get_post_meta( $new_autosave['ID'], '_revision_post_format', true ) != $_POST['post_format'] ) { 
     1296            delete_metadata( 'post', $new_autosave['ID'], '_revision_post_format' ); 
     1297 
     1298            if ( ! empty( $_POST['post_format'] ) ) 
     1299                add_metadata( 'post', $new_autosave['ID'], '_revision_post_format', $_POST['post_format'] ); 
    12911300        } 
    12921301 
  • trunk/wp-includes/js/autosave.js

    r23886 r23928  
    306306// Returns the data for saving in both localStorage and autosaves to the server 
    307307wp.autosave.getPostData = function() { 
    308     var ed = typeof tinymce != 'undefined' ? tinymce.activeEditor : null, post_name, parent_id, cats = [], 
     308    var ed = typeof tinymce != 'undefined' ? tinymce.activeEditor : null, post_name, parent_id, post_format, cats = [], 
    309309        data = { 
    310310            action: 'autosave', 
     
    364364    if ( $('#auto_draft').val() == '1' ) 
    365365        data['auto_draft'] = '1'; 
     366 
     367    post_format = $('#post_format').val() || ''; 
     368    data['post_format'] = post_format == 'standard' ? '' : post_format; 
     369 
     370    $('.post-formats-fields').find('input[name^="_wp_format_"], textarea[name^="_wp_format_"]').each( function(i, field) { 
     371        data[ field.name ] = field.value || ''; 
     372    }); 
    366373 
    367374    return data; 
  • trunk/wp-includes/revision.php

    r23898 r23928  
    135135            // Check whether revisioned meta fields have changed. 
    136136            foreach ( _wp_post_revision_meta_keys() as $meta_key ) { 
    137                 if ( get_post_meta( $post->ID, $meta_key ) != get_post_meta( $last_revision->ID, $meta_key ) ) { 
     137                if ( get_post_meta( $post->ID, $meta_key, true ) != get_post_meta( $last_revision->ID, $meta_key, true ) ) { 
    138138                    $post_has_changed = true; 
    139139                    break; 
    140140                } 
    141141            } 
     142 
     143            // Check whether the post format has changed 
     144            if ( get_post_format( $post->ID ) != get_post_meta( $last_revision->ID, '_revision_post_format', true ) ) 
     145                $post_has_changed = true; 
    142146 
    143147            //don't save revision if post unchanged 
     
    281285    // Save revisioned meta fields. 
    282286    foreach ( _wp_post_revision_meta_keys() as $meta_key ) { 
    283         $meta_values = get_post_meta( $post_id, $meta_key ); 
    284         if ( false === $meta_values ) 
     287        $meta_value = get_post_meta( $post_id, $meta_key, true ); 
     288        if ( empty( $meta_value ) ) 
    285289            continue; 
    286290 
    287291        // Use the underlying add_metadata vs add_post_meta to make sure 
    288292        // metadata is added to the revision post and not its parent. 
    289         foreach ( $meta_values as $meta_value ) 
    290             add_metadata( 'post', $revision_id, $meta_key, $meta_value ); 
    291     } 
     293        add_metadata( 'post', $revision_id, $meta_key, wp_slash( $meta_value ) ); 
     294    } 
     295 
     296    // Save the post format 
     297    if ( $post_format = get_post_format( $post_id ) ) 
     298        add_metadata( 'post', $revision_id, '_revision_post_format', $post_format ); 
    292299 
    293300    return $revision_id; 
     
    367374    // Restore revisioned meta fields. 
    368375    foreach ( _wp_post_revision_meta_keys() as $meta_key ) { 
    369         delete_post_meta( $update['ID'], $meta_key ); 
    370         $meta_values = get_post_meta( $revision['ID'], $meta_key ); 
    371         if ( false === $meta_values ) 
    372             continue; 
    373  
    374         foreach ( $meta_values as $meta_value ) 
    375             add_post_meta( $update['ID'], $meta_key, $meta_value ); 
    376     } 
     376        $meta_value = get_post_meta( $revision['ID'], $meta_key, true ); 
     377        if ( empty( $meta_value ) ) 
     378            $meta_value = ''; 
     379        // Add slashes to data pulled from the db 
     380        update_post_meta( $update['ID'], $meta_key, wp_slash( $meta_value ) ); 
     381    } 
     382 
     383    // Restore post format 
     384    set_post_format( $update['ID'], get_post_meta( $revision['ID'], '_revision_post_format', true ) ); 
    377385 
    378386    $post_id = wp_update_post( $update ); 
     
    506514 
    507515    add_filter( 'get_post_metadata', '_wp_preview_meta_filter', 10, 4 ); 
     516    add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 ); 
    508517 
    509518    return $post; 
     
    540549 
    541550    return get_post_meta( $preview->ID, $meta_key, $single ); 
     551} 
     552 
     553/** 
     554 * Filters terms lookup to get the post format saved with the preview revision. 
     555 * 
     556 * @since 2.6 
     557 * @access private 
     558 */ 
     559function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) { 
     560    $post = get_post(); 
     561 
     562    if ( $post->ID != $post_id || 'post_format' != $taxonomy || 'revision' == $post->post_type ) 
     563        return $terms; 
     564 
     565    if ( ! $preview = wp_get_post_autosave( $post->ID ) ) 
     566        return $terms; 
     567 
     568    if ( $post_format = get_post_meta( $preview->ID, '_revision_post_format', true ) ) { 
     569        if ( $term = get_term_by( 'slug', 'post-format-' . sanitize_key( $post_format ), 'post_format' ) ) 
     570            $terms = array( $term ); // Can only have one post format 
     571    } 
     572 
     573    return $terms; 
    542574} 
    543575 
Note: See TracChangeset for help on using the changeset viewer.