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/default-filters.php

    r56693 r56714  
    412412add_action( 'shutdown', 'wp_ob_end_flush_all', 1 );
    413413// Create a revision whenever a post is updated.
     414add_action( 'wp_after_insert_post', 'wp_save_post_revision_on_insert', 9, 3 );
    414415add_action( 'post_updated', 'wp_save_post_revision', 10, 1 );
    415416add_action( 'publish_post', '_publish_post_hook', 5, 1 );
    720721add_action( 'init', 'wp_create_initial_post_meta' );
     723// Include revisioned meta when considering whether a post revision has changed.
     724add_filter( 'wp_save_post_revision_post_has_changed', 'wp_check_revisioned_meta_fields_have_changed', 10, 3 );
     726// Save revisioned post meta immediately after a revision is saved
     727add_action( '_wp_put_post_revision', 'wp_save_revisioned_meta_fields', 10, 2 );
     729// Include revisioned meta when creating or updating an autosave revision.
     730add_action( 'wp_creating_autosave', 'wp_autosave_post_revisioned_meta_fields' );
     732// When restoring revisions, also restore revisioned meta.
     733add_action( 'wp_restore_post_revision', 'wp_restore_post_revision_meta', 10, 2 );
    722735// Font management.
    723736add_action( 'wp_head', 'wp_print_font_faces', 50 );
Note: See TracChangeset for help on using the changeset viewer.