Make WordPress Core

Changeset 48070


Ignore:
Timestamp:
06/17/2020 10:14:36 AM (6 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.wp-scripts.php

    r47550 r48070  
    2020function wp_scripts() {
    2121        global $wp_scripts;
     22
    2223        if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
    2324                $wp_scripts = new WP_Scripts();
    2425        }
     26
    2527        return $wp_scripts;
    2628}
     
    3133 * @ignore
    3234 * @since 4.2.0
     35 * @since 5.5.0 Added the `$handle` parameter.
    3336 *
    3437 * @param string $function Function name.
    35  */
    36 function _wp_scripts_maybe_doing_it_wrong( $function ) {
    37         if ( did_action( 'init' ) || did_action( 'admin_enqueue_scripts' ) || did_action( 'wp_enqueue_scripts' ) || did_action( 'login_enqueue_scripts' ) ) {
     38 * @param string $handle   Optional. Name of the script or stylesheet that was
     39 *                         registered or enqueued too early. Default empty.
     40 */
     41function _wp_scripts_maybe_doing_it_wrong( $function, $handle = '' ) {
     42        if ( did_action( 'init' ) || did_action( 'wp_enqueue_scripts' )
     43                || did_action( 'admin_enqueue_scripts' ) || did_action( 'login_enqueue_scripts' )
     44        ) {
    3845                return;
     46        }
     47
     48        $message = sprintf(
     49                /* translators: 1: wp_enqueue_scripts, 2: admin_enqueue_scripts, 3: login_enqueue_scripts */
     50                __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     51                '<code>wp_enqueue_scripts</code>',
     52                '<code>admin_enqueue_scripts</code>',
     53                '<code>login_enqueue_scripts</code>'
     54        );
     55
     56        if ( $handle ) {
     57                $message .= ' ' . sprintf(
     58                        /* translators: %s: Name of the script or stylesheet. */
     59                        __( 'This notice was triggered by the %s handle.' ),
     60                        '<code>' . $handle . '</code>'
     61                );
    3962        }
    4063
    4164        _doing_it_wrong(
    4265                $function,
    43                 sprintf(
    44                         /* translators: 1: wp_enqueue_scripts, 2: admin_enqueue_scripts, 3: login_enqueue_scripts */
    45                         __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    46                         '<code>wp_enqueue_scripts</code>',
    47                         '<code>admin_enqueue_scripts</code>',
    48                         '<code>login_enqueue_scripts</code>'
    49                 ),
     66                $message,
    5067                '3.3.0'
    5168        );
     
    6986 */
    7087function wp_print_scripts( $handles = false ) {
     88        global $wp_scripts;
     89
    7190        /**
    7291         * Fires before scripts in the $handles queue are printed.
     
    7594         */
    7695        do_action( 'wp_print_scripts' );
     96
    7797        if ( '' === $handles ) { // For 'wp_head'.
    7898                $handles = false;
     
    81101        _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
    82102
    83         global $wp_scripts;
    84103        if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
    85104                if ( ! $handles ) {
     
    110129 */
    111130function wp_add_inline_script( $handle, $data, $position = 'after' ) {
    112         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     131        _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
    113132
    114133        if ( false !== stripos( $data, '</script>' ) ) {
     
    153172 */
    154173function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
     174        _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
     175
    155176        $wp_scripts = wp_scripts();
    156         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
    157177
    158178        $registered = $wp_scripts->add( $handle, $src, $deps, $ver );
     
    193213function wp_localize_script( $handle, $object_name, $l10n ) {
    194214        global $wp_scripts;
     215
    195216        if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
    196                 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     217                _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
    197218                return false;
    198219        }
     
    219240function wp_set_script_translations( $handle, $domain = 'default', $path = null ) {
    220241        global $wp_scripts;
     242
    221243        if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
    222                 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     244                _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
    223245                return false;
    224246        }
     
    240262 */
    241263function wp_deregister_script( $handle ) {
    242         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     264        _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
    243265
    244266        /**
     
    316338 */
    317339function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) {
     340        _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
     341
    318342        $wp_scripts = wp_scripts();
    319 
    320         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
    321343
    322344        if ( $src || $in_footer ) {
     
    345367 */
    346368function wp_dequeue_script( $handle ) {
    347         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     369        _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
    348370
    349371        wp_scripts()->dequeue( $handle );
     
    366388 */
    367389function wp_script_is( $handle, $list = 'enqueued' ) {
    368         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     390        _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
    369391
    370392        return (bool) wp_scripts()->query( $handle, $list );
  • trunk/src/wp-includes/functions.wp-styles.php

    r47204 r48070  
    2020function wp_styles() {
    2121        global $wp_styles;
     22
    2223        if ( ! ( $wp_styles instanceof WP_Styles ) ) {
    2324                $wp_styles = new WP_Styles();
    2425        }
     26
    2527        return $wp_styles;
    2628}
     
    4143 */
    4244function wp_print_styles( $handles = false ) {
     45        global $wp_styles;
     46
    4347        if ( '' === $handles ) { // For 'wp_head'.
    4448                $handles = false;
     
    5660        _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
    5761
    58         global $wp_styles;
    5962        if ( ! ( $wp_styles instanceof WP_Styles ) ) {
    6063                if ( ! $handles ) {
     
    8386 */
    8487function wp_add_inline_style( $handle, $data ) {
    85         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     88        _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
    8689
    8790        if ( false !== stripos( $data, '</style>' ) ) {
     
    125128 */
    126129function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
    127         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     130        _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
    128131
    129132        return wp_styles()->add( $handle, $src, $deps, $ver, $media );
     
    140143 */
    141144function wp_deregister_style( $handle ) {
    142         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     145        _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
    143146
    144147        wp_styles()->remove( $handle );
     
    169172 */
    170173function wp_enqueue_style( $handle, $src = '', $deps = array(), $ver = false, $media = 'all' ) {
    171         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     174        _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
    172175
    173176        $wp_styles = wp_styles();
     
    177180                $wp_styles->add( $_handle[0], $src, $deps, $ver, $media );
    178181        }
     182
    179183        $wp_styles->enqueue( $handle );
    180184}
     
    190194 */
    191195function wp_dequeue_style( $handle ) {
    192         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     196        _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
    193197
    194198        wp_styles()->dequeue( $handle );
     
    206210 */
    207211function wp_style_is( $handle, $list = 'enqueued' ) {
    208         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     212        _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
    209213
    210214        return (bool) wp_styles()->query( $handle, $list );
Note: See TracChangeset for help on using the changeset viewer.