-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move and refactor the interactive scritps registration
- Loading branch information
Showing
3 changed files
with
51 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
/** | ||
* Updates the script-loader.php file | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Registers interactivity runtime and vendor scripts to use with interactive blocks. | ||
* | ||
* @param WP_Scripts $scripts WP_Scripts instance. | ||
*/ | ||
function gutenberg_register_interactivity_scripts( $scripts ) { | ||
gutenberg_override_script( | ||
$scripts, | ||
'interactivity-runtime', | ||
gutenberg_url( | ||
'build/block-library/interactive-blocks/interactivity.min.js' | ||
), | ||
array( 'interactivity-vendors') | ||
); | ||
|
||
gutenberg_override_script( | ||
$scripts, | ||
'interactivity-vendors', | ||
gutenberg_url( | ||
'build/block-library/interactive-blocks/vendors.min.js' | ||
) | ||
); | ||
} | ||
add_action( 'wp_default_scripts', 'gutenberg_register_interactivity_scripts', 10, 1 ); | ||
|
||
/** | ||
* Adds the "defer" attribute to all the interactivity script tags. | ||
* | ||
* @param string $tag The generated script tag. | ||
* @param string $handle The script's registered handle. | ||
* | ||
* @return string The modified script tag. | ||
*/ | ||
function gutenberg_interactivity_scripts_add_defer_attribute( $tag, $handle ) { | ||
if ( 0 === strpos( $handle, 'interactivity-' ) ) { | ||
$p = new WP_HTML_Tag_Processor( $tag ); | ||
$p->next_tag( array( 'tag' => 'script' ) ); | ||
$p->set_attribute( 'defer', true ); | ||
return $p->get_updated_html(); | ||
} | ||
return $tag; | ||
} | ||
add_filter( 'script_loader_tag', 'gutenberg_interactivity_scripts_add_defer_attribute', 10, 2 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters