[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-content/themes/twentytwenty/classes/ -> class-twentytwenty-script-loader.php (source)

   1  <?php
   2  /**
   3   * JavaScript Loader Class
   4   *
   5   * Allow `async` and `defer` while enqueuing JavaScript.
   6   *
   7   * Based on a solution in WP Rig.
   8   *
   9   * @package WordPress
  10   * @subpackage Twenty_Twenty
  11   * @since Twenty Twenty 1.0
  12   */
  13  
  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       * @since Twenty Twenty 1.0
  19       */
  20      class TwentyTwenty_Script_Loader {
  21  
  22          /**
  23           * Migrates legacy async/defer script data which might be used by child themes.
  24           *
  25           * This method is used on the `print_scripts_array` filter.
  26           *
  27           * @since Twenty Twenty 2.0
  28           *
  29           * @param string[] $to_do An array of script dependency handles.
  30           * @return string[] Unchanged array of script dependency handles.
  31           */
  32  		public function migrate_legacy_strategy_script_data( $to_do ) {
  33              foreach ( $to_do as $handle ) {
  34                  foreach ( array( 'async', 'defer' ) as $strategy ) {
  35                      if ( wp_scripts()->get_data( $handle, $strategy ) ) {
  36                          wp_script_add_data( $handle, 'strategy', $strategy );
  37                      }
  38                  }
  39              }
  40              return $to_do;
  41          }
  42  
  43          /**
  44           * Adds async/defer attributes to enqueued / registered scripts.
  45           *
  46           * Now that #12009 has landed in WordPress 6.3, this method is only used for older versions of WordPress.
  47           * This method is used on the `script_loader_tag` filter.
  48           *
  49           * @since Twenty Twenty 1.0
  50           *
  51           * @link https://core.trac.wordpress.org/ticket/12009
  52           *
  53           * @param string $tag    The script tag.
  54           * @param string $handle The script handle.
  55           * @return string Script HTML string.
  56           */
  57  		public function filter_script_loader_tag( $tag, $handle ) {
  58              $strategies = array(
  59                  'async' => (bool) wp_scripts()->get_data( $handle, 'async' ),
  60                  'defer' => (bool) wp_scripts()->get_data( $handle, 'defer' ),
  61              );
  62              $strategy   = wp_scripts()->get_data( $handle, 'strategy' );
  63              if ( $strategy && isset( $strategies[ $strategy ] ) ) {
  64                  $strategies[ $strategy ] = true;
  65              }
  66  
  67              foreach ( array_keys( array_filter( $strategies ) ) as $attr ) {
  68  
  69                  // Prevent adding attribute when already added in #12009.
  70                  if ( ! preg_match( ":\s$attr(=|>|\s):", $tag ) ) {
  71                      $tag = preg_replace( ':(?=></script>):', " $attr", $tag, 1 );
  72                  }
  73                  // Only allow async or defer, not both.
  74                  break;
  75              }
  76              return $tag;
  77          }
  78      }
  79  }


Generated : Wed Dec 18 08:20:01 2024 Cross-referenced by PHPXref