Make WordPress Core

Changeset 31194


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

After [31192], create a function, wp_styles(), to reduce duplicated code in functions.wp-styles.php. The style functions can reuse wp_scripts_maybe_doing_it_wrong( $function ) internally.

See #20513.

File:
1 edited

Legend:

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

    r31188 r31194  
    88 * @subpackage BackPress
    99 */
     10
     11/**
     12 * Initialize $wp_styles if it has not been set.
     13 *
     14 * @global WP_Styles $wp_styles
     15 *
     16 * @since 4.2.0
     17 *
     18 * @return WP_Styles
     19 */
     20function wp_styles() {
     21        global $wp_styles;
     22        if ( ! ( $wp_styles instanceof WP_Styles ) ) {
     23                $wp_styles = new WP_Styles();
     24        }
     25        return $wp_styles;
     26}
    1027
    1128/**
    … …  
    2441 */
    2542function wp_print_styles( $handles = false ) {
    26         if ( '' === $handles ) // for wp_head
     43        if ( '' === $handles ) { // for wp_head
    2744                $handles = false;
     45        }
    2846        /**
    2947         * Fires before styles in the $handles queue are printed.
    … …  
    3149         * @since 2.6.0
    3250         */
    33         if ( ! $handles )
     51        if ( ! $handles ) {
    3452                do_action( 'wp_print_styles' );
     53        }
     54
     55        wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
    3556
    3657        global $wp_styles;
    3758        if ( ! ( $wp_styles instanceof WP_Styles ) ) {
    38                 if ( ! did_action( 'init' ) )
    39                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    40                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
    41 
    42                 if ( !$handles )
     59                if ( ! $handles ) {
    4360                        return array(); // No need to instantiate if nothing is there.
    44                 else
    45                         $wp_styles = new WP_Styles();
    46         }
    47 
    48         return $wp_styles->do_items( $handles );
     61                }
     62        }
     63
     64        return wp_styles()->do_items( $handles );
    4965}
    5066
    … …  
    5874 *
    5975 * @see WP_Styles::add_inline_style()
    60  * @global WP_Styles $wp_styles The WP_Styles object for printing styles.
    6176 *
    6277 * @since 3.3.0
    … …  
    6782 */
    6883function wp_add_inline_style( $handle, $data ) {
    69         global $wp_styles;
    70         if ( ! ( $wp_styles instanceof WP_Styles ) ) {
    71                 if ( ! did_action( 'init' ) )
    72                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    73                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
    74                 $wp_styles = new WP_Styles();
    75         }
     84        wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
    7685
    7786        if ( false !== stripos( $data, '</style>' ) ) {
    … …  
    8089        }
    8190
    82         return $wp_styles->add_inline_style( $handle, $data );
     91        return wp_styles()->add_inline_style( $handle, $data );
    8392}
    8493
    … …  
    8897 * @see WP_Dependencies::add()
    8998 * @link http://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types.
    90  * @global WP_Styles $wp_styles The WP_Styles object for printing styles.
    9199 *
    92100 * @since 2.6.0
    … …  
    102110 */
    103111function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
    104         global $wp_styles;
    105         if ( ! ( $wp_styles instanceof WP_Styles ) ) {
    106                 if ( ! did_action( 'init' ) )
    107                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    108                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
    109                 $wp_styles = new WP_Styles();
    110         }
    111 
    112         $wp_styles->add( $handle, $src, $deps, $ver, $media );
     112        wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     113
     114        wp_styles()->add( $handle, $src, $deps, $ver, $media );
    113115}
    114116
    … …  
    117119 *
    118120 * @see WP_Dependencies::remove()
    119  * @global WP_Styles $wp_styles The WP_Styles object for printing styles.
    120121 *
    121122 * @since 2.1.0
    … …  
    124125 */
    125126function wp_deregister_style( $handle ) {
    126         global $wp_styles;
    127         if ( ! ( $wp_styles instanceof WP_Styles ) ) {
    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>login_enqueue_scripts</code>' ), '3.3' );
    131                 $wp_styles = new WP_Styles();
    132         }
    133 
    134         $wp_styles->remove( $handle );
     127        wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     128
     129        wp_styles()->remove( $handle );
    135130}
    136131
    … …  
    142137 * @see WP_Dependencies::add(), WP_Dependencies::enqueue()
    143138 * @link http://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types.
    144  * @global WP_Styles $wp_styles The WP_Styles object for printing styles.
    145139 *
    146140 * @since 2.6.0
    … …  
    158152function wp_enqueue_style( $handle, $src = false, $deps = array(), $ver = false, $media = 'all' ) {
    159153        global $wp_styles;
    160         if ( ! ( $wp_styles instanceof WP_Styles ) ) {
    161                 if ( ! did_action( 'init' ) )
    162                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    163                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
    164                 $wp_styles = new WP_Styles();
    165         }
     154        wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     155
     156        $wp_styles = wp_styles();
    166157
    167158        if ( $src ) {
    … …  
    176167 *
    177168 * @see WP_Dependencies::dequeue()
    178  * @global WP_Styles $wp_styles The WP_Styles object for printing styles.
    179169 *
    180170 * @since 3.1.0
    … …  
    183173 */
    184174function wp_dequeue_style( $handle ) {
    185         global $wp_styles;
    186         if ( ! ( $wp_styles instanceof WP_Styles ) ) {
    187                 if ( ! did_action( 'init' ) )
    188                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    189                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
    190                 $wp_styles = new WP_Styles();
    191         }
    192 
    193         $wp_styles->dequeue( $handle );
     175        wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     176
     177        wp_styles()->dequeue( $handle );
    194178}
    195179
    … …  
    207191 */
    208192function wp_style_is( $handle, $list = 'enqueued' ) {
    209         global $wp_styles;
    210         if ( ! ( $wp_styles instanceof WP_Styles ) ) {
    211                 if ( ! did_action( 'init' ) )
    212                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
    213                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
    214                 $wp_styles = new WP_Styles();
    215         }
    216 
    217         return (bool) $wp_styles->query( $handle, $list );
     193        wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     194
     195        return (bool) wp_styles()->query( $handle, $list );
    218196}
    219197
    … …  
    241219 */
    242220function wp_style_add_data( $handle, $key, $value ) {
    243         global $wp_styles;
    244         return $wp_styles->add_data( $handle, $key, $value );
    245 }
     221        return wp_styles()->add_data( $handle, $key, $value );
     222}
Note: See TracChangeset for help on using the changeset viewer.