WordPress.org

Make WordPress Core

Ticket #12009: async_defer_12009-2.patch

File async_defer_12009-2.patch, 2.5 KB (added by vanaf1979, 9 months ago)

Diff in the correct direction

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

    diff --git a/wp-includes/class.wp-scripts.php b/wp-includes/class.wp-scripts.php
    index cc88455bc1..f4277c8586 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
    354367                // A single item may alias a set of items, by having dependencies, but no source.
    355368                if ( ! $src ) {
    356369                        if ( $inline_script_tag ) {
    class WP_Scripts extends WP_Dependencies { 
    385398                }
    386399
    387400                $tag  = $translations . $cond_before . $before_handle;
    388                 $tag .= sprintf( "<script%s src='%s'></script>\n", $this->type_attr, $src );
     401        $tag .= sprintf( "<script%s src='%s'%s%s></script>\n", $this->type_attr, $src, $async_attr, $defer_attr );
    389402                $tag .= $after_handle . $cond_after;
    390403
    391404                /**
  • wp-includes/functions.wp-scripts.php

    diff --git a/wp-includes/functions.wp-scripts.php b/wp-includes/functions.wp-scripts.php
    index 3eefe4f903..b900fa2b2a 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 */
     401function 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 */
     422function 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    }
    392434}