Script Loader: Include the script or style handle in _wp_scripts_maybe_doing_it_wrong() message.

This makes the message more helpful and allows for easier debugging.

Props janthiel.
Fixes #50406.
Built from https://develop.svn.wordpress.org/trunk@48070


git-svn-id: http://core.svn.wordpress.org/trunk@47837 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2020-06-17 10:16:08 +00:00
parent 41f70ae5f8
commit a9a6b25cae
3 changed files with 53 additions and 27 deletions

View File

@ -19,9 +19,11 @@
*/
function wp_scripts() {
global $wp_scripts;
if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
$wp_scripts = new WP_Scripts();
}
return $wp_scripts;
}
@ -30,23 +32,38 @@ function wp_scripts() {
*
* @ignore
* @since 4.2.0
* @since 5.5.0 Added the `$handle` parameter.
*
* @param string $function Function name.
* @param string $handle Optional. Name of the script or stylesheet that was
* registered or enqueued too early. Default empty.
*/
function _wp_scripts_maybe_doing_it_wrong( $function ) {
if ( did_action( 'init' ) || did_action( 'admin_enqueue_scripts' ) || did_action( 'wp_enqueue_scripts' ) || did_action( 'login_enqueue_scripts' ) ) {
function _wp_scripts_maybe_doing_it_wrong( $function, $handle = '' ) {
if ( did_action( 'init' ) || did_action( 'wp_enqueue_scripts' )
|| did_action( 'admin_enqueue_scripts' ) || did_action( 'login_enqueue_scripts' )
) {
return;
}
$message = sprintf(
/* translators: 1: wp_enqueue_scripts, 2: admin_enqueue_scripts, 3: login_enqueue_scripts */
__( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
'<code>wp_enqueue_scripts</code>',
'<code>admin_enqueue_scripts</code>',
'<code>login_enqueue_scripts</code>'
);
if ( $handle ) {
$message .= ' ' . sprintf(
/* translators: %s: Name of the script or stylesheet. */
__( 'This notice was triggered by the %s handle.' ),
'<code>' . $handle . '</code>'
);
}
_doing_it_wrong(
$function,
sprintf(
/* translators: 1: wp_enqueue_scripts, 2: admin_enqueue_scripts, 3: login_enqueue_scripts */
__( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
'<code>wp_enqueue_scripts</code>',
'<code>admin_enqueue_scripts</code>',
'<code>login_enqueue_scripts</code>'
),
$message,
'3.3.0'
);
}
@ -68,19 +85,21 @@ function _wp_scripts_maybe_doing_it_wrong( $function ) {
* @return string[] On success, an array of handles of processed WP_Dependencies items; otherwise, an empty array.
*/
function wp_print_scripts( $handles = false ) {
global $wp_scripts;
/**
* Fires before scripts in the $handles queue are printed.
*
* @since 2.1.0
*/
do_action( 'wp_print_scripts' );
if ( '' === $handles ) { // For 'wp_head'.
$handles = false;
}
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
global $wp_scripts;
if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
if ( ! $handles ) {
return array(); // No need to instantiate if nothing is there.
@ -109,7 +128,7 @@ function wp_print_scripts( $handles = false ) {
* @return bool True on success, false on failure.
*/
function wp_add_inline_script( $handle, $data, $position = 'after' ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
if ( false !== stripos( $data, '</script>' ) ) {
_doing_it_wrong(
@ -152,8 +171,9 @@ function wp_add_inline_script( $handle, $data, $position = 'after' ) {
* @return bool Whether the script has been registered. True on success, false on failure.
*/
function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
$wp_scripts = wp_scripts();
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
$registered = $wp_scripts->add( $handle, $src, $deps, $ver );
if ( $in_footer ) {
@ -192,8 +212,9 @@ function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_f
*/
function wp_localize_script( $handle, $object_name, $l10n ) {
global $wp_scripts;
if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
return false;
}
@ -218,8 +239,9 @@ function wp_localize_script( $handle, $object_name, $l10n ) {
*/
function wp_set_script_translations( $handle, $domain = 'default', $path = null ) {
global $wp_scripts;
if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
return false;
}
@ -239,7 +261,7 @@ function wp_set_script_translations( $handle, $domain = 'default', $path = null
* @param string $handle Name of the script to be removed.
*/
function wp_deregister_script( $handle ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
/**
* Do not allow accidental or negligent de-registering of critical scripts in the admin.
@ -315,9 +337,9 @@ function wp_deregister_script( $handle ) {
* Default 'false'.
*/
function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) {
$wp_scripts = wp_scripts();
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
$wp_scripts = wp_scripts();
if ( $src || $in_footer ) {
$_handle = explode( '?', $handle );
@ -344,7 +366,7 @@ function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $
* @param string $handle Name of the script to be removed.
*/
function wp_dequeue_script( $handle ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
wp_scripts()->dequeue( $handle );
}
@ -365,7 +387,7 @@ function wp_dequeue_script( $handle ) {
* @return bool Whether the script is queued.
*/
function wp_script_is( $handle, $list = 'enqueued' ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
return (bool) wp_scripts()->query( $handle, $list );
}

View File

@ -19,9 +19,11 @@
*/
function wp_styles() {
global $wp_styles;
if ( ! ( $wp_styles instanceof WP_Styles ) ) {
$wp_styles = new WP_Styles();
}
return $wp_styles;
}
@ -40,6 +42,8 @@ function wp_styles() {
* @return string[] On success, an array of handles of processed WP_Dependencies items; otherwise, an empty array.
*/
function wp_print_styles( $handles = false ) {
global $wp_styles;
if ( '' === $handles ) { // For 'wp_head'.
$handles = false;
}
@ -55,7 +59,6 @@ function wp_print_styles( $handles = false ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
global $wp_styles;
if ( ! ( $wp_styles instanceof WP_Styles ) ) {
if ( ! $handles ) {
return array(); // No need to instantiate if nothing is there.
@ -82,7 +85,7 @@ function wp_print_styles( $handles = false ) {
* @return bool True on success, false on failure.
*/
function wp_add_inline_style( $handle, $data ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
if ( false !== stripos( $data, '</style>' ) ) {
_doing_it_wrong(
@ -124,7 +127,7 @@ function wp_add_inline_style( $handle, $data ) {
* @return bool Whether the style has been registered. True on success, false on failure.
*/
function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
return wp_styles()->add( $handle, $src, $deps, $ver, $media );
}
@ -139,7 +142,7 @@ function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media
* @param string $handle Name of the stylesheet to be removed.
*/
function wp_deregister_style( $handle ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
wp_styles()->remove( $handle );
}
@ -168,7 +171,7 @@ function wp_deregister_style( $handle ) {
* '(orientation: portrait)' and '(max-width: 640px)'.
*/
function wp_enqueue_style( $handle, $src = '', $deps = array(), $ver = false, $media = 'all' ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
$wp_styles = wp_styles();
@ -176,6 +179,7 @@ function wp_enqueue_style( $handle, $src = '', $deps = array(), $ver = false, $m
$_handle = explode( '?', $handle );
$wp_styles->add( $_handle[0], $src, $deps, $ver, $media );
}
$wp_styles->enqueue( $handle );
}
@ -189,7 +193,7 @@ function wp_enqueue_style( $handle, $src = '', $deps = array(), $ver = false, $m
* @param string $handle Name of the stylesheet to be removed.
*/
function wp_dequeue_style( $handle ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
wp_styles()->dequeue( $handle );
}
@ -205,7 +209,7 @@ function wp_dequeue_style( $handle ) {
* @return bool Whether style is queued.
*/
function wp_style_is( $handle, $list = 'enqueued' ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
return (bool) wp_styles()->query( $handle, $list );
}

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.5-alpha-48069';
$wp_version = '5.5-alpha-48070';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.