[ 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      class TwentyTwenty_Script_Loader {
  19  
  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;
  42              }
  43              return $tag;
  44          }
  45  
  46      }
  47  }


Generated: Sun Dec 8 01:00:03 2019 Cross-referenced by PHPXref 0.7.1