Make WordPress Core


Ignore:
Timestamp:
08/17/2011 05:48:13 AM (13 years ago)
Author:
azaozz
Message:

Add _doing_it_wrong() when a plugin or theme accesses $wp_scripts or $wp_styles too early (also fixes localization), props SergeyBiryukov, fixes #11526

File:
1 edited

Legend:

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

    r18490 r18556  
    4848function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
    4949    global $wp_scripts;
    50     if ( !is_a($wp_scripts, 'WP_Scripts') )
    51         $wp_scripts = new WP_Scripts();
     50
     51    wp_scripts_init();
    5252
    5353    $wp_scripts->add( $handle, $src, $deps, $ver );
     
    7676function wp_localize_script( $handle, $name, $data ) {
    7777    global $wp_scripts;
    78     if ( !is_a($wp_scripts, 'WP_Scripts') )
    79         return false;
     78
     79    wp_scripts_init();
    8080
    8181    return $wp_scripts->add_script_data( $handle, $name, $data );
     
    9090function wp_deregister_script( $handle ) {
    9191    global $wp_scripts;
    92     if ( !is_a($wp_scripts, 'WP_Scripts') )
    93         $wp_scripts = new WP_Scripts();
     92
     93    wp_scripts_init();
    9494
    9595    $wp_scripts->remove( $handle );
     
    106106function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
    107107    global $wp_scripts;
    108     if ( !is_a($wp_scripts, 'WP_Scripts') )
    109         $wp_scripts = new WP_Scripts();
     108
     109    wp_scripts_init();
    110110
    111111    if ( $src ) {
     
    126126function wp_dequeue_script( $handle ) {
    127127    global $wp_scripts;
    128     if ( !is_a($wp_scripts, 'WP_Scripts') )
    129         $wp_scripts = new WP_Scripts();
     128
     129    wp_scripts_init();
    130130
    131131    $wp_scripts->dequeue( $handle );
     
    146146function wp_script_is( $handle, $list = 'queue' ) {
    147147    global $wp_scripts;
    148     if ( !is_a($wp_scripts, 'WP_Scripts') )
    149         $wp_scripts = new WP_Scripts();
     148
     149    wp_scripts_init();
    150150
    151151    $query = $wp_scripts->query( $handle, $list );
     
    156156    return $query;
    157157}
     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 */
     164function 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            $func = debug_backtrace();
     171            $trace = !empty($func[1]['function']) ? $func[1]['function'] : __FUNCTION__;
     172
     173            _doing_it_wrong( $trace, __( '$wp_scripts should not be accessed before the "init" hook.' ), '3.3' );
     174        }
     175
     176        $wp_scripts = new WP_Scripts();
     177        $done = true;
     178    }
     179}
     180
Note: See TracChangeset for help on using the changeset viewer.