Skip to content

Commit

Permalink
Merge branch 'trunk' into feature/remove-shared-assets-clayton
Browse files Browse the repository at this point in the history
  • Loading branch information
darylldoyle authored Feb 27, 2025
2 parents d580222 + 072ff1e commit 1474dcf
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 75 deletions.
38 changes: 0 additions & 38 deletions mu-plugins/10up-plugin/src/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public function register() {

// Editor styles. add_editor_style() doesn't work outside of a theme.
add_filter( 'mce_css', [ $this, 'mce_css' ] );
// Hook to allow async or defer on asset loading.
add_filter( 'script_loader_tag', [ $this, 'script_loader_tag' ], 10, 2 );
}

/**
Expand Down Expand Up @@ -90,40 +88,4 @@ public function mce_css( $stylesheets ) {
'assets/css/frontend/editor-style.css' :
'dist/css/editor-style.min.css' );
}

/**
* Add async/defer attributes to enqueued scripts that have the specified script_execution flag.
*
* @link https://core.trac.wordpress.org/ticket/12009
*
* @param string $tag The script tag.
* @param string $handle The script handle.
*
* @return string|null
*/
public function script_loader_tag( $tag, $handle ) {
$script_execution = wp_scripts()->get_data( $handle, 'script_execution' );

if ( ! $script_execution ) {
return $tag;
}

if ( 'async' !== $script_execution && 'defer' !== $script_execution ) {
return $tag;
}

// Abort adding async/defer for scripts that have this script as a dependency. _doing_it_wrong()?
foreach ( wp_scripts()->registered as $script ) {
if ( in_array( $handle, $script->deps, true ) ) {
return $tag;
}
}

// Add the attribute if it hasn't already been added.
if ( ! preg_match( ":\s$script_execution(=|>|\s):", $tag ) ) {
$tag = preg_replace( ':(?=></script>):', " $script_execution", $tag, 1 );
}

return $tag;
}
}
2 changes: 1 addition & 1 deletion themes/10up-block-theme/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
define( 'TENUP_BLOCK_THEME_PATH', get_template_directory() . '/' );
define( 'TENUP_BLOCK_THEME_DIST_PATH', TENUP_BLOCK_THEME_PATH . 'dist/' );
define( 'TENUP_BLOCK_THEME_DIST_URL', TENUP_BLOCK_THEME_TEMPLATE_URL . '/dist/' );
define( 'TENUP_BLOCK_THEME_INC', TENUP_BLOCK_THEME_PATH . 'blocks/' );
define( 'TENUP_BLOCK_THEME_INC', TENUP_BLOCK_THEME_PATH . 'src/' );
define( 'TENUP_BLOCK_THEME_BLOCK_DIST_DIR', TENUP_BLOCK_THEME_DIST_PATH . '/blocks/' );

// Require Composer autoloader if it exists.
Expand Down
36 changes: 0 additions & 36 deletions themes/10up-theme/src/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public function register() {
add_action( 'wp_enqueue_scripts', [ $this, 'styles' ] );
add_action( 'wp_head', [ $this, 'js_detection' ], 0 );
add_action( 'wp_head', [ $this, 'embed_ct_css' ], 0 );

add_filter( 'script_loader_tag', [ $this, 'script_loader_tag' ], 10, 2 );
}


Expand Down Expand Up @@ -105,40 +103,6 @@ public function js_detection() {
echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
}

/**
* Add async/defer attributes to enqueued scripts that have the specified script_execution flag.
*
* @link https://core.trac.wordpress.org/ticket/12009
* @param string $tag The script tag.
* @param string $handle The script handle.
* @return string|null
*/
public function script_loader_tag( $tag, $handle ) {
$script_execution = wp_scripts()->get_data( $handle, 'script_execution' );

if ( ! $script_execution ) {
return $tag;
}

if ( 'async' !== $script_execution && 'defer' !== $script_execution ) {
return $tag;
}

// Abort adding async/defer for scripts that have this script as a dependency. _doing_it_wrong()?
foreach ( wp_scripts()->registered as $script ) {
if ( in_array( $handle, $script->deps, true ) ) {
return $tag;
}
}

// Add the attribute if it hasn't already been added.
if ( ! preg_match( ":\s$script_execution(=|>|\s):", $tag ) ) {
$tag = preg_replace( ':(?=></script>):', " $script_execution", $tag, 1 );
}

return $tag;
}

/**
* Inlines ct.css in the head
*
Expand Down

0 comments on commit 1474dcf

Please sign in to comment.