Make WordPress Core

09/26/2023 03:30:34 PM (9 months ago)

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.

1 edited


  • trunk/src/wp-includes/meta.php

    r56191 r56714  
    13681368 * @since 5.3.0 Valid meta types expanded to include "array" and "object".
    13691369 * @since 5.5.0 The `$default` argument was added to the arguments array.
     1370 * @since 6.4.0 The `$revisions_enabled` argument was added to the arguments array.
    13701371 *
    13711372 * @param string       $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
    13931394 *                                         When registering complex meta values this argument may optionally be an
    13941395 *                                         array with 'schema' or 'prepare_callback' keys instead of a boolean.
     1396 *     @type bool       $revisions_enabled Whether to enable revisions support for this meta_key. Can only be used when the
     1397 *                                         object type is 'post'.
    13951398 * }
    13961399 * @param string|array $deprecated Deprecated. Use `$args` instead.
    14151418        'auth_callback'     => null,
    14161419        'show_in_rest'      => false,
     1420        'revisions_enabled' => false,
    14171421    );
    14621466    $object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : '';
     1467    if ( $args['revisions_enabled'] ) {
     1468        if ( 'post' !== $object_type ) {
     1469            _doing_it_wrong( __FUNCTION__, __( 'Meta keys cannot enable revisions support unless the object type supports revisions.' ), '6.4.0' );
     1471            return false;
     1472        } elseif ( ! empty( $object_subtype ) && ! post_type_supports( $object_subtype, 'revisions' ) ) {
     1473            _doing_it_wrong( __FUNCTION__, __( 'Meta keys cannot enable revisions support unless the object subtype supports revisions.' ), '6.4.0' );
     1475            return false;
     1476        }
     1477    }
    14641479    // If `auth_callback` is not provided, fall back to `is_protected_meta()`.
Note: See TracChangeset for help on using the changeset viewer.