[ Index ]

PHP Cross Reference of WordPress

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           * Adds async/defer attributes to enqueued / registered scripts.
  24           *
  25           * If #12009 lands in WordPress, this function can no-op since it would be handled in core.
  26           *
  27           * @since Twenty Twenty 1.0
  28           *
  29           * @link https://core.trac.wordpress.org/ticket/12009
  30           *
  31           * @param string $tag    The script tag.
  32           * @param string $handle The script handle.
  33           * @return string Script HTML string.
  34           */
  35  		public function filter_script_loader_tag( $tag, $handle ) {
  36              foreach ( array( 'async', 'defer' ) as $attr ) {
  37                  if ( ! wp_scripts()->get_data( $handle, $attr ) ) {
  38                      continue;
  39                  }
  40                  // Prevent adding attribute when already added in #12009.
  41                  if ( ! preg_match( ":\s$attr(=|>|\s):", $tag ) ) {
  42                      $tag = preg_replace( ':(?=></script>):', " $attr", $tag, 1 );
  43                  }
  44                  // Only allow async or defer, not both.
  45                  break;
  46              }
  47              return $tag;
  48          }
  49  
  50      }
  51  }


Generated: Sun Nov 3 01:00:03 2024 Cross-referenced by PHPXref 0.7.1