Make WordPress Core

Ticket #12009: async_defer_12009.patch

File async_defer_12009.patch, 2.5 KB (added by vanaf1979, 5 years ago)
  • wp-includes/class.wp-scripts.php

    diff --git a/wp-includes/class.wp-scripts.php b/wp-includes/class.wp-scripts.php
    index f4277c8586..cc88455bc1 100644
    a b class WP_Scripts extends WP_Dependencies { 
    351351                        echo $cond_after;
    352352                }
    353353
    354         $async_attr = '';
    355         $defer_attr = '';
    356 
    357                 if ( false === in_array( $handle, $this->in_footer ) ) {
    358             if( true === apply_filters( 'script_attr_async', $obj->extra['is_async'] ?? false ) ) {
    359                 $async_attr = ' async';
    360             }
    361 
    362             if( true === apply_filters( 'script_attr_defer', $obj->extra['is_defer'] ?? false ) ) {
    363                 $defer_attr = ' defer';
    364             }
    365         }
    366 
    367354                // A single item may alias a set of items, by having dependencies, but no source.
    368355                if ( ! $src ) {
    369356                        if ( $inline_script_tag ) {
    class WP_Scripts extends WP_Dependencies { 
    398385                }
    399386
    400387                $tag  = $translations . $cond_before . $before_handle;
    401         $tag .= sprintf( "<script%s src='%s'%s%s></script>\n", $this->type_attr, $src, $async_attr, $defer_attr );
     388                $tag .= sprintf( "<script%s src='%s'></script>\n", $this->type_attr, $src );
    402389                $tag .= $after_handle . $cond_after;
    403390
    404391                /**
  • wp-includes/functions.wp-scripts.php

    diff --git a/wp-includes/functions.wp-scripts.php b/wp-includes/functions.wp-scripts.php
    index b900fa2b2a..3eefe4f903 100644
    a b function wp_script_is( $handle, $list = 'enqueued' ) { 
    389389 */
    390390function wp_script_add_data( $handle, $key, $value ) {
    391391        return wp_scripts()->add_data( $handle, $key, $value );
    392  }
    393 
    394 /**
    395  * Add 'is_async' metadata to a script.
    396  *
    397  * @see WP_Dependencies::add_data()
    398  *
    399  * @param string|array $handles Names of the scripts.
    400  */
    401 function wp_async_script($handles) {
    402     if('string' === gettype($handles)) {
    403         $handles = [$handles];
    404     }
    405 
    406     if('array' !== gettype($handles)) {
    407         return;
    408     }
    409 
    410     foreach($handles as $handle ) {
    411         wp_scripts()->add_data( $handle, 'is_async', true );
    412     }
    413 }
    414 
    415 /**
    416  * Add 'is_defer' metadata to a script.
    417  *
    418  * @see WP_Dependencies::add_data()
    419  *
    420  * @param string|array $handles Names of the scripts.
    421  */
    422 function wp_defer_script($handles) {
    423     if('string' === gettype($handles)) {
    424         $handles = [$handles];
    425     }
    426 
    427     if('array' !== gettype($handles)) {
    428         return;
    429     }
    430 
    431     foreach($handles as $handle ) {
    432         wp_scripts()->add_data( $handle, 'is_defer', true );
    433     }
    434392}