Make WordPress Core


Ignore:
Timestamp:
09/26/2023 03:30:34 PM (9 months ago)
Author:
adamsilverstein
Message:

Revisions: framework for storing post meta revisions.

Enable the storing of post meta in revisions including autosaves and previews:

Add a new argument revisions_enabled to the register_meta function which enables storing meta in revisions.

Add a new wp_post_revision_meta_keys filter which developers can use to control which meta is revisioned - it passes an array of the meta keys with revisions enabled as well as the post type.

Meta keys with revisions enabled are also stored for autosaves, and are restored when a revision or autosave is restored. In addition, meta values are now stored with the autosave revision used for previews. Changes to meta can now be previewed correctly without overwriting the published meta (see #20299) or passing data as a query variable, as the editor currently does to preview changes to the featured image.

Changes to meta with revisions enabled are considered when determining if a new revision should be created. A new revision is created if the meta value has changed since the last revision.

Revisions are now saved on the wp_after_insert_post hook instead of post_updated. The wp_after_insert_post action is fired after post meta has been saved by the REST API which enables attaching meta to the revision. To ensure backwards compatibility with existing action uses, wp_save_post_revision_on_insert function exits early if plugins have removed the previous do_action( 'post_updated', 'wp_save_post_revision' ) call.

Props: alexkingorg, johnbillion, markjaquith, WraithKenny, kovshenin, azaozz, tv-productions, p51labs, mattheu, mikeschroder, Mamaduka, ellatrix, timothyblynjacobs, jakemgold, bookwyrm, ryanduff, mintindeed, wonderboymusic, sanchothefat, westonruter, spacedmonkey, hellofromTonya, drewapicture, adamsilverstein, swisspiddy.
Fixes #20564, #20299.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/meta/registerMeta.php

    r56548 r56714  
    9898                        'auth_callback'     => '__return_true',
    9999                        'show_in_rest'      => false,
     100                        'revisions_enabled' => false,
    100101                    ),
    101102                ),
     
    122123                        'auth_callback'     => '__return_true',
    123124                        'show_in_rest'      => false,
     125                        'revisions_enabled' => false,
    124126                    ),
    125127                ),
     
    176178                        'auth_callback'     => '__return_true',
    177179                        'show_in_rest'      => false,
     180                        'revisions_enabled' => false,
    178181                    ),
    179182                ),
     
    343346                        'auth_callback'     => '__return_true',
    344347                        'show_in_rest'      => false,
     348                        'revisions_enabled' => false,
    345349                    ),
    346350                ),
     
    396400                        'auth_callback'     => '__return_true',
    397401                        'show_in_rest'      => false,
     402                        'revisions_enabled' => false,
    398403                    ),
    399404                ),
     
    10821087        );
    10831088    }
     1089
     1090    /**
     1091     * Test that attempting to register meta with revisions_enabled set to true on a
     1092     * post type that does not have revisions enabled fails and throws a `doing_it_wrong` notice.
     1093     *
     1094     * @ticket 20564
     1095     */
     1096    public function test_register_meta_with_revisions_enabled_on_post_type_without_revisions() {
     1097        $this->setExpectedIncorrectUsage( 'register_meta' );
     1098
     1099        // Set up a custom post type with revisions disabled.
     1100        register_post_type(
     1101            'test_post_type',
     1102            array(
     1103                'supports' => array( 'title', 'editor' ),
     1104            )
     1105        );
     1106
     1107        $meta_key = 'registered_key1';
     1108        $args     = array(
     1109            'revisions_enabled' => true,
     1110        );
     1111
     1112        $register = register_meta(
     1113            'test_post_type',
     1114            $meta_key,
     1115            $args
     1116        );
     1117
     1118        $this->assertFalse( $register );
     1119    }
    10841120}
Note: See TracChangeset for help on using the changeset viewer.