WordPress.org

Make WordPress Core

Ticket #20513: 20513.3.diff

File 20513.3.diff, 5.7 KB (added by scribu, 6 years ago)

wp_scripts()

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

    diff --git wp-includes/functions.wp-scripts.php wp-includes/functions.wp-scripts.php
    index a8dac35..cd2943e 100644
    function wp_print_scripts( $handles = false ) { 
    2323        if ( '' === $handles ) // for wp_head
    2424                $handles = false;
    2525
    26         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         }
     26        if ( !$handles )
     27                return array(); // No need to instantiate if nothing is there.
    3728
    38         return $wp_scripts->do_items( $handles );
     29        return wp_scripts()->do_items( $handles );
    3930}
    4031
    4132/**
    function wp_print_scripts( $handles = false ) { 
    5041 * @return null
    5142 */
    5243function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
    53         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         }
     44        $wp_scripts = wp_scripts();
    6045
    6146        $wp_scripts->add( $handle, $src, $deps, $ver );
    6247        if ( $in_footer )
    function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_f 
    8469 * @return bool Whether the localization was added successfully.
    8570 */
    8671function wp_localize_script( $handle, $object_name, $l10n ) {
    87         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 
    93                 return false;
    94         }
    95 
    96         return $wp_scripts->localize( $handle, $object_name, $l10n );
     72        return wp_scripts()->localize( $handle, $object_name, $l10n );
    9773}
    9874
    9975/**
    function wp_localize_script( $handle, $object_name, $l10n ) { 
    10379 * @see WP_Scripts::remove() For parameter information.
    10480 */
    10581function wp_deregister_script( $handle ) {
    106         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 
    114         $wp_scripts->remove( $handle );
     82        wp_scripts()->remove( $handle );
    11583}
    11684
    11785/**
    function wp_deregister_script( $handle ) { 
    12391 * @see wp_register_script() For parameter information.
    12492 */
    12593function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
    126         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         }
     94        $wp_scripts = wp_scripts();
    13395
    13496        if ( $src ) {
    13597                $_handle = explode('?', $handle);
    function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false 
    147109 * @see WP_Scripts::dequeue() For parameter information.
    148110 */
    149111function wp_dequeue_script( $handle ) {
    150         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 
    158         $wp_scripts->dequeue( $handle );
     112        wp_scripts()->dequeue( $handle );
    159113}
    160114
    161115/**
    function wp_dequeue_script( $handle ) { 
    171125 * @return bool
    172126 */
    173127function wp_script_is( $handle, $list = 'queue' ) {
    174         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         }
    181 
    182         $query = $wp_scripts->query( $handle, $list );
     128        $query = wp_scripts()->query( $handle, $list );
    183129
    184130        if ( is_object( $query ) )
    185131                return true;
    186132
    187133        return $query;
    188134}
     135
     136/**
     137 * Return the global instance of WP_Scripts
     138 *
     139 * @since 3.5.0
     140 *
     141 * @return object WP_Scripts instance
     142 */
     143function wp_scripts() {
     144        global $wp_scripts;
     145
     146        if ( is_a( $wp_scripts, 'WP_Scripts' ) )
     147                return $wp_scripts;
     148
     149        if ( ! did_action( 'init' ) )
     150                _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     151                        '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     152
     153        $wp_scripts = new WP_Scripts;
     154
     155        return $wp_scripts;
     156}
     157