get_data( $handle, $strategy ) ) { wp_script_add_data( $handle, 'strategy', $strategy ); } } } return $to_do; } /** * Adds async/defer attributes to enqueued / registered scripts. * * Now that #12009 has landed in WordPress 6.3, this method is only used for older versions of WordPress. * This method is used on the `script_loader_tag` filter. * * @since Twenty Twenty 1.0 * * @link https://core.trac.wordpress.org/ticket/12009 * * @param string $tag The script tag. * @param string $handle The script handle. * @return string Script HTML string. */ public function filter_script_loader_tag( $tag, $handle ) { $strategies = array( 'async' => (bool) wp_scripts()->get_data( $handle, 'async' ), 'defer' => (bool) wp_scripts()->get_data( $handle, 'defer' ), ); $strategy = wp_scripts()->get_data( $handle, 'strategy' ); if ( $strategy && isset( $strategies[ $strategy ] ) ) { $strategies[ $strategy ] = true; } foreach ( array_keys( array_filter( $strategies ) ) as $attr ) { // Prevent adding attribute when already added in #12009. if ( ! preg_match( ":\s$attr(=|>|\s):", $tag ) ) { $tag = preg_replace( ':(?=>):', " $attr", $tag, 1 ); } // Only allow async or defer, not both. break; } return $tag; } } }