mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-04-01 11:14:36 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@48070 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -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 );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user