PluginProbe
Gutenberg / 17.6.4
Gutenberg v17.6.4
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 / script-modules.php

script-modules.php in Gutenberg 17.6.4, at lib/experimental/script-modules.php

235 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Add module fields from block metadata to WP_Block_Type settings.
4 *
5 * This filter allows us to register modules from block metadata and attach additional fields to
6 * WP_Block_Type instances.
7 *
8 * @param array $settings Array of determined settings for registering a block type.
9 * @param array $metadata Metadata provided for registering a block type.
10 */
11 function gutenberg_filter_block_type_metadata_settings_register_modules( $settings, $metadata = null ) {
12 $module_fields = array(
13 'viewModule' => 'view_module_ids',
14 );
15 foreach ( $module_fields as $metadata_field_name => $settings_field_name ) {
16 if ( ! empty( $settings[ $metadata_field_name ] ) ) {
17 $metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ];
18 }
19 if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
20 $modules = $metadata[ $metadata_field_name ];
21 $processed_modules = array();
22 if ( is_array( $modules ) ) {
23 for ( $index = 0; $index < count( $modules ); $index++ ) {
24 $processed_modules[] = gutenberg_register_block_module_id(
25 $metadata,
26 $metadata_field_name,
27 $index
28 );
29 }
30 } else {
31 $processed_modules[] = gutenberg_register_block_module_id(
32 $metadata,
33 $metadata_field_name
34 );
35 }
36 $settings[ $settings_field_name ] = $processed_modules;
37 }
38 }
39
40 return $settings;
41 }
42
43 add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_register_modules', 10, 2 );
44
45 /**
46 * Enqueue modules associated with the block.
47 *
48 * @param string $block_content The block content.
49 * @param array $parsed_block The full block, including name and attributes.
50 * @param WP_Block $block_instance The block instance.
51 */
52 function gutenberg_filter_render_block_enqueue_view_modules( $block_content, $parsed_block, $block_instance ) {
53 $block_type = $block_instance->block_type;
54
55 if ( ! empty( $block_type->view_module_ids ) ) {
56 foreach ( $block_type->view_module_ids as $module_id ) {
57 wp_enqueue_script_module( $module_id );
58 }
59 }
60
61 return $block_content;
62 }
63
64 add_filter( 'render_block', 'gutenberg_filter_render_block_enqueue_view_modules', 10, 3 );
65
66 /**
67 * Finds a module ID for the selected block metadata field. It detects
68 * when a path to file was provided and finds a corresponding asset file
69 * with details necessary to register the module under an automatically
70 * generated module ID.
71 *
72 * This is analogous to the `register_block_script_handle` in WordPress Core.
73 *
74 * @param array $metadata Block metadata.
75 * @param string $field_name Field name to pick from metadata.
76 * @param int $index Optional. Index of the script to register when multiple items passed.
77 * Default 0.
78 * @return string Module ID.
79 */
80 function gutenberg_register_block_module_id( $metadata, $field_name, $index = 0 ) {
81 if ( empty( $metadata[ $field_name ] ) ) {
82 return false;
83 }
84
85 $module_id = $metadata[ $field_name ];
86 if ( is_array( $module_id ) ) {
87 if ( empty( $module_id[ $index ] ) ) {
88 return false;
89 }
90 $module_id = $module_id[ $index ];
91 }
92
93 $module_path = remove_block_asset_path_prefix( $module_id );
94 if ( $module_id === $module_path ) {
95 return $module_id;
96 }
97
98 $path = dirname( $metadata['file'] );
99 $module_asset_raw_path = $path . '/' . substr_replace( $module_path, '.asset.php', - strlen( '.js' ) );
100 $module_id = gutenberg_generate_block_asset_module_id( $metadata['name'], $field_name, $index );
101 $module_asset_path = wp_normalize_path( realpath( $module_asset_raw_path ) );
102
103 if ( empty( $module_asset_path ) ) {
104 _doing_it_wrong(
105 __FUNCTION__,
106 sprintf(
107 // This string is from WordPress Core. See `register_block_script_handle`.
108 // Translators: This is a translation from WordPress Core (default). No need to translate.
109 __( 'The asset file (%1$s) for the "%2$s" defined in "%3$s" block definition is missing.', 'default' ),
110 $module_asset_raw_path,
111 $field_name,
112 $metadata['name']
113 ),
114 '6.5.0'
115 );
116 return false;
117 }
118
119 $module_path_norm = wp_normalize_path( realpath( $path . '/' . $module_path ) );
120 $module_uri = get_block_asset_url( $module_path_norm );
121 $module_asset = require $module_asset_path;
122 $module_dependencies = isset( $module_asset['dependencies'] ) ? $module_asset['dependencies'] : array();
123
124 wp_register_script_module(
125 $module_id,
126 $module_uri,
127 $module_dependencies,
128 isset( $module_asset['version'] ) ? $module_asset['version'] : false
129 );
130
131 return $module_id;
132 }
133
134 /**
135 * Generates the module ID for an asset based on the name of the block
136 * and the field name provided.
137 *
138 * This is analogous to the `generate_block_asset_handle` in WordPress Core.
139 *
140 * @param string $block_name Name of the block.
141 * @param string $field_name Name of the metadata field.
142 * @param int $index Optional. Index of the asset when multiple items passed.
143 * Default 0.
144 * @return string Generated module ID for the block's field.
145 */
146 function gutenberg_generate_block_asset_module_id( $block_name, $field_name, $index = 0 ) {
147 if ( str_starts_with( $block_name, 'core/' ) ) {
148 $asset_handle = str_replace( 'core/', 'wp-block-', $block_name );
149 if ( str_starts_with( $field_name, 'editor' ) ) {
150 $asset_handle .= '-editor';
151 }
152 if ( str_starts_with( $field_name, 'view' ) ) {
153 $asset_handle .= '-view';
154 }
155 if ( $index > 0 ) {
156 $asset_handle .= '-' . ( $index + 1 );
157 }
158 return $asset_handle;
159 }
160
161 $field_mappings = array(
162 'viewModule' => 'view-module',
163 );
164 $asset_handle = str_replace( '/', '-', $block_name ) .
165 '-' . $field_mappings[ $field_name ];
166 if ( $index > 0 ) {
167 $asset_handle .= '-' . ( $index + 1 );
168 }
169 return $asset_handle;
170 }
171
172 /**
173 * Registers a REST field for block types to provide view module IDs.
174 *
175 * Adds the `view_module_ids` field to block type objects in the REST API, which
176 * lists the script module IDs for any script modules associated with the
177 * block's viewModule(s) key.
178 *
179 * @since 6.5.0
180 */
181 function gutenberg_register_view_module_ids_rest_field() {
182 register_rest_field(
183 'block-type',
184 'view_module_ids',
185 array(
186 'get_callback' => function ( $item ) {
187 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $item['name'] );
188 if ( isset( $block_type->view_module_ids ) ) {
189 return $block_type->view_module_ids;
190 }
191 return array();
192 },
193 )
194 );
195 }
196
197 add_action( 'rest_api_init', 'gutenberg_register_view_module_ids_rest_field' );
198
199 /**
200 * Registers the module if no module with that module identifier has already
201 * been registered.
202 *
203 * @param string $module_identifier The identifier of the module. Should be unique. It will be used in the final import map.
204 * @param string $src Full URL of the module, or path of the script relative to the WordPress root directory.
205 * @param array $dependencies Optional. An array of module identifiers of the dependencies of this module. The dependencies can be strings or arrays. If they are arrays, they need an `id` key with the module identifier, and can contain an `import` key with either `static` or `dynamic`. By default, dependencies that don't contain an import are considered static.
206 * @param string|false|null $version Optional. String specifying module version number. Defaults to false. It is added to the URL as a query string for cache busting purposes. If $version is set to false, the version number is the currently installed WordPress version. If $version is set to null, no version is added.
207 * @deprecated 17.6.0 gutenberg_register_module is deprecated. Please use wp_register_script_module instead.
208 */
209 function gutenberg_register_module( $module_identifier, $src = '', $dependencies = array(), $version = false ) {
210 _deprecated_function( __FUNCTION__, 'Gutenberg 17.6.0', 'wp_register_script_module' );
211 wp_script_modules()->register( $module_identifier, $src, $dependencies, $version );
212 }
213
214 /**
215 * Marks the module to be enqueued in the page.
216 *
217 * @param string $module_identifier The identifier of the module.
218 * @deprecated 17.6.0 gutenberg_enqueue_module is deprecated. Please use wp_enqueue_script_module instead.
219 */
220 function gutenberg_enqueue_module( $module_identifier ) {
221 _deprecated_function( __FUNCTION__, 'Gutenberg 17.6.0', 'wp_enqueue_script_module' );
222 wp_script_modules()->enqueue( $module_identifier );
223 }
224
225 /**
226 * Unmarks the module so it is not longer enqueued in the page.
227 *
228 * @param string $module_identifier The identifier of the module.
229 * @deprecated 17.6.0 gutenberg_dequeue_module is deprecated. Please use wp_dequeue_script_module instead.
230 */
231 function gutenberg_dequeue_module( $module_identifier ) {
232 _deprecated_function( __FUNCTION__, 'Gutenberg 17.6.0', 'wp_dequeue_script_module' );
233 wp_script_modules()->dequeue( $module_identifier );
234 }
235