wp_check_widget_editor_deps()
云策文档标注
概述
wp_check_widget_editor_deps() 函数用于检测并显示冲突的小部件编辑器脚本和样式依赖,通过 _doing_it_wrong() 发出警告。主要解决 'wp-editor' 脚本与 'wp-edit-widgets' 或 'wp-customize-widgets' 脚本的冲突,以及 'wp-edit-post' 样式与小部件编辑器的冲突。
关键要点
- 函数检查是否同时入队了 'wp-edit-widgets' 或 'wp-customize-widgets' 脚本与 'wp-editor' 脚本,若冲突则触发 _doing_it_wrong() 警告。
- 同时检查是否同时入队了 'wp-edit-post' 样式与新的小部件编辑器脚本,若冲突也触发 _doing_it_wrong() 警告。
- 冲突源于 'wp-editor' 脚本模块覆盖了旧版 TinyMCE 编辑器模块,导致小部件编辑器无法正常工作。
- 相关问题和详细信息可参考 WordPress 核心 Trac 票证 #53569。
代码示例
function wp_check_widget_editor_deps() {
global $wp_scripts, $wp_styles;
if (
$wp_scripts->query( 'wp-edit-widgets', 'enqueued' ) ||
$wp_scripts->query( 'wp-customize-widgets', 'enqueued' )
) {
if ( $wp_scripts->query( 'wp-editor', 'enqueued' ) ) {
_doing_it_wrong(
'wp_enqueue_script()',
sprintf(
/* translators: 1: 'wp-editor', 2: 'wp-edit-widgets', 3: 'wp-customize-widgets'. */
__( '"%1$s" script should not be enqueued together with the new widgets editor (%2$s or %3$s).' ),
'wp-editor',
'wp-edit-widgets',
'wp-customize-widgets'
),
'5.8.0'
);
}
if ( $wp_styles->query( 'wp-edit-post', 'enqueued' ) ) {
_doing_it_wrong(
'wp_enqueue_style()',
sprintf(
/* translators: 1: 'wp-edit-post', 2: 'wp-edit-widgets', 3: 'wp-customize-widgets'. */
__( '"%1$s" style should not be enqueued together with the new widgets editor (%2$s or %3$s).' ),
'wp-edit-post',
'wp-edit-widgets',
'wp-customize-widgets'
),
'5.8.0'
);
}
}
}注意事项
- 此函数自 WordPress 5.8.0 版本引入,旨在帮助开发者避免在入队脚本和样式时产生冲突。
- 开发者应确保在开发插件或主题时,避免同时使用冲突的脚本和样式模块,以保障小部件编辑器的正常功能。
原文内容
Displays a _doing_it_wrong() message for conflicting widget editor scripts.
Description
The ‘wp-editor’ script module is exposed as window.wp.editor. This overrides the legacy TinyMCE editor module which is required by the widgets editor.
Because of that conflict, these two shouldn’t be enqueued together.
See https://core.trac.wordpress.org/ticket/53569.
There is also another conflict related to styles where the block widgets editor is hidden if a block enqueues ‘wp-edit-post’ stylesheet.
See https://core.trac.wordpress.org/ticket/53569.
Source
function wp_check_widget_editor_deps() {
global $wp_scripts, $wp_styles;
if (
$wp_scripts->query( 'wp-edit-widgets', 'enqueued' ) ||
$wp_scripts->query( 'wp-customize-widgets', 'enqueued' )
) {
if ( $wp_scripts->query( 'wp-editor', 'enqueued' ) ) {
_doing_it_wrong(
'wp_enqueue_script()',
sprintf(
/* translators: 1: 'wp-editor', 2: 'wp-edit-widgets', 3: 'wp-customize-widgets'. */
__( '"%1$s" script should not be enqueued together with the new widgets editor (%2$s or %3$s).' ),
'wp-editor',
'wp-edit-widgets',
'wp-customize-widgets'
),
'5.8.0'
);
}
if ( $wp_styles->query( 'wp-edit-post', 'enqueued' ) ) {
_doing_it_wrong(
'wp_enqueue_style()',
sprintf(
/* translators: 1: 'wp-edit-post', 2: 'wp-edit-widgets', 3: 'wp-customize-widgets'. */
__( '"%1$s" style should not be enqueued together with the new widgets editor (%2$s or %3$s).' ),
'wp-edit-post',
'wp-edit-widgets',
'wp-customize-widgets'
),
'5.8.0'
);
}
}
}
Changelog
| Version | Description |
|---|---|
| 5.8.0 | Introduced. |