函数文档

_wp_get_iframed_editor_assets()

💡 云策文档标注

概述

_wp_get_iframed_editor_assets() 函数用于收集需要在编辑器 iframe 中加载的块编辑器资源。它返回一个包含样式和脚本 HTML 字符串的数组,确保 iframe 内正确渲染内容。

关键要点

  • 函数返回数组,包含 'styles' 和 'scripts' 键,分别对应样式和脚本的 HTML 字符串
  • 通过临时替换全局 $wp_styles 和 $wp_scripts 实例来收集资源,避免影响主环境
  • 根据主题是否支持 theme.json 决定是否加载重置样式,以兼容经典主题
  • 使用过滤器控制仅加载前端资源,避免编辑器脚本在 iframe 中加载
  • 遍历所有注册的块类型,确保编辑器样式(editorStyle)被正确入队
  • 处理 emoji 样式以避免弃用消息干扰样式生成
  • 最终恢复原始全局实例,确保系统稳定性

代码示例

function _wp_get_iframed_editor_assets() {
    global $wp_styles, $wp_scripts;
    $current_wp_styles = $wp_styles;
    $current_wp_scripts = $wp_scripts;
    $wp_styles = new WP_Styles();
    $wp_scripts = new WP_Scripts();
    $wp_styles->registered = $current_wp_styles->registered;
    $wp_scripts->registered = $current_wp_scripts->registered;
    $wp_styles->done = wp_theme_has_theme_json() ? array( 'wp-reset-editor-styles' ) : array();
    wp_enqueue_script( 'wp-polyfill' );
    wp_enqueue_style( 'wp-edit-blocks' );
    if ( current_theme_supports( 'wp-block-styles' ) ) {
        wp_enqueue_style( 'wp-block-library-theme' );
    }
    add_filter( 'should_load_block_editor_scripts_and_styles', '__return_false' );
    do_action( 'enqueue_block_assets' );
    remove_filter( 'should_load_block_editor_scripts_and_styles', '__return_false' );
    $block_registry = WP_Block_Type_Registry::get_instance();
    foreach ( $block_registry->get_all_registered() as $block_type ) {
        if ( isset( $block_type->editor_style_handles ) && is_array( $block_type->editor_style_handles ) ) {
            foreach ( $block_type->editor_style_handles as $style_handle ) {
                wp_enqueue_style( $style_handle );
            }
        }
    }
    $has_emoji_styles = has_action( 'wp_print_styles', 'print_emoji_styles' );
    if ( $has_emoji_styles ) {
        remove_action( 'wp_print_styles', 'print_emoji_styles' );
    }
    ob_start();
    wp_print_styles();
    wp_print_font_faces();
    wp_print_font_faces_from_style_variations();
    $styles = ob_get_clean();
    if ( $has_emoji_styles ) {
        add_action( 'wp_print_styles', 'print_emoji_styles' );
    }
    ob_start();
    wp_print_head_scripts();
    wp_print_footer_scripts();
    $scripts = ob_get_clean();
    $wp_styles = $current_wp_styles;
    $wp_scripts = $current_wp_scripts;
    return array(
        'styles' => $styles,
        'scripts' => $scripts,
    );
}

注意事项

  • 此函数主要用于内部调用,如 get_block_editor_settings(),开发者通常不直接使用
  • 确保在 iframe 环境中正确加载资源,避免样式和脚本冲突
  • 注意主题兼容性处理,特别是经典主题与块主题的差异

📄 原文内容

Collect the block editor assets that need to be loaded into the editor’s iframe.

Return

array The block editor assets.

  • styles string|false
    String containing the HTML for styles.
  • scripts string|false
    String containing the HTML for scripts.

Source

function _wp_get_iframed_editor_assets() {
	global $wp_styles, $wp_scripts;

	// Keep track of the styles and scripts instance to restore later.
	$current_wp_styles  = $wp_styles;
	$current_wp_scripts = $wp_scripts;

	// Create new instances to collect the assets.
	$wp_styles  = new WP_Styles();
	$wp_scripts = new WP_Scripts();

	/*
	 * Register all currently registered styles and scripts. The actions that
	 * follow enqueue assets, but don't necessarily register them.
	 */
	$wp_styles->registered  = $current_wp_styles->registered;
	$wp_scripts->registered = $current_wp_scripts->registered;

	/*
	 * We generally do not need reset styles for the iframed editor.
	 * However, if it's a classic theme, margins will be added to every block,
	 * which is reset specifically for list items, so classic themes rely on
	 * these reset styles.
	 */
	$wp_styles->done =
		wp_theme_has_theme_json() ? array( 'wp-reset-editor-styles' ) : array();

	wp_enqueue_script( 'wp-polyfill' );
	// Enqueue the `editorStyle` handles for all core block, and dependencies.
	wp_enqueue_style( 'wp-edit-blocks' );

	if ( current_theme_supports( 'wp-block-styles' ) ) {
		wp_enqueue_style( 'wp-block-library-theme' );
	}

	/*
	 * We don't want to load EDITOR scripts in the iframe, only enqueue
	 * front-end assets for the content.
	 */
	add_filter( 'should_load_block_editor_scripts_and_styles', '__return_false' );
	do_action( 'enqueue_block_assets' );
	remove_filter( 'should_load_block_editor_scripts_and_styles', '__return_false' );

	$block_registry = WP_Block_Type_Registry::get_instance();

	/*
	 * Additionally, do enqueue `editorStyle` assets for all blocks, which
	 * contains editor-only styling for blocks (editor content).
	 */
	foreach ( $block_registry->get_all_registered() as $block_type ) {
		if ( isset( $block_type->editor_style_handles ) && is_array( $block_type->editor_style_handles ) ) {
			foreach ( $block_type->editor_style_handles as $style_handle ) {
				wp_enqueue_style( $style_handle );
			}
		}
	}

	/**
	 * Remove the deprecated `print_emoji_styles` handler.
	 * It avoids breaking style generation with a deprecation message.
	 */
	$has_emoji_styles = has_action( 'wp_print_styles', 'print_emoji_styles' );
	if ( $has_emoji_styles ) {
		remove_action( 'wp_print_styles', 'print_emoji_styles' );
	}

	ob_start();
	wp_print_styles();
	wp_print_font_faces();
	wp_print_font_faces_from_style_variations();
	$styles = ob_get_clean();

	if ( $has_emoji_styles ) {
		add_action( 'wp_print_styles', 'print_emoji_styles' );
	}

	ob_start();
	wp_print_head_scripts();
	wp_print_footer_scripts();
	$scripts = ob_get_clean();

	// Restore the original instances.
	$wp_styles  = $current_wp_styles;
	$wp_scripts = $current_wp_scripts;

	return array(
		'styles'  => $styles,
		'scripts' => $scripts,
	);
}

Hooks

do_action( ‘enqueue_block_assets’ )

Fires after enqueuing block assets for both editor and front-end.

Changelog

Version Description
6.0.0 Introduced.