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/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

    r56586 r56714  
    2626
    2727    /**
     28     * Instance of a revision meta fields object.
     29     *
     30     * @since 6.4.0
     31     * @var WP_REST_Post_Meta_Fields
     32     */
     33    protected $meta;
     34
     35    /**
    2836     * Parent controller.
    2937     *
     
    6169        $this->parent_base       = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
    6270        $this->namespace         = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2';
     71        $this->meta              = new WP_REST_Post_Meta_Fields( $parent_post_type );
    6372    }
    6473
     
    620629        }
    621630
     631        if ( rest_is_field_included( 'meta', $fields ) ) {
     632            $data['meta'] = $this->meta->get_value( $post->ID, $request );
     633        }
     634
    622635        $context  = ! empty( $request['context'] ) ? $request['context'] : 'view';
    623636        $data     = $this->add_additional_fields_to_object( $data, $request );
     
    752765            $schema['properties']['guid'] = $parent_schema['properties']['guid'];
    753766        }
     767
     768        $schema['properties']['meta'] = $this->meta->get_field_schema();
    754769
    755770        $this->schema = $schema;
Note: See TracChangeset for help on using the changeset viewer.