From f4b6c0a9536bf878ba12f54032bcf4186dc3997a Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Fri, 28 Apr 2023 14:39:48 +0200 Subject: [PATCH] Move and refactor the interactive scritps registration --- lib/client-assets.php | 47 ----------------- .../interactivity-api/script-loader.php | 50 +++++++++++++++++++ lib/load.php | 1 + 3 files changed, 51 insertions(+), 47 deletions(-) create mode 100644 lib/experimental/interactivity-api/script-loader.php diff --git a/lib/client-assets.php b/lib/client-assets.php index a0dc00cbcd6767..9c0483ea539b93 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -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 ); diff --git a/lib/experimental/interactivity-api/script-loader.php b/lib/experimental/interactivity-api/script-loader.php new file mode 100644 index 00000000000000..206a1d8e5e0b78 --- /dev/null +++ b/lib/experimental/interactivity-api/script-loader.php @@ -0,0 +1,50 @@ +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 ); diff --git a/lib/load.php b/lib/load.php index 8cceb21906293b..785ac2fb1cbf57 100644 --- a/lib/load.php +++ b/lib/load.php @@ -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';