Make WordPress Core


Ignore:
Timestamp:
05/24/2021 07:31:49 AM (4 years ago)
Author:
gziolo
Message:

Editor: Extract block_editor_rest_api_preload method for use with different editor screens

It is going to be used on the new widgets editor screen. This patch also introduced a new class WP_Block_Editor_Context that is going to be used with revised block editor filters to let extenders to keep their existing behavior. It should also allow to provide more settings through the context class as new screens get introduced like the navigation editor.

Props azaozz, chrisvanpatten, timothyblynjacobs, youknowriad.
Fixes #52920.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-editor.php

    r50920 r50956  
    270270    return $editor_settings;
    271271}
     272
     273/**
     274 * Preloads common data used with the block editor by specifying an array of
     275 * REST API paths that will be preloaded for a given block editor context.
     276 *
     277 * @since 5.8.0
     278 *
     279 * @global WP_Post $post Global post object.
     280 *
     281 * @param array                   $preload_paths        List of paths to preload.
     282 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
     283 *
     284 * @return void
     285 */
     286function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) {
     287    global $post;
     288
     289    /**
     290     * Filters the array of REST API paths that will be used to preloaded common data
     291     * to use with the block editor.
     292     *
     293     * @since 5.8.0
     294     *
     295     * @param string[] $preload_paths Array of paths to preload.
     296     */
     297    $preload_paths = apply_filters( 'block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context );
     298    if ( ! empty( $block_editor_context->post ) ) {
     299        $selected_post = $block_editor_context->post;
     300
     301        /**
     302         * Preload common data by specifying an array of REST API paths that will be preloaded.
     303         *
     304         * Filters the array of paths that will be preloaded.
     305         *
     306         * @since 5.0.0
     307         * @deprecated 5.8.0 The hook transitioned to support also screens that don't contain $post instance.
     308         *
     309         * @param string[] $preload_paths Array of paths to preload.
     310         * @param WP_Post  $selected_post Post being edited.
     311         */
     312        $preload_paths = apply_filters_deprecated( 'block_editor_preload_paths', array( $preload_paths, $selected_post ), '5.8.0', 'block_editor_rest_api_preload_paths' );
     313    }
     314
     315    if ( empty( $preload_paths ) ) {
     316        return;
     317    }
     318
     319    /*
     320     * Ensure the global $post remains the same after API data is preloaded.
     321     * Because API preloading can call the_content and other filters, plugins
     322     * can unexpectedly modify $post.
     323     */
     324    $backup_global_post = ! empty( $post ) ? clone $post : $post;
     325
     326    $preload_data = array_reduce(
     327        $preload_paths,
     328        'rest_preload_api_request',
     329        array()
     330    );
     331
     332    // Restore the global $post as it was before API preloading.
     333    $post = $backup_global_post;
     334
     335    wp_add_inline_script(
     336        'wp-api-fetch',
     337        sprintf(
     338            'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
     339            wp_json_encode( $preload_data )
     340        ),
     341        'after'
     342    );
     343}
Note: See TracChangeset for help on using the changeset viewer.