Make WordPress Core

Changeset 31192


Ignore:
Timestamp:
01/16/2015 02:06:03 AM (12 years ago)
Author:
wonderboymusic
Message:

functions.wp-scripts.php contains a lot of duplicated code. Make 2 new functions: wp_scripts() and wp_scripts_maybe_doing_it_wrong( $function ), to encapsulate the repeated logic.

Props GaryJ, scribu, wonderboymusic.
Fixes #20513.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.wp-scripts.php

    r31188 r31192  
    88 * @subpackage BackPress
    99 */
     10
     11/**
     12 * Initialize $wp_scripts if it has not been set.
     13 *
     14 * @global WP_Scripts $wp_scripts
     15 *
     16 * @since 4.2.0
     17 *
     18 * @return WP_Scripts
     19 */
     20function wp_scripts() {
     21        global $wp_scripts;
     22        if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
     23                $wp_scripts = new WP_Scripts();
     24        }
     25        return $wp_scripts;
     26}
     27
     28/**
     29 * Helper function to output a _doing_it_wrong message when applicable
     30 *
     31 * @since 4.2.0
     32 *
     33 * @param string $function
     34 */
     35function wp_scripts_maybe_doing_it_wrong( $function ) {
     36        if ( did_action( 'init' ) ) {
     37                return;
     38        }
     39
     40        _doing_it_wrong( $function, sprintf(
     41                __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     42                '<code>wp_enqueue_scripts</code>',
     43                '<code>admin_enqueue_scripts</code>',
     44                '<code>login_enqueue_scripts</code>'
     45        ), '3.3' );
     46}
    1047
    1148/**
     
    3269         */
    3370        do_action( 'wp_print_scripts' );
    34         if ( '' === $handles ) // for wp_head
     71        if ( '' === $handles ) { // for wp_head
    3572                $handles = false;
    36 
    37         global $wp_scripts;
    38         if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
    39                 if ( ! did_action( 'init' ) )
    40                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    41                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
    42 
    43                 if ( !$handles )
    44                         return array(); // No need to instantiate if nothing is there.
    45                 else
    46                         $wp_scripts = new WP_Scripts();
    47         }
    48 
    49         return $wp_scripts->do_items( $handles );
     73        }
     74
     75        wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     76
     77        if ( ! $handles ) {
     78                return array(); // No need to instantiate if nothing is there.
     79        }
     80        return wp_scripts()->do_items( $handles );
    5081}
    5182
     
    72103 */
    73104function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
    74         global $wp_scripts;
    75         if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
    76                 if ( ! did_action( 'init' ) )
    77                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    78                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
    79                 $wp_scripts = new WP_Scripts();
    80         }
     105        $wp_scripts = wp_scripts();
     106        wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
    81107
    82108        $wp_scripts->add( $handle, $src, $deps, $ver );
    83         if ( $in_footer )
     109        if ( $in_footer ) {
    84110                $wp_scripts->add_data( $handle, 'group', 1 );
     111        }
    85112}
    86113
     
    117144        global $wp_scripts;
    118145        if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
    119                 if ( ! did_action( 'init' ) )
    120                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    121                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
    122 
     146                wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
    123147                return false;
    124148        }
    125149
    126         return $wp_scripts->localize( $handle, $object_name, $l10n );
     150        return wp_scripts()->localize( $handle, $object_name, $l10n );
    127151}
    128152
     
    141165 */
    142166function wp_deregister_script( $handle ) {
    143         global $wp_scripts;
    144         if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
    145                 if ( ! did_action( 'init' ) )
    146                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    147                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
    148                 $wp_scripts = new WP_Scripts();
    149         }
     167        wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
    150168
    151169        /**
     
    174192        }
    175193
    176         $wp_scripts->remove( $handle );
     194        wp_scripts()->remove( $handle );
    177195}
    178196
     
    197215 */
    198216function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
    199         global $wp_scripts;
    200         if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
    201                 if ( ! did_action( 'init' ) )
    202                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    203                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
    204                 $wp_scripts = new WP_Scripts();
    205         }
     217        $wp_scripts = wp_scripts();
     218
     219        wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
    206220
    207221        $_handle = explode( '?', $handle );
     
    229243 */
    230244function wp_dequeue_script( $handle ) {
    231         global $wp_scripts;
    232         if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
    233                 if ( ! did_action( 'init' ) )
    234                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    235                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
    236                 $wp_scripts = new WP_Scripts();
    237         }
    238 
    239         $wp_scripts->dequeue( $handle );
     245        wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     246
     247        wp_scripts()->dequeue( $handle );
    240248}
    241249
     
    254262 */
    255263function wp_script_is( $handle, $list = 'enqueued' ) {
    256         global $wp_scripts;
    257         if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
    258                 if ( ! did_action( 'init' ) )
    259                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    260                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
    261                 $wp_scripts = new WP_Scripts();
    262         }
    263 
    264         return (bool) $wp_scripts->query( $handle, $list );
    265 }
     264        wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     265
     266        return (bool) wp_scripts()->query( $handle, $list );
     267}
Note: See TracChangeset for help on using the changeset viewer.