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-autosaves-controller.php

    r56586 r56714  
    235235            $autosave_id = wp_update_post( wp_slash( (array) $prepared_post ), true );
    236236        } else {
    237             // Non-draft posts: create or update the post autosave.
    238             $autosave_id = $this->create_post_autosave( (array) $prepared_post );
     237            // Non-draft posts: create or update the post autosave. Pass the meta data.
     238            $autosave_id = $this->create_post_autosave( (array) $prepared_post, (array) $request->get_param( 'meta' ) );
    239239        }
    240240
     
    349349     *
    350350     * @since 5.0.0
     351     * @since 6.4.0 The `$meta` parameter was added.
    351352     *
    352353     * @param array $post_data Associative array containing the post data.
     354     * @param array $meta      Associative array containing the post meta data.
    353355     * @return mixed The autosave revision ID or WP_Error.
    354356     */
    355     public function create_post_autosave( $post_data ) {
     357    public function create_post_autosave( $post_data, array $meta = array() ) {
    356358
    357359        $post_id = (int) $post_data['ID'];
     
    373375        }
    374376
     377        // Check if meta values have changed.
     378        if ( ! empty( $meta ) ) {
     379            $revisioned_meta_keys = wp_post_revision_meta_keys( $post->post_type );
     380            foreach ( $revisioned_meta_keys as $meta_key ) {
     381                // get_metadata_raw is used to avoid retrieving the default value.
     382                $old_meta = get_metadata_raw( 'post', $post_id, $meta_key, true );
     383                $new_meta = isset( $meta[ $meta_key ] ) ? $meta[ $meta_key ] : '';
     384
     385                if ( $new_meta !== $old_meta ) {
     386                    $autosave_is_different = true;
     387                    break;
     388                }
     389            }
     390        }
     391
    375392        $user_id = get_current_user_id();
    376393
     
    391408
    392409            // wp_update_post() expects escaped array.
    393             return wp_update_post( wp_slash( $new_autosave ) );
    394         }
    395 
    396         // Create the new autosave as a special post revision.
    397         return _wp_put_post_revision( $post_data, true );
     410            $revision_id = wp_update_post( wp_slash( $new_autosave ) );
     411        } else {
     412            // Create the new autosave as a special post revision.
     413            $revision_id = _wp_put_post_revision( $post_data, true );
     414        }
     415
     416        if ( is_wp_error( $revision_id ) || 0 === $revision_id ) {
     417            return $revision_id;
     418        }
     419
     420        // Attached any passed meta values that have revisions enabled.
     421        if ( ! empty( $meta ) ) {
     422            foreach ( $revisioned_meta_keys as $meta_key ) {
     423                if ( isset( $meta[ $meta_key ] ) ) {
     424                    update_metadata( 'post', $revision_id, $meta_key, $meta[ $meta_key ] );
     425                }
     426            }
     427        }
     428
     429        return $revision_id;
    398430    }
    399431
Note: See TracChangeset for help on using the changeset viewer.