Make WordPress Core

Ticket #44786: 44786.withtests.patch

File 44786.withtests.patch, 3.8 KB (added by jblz, 7 years ago)

Inverts the semantics of the filter (block vs. allow), pass $autosave as the default value, pass $post_id, include unit test

  • src/wp-includes/revision.php

     
    109109 * @return int|WP_Error|void Void or 0 if error, new revision ID, if success.
    110110 */
    111111function wp_save_post_revision( $post_id ) {
    112         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
     112        $autosave = defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE;
     113        /**
     114         * Filters whether saving of revisions is allowed during autosaves.
     115         * Normally, it is disallowed whenever the `DOING_AUTOSAVE` constant is truthy.
     116         *
     117         * @since 4.9.9
     118         *
     119         * @param bool    $block Is saving of revisions blocked for autosaves. Default is the value of `DOING_AUTOSAVE`
     120         */
     121        if ( apply_filters( 'wp_save_post_block_revision_for_autosave', $autosave, $post_id ) ) {
    113122                return;
    114123        }
    115124
  • tests/phpunit/tests/post/revisions.php

     
    119119                ); //content unchanged, shouldn't save
    120120                $this->assertCount( 3, wp_get_post_revisions( $post_id ) ); //should still be 3 revision
    121121
    122                 //next try to save another update, same content, but new ttile, should save revision
     122                //next try to save another update, same content, but new title, should save revision
    123123                wp_update_post(
    124124                        array(
    125125                                'post_title'   => 'some-post-changed',
     
    190190                ); //content unchanged, shouldn't save
    191191                $this->assertCount( 4, wp_get_post_revisions( $post_id ) );
    192192
    193                 //next try to save another update, same content, but new ttile, should save revision
     193                //next try to save another update, same content, but new title, should save revision
    194194                wp_update_post(
    195195                        array(
    196196                                'post_title'   => 'some-post-changed',
     
    214214        }
    215215
    216216        /**
     217         * @ticket 44786
     218         */
     219        function test_revision_block_revision_for_autosave() {
     220                add_filter( 'wp_save_post_block_revision_for_autosave', '__return_false' );
     221                $post    = get_default_post_to_edit( 'post', true );
     222                remove_filter( 'wp_save_post_block_revision_for_autosave', '__return_false' );
     223
     224                $post_id = $post->ID;
     225
     226                $this->assertCount( 0, wp_get_post_revisions( $post_id ), 'There should be no revisions on auto-draft creation.' );
     227
     228                $filter_false_and_check_post_id = function( $autosave, $_post_id ) use ( $post_id ) {
     229                        $this->assertSame( $post_id, $_post_id, 'post_id in the filtering function does not match.' );
     230                        return false;
     231                };
     232
     233                add_filter( 'wp_save_post_block_revision_for_autosave', $filter_false_and_check_post_id, 10, 2 );
     234
     235                wp_update_post(
     236                        array(
     237                                'post_status'  => 'draft',
     238                                'post_title'   => 'some-post',
     239                                'post_type'    => 'post',
     240                                'post_content' => 'some_content',
     241                                'ID'           => $post_id,
     242                        )
     243                );
     244
     245                $this->assertCount( 1, wp_get_post_revisions( $post_id ), 'Revision count should be 1 after an update.' );
     246
     247                wp_update_post(
     248                        array(
     249                                'post_content' => 'some updated content',
     250                                'ID'           => $post_id,
     251                        )
     252                );
     253                $this->assertCount( 2, wp_get_post_revisions( $post_id ), 'Revision count should be 2 after another update.' );
     254
     255                remove_filter( 'wp_save_post_block_revision_for_autosave', $filter_false_and_check_post_id );
     256
     257                add_filter( 'wp_save_post_block_revision_for_autosave', '__return_true' );
     258                wp_update_post(
     259                        array(
     260                                'post_content' => 'new update for some updated content',
     261                                'ID'           => $post_id,
     262                        )
     263                );
     264                remove_filter( 'wp_save_post_block_revision_for_autosave', '__return_true' );
     265
     266                $this->assertCount( 2, wp_get_post_revisions( $post_id ), 'Revision count should still be 2 after another update with blocking re-enabled.' );
     267        }
     268
     269        /**
    217270         * Tests the Caps used in the action=view case of wp-admin/revision.php
    218271         *
    219272         * @ticket 16847