WordPress.org

Make WordPress Core

Ticket #11526: 11526.doing_it_wrong.patch

File 11526.doing_it_wrong.patch, 3.3 KB (added by jczorkmid, 8 years ago)

Here's an attempt at a patch for the script related functions. I tested it with a default install, and with "bad" plugin and a "bad" theme (both of which triggered the message).

  • wp-includes/functions.wp-scripts.php

     
    4747 */
    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') )
     50        if ( !is_a($wp_scripts, 'WP_Scripts') ) {
     51                _doing_it_wrong(__FUNCTION__, __( '$wp_scripts should not be accessed before init hook.' ), '3.3' );
    5152                $wp_scripts = new WP_Scripts();
    52 
     53        }
    5354        $wp_scripts->add( $handle, $src, $deps, $ver );
    5455        if ( $in_footer )
    5556                $wp_scripts->add_data( $handle, 'group', 1 );
     
    6566 */
    6667function wp_localize_script( $handle, $object_name, $l10n ) {
    6768        global $wp_scripts;
    68         if ( !is_a($wp_scripts, 'WP_Scripts') )
     69        if ( !is_a($wp_scripts, 'WP_Scripts') ) {
     70                _doing_it_wrong(__FUNCTION__, __( '$wp_scripts should not be accessed before init hook.' ), '3.3' );
    6971                return false;
     72        }
    7073
    7174        return $wp_scripts->localize( $handle, $object_name, $l10n );
    7275}
     
    7982 */
    8083function wp_deregister_script( $handle ) {
    8184        global $wp_scripts;
    82         if ( !is_a($wp_scripts, 'WP_Scripts') )
     85        if ( !is_a($wp_scripts, 'WP_Scripts') ) {
     86                _doing_it_wrong(__FUNCTION__, __( '$wp_scripts should not be accessed before init hook.' ), '3.3' );
    8387                $wp_scripts = new WP_Scripts();
     88        }
    8489
    8590        $wp_scripts->remove( $handle );
    8691}
     
    95100 */
    96101function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
    97102        global $wp_scripts;
    98         if ( !is_a($wp_scripts, 'WP_Scripts') )
     103        if ( !is_a($wp_scripts, 'WP_Scripts') ) {
     104                _doing_it_wrong(__FUNCTION__, __( '$wp_scripts should not be accessed before init hook.' ), '3.3' );
    99105                $wp_scripts = new WP_Scripts();
     106        }
    100107
    101108        if ( $src ) {
    102109                $_handle = explode('?', $handle);
     
    115122 */
    116123function wp_dequeue_script( $handle ) {
    117124        global $wp_scripts;
    118         if ( !is_a($wp_scripts, 'WP_Scripts') )
     125        if ( !is_a($wp_scripts, 'WP_Scripts') ) {
     126                _doing_it_wrong(__FUNCTION__, __( '$wp_scripts should not be accessed before init hook.' ), '3.3' );
    119127                $wp_scripts = new WP_Scripts();
     128        }
    120129
    121130        $wp_scripts->dequeue( $handle );
    122131}
     
    135144 */
    136145function wp_script_is( $handle, $list = 'queue' ) {
    137146        global $wp_scripts;
    138         if ( !is_a($wp_scripts, 'WP_Scripts') )
     147        if ( !is_a($wp_scripts, 'WP_Scripts') ) {
     148                _doing_it_wrong(__FUNCTION__, __( '$wp_scripts should not be accessed before init hook.' ), '3.3' );
    139149                $wp_scripts = new WP_Scripts();
     150        }
    140151
    141152        $query = $wp_scripts->query( $handle, $list );
    142153
  • wp-includes/script-loader.php

     
    793793        }
    794794}
    795795
     796/**
     797 * Initializes $wp_scripts global (if it hasn't already been initialized by a faulty plugin or theme).
     798 *
     799 * @since 3.3
     800 */
     801function wp_scripts_init() {
     802        global $wp_scripts;
     803        if ( !is_a($wp_scripts, 'WP_Scripts') ) {
     804                $wp_scripts = new WP_Scripts();
     805        }
     806}
     807
     808add_action( 'init', 'wp_scripts_init', 0 ); // highest priority
    796809add_action( 'wp_default_scripts', 'wp_default_scripts' );
    797810add_filter( 'wp_print_scripts', 'wp_just_in_time_script_localization' );
    798811add_filter( 'print_scripts_array', 'wp_prototype_before_jquery' );