Skip to content

Commit

Permalink
Move and refactor the interactive scritps registration
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Apr 28, 2023
1 parent 29e11ab commit f4b6c0a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 47 deletions.
47 changes: 0 additions & 47 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,50 +577,3 @@ function gutenberg_register_vendor_scripts( $scripts ) {
// Enqueue stored styles.
add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_stored_styles' );
add_action( 'wp_footer', 'gutenberg_enqueue_stored_styles', 1 );

/**
* Registers interactivity scripts for Gutenberg.
*
* This function registers interactivity scripts for Gutenberg when not in the
* admin panel.
*/
function gutenberg_register_interactivity_scripts() {
if ( ! is_admin() ) {
wp_register_script(
'interactivity-runtime',
plugins_url(
'../build/block-library/interactive-blocks/interactivity.min.js',
__FILE__
),
array( 'interactivity-vendors')
);

wp_register_script(
'interactivity-vendors',
plugins_url(
'../build/block-library/interactive-blocks/vendors.min.js',
__FILE__
)
);
}
}
add_action( 'wp_enqueue_scripts', 'gutenberg_register_interactivity_scripts' );

/**
* 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 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', 'add_defer_attribute', 10, 2 );
50 changes: 50 additions & 0 deletions lib/experimental/interactivity-api/script-loader.php
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 );
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/experimental/block-editor-settings-mobile.php';
require __DIR__ . '/experimental/block-editor-settings.php';
require __DIR__ . '/experimental/blocks.php';
require __DIR__ . '/experimental/interactivity-api/script-loader.php';
require __DIR__ . '/experimental/navigation-theme-opt-in.php';
require __DIR__ . '/experimental/kses.php';
require __DIR__ . '/experimental/l10n.php';
Expand Down

0 comments on commit f4b6c0a

Please sign in to comment.