WordPress.org

Make WordPress Core

Ticket #20513: 20513.2.diff

File 20513.2.diff, 5.8 KB (added by scribu, 2 years ago)

Second pass

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

    diff --git wp-includes/functions.wp-scripts.php wp-includes/functions.wp-scripts.php
    index a8dac35..30667d2 100644
    function wp_print_scripts( $handles = false ) { 
    2323        if ( '' === $handles ) // for wp_head 
    2424                $handles = false; 
    2525 
     26        if ( !$handles ) 
     27                return array(); // No need to instantiate if nothing is there. 
     28 
    2629        global $wp_scripts; 
    27         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) { 
    28                 if ( ! did_action( 'init' ) ) 
    29                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ), 
    30                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' ); 
    31  
    32                 if ( !$handles ) 
    33                         return array(); // No need to instantiate if nothing is there. 
    34                 else 
    35                         $wp_scripts = new WP_Scripts(); 
    36         } 
     30        wp_scripts_maybe_initialize( $wp_scripts, __FUNCTION__ ); 
    3731 
    3832        return $wp_scripts->do_items( $handles ); 
    3933} 
    function wp_print_scripts( $handles = false ) { 
    5145 */ 
    5246function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) { 
    5347        global $wp_scripts; 
    54         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) { 
    55                 if ( ! did_action( 'init' ) ) 
    56                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ), 
    57                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' ); 
    58                 $wp_scripts = new WP_Scripts(); 
    59         } 
    60  
     48        wp_scripts_maybe_initialize( $wp_scripts, __FUNCTION__ ); 
    6149        $wp_scripts->add( $handle, $src, $deps, $ver ); 
    6250        if ( $in_footer ) 
    6351                $wp_scripts->add_data( $handle, 'group', 1 ); 
    function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_f 
    8573 */ 
    8674function wp_localize_script( $handle, $object_name, $l10n ) { 
    8775        global $wp_scripts; 
    88         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) { 
    89                 if ( ! did_action( 'init' ) ) 
    90                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ), 
    91                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' ); 
    92  
     76        if ( !is_a( $wp_scripts, 'WP_Scripts' ) ) 
    9377                return false; 
    94         } 
     78 
     79        wp_scripts_maybe_initialize( $wp_scripts, __FUNCTION__ ); 
    9580 
    9681        return $wp_scripts->localize( $handle, $object_name, $l10n ); 
    9782} 
    function wp_localize_script( $handle, $object_name, $l10n ) { 
    10489 */ 
    10590function wp_deregister_script( $handle ) { 
    10691        global $wp_scripts; 
    107         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) { 
    108                 if ( ! did_action( 'init' ) ) 
    109                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ), 
    110                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' ); 
    111                 $wp_scripts = new WP_Scripts(); 
    112         } 
    113  
     92        wp_scripts_maybe_initialize( $wp_scripts, __FUNCTION__ ); 
    11493        $wp_scripts->remove( $handle ); 
    11594} 
    11695 
    function wp_deregister_script( $handle ) { 
    124103 */ 
    125104function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) { 
    126105        global $wp_scripts; 
    127         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) { 
    128                 if ( ! did_action( 'init' ) ) 
    129                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ), 
    130                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' ); 
    131                 $wp_scripts = new WP_Scripts(); 
    132         } 
     106        wp_scripts_maybe_initialize( $wp_scripts, __FUNCTION__ ); 
    133107 
    134108        if ( $src ) { 
    135109                $_handle = explode('?', $handle); 
    function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false 
    148122 */ 
    149123function wp_dequeue_script( $handle ) { 
    150124        global $wp_scripts; 
    151         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) { 
    152                 if ( ! did_action( 'init' ) ) 
    153                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ), 
    154                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' ); 
    155                 $wp_scripts = new WP_Scripts(); 
    156         } 
    157  
     125        wp_scripts_maybe_initialize( $wp_scripts, __FUNCTION__ ); 
    158126        $wp_scripts->dequeue( $handle ); 
    159127} 
    160128 
    function wp_dequeue_script( $handle ) { 
    172140 */ 
    173141function wp_script_is( $handle, $list = 'queue' ) { 
    174142        global $wp_scripts; 
    175         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) { 
    176                 if ( ! did_action( 'init' ) ) 
    177                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ), 
    178                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' ); 
    179                 $wp_scripts = new WP_Scripts(); 
    180         } 
     143        wp_scripts_maybe_initialize( $wp_scripts, __FUNCTION__ ); 
    181144 
    182145        $query = $wp_scripts->query( $handle, $list ); 
    183146 
    function wp_script_is( $handle, $list = 'queue' ) { 
    186149 
    187150        return $query; 
    188151} 
     152 
     153/** 
     154 * Checks if first argument is an instance of WP_Scripts and optionally creates 
     155 * and returns it, or returns a fallback. 
     156 * 
     157 * @since 3.5.0 
     158 * 
     159 * @param string &$wp_scripts Variable to test. 
     160 * @param string $function The function from which it was called. 
     161 */ 
     162function wp_scripts_maybe_initialize( &$wp_scripts, $function ) { 
     163        if ( is_a( $wp_scripts, 'WP_Scripts' ) ) 
     164                return; 
     165 
     166        if ( ! did_action( 'init' ) ) 
     167                _doing_it_wrong( $function, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ), 
     168                        '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' ); 
     169 
     170        $wp_scripts = new WP_Scripts; 
     171} 
     172