WordPress.org

Make WordPress Core

Changeset 18556


Ignore:
Timestamp:
08/17/2011 05:48:13 AM (8 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

Location:
trunk/wp-includes
Files:
2 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
  • trunk/wp-includes/functions.wp-styles.php

    r18480 r18556  
    7171function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
    7272    global $wp_styles;
    73     if ( !is_a($wp_styles, 'WP_Styles') )
    74         $wp_styles = new WP_Styles();
     73
     74    wp_styles_init();
    7575
    7676    $wp_styles->add( $handle, $src, $deps, $ver, $media );
     
    8888function wp_deregister_style( $handle ) {
    8989    global $wp_styles;
    90     if ( !is_a($wp_styles, 'WP_Styles') )
    91         $wp_styles = new WP_Styles();
     90
     91    wp_styles_init();
    9292
    9393    $wp_styles->remove( $handle );
     
    115115function wp_enqueue_style( $handle, $src = false, $deps = array(), $ver = false, $media = 'all' ) {
    116116    global $wp_styles;
    117     if ( !is_a($wp_styles, 'WP_Styles') )
    118         $wp_styles = new WP_Styles();
     117
     118    wp_styles_init();
    119119
    120120    if ( $src ) {
     
    133133function wp_dequeue_style( $handle ) {
    134134    global $wp_styles;
    135     if ( !is_a($wp_styles, 'WP_Styles') )
    136         $wp_styles = new WP_Styles();
     135
     136    wp_styles_init();
    137137
    138138    $wp_styles->dequeue( $handle );
     
    153153function wp_style_is( $handle, $list = 'queue' ) {
    154154    global $wp_styles;
    155     if ( !is_a($wp_styles, 'WP_Styles') )
    156         $wp_styles = new WP_Styles();
     155
     156    wp_styles_init();
    157157
    158158    $query = $wp_styles->query( $handle, $list );
     
    163163    return $query;
    164164}
     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 */
     171function 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            $func = debug_backtrace();
     178            $trace = !empty($func[1]['function']) ? $func[1]['function'] : __FUNCTION__;
     179
     180            _doing_it_wrong( $trace, __( '$wp_styles should not be accessed before the "init" hook.' ), '3.3' );
     181        }
     182
     183        $wp_styles = new WP_Styles();
     184        $done = true;
     185    }
     186}
     187
Note: See TracChangeset for help on using the changeset viewer.