PluginProbe
Gutenberg / 16.0.0
Gutenberg v16.0.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / experimental / interactivity-api / script-loader.php

script-loader.php in Gutenberg 16.0.0, at lib/experimental/interactivity-api/script-loader.php

57 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Updates the script-loader.php file
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Registers interactivity runtime and vendor scripts to use with interactive blocks.
10 *
11 * @param WP_Scripts $scripts WP_Scripts instance.
12 */
13 function gutenberg_register_interactivity_scripts( $scripts ) {
14 // When in production, use the plugin's version as the default asset version;
15 // else (for development or test) default to use the current time.
16 $default_version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time();
17
18 foreach ( array( 'vendors', 'runtime' ) as $script_name ) {
19 $script_path = "build/block-library/interactivity/$script_name.min.js";
20 // Replace extension with `.asset.php` to find the generated dependencies file.
21 $asset_file = gutenberg_dir_path() . substr( $script_path, 0, -( strlen( '.js' ) ) ) . '.asset.php';
22 $asset = file_exists( $asset_file )
23 ? require $asset_file
24 : null;
25 $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array();
26 $version = isset( $asset['version'] ) ? $asset['version'] : $default_version;
27
28 gutenberg_override_script(
29 $scripts,
30 "wp-interactivity-$script_name",
31 gutenberg_url( $script_path ),
32 $dependencies,
33 $version
34 );
35 }
36 }
37 add_action( 'wp_default_scripts', 'gutenberg_register_interactivity_scripts', 10, 1 );
38
39 /**
40 * Adds the "defer" attribute to all the interactivity script tags.
41 *
42 * @param string $tag The generated script tag.
43 * @param string $handle The script handle.
44 *
45 * @return string The modified script tag.
46 */
47 function gutenberg_interactivity_scripts_add_defer_attribute( $tag, $handle ) {
48 if ( str_starts_with( $handle, 'wp-interactivity-' ) || str_contains( $tag, '/interactivity.min.js' ) ) {
49 $p = new WP_HTML_Tag_Processor( $tag );
50 $p->next_tag( array( 'tag' => 'script' ) );
51 $p->set_attribute( 'defer', true );
52 return $p->get_updated_html();
53 }
54 return $tag;
55 }
56 add_filter( 'script_loader_tag', 'gutenberg_interactivity_scripts_add_defer_attribute', 10, 2 );
57