mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-12 04:40:26 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@50956 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -269,3 +269,75 @@ function get_block_editor_settings( $editor_name, $custom_settings = array() ) {
|
||||
|
||||
return $editor_settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Preloads common data used with the block editor by specifying an array of
|
||||
* REST API paths that will be preloaded for a given block editor context.
|
||||
*
|
||||
* @since 5.8.0
|
||||
*
|
||||
* @global WP_Post $post Global post object.
|
||||
*
|
||||
* @param array $preload_paths List of paths to preload.
|
||||
* @param WP_Block_Editor_Context $block_editor_context The current block editor context.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) {
|
||||
global $post;
|
||||
|
||||
/**
|
||||
* Filters the array of REST API paths that will be used to preloaded common data
|
||||
* to use with the block editor.
|
||||
*
|
||||
* @since 5.8.0
|
||||
*
|
||||
* @param string[] $preload_paths Array of paths to preload.
|
||||
*/
|
||||
$preload_paths = apply_filters( 'block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context );
|
||||
if ( ! empty( $block_editor_context->post ) ) {
|
||||
$selected_post = $block_editor_context->post;
|
||||
|
||||
/**
|
||||
* Preload common data by specifying an array of REST API paths that will be preloaded.
|
||||
*
|
||||
* Filters the array of paths that will be preloaded.
|
||||
*
|
||||
* @since 5.0.0
|
||||
* @deprecated 5.8.0 The hook transitioned to support also screens that don't contain $post instance.
|
||||
*
|
||||
* @param string[] $preload_paths Array of paths to preload.
|
||||
* @param WP_Post $selected_post Post being edited.
|
||||
*/
|
||||
$preload_paths = apply_filters_deprecated( 'block_editor_preload_paths', array( $preload_paths, $selected_post ), '5.8.0', 'block_editor_rest_api_preload_paths' );
|
||||
}
|
||||
|
||||
if ( empty( $preload_paths ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Ensure the global $post remains the same after API data is preloaded.
|
||||
* Because API preloading can call the_content and other filters, plugins
|
||||
* can unexpectedly modify $post.
|
||||
*/
|
||||
$backup_global_post = ! empty( $post ) ? clone $post : $post;
|
||||
|
||||
$preload_data = array_reduce(
|
||||
$preload_paths,
|
||||
'rest_preload_api_request',
|
||||
array()
|
||||
);
|
||||
|
||||
// Restore the global $post as it was before API preloading.
|
||||
$post = $backup_global_post;
|
||||
|
||||
wp_add_inline_script(
|
||||
'wp-api-fetch',
|
||||
sprintf(
|
||||
'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
|
||||
wp_json_encode( $preload_data )
|
||||
),
|
||||
'after'
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user