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

git-svn-id: http://svn.automattic.com/wordpress/trunk@18556 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
azaozz 2011-08-17 05:48:13 +00:00
parent 05b62b9b8e
commit e5e90d732b
2 changed files with 68 additions and 22 deletions

View File

@ -47,8 +47,8 @@ function wp_print_scripts( $handles = false ) {
*/
function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
global $wp_scripts;
if ( !is_a($wp_scripts, 'WP_Scripts') )
$wp_scripts = new WP_Scripts();
wp_scripts_init();
$wp_scripts->add( $handle, $src, $deps, $ver );
if ( $in_footer )
@ -75,8 +75,8 @@ function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_f
*/
function wp_localize_script( $handle, $name, $data ) {
global $wp_scripts;
if ( !is_a($wp_scripts, 'WP_Scripts') )
return false;
wp_scripts_init();
return $wp_scripts->add_script_data( $handle, $name, $data );
}
@ -89,8 +89,8 @@ function wp_localize_script( $handle, $name, $data ) {
*/
function wp_deregister_script( $handle ) {
global $wp_scripts;
if ( !is_a($wp_scripts, 'WP_Scripts') )
$wp_scripts = new WP_Scripts();
wp_scripts_init();
$wp_scripts->remove( $handle );
}
@ -105,8 +105,8 @@ function wp_deregister_script( $handle ) {
*/
function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
global $wp_scripts;
if ( !is_a($wp_scripts, 'WP_Scripts') )
$wp_scripts = new WP_Scripts();
wp_scripts_init();
if ( $src ) {
$_handle = explode('?', $handle);
@ -125,8 +125,8 @@ function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false
*/
function wp_dequeue_script( $handle ) {
global $wp_scripts;
if ( !is_a($wp_scripts, 'WP_Scripts') )
$wp_scripts = new WP_Scripts();
wp_scripts_init();
$wp_scripts->dequeue( $handle );
}
@ -145,8 +145,8 @@ function wp_dequeue_script( $handle ) {
*/
function wp_script_is( $handle, $list = 'queue' ) {
global $wp_scripts;
if ( !is_a($wp_scripts, 'WP_Scripts') )
$wp_scripts = new WP_Scripts();
wp_scripts_init();
$query = $wp_scripts->query( $handle, $list );
@ -155,3 +155,26 @@ function wp_script_is( $handle, $list = 'queue' ) {
return $query;
}
/**
* Initializes $wp_scripts global (if it hasn't already been initialized by a faulty plugin or theme).
*
* @since 3.3
*/
function wp_scripts_init() {
global $wp_scripts;
static $done = false;
if ( !$done && !is_a($wp_scripts, 'WP_Scripts') ) {
if ( !did_action('after_setup_theme') ) { // last action before init
$func = debug_backtrace();
$trace = !empty($func[1]['function']) ? $func[1]['function'] : __FUNCTION__;
_doing_it_wrong( $trace, __( '$wp_scripts should not be accessed before the "init" hook.' ), '3.3' );
}
$wp_scripts = new WP_Scripts();
$done = true;
}
}

View File

@ -70,8 +70,8 @@ function wp_add_inline_style( $handle, $data ) {
*/
function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
global $wp_styles;
if ( !is_a($wp_styles, 'WP_Styles') )
$wp_styles = new WP_Styles();
wp_styles_init();
$wp_styles->add( $handle, $src, $deps, $ver, $media );
}
@ -87,8 +87,8 @@ function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media
*/
function wp_deregister_style( $handle ) {
global $wp_styles;
if ( !is_a($wp_styles, 'WP_Styles') )
$wp_styles = new WP_Styles();
wp_styles_init();
$wp_styles->remove( $handle );
}
@ -114,8 +114,8 @@ function wp_deregister_style( $handle ) {
*/
function wp_enqueue_style( $handle, $src = false, $deps = array(), $ver = false, $media = 'all' ) {
global $wp_styles;
if ( !is_a($wp_styles, 'WP_Styles') )
$wp_styles = new WP_Styles();
wp_styles_init();
if ( $src ) {
$_handle = explode('?', $handle);
@ -132,8 +132,8 @@ function wp_enqueue_style( $handle, $src = false, $deps = array(), $ver = false,
*/
function wp_dequeue_style( $handle ) {
global $wp_styles;
if ( !is_a($wp_styles, 'WP_Styles') )
$wp_styles = new WP_Styles();
wp_styles_init();
$wp_styles->dequeue( $handle );
}
@ -152,8 +152,8 @@ function wp_dequeue_style( $handle ) {
*/
function wp_style_is( $handle, $list = 'queue' ) {
global $wp_styles;
if ( !is_a($wp_styles, 'WP_Styles') )
$wp_styles = new WP_Styles();
wp_styles_init();
$query = $wp_styles->query( $handle, $list );
@ -162,3 +162,26 @@ function wp_style_is( $handle, $list = 'queue' ) {
return $query;
}
/**
* Initializes $wp_styles global (if it hasn't already been initialized by a faulty plugin or theme).
*
* @since 3.3
*/
function wp_styles_init() {
global $wp_styles;
static $done = false;
if ( !$done && !is_a($wp_styles, 'WP_Styles') ) {
if ( !did_action('after_setup_theme') ) {
$func = debug_backtrace();
$trace = !empty($func[1]['function']) ? $func[1]['function'] : __FUNCTION__;
_doing_it_wrong( $trace, __( '$wp_styles should not be accessed before the "init" hook.' ), '3.3' );
}
$wp_styles = new WP_Styles();
$done = true;
}
}