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-admin/edit-form-blocks.php

    r50776 r50956  
    2424global $post_type, $post_type_object, $post, $title, $editor_styles, $wp_meta_boxes;
    2525
    26 $editor_name = 'post-editor';
     26$editor_name          = 'post-editor';
     27$block_editor_context = new WP_Block_Editor_Context( array( 'post' => $post ) );
    2728
    2829// Flag that we're loading the block editor.
     
    5960);
    6061
    61 
    62 /**
    63  * Preload common data by specifying an array of REST API paths that will be preloaded.
    64  *
    65  * Filters the array of paths that will be preloaded.
    66  *
    67  * @since 5.0.0
    68  *
    69  * @param string[] $preload_paths Array of paths to preload.
    70  * @param WP_Post  $post          Post being edited.
    71  */
    72 $preload_paths = apply_filters( 'block_editor_preload_paths', $preload_paths, $post );
    73 
    74 /*
    75  * Ensure the global $post remains the same after API data is preloaded.
    76  * Because API preloading can call the_content and other filters, plugins
    77  * can unexpectedly modify $post.
    78  */
    79 $backup_global_post = clone $post;
    80 
    81 $preload_data = array_reduce(
    82     $preload_paths,
    83     'rest_preload_api_request',
    84     array()
    85 );
    86 
    87 // Restore the global $post as it was before API preloading.
    88 $post = $backup_global_post;
    89 
    90 wp_add_inline_script(
    91     'wp-api-fetch',
    92     sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode( $preload_data ) ),
    93     'after'
    94 );
     62block_editor_rest_api_preload( $preload_paths, $block_editor_context );
    9563
    9664wp_add_inline_script(
Note: See TracChangeset for help on using the changeset viewer.