Make WordPress Core


Ignore:
Timestamp:
08/01/2023 07:59:33 AM (15 months ago)
Author:
isabel_brison
Message:

Editor: second update of npm packages for 6.3 RC3.

Includes bug fixes for footnotes, patterns, command palette, top toolbar and other small regressions.

Props andrewserong, spacedmonkey.
See #58926.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks/footnotes.php

    r56298 r56332  
    212212    return get_metadata( 'post', $revision->ID, $field, true );
    213213}
    214 add_filter( 'wp_post_revision_field_footnotes', 'wp_get_footnotes_from_revision', 10, 3 );
     214add_filter( '_wp_post_revision_field_footnotes', 'wp_get_footnotes_from_revision', 10, 3 );
     215
     216/**
     217 * The REST API autosave endpoint doesn't save meta, so we can use the
     218 * `wp_creating_autosave` when it updates an exiting autosave, and
     219 * `_wp_put_post_revision` when it creates a new autosave.
     220 *
     221 * @since 6.3.0
     222 *
     223 * @param int|array $autosave The autosave ID or array.
     224 */
     225function _wp_rest_api_autosave_meta( $autosave ) {
     226    // Ensure it's a REST API request.
     227    if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {
     228        return;
     229    }
     230
     231    $body = rest_get_server()->get_raw_data();
     232    $body = json_decode( $body, true );
     233
     234    if ( ! isset( $body['meta']['footnotes'] ) ) {
     235        return;
     236    }
     237
     238    // `wp_creating_autosave` passes the array,
     239    // `_wp_put_post_revision` passes the ID.
     240    $id = is_int( $autosave ) ? $autosave : $autosave['ID'];
     241
     242    if ( ! $id ) {
     243        return;
     244    }
     245
     246    update_post_meta( $id, 'footnotes', $body['meta']['footnotes'] );
     247}
     248// See https://github.com/WordPress/wordpress-develop/blob/2103cb9966e57d452c94218bbc3171579b536a40/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php#L391C1-L391C1.
     249add_action( 'wp_creating_autosave', '_wp_rest_api_autosave_meta' );
     250// See https://github.com/WordPress/wordpress-develop/blob/2103cb9966e57d452c94218bbc3171579b536a40/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php#L398.
     251// Then https://github.com/WordPress/wordpress-develop/blob/2103cb9966e57d452c94218bbc3171579b536a40/src/wp-includes/revision.php#L367.
     252add_action( '_wp_put_post_revision', '_wp_rest_api_autosave_meta' );
     253
     254/**
     255 * This is a workaround for the autosave endpoint returning early if the
     256 * revision field are equal. The problem is that "footnotes" is not real
     257 * revision post field, so there's nothing to compare against.
     258 *
     259 * This trick sets the "footnotes" field (value doesn't matter), which will
     260 * cause the autosave endpoint to always update the latest revision. That should
     261 * be fine, it should be ok to update the revision even if nothing changed. Of
     262 * course, this is temporary fix.
     263 *
     264 * @since 6.3.0
     265 *
     266 * @param WP_Post         $prepared_post The prepared post object.
     267 * @param WP_REST_Request $request       The request object.
     268 *
     269 * See https://github.com/WordPress/wordpress-develop/blob/2103cb9966e57d452c94218bbc3171579b536a40/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php#L365-L384.
     270 * See https://github.com/WordPress/wordpress-develop/blob/2103cb9966e57d452c94218bbc3171579b536a40/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php#L219.
     271 */
     272function _wp_rest_api_force_autosave_difference( $prepared_post, $request ) {
     273    // We only want to be altering POST requests.
     274    if ( $request->get_method() !== 'POST' ) {
     275        return $prepared_post;
     276    }
     277
     278    // Only alter requests for the '/autosaves' route.
     279    if ( substr( $request->get_route(), -strlen( '/autosaves' ) ) !== '/autosaves' ) {
     280        return $prepared_post;
     281    }
     282
     283    $prepared_post->footnotes = '[]';
     284    return $prepared_post;
     285}
     286
     287add_filter( 'rest_pre_insert_post', '_wp_rest_api_force_autosave_difference', 10, 2 );
Note: See TracChangeset for help on using the changeset viewer.