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.

git-svn-id: http://svn.automattic.com/wordpress/trunk@18561 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
nacin 2011-08-17 21:02:43 +00:00
parent e53fe816b2
commit 51ca772adf
2 changed files with 82 additions and 64 deletions

View File

@ -25,6 +25,10 @@ function wp_print_scripts( $handles = false ) {
global $wp_scripts; global $wp_scripts;
if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) { if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
if ( ! did_action( 'init' ) )
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
if ( !$handles ) if ( !$handles )
return array(); // No need to instantiate if nothing is there. return array(); // No need to instantiate if nothing is there.
else else
@ -47,8 +51,12 @@ function wp_print_scripts( $handles = false ) {
*/ */
function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) { function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
global $wp_scripts; global $wp_scripts;
if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
wp_scripts_init(); if ( ! did_action( 'init' ) )
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
$wp_scripts = new WP_Scripts();
}
$wp_scripts->add( $handle, $src, $deps, $ver ); $wp_scripts->add( $handle, $src, $deps, $ver );
if ( $in_footer ) if ( $in_footer )
@ -75,8 +83,12 @@ function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_f
*/ */
function wp_localize_script( $handle, $name, $data ) { function wp_localize_script( $handle, $name, $data ) {
global $wp_scripts; global $wp_scripts;
if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
wp_scripts_init(); if ( ! did_action( 'init' ) )
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
return false;
}
return $wp_scripts->add_script_data( $handle, $name, $data ); return $wp_scripts->add_script_data( $handle, $name, $data );
} }
@ -89,8 +101,12 @@ function wp_localize_script( $handle, $name, $data ) {
*/ */
function wp_deregister_script( $handle ) { function wp_deregister_script( $handle ) {
global $wp_scripts; global $wp_scripts;
if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
wp_scripts_init(); if ( ! did_action( 'init' ) )
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
$wp_scripts = new WP_Scripts();
}
$wp_scripts->remove( $handle ); $wp_scripts->remove( $handle );
} }
@ -105,8 +121,12 @@ function wp_deregister_script( $handle ) {
*/ */
function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) { function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
global $wp_scripts; global $wp_scripts;
if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
wp_scripts_init(); if ( ! did_action( 'init' ) )
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
$wp_scripts = new WP_Scripts();
}
if ( $src ) { if ( $src ) {
$_handle = explode('?', $handle); $_handle = explode('?', $handle);
@ -125,8 +145,12 @@ function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false
*/ */
function wp_dequeue_script( $handle ) { function wp_dequeue_script( $handle ) {
global $wp_scripts; global $wp_scripts;
if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
wp_scripts_init(); if ( ! did_action( 'init' ) )
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
$wp_scripts = new WP_Scripts();
}
$wp_scripts->dequeue( $handle ); $wp_scripts->dequeue( $handle );
} }
@ -145,8 +169,12 @@ function wp_dequeue_script( $handle ) {
*/ */
function wp_script_is( $handle, $list = 'queue' ) { function wp_script_is( $handle, $list = 'queue' ) {
global $wp_scripts; global $wp_scripts;
if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
wp_scripts_init(); if ( ! did_action( 'init' ) )
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
$wp_scripts = new WP_Scripts();
}
$query = $wp_scripts->query( $handle, $list ); $query = $wp_scripts->query( $handle, $list );
@ -155,22 +183,3 @@ function wp_script_is( $handle, $list = 'queue' ) {
return $query; 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
_doing_it_wrong( __FUNCTION__, __( '$wp_scripts should not be accessed before the "init" hook.' ), '3.3' );
$wp_scripts = new WP_Scripts();
$done = true;
}
}

View File

@ -24,6 +24,10 @@ function wp_print_styles( $handles = false ) {
global $wp_styles; global $wp_styles;
if ( ! is_a( $wp_styles, 'WP_Styles' ) ) { if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
if ( ! did_action( 'init' ) )
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
if ( !$handles ) if ( !$handles )
return array(); // No need to instantiate if nothing is there. return array(); // No need to instantiate if nothing is there.
else else
@ -46,8 +50,12 @@ function wp_print_styles( $handles = false ) {
*/ */
function wp_add_inline_style( $handle, $data ) { function wp_add_inline_style( $handle, $data ) {
global $wp_styles; global $wp_styles;
if ( !is_a($wp_styles, 'WP_Styles') ) if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
return false; if ( ! did_action( 'init' ) )
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
$wp_styles = new WP_Styles();
}
return $wp_styles->add_inline_style( $handle, $data ); return $wp_styles->add_inline_style( $handle, $data );
} }
@ -70,8 +78,12 @@ function wp_add_inline_style( $handle, $data ) {
*/ */
function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) { function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
global $wp_styles; global $wp_styles;
if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
wp_styles_init(); if ( ! did_action( 'init' ) )
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
$wp_styles = new WP_Styles();
}
$wp_styles->add( $handle, $src, $deps, $ver, $media ); $wp_styles->add( $handle, $src, $deps, $ver, $media );
} }
@ -87,8 +99,12 @@ function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media
*/ */
function wp_deregister_style( $handle ) { function wp_deregister_style( $handle ) {
global $wp_styles; global $wp_styles;
if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
wp_styles_init(); if ( ! did_action( 'init' ) )
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
$wp_styles = new WP_Styles();
}
$wp_styles->remove( $handle ); $wp_styles->remove( $handle );
} }
@ -114,8 +130,12 @@ function wp_deregister_style( $handle ) {
*/ */
function wp_enqueue_style( $handle, $src = false, $deps = array(), $ver = false, $media = 'all' ) { function wp_enqueue_style( $handle, $src = false, $deps = array(), $ver = false, $media = 'all' ) {
global $wp_styles; global $wp_styles;
if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
wp_styles_init(); if ( ! did_action( 'init' ) )
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
$wp_styles = new WP_Styles();
}
if ( $src ) { if ( $src ) {
$_handle = explode('?', $handle); $_handle = explode('?', $handle);
@ -132,8 +152,12 @@ function wp_enqueue_style( $handle, $src = false, $deps = array(), $ver = false,
*/ */
function wp_dequeue_style( $handle ) { function wp_dequeue_style( $handle ) {
global $wp_styles; global $wp_styles;
if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
wp_styles_init(); if ( ! did_action( 'init' ) )
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
$wp_styles = new WP_Styles();
}
$wp_styles->dequeue( $handle ); $wp_styles->dequeue( $handle );
} }
@ -152,8 +176,12 @@ function wp_dequeue_style( $handle ) {
*/ */
function wp_style_is( $handle, $list = 'queue' ) { function wp_style_is( $handle, $list = 'queue' ) {
global $wp_styles; global $wp_styles;
if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
wp_styles_init(); if ( ! did_action( 'init' ) )
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
$wp_styles = new WP_Styles();
}
$query = $wp_styles->query( $handle, $list ); $query = $wp_styles->query( $handle, $list );
@ -162,22 +190,3 @@ function wp_style_is( $handle, $list = 'queue' ) {
return $query; 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') )
_doing_it_wrong( __FUNCTION__, __( '$wp_styles should not be accessed before the "init" hook.' ), '3.3' );
$wp_styles = new WP_Styles();
$done = true;
}
}