Widgets: Defer register inline script in WP_Widget_Custom_HTML and WP_Widget_Text.

In [41376] an inline script was added to push `idBases` for the custom html and text widgets. However, this script is not used unless the widget script is output in the widget screen / customizer. Deferring registering this script until it is needed, results in a faster server response times. 


Props spacedmonkey, sakibmd, flixos90, westonruter.
Fixes #57864.

git-svn-id: https://develop.svn.wordpress.org/trunk@55553 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris
2023-03-15 11:36:52 +00:00
parent 6df9033741
commit 767f16f6a5
5 changed files with 7 additions and 10 deletions
+4 -4
View File
@@ -11941,7 +11941,7 @@
"from2-string": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/from2-string/-/from2-string-1.1.0.tgz",
"integrity": "sha512-m8vCh+KnXXXBtfF2VUbiYlQ+nczLcntB0BrtNgpmLkHylhObe9WF1b2LZjBBzrZzA6P4mkEla6ZYQoOUTG8cYA==",
"integrity": "sha1-GCgrJ9CKJnyzAwzSuLSw8hKvdSo=",
"requires": {
"from2": "^2.0.3"
}
@@ -21099,7 +21099,7 @@
"promise-polyfill": {
"version": "1.1.6",
"resolved": "https://registry.npmjs.org/promise-polyfill/-/promise-polyfill-1.1.6.tgz",
"integrity": "sha512-7rrONfyLkDEc7OJ5QBkqa4KI4EBhCd340xRuIUPGCfu13znS+vx+VDdrT9ODAJHlXm7w4lbxN3DRjyv58EuzDg=="
"integrity": "sha1-zQTv9G9clcOn0EVZHXm14+AfEtc="
},
"prompts": {
"version": "2.4.2",
@@ -23238,7 +23238,7 @@
"stream-from-promise": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/stream-from-promise/-/stream-from-promise-1.0.0.tgz",
"integrity": "sha512-j84KLkudt+gr8KJ21RB02btPLx61uGbrLnewsWz6QKmsz8/c4ZFqXw6mJh5+G4oRN7DgDxdbjPxnpySpg1mUig=="
"integrity": "sha1-djaH9913fkyJT2QIMz/Gs/yKYbs="
},
"stream-to-string": {
"version": "1.2.0",
@@ -24506,7 +24506,7 @@
"toposort": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/toposort/-/toposort-2.0.2.tgz",
"integrity": "sha512-0a5EOkAUp8D4moMi2W8ZF8jcga7BgZd91O/yabJCFY8az+XSzeGyTKs0Aoo897iV1Nj6guFq8orWDS96z91oGg=="
"integrity": "sha1-riF2gXXRVZ1IvvNUILL0li8JwzA="
},
"totalist": {
"version": "1.1.0",
@@ -69,8 +69,6 @@ class WP_Widget_Custom_HTML extends WP_Widget {
}
$this->registered = true;
wp_add_inline_script( 'custom-html-widgets', sprintf( 'wp.customHtmlWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) );
// Note that the widgets component in the customizer will also do
// the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts().
add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) );
@@ -216,6 +214,8 @@ class WP_Widget_Custom_HTML extends WP_Widget {
);
wp_enqueue_script( 'custom-html-widgets' );
wp_add_inline_script( 'custom-html-widgets', sprintf( 'wp.customHtmlWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) );
if ( empty( $settings ) ) {
$settings = array(
'disabled' => true,
@@ -56,8 +56,6 @@ class WP_Widget_Text extends WP_Widget {
}
$this->registered = true;
wp_add_inline_script( 'text-widgets', sprintf( 'wp.textWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) );
if ( $this->is_preview() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_preview_scripts' ) );
}
@@ -436,6 +434,7 @@ class WP_Widget_Text extends WP_Widget {
wp_enqueue_editor();
wp_enqueue_media();
wp_enqueue_script( 'text-widgets' );
wp_add_inline_script( 'text-widgets', sprintf( 'wp.textWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) );
wp_add_inline_script( 'text-widgets', 'wp.textWidgets.init();', 'after' );
}
@@ -67,7 +67,6 @@ class Tests_Widgets_wpWidgetCustomHtml extends WP_UnitTestCase {
$this->assertSame( 10, has_action( 'admin_print_scripts-widgets.php', array( $widget, 'enqueue_admin_scripts' ) ) );
$this->assertSame( 10, has_action( 'admin_footer-widgets.php', array( 'WP_Widget_Custom_HTML', 'render_control_template_scripts' ) ) );
$this->assertSame( 10, has_action( 'admin_head-widgets.php', array( 'WP_Widget_Custom_HTML', 'add_help_text' ) ) );
$this->assertContains( 'wp.customHtmlWidgets.idBases.push( "custom_html" );', wp_scripts()->registered['custom-html-widgets']->extra['after'] );
}
/**
@@ -67,7 +67,6 @@ class Tests_Widgets_wpWidgetText extends WP_UnitTestCase {
$this->assertSame( 10, has_action( 'admin_print_scripts-widgets.php', array( $widget, 'enqueue_admin_scripts' ) ) );
$this->assertSame( 10, has_action( 'admin_footer-widgets.php', array( 'WP_Widget_Text', 'render_control_template_scripts' ) ) );
$this->assertContains( 'wp.textWidgets.idBases.push( "text" );', wp_scripts()->registered['text-widgets']->extra['after'] );
$this->assertFalse( has_action( 'wp_enqueue_scripts', array( $widget, 'enqueue_preview_scripts' ) ) );
}