函数文档

block_editor_rest_api_preload()

💡 云策文档标注

概述

block_editor_rest_api_preload() 函数用于预加载块编辑器所需的通用数据,通过指定 REST API 路径数组来实现。它确保在预加载过程中全局变量如 $post、$wp_scripts 和 $wp_styles 保持不变,以避免插件意外修改。

关键要点

  • 函数接受两个参数:$preload_paths(必需,预加载路径数组)和 $block_editor_context(必需,当前块编辑器上下文)。
  • 提供过滤器 block_editor_rest_api_preload_paths 用于修改预加载路径,并有一个已弃用的过滤器 block_editor_preload_paths。
  • 在预加载前备份全局变量,预加载后恢复,以防止副作用影响块编辑器环境。
  • 自动为路径添加前导斜杠,确保 REST API 路径格式正确。
  • 使用 wp_add_inline_script 将预加载数据注入到 wp-api-fetch 脚本中,以优化前端性能。

代码示例

block_editor_rest_api_preload( array( '/wp/v2/posts', '/wp/v2/categories' ), $block_editor_context );

注意事项

  • 从 WordPress 5.8.0 版本开始引入,替代了旧有的预加载机制。
  • 使用已弃用的过滤器 block_editor_preload_paths 时需注意兼容性,建议迁移到 block_editor_rest_api_preload_paths。
  • 预加载路径应为字符串或字符串数组,函数会自动处理格式以确保以斜杠开头。

📄 原文内容

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.

Parameters

$preload_paths(string|string[])[]required
List of paths to preload.
$block_editor_contextWP_Block_Editor_Contextrequired
The current block editor context.

Source

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, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
		),
		'after'
	);
}

Hooks

apply_filters_deprecated( ‘block_editor_preload_paths’, (string|string[])[] $preload_paths, WP_Post $selected_post )

Filters the array of paths that will be preloaded.

apply_filters( ‘block_editor_rest_api_preload_paths’, (string|string[])[] $preload_paths, WP_Block_Editor_Context $block_editor_context )

Filters the array of REST API paths that will be used to preloaded common data for the block editor.

Changelog

Version Description
5.8.0 Introduced.