WordPress.org

Make WordPress Core

Changeset 18561


Ignore:
Timestamp:
08/17/2011 09:02:43 PM (8 years ago)
Author:
nacin
Message:

Call _doing_it_wrong() individually in wp_scripts and wp_styles functions. Partially reverts [18556], [18557], removes wp_styles_init(), wp_scripts_init(). fixes #11526.

Location:
trunk/wp-includes
Files:
2 edited

Legend:

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

    r18557 r18561  
    2525
    2626    global $wp_scripts;
    27     if ( !is_a($wp_scripts, 'WP_Scripts') ) {
     27    if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
     28        if ( ! did_action( 'init' ) )
     29            _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     30                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     31
    2832        if ( !$handles )
    2933            return array(); // No need to instantiate if nothing is there.
     
    4852function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
    4953    global $wp_scripts;
    50 
    51     wp_scripts_init();
     54    if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
     55        if ( ! did_action( 'init' ) )
     56            _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     57                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     58        $wp_scripts = new WP_Scripts();
     59    }
    5260
    5361    $wp_scripts->add( $handle, $src, $deps, $ver );
     
    7684function wp_localize_script( $handle, $name, $data ) {
    7785    global $wp_scripts;
    78 
    79     wp_scripts_init();
     86    if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
     87        if ( ! did_action( 'init' ) )
     88            _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     89                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     90        return false;
     91    }
    8092
    8193    return $wp_scripts->add_script_data( $handle, $name, $data );
     
    90102function wp_deregister_script( $handle ) {
    91103    global $wp_scripts;
    92 
    93     wp_scripts_init();
     104    if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
     105        if ( ! did_action( 'init' ) )
     106            _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     107                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     108        $wp_scripts = new WP_Scripts();
     109    }
    94110
    95111    $wp_scripts->remove( $handle );
     
    106122function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
    107123    global $wp_scripts;
    108 
    109     wp_scripts_init();
     124    if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
     125        if ( ! did_action( 'init' ) )
     126            _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     127                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     128        $wp_scripts = new WP_Scripts();
     129    }
    110130
    111131    if ( $src ) {
     
    126146function wp_dequeue_script( $handle ) {
    127147    global $wp_scripts;
    128 
    129     wp_scripts_init();
     148    if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
     149        if ( ! did_action( 'init' ) )
     150            _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     151                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     152        $wp_scripts = new WP_Scripts();
     153    }
    130154
    131155    $wp_scripts->dequeue( $handle );
     
    146170function wp_script_is( $handle, $list = 'queue' ) {
    147171    global $wp_scripts;
    148 
    149     wp_scripts_init();
     172    if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
     173        if ( ! did_action( 'init' ) )
     174            _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     175                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     176        $wp_scripts = new WP_Scripts();
     177    }
    150178
    151179    $query = $wp_scripts->query( $handle, $list );
     
    156184    return $query;
    157185}
    158 
    159 /**
    160  * Initializes $wp_scripts global (if it hasn't already been initialized by a faulty plugin or theme).
    161  *
    162  * @since 3.3
    163  */
    164 function wp_scripts_init() {
    165     global $wp_scripts;
    166     static $done = false;
    167 
    168     if ( !$done && !is_a($wp_scripts, 'WP_Scripts') ) {
    169         if ( !did_action('after_setup_theme') ) // last action before init
    170             _doing_it_wrong( __FUNCTION__, __( '$wp_scripts should not be accessed before the "init" hook.' ), '3.3' );
    171 
    172         $wp_scripts = new WP_Scripts();
    173         $done = true;
    174     }
    175 }
    176 
  • trunk/wp-includes/functions.wp-styles.php

    r18557 r18561  
    2424
    2525    global $wp_styles;
    26     if ( !is_a($wp_styles, 'WP_Styles') ) {
     26    if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
     27        if ( ! did_action( 'init' ) )
     28            _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     29                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     30
    2731        if ( !$handles )
    2832            return array(); // No need to instantiate if nothing is there.
     
    4751function wp_add_inline_style( $handle, $data ) {
    4852    global $wp_styles;
    49     if ( !is_a($wp_styles, 'WP_Styles') )
    50         return false;
     53    if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
     54        if ( ! did_action( 'init' ) )
     55            _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     56                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     57        $wp_styles = new WP_Styles();
     58    }
    5159
    5260    return $wp_styles->add_inline_style( $handle, $data );
     
    7179function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
    7280    global $wp_styles;
    73 
    74     wp_styles_init();
     81    if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
     82        if ( ! did_action( 'init' ) )
     83            _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     84                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     85        $wp_styles = new WP_Styles();
     86    }
    7587
    7688    $wp_styles->add( $handle, $src, $deps, $ver, $media );
     
    88100function wp_deregister_style( $handle ) {
    89101    global $wp_styles;
    90 
    91     wp_styles_init();
     102    if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
     103        if ( ! did_action( 'init' ) )
     104            _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     105                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     106        $wp_styles = new WP_Styles();
     107    }
    92108
    93109    $wp_styles->remove( $handle );
     
    115131function wp_enqueue_style( $handle, $src = false, $deps = array(), $ver = false, $media = 'all' ) {
    116132    global $wp_styles;
    117 
    118     wp_styles_init();
     133    if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
     134        if ( ! did_action( 'init' ) )
     135            _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     136                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     137        $wp_styles = new WP_Styles();
     138    }
    119139
    120140    if ( $src ) {
     
    133153function wp_dequeue_style( $handle ) {
    134154    global $wp_styles;
    135 
    136     wp_styles_init();
     155    if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
     156        if ( ! did_action( 'init' ) )
     157            _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     158                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     159        $wp_styles = new WP_Styles();
     160    }
    137161
    138162    $wp_styles->dequeue( $handle );
     
    153177function wp_style_is( $handle, $list = 'queue' ) {
    154178    global $wp_styles;
    155 
    156     wp_styles_init();
     179    if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
     180        if ( ! did_action( 'init' ) )
     181            _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     182                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     183        $wp_styles = new WP_Styles();
     184    }
    157185
    158186    $query = $wp_styles->query( $handle, $list );
     
    163191    return $query;
    164192}
    165 
    166 /**
    167  * Initializes $wp_styles global (if it hasn't already been initialized by a faulty plugin or theme).
    168  *
    169  * @since 3.3
    170  */
    171 function wp_styles_init() {
    172     global $wp_styles;
    173     static $done = false;
    174 
    175     if ( !$done && !is_a($wp_styles, 'WP_Styles') ) {
    176         if ( !did_action('after_setup_theme') )
    177             _doing_it_wrong( __FUNCTION__, __( '$wp_styles should not be accessed before the "init" hook.' ), '3.3' );
    178 
    179         $wp_styles = new WP_Styles();
    180         $done = true;
    181     }
    182 }
    183 
Note: See TracChangeset for help on using the changeset viewer.