Make WordPress Core

Ticket #20513: 20513.diff

File 20513.diff, 5.5 KB (added by GaryJ, 11 years ago)

First pass

  • functions.wp-scripts.php

     
    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' );
     30        $wp_scripts = wp_scripts_maybe_initialize( $wp_scripts );
    3131
    32                 if ( !$handles )
    33                         return array(); // No need to instantiate if nothing is there.
    34                 else
    35                         $wp_scripts = new WP_Scripts();
    36         }
    37 
    3832        return $wp_scripts->do_items( $handles );
    3933}
    4034
     
    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 = wp_scripts_maybe_initialize( $wp_scripts );
    6149        $wp_scripts->add( $handle, $src, $deps, $ver );
    6250        if ( $in_footer )
    6351                $wp_scripts->add_data( $handle, 'group', 1 );
     
    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 ( ! wp_scripts_maybe_initialize( $wp_scripts, false ) )
    9377                return false;
    94         }
    9578
    9679        return $wp_scripts->localize( $handle, $object_name, $l10n );
    9780}
     
    10487 */
    10588function wp_deregister_script( $handle ) {
    10689        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 
     90        $wp_scripts = wp_scripts_maybe_initialize( $wp_scripts );
    11491        $wp_scripts->remove( $handle );
    11592}
    11693
     
    124101 */
    125102function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
    126103        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         }
     104        $wp_scripts = wp_scripts_maybe_initialize( $wp_scripts );
    133105
    134106        if ( $src ) {
    135107                $_handle = explode('?', $handle);
     
    148120 */
    149121function wp_dequeue_script( $handle ) {
    150122        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 
     123        $wp_scripts = wp_scripts_maybe_initialize( $wp_scripts );
    158124        $wp_scripts->dequeue( $handle );
    159125}
    160126
     
    172138 */
    173139function wp_script_is( $handle, $list = 'queue' ) {
    174140        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         }
     141        $wp_scripts = wp_scripts_maybe_initialize( $wp_scripts );
    181142
    182143        $query = $wp_scripts->query( $handle, $list );
    183144
     
    186147
    187148        return $query;
    188149}
     150
     151/**
     152 * Checks if first argument is an instance of WP_Scripts and optionally creates
     153 * and returns it, or returns a fallback.
     154 *
     155 * @since 3.5.0
     156 *
     157 * @param string $wp_scripts Variable to test.
     158 * @param mixed  $fallback   Optional. What to return - if the default string
     159 *                           'WP_Scripts' then an instance of the class is returned.
     160 *
     161 * @return WP_Scripts|mixed
     162 */
     163function wp_scripts_maybe_initialize( $wp_scripts, $fallback = 'WP_Scripts' ) {
     164        if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
     165                if ( ! did_action( 'init' ) )
     166                        _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     167                                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     168
     169                if ( 'WP_Scripts' == $fallback )
     170                        return new WP_Scripts();
     171
     172                return $fallback;
     173        }
     174        return $wp_scripts;
     175}