Ticket #16215: 16215.3.diff

File 16215.3.diff, 4.2 KB (added by adamsilverstein, 3 months ago)

add revision after post update, new hook, new check for duplicate before saving

  • wp-includes/default-filters.php

     
    250250add_action( 'plugins_loaded',             'wp_maybe_load_widgets',                    0    ); 
    251251add_action( 'plugins_loaded',             'wp_maybe_load_embeds',                     0    ); 
    252252add_action( 'shutdown',                   'wp_ob_end_flush_all',                      1    ); 
    253 add_action( 'pre_post_update',            'wp_save_post_revision',                   10, 2 ); 
     253add_action( 'post_post_update',           'wp_save_post_revision',                   10, 2 ); 
    254254add_action( 'publish_post',               '_publish_post_hook',                       5, 1 ); 
    255255add_action( 'transition_post_status',     '_transition_post_status',                  5, 3 ); 
    256256add_action( 'transition_post_status',     '_update_term_count_on_transition_post_status', 10, 3 ); 
  • wp-includes/post.php

     
    28782878 
    28792879        if ( $update ) { 
    28802880                do_action('edit_post', $post_ID, $post); 
     2881                do_action( 'post_post_update', $post_ID, $data ); 
     2882 
    28812883                $post_after = get_post($post_ID); 
    28822884                do_action( 'post_updated', $post_ID, $post_after, $post_before); 
    28832885        } 
  • wp-includes/revision.php

     
    8282        if ( ! WP_POST_REVISIONS ) 
    8383                return; 
    8484 
    85         if ( !$post = get_post( $post_id, ARRAY_A ) ) 
     85        if ( ! $post = get_post( $post_id, ARRAY_A ) ) 
    8686                return; 
    8787 
    8888        if ( 'auto-draft' == $post['post_status'] ) 
    8989                return; 
    9090 
    91         if ( !post_type_supports($post['post_type'], 'revisions') ) 
     91        if ( ! post_type_supports( $post['post_type'], 'revisions' ) ) 
    9292                return; 
    9393 
    9494        // if new data is supplied, check that it is different from last saved revision, unless a plugin tells us to always save regardless 
    95         if ( apply_filters( 'wp_save_post_revision_check_for_changes', true, $post, $new_data ) && is_array( $new_data ) ) { 
    96                 $post_has_changed = false; 
    97                 foreach ( array_keys( _wp_post_revision_fields() ) as $field ) { 
    98                         if ( normalize_whitespace( $new_data[ $field ] ) != normalize_whitespace( $post[ $field ] ) ) { 
    99                                 $post_has_changed = true; 
    100                                 break; 
     95        if ( $revisions = wp_get_post_revisions( $post_id ) ) { // grab the last revision 
     96                $last_revision = array_shift( $revisions ); 
     97                if ( ! $last_revision_array = get_post( $last_revision->ID, ARRAY_A ) ) //if no previous revisions, save one for sure 
     98                        return; 
     99 
     100                if ( apply_filters( 'wp_save_post_revision_check_for_changes', true, $last_revision_array, $new_data ) && is_array( $new_data ) ) { 
     101                        $post_has_changed = false; 
     102                        foreach ( array_keys( _wp_post_revision_fields() ) as $field ) { 
     103                                if ( normalize_whitespace( $new_data[ $field ] ) != normalize_whitespace( $last_revision_array[ $field ] ) ) { 
     104                                        $post_has_changed = true; 
     105                                        break; 
     106                                } 
    101107                        } 
     108                        //don't save revision if post unchanged 
     109                        if( ! $post_has_changed ) 
     110                                return; 
    102111                } 
    103                 //don't save revision if post unchanged 
    104                 if( ! $post_has_changed ) 
    105                         return; 
    106112        } 
    107113 
    108114        $return = _wp_put_post_revision( $post ); 
    109115 
    110116        // WP_POST_REVISIONS = true (default), -1 
    111         if ( !is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 ) 
     117        if ( ! is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 ) 
    112118                return $return; 
    113119 
    114120        // all revisions and (possibly) one autosave 
    115121        $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) ); 
    116122 
    117123        // WP_POST_REVISIONS = (int) (# of autosaves to save) 
    118         $delete = count($revisions) - WP_POST_REVISIONS; 
     124        $delete = count( $revisions ) - WP_POST_REVISIONS; 
    119125 
    120126        if ( $delete < 1 ) 
    121127                return $return; 
     
    123129        $revisions = array_slice( $revisions, 0, $delete ); 
    124130 
    125131        for ( $i = 0; isset($revisions[$i]); $i++ ) { 
    126                 if ( false !== strpos( $revisions[$i]->post_name, 'autosave' ) ) 
     132                if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) ) 
    127133                        continue; 
    128                 wp_delete_post_revision( $revisions[$i]->ID ); 
     134                wp_delete_post_revision( $revisions[ $i ]->ID ); 
    129135        } 
    130136 
    131137        return $return;