WordPress函数block_editor_rest_api_preload()给定块编辑器上下文预加载的 REST API
在 WordPress CMS内容管理系统中,block_editor_rest_api_preload()一个内置函数,block_editor_rest_api_preload()是WordPress 5.8中引入的一个函数,用于在使用Block Editor时预加载REST API资源,优化编辑器性能。这个函数的主要作用是预加载Block编辑器会用到的REST API资源,减少后续请求
推荐:[最新版]Swift Performance性能插件WordPress优化插件
block_editor_rest_api_preload()函数基本语法
描述
确定是否可以编辑此页面中的此网络
用法
block_editor_rest_api_preload(array('/wp/v2/posts?per_page=10', '/wp/v2/pages?per_page=10'), $block_editor_context);
- $preload_paths(字符串数组) – 这是必需的参数,它是要预加载的路径列表。
- $block_editor_context (WP_Block_Editor_Context) – 这是必需参数。它代表当前的块编辑器上下文。
推荐:WordPress函数block_template_part()打印特定的块模板
block_editor_rest_api_preload()函数
block_editor_rest_api_preload()让编辑器实例化和交互时不再重复发起REST请求,有效优化了编辑器性能,提升了用户体验。它利用了REST API预加载的方式实现了这个效果(源文件可参考这里)
function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) {
global $post, $wp_scripts, $wp_styles;
/**
* Filters the array of REST API paths that will be used to preloaded common data for the block editor.
*
* @since 5.8.0
*
* @param (string|string[])[] $preload_paths Array of paths to preload.
* @param WP_Block_Editor_Context $block_editor_context The current block editor context.
*/
$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;
/**
* Filters the array of paths that will be preloaded.
*
* Preload common data by specifying an array of REST API paths that will be preloaded.
*
* @since 5.0.0
* @deprecated 5.8.0 Use the 'block_editor_rest_api_preload_paths' filter instead.
*
* @param (string|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, $wp_scripts, and $wp_styles remain the same after
* API data is preloaded.
* Because API preloading can call the_content and other filters, plugins
* can unexpectedly modify the global $post or enqueue assets which are not
* intended for the block editor.
*/
$backup_global_post = ! empty( $post ) ? clone $post : $post;
$backup_wp_scripts = ! empty( $wp_scripts ) ? clone $wp_scripts : $wp_scripts;
$backup_wp_styles = ! empty( $wp_styles ) ? clone $wp_styles : $wp_styles;
foreach ( $preload_paths as &$path ) {
if ( is_string( $path ) && ! str_starts_with( $path, '/' ) ) {
$path = '/' . $path;
continue;
}
if ( is_array( $path ) && is_string( $path[0] ) && ! str_starts_with( $path[0], '/' ) ) {
$path[0] = '/' . $path[0];
}
}
unset( $path );
$preload_data = array_reduce(
$preload_paths,
'rest_preload_api_request',
array()
);
// Restore the global $post, $wp_scripts, and $wp_styles as they were before API preloading.
$post = $backup_global_post;
$wp_scripts = $backup_wp_scripts;
$wp_styles = $backup_wp_styles;
wp_add_inline_script(
'wp-api-fetch',
sprintf(
'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
wp_json_encode( $preload_data )
),
'after'
);
}
推荐:WordPress函数email_exists()确定给定的电子邮件是否存在
如何使用block_editor_rest_api_preload()
预加载帖子和页面,此代码将为块编辑器上下文预加载最新的5个帖子和5个页面。
$preload_paths = array('/wp/v2/posts?per_page=5', '/wp/v2/pages?per_page=5');
block_editor_rest_api_preload($preload_paths, $block_editor_context);
预加载特定帖子和页面,此代码将预加载ID为1的帖子和ID为2的页面。
$preload_paths = array('/wp/v2/posts/1', '/wp/v2/pages/2');
block_editor_rest_api_preload($preload_paths, $block_editor_context);
预加载具有特定状态的帖子,此代码将预加载所有具有“发布”状态的帖子。
$preload_paths = array('/wp/v2/posts?status=publish');
block_editor_rest_api_preload($preload_paths, $block_editor_context);
预加载特定类别的帖子,此代码将预加载ID为10的类别中的所有帖子。
$preload_paths = array('/wp/v2/posts?categories=10');
block_editor_rest_api_preload($preload_paths, $block_editor_context);
预加载用户,此代码将为块编辑器上下文预加载最新的5个用户
$preload_paths = array('/wp/v2/users?per_page=5');
block_editor_rest_api_preload($preload_paths, $block_editor_context);
推荐:WordPress函数capital_P_dangit()自动将WordPress中的P大写