Changeset 46357 for trunk/src/wp-content/themes/twentytwenty/classes/class-twentytwenty-script-loader.php
- Timestamp:
- 09/30/2019 05:40:14 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-content/themes/twentytwenty/classes/class-twentytwenty-script-loader.php
r46271 r46357 1 1 <?php 2 2 /** 3 * Jav sscript Loader Class3 * Javascript Loader Class 4 4 * 5 5 * Allow `async` and `defer` while enqueuing Javascript. 6 6 * 7 * Based on a sol tion in WP Rig.7 * Based on a solution in WP Rig. 8 8 * 9 9 * @package WordPress … … 12 12 */ 13 13 14 /** 15 * A class that provides a way to add `async` or `defer` attributes to scripts. 16 */ 17 class TwentyTwenty_Script_Loader { 14 if ( ! class_exists( 'TwentyTwenty_Script_Loader' ) ) { 15 /** 16 * A class that provides a way to add `async` or `defer` attributes to scripts. 17 */ 18 class TwentyTwenty_Script_Loader { 18 19 19 /** 20 * Adds async/defer attributes to enqueued / registered scripts. 21 * 22 * If #12009 lands in WordPress, this function can no-op since it would be handled in core. 23 * 24 * @link https://core.trac.wordpress.org/ticket/12009 25 * 26 * @param string $tag The script tag. 27 * @param string $handle The script handle. 28 * @return string Script HTML string. 29 */ 30 public function filter_script_loader_tag( $tag, $handle ) { 31 foreach ( array( 'async', 'defer' ) as $attr ) { 32 if ( ! wp_scripts()->get_data( $handle, $attr ) ) { 33 continue; 20 /** 21 * Adds async/defer attributes to enqueued / registered scripts. 22 * 23 * If #12009 lands in WordPress, this function can no-op since it would be handled in core. 24 * 25 * @link https://core.trac.wordpress.org/ticket/12009 26 * 27 * @param string $tag The script tag. 28 * @param string $handle The script handle. 29 * @return string Script HTML string. 30 */ 31 public function filter_script_loader_tag( $tag, $handle ) { 32 foreach ( [ 'async', 'defer' ] as $attr ) { 33 if ( ! wp_scripts()->get_data( $handle, $attr ) ) { 34 continue; 35 } 36 // Prevent adding attribute when already added in #12009. 37 if ( ! preg_match( ":\s$attr(=|>|\s):", $tag ) ) { 38 $tag = preg_replace( ':(?=></script>):', " $attr", $tag, 1 ); 39 } 40 // Only allow async or defer, not both. 41 break; 34 42 } 35 // Prevent adding attribute when already added in #12009. 36 if ( ! preg_match( ":\s$attr(=|>|\s):", $tag ) ) { 37 $tag = preg_replace( ':(?=></script>):', " $attr", $tag, 1 ); 38 } 39 // Only allow async or defer, not both. 40 break; 43 return $tag; 41 44 } 42 return $tag; 45 43 46 } 44 45 47 }
Note: See TracChangeset
for help on using the changeset viewer.