Make WordPress Core


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