PluginProbe
Gutenberg / 23.6.2
Gutenberg v23.6.2
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 23.6.2, at lib/experimental/script-modules.php

201 lines 7.9 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 * @return array The filtered block settings.
11 */
12 function gutenberg_filter_block_type_metadata_settings_register_view_module( $settings, $metadata = null ) {
13 $module_fields = array(
14 // @todo remove viewModule support in Gutenberg >= 17.8 (replaced by viewScriptModule).
15 'viewModule' => 'view_script_module_ids',
16 );
17 foreach ( $module_fields as $metadata_field_name => $settings_field_name ) {
18 if ( ! empty( $settings[ $metadata_field_name ] ) ) {
19 $metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ];
20 }
21 if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
22 $modules = $metadata[ $metadata_field_name ];
23 $processed_modules = array();
24 if ( is_array( $modules ) ) {
25 for ( $index = 0; $index < count( $modules ); $index++ ) {
26 $processed_modules[] = gutenberg_register_block_module_id(
27 $metadata,
28 $metadata_field_name,
29 $index
30 );
31 }
32 } else {
33 $processed_modules[] = gutenberg_register_block_module_id(
34 $metadata,
35 $metadata_field_name
36 );
37 }
38 $settings[ $settings_field_name ] = $processed_modules;
39 }
40 }
41
42 return $settings;
43 }
44
45 add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_register_view_module', 20, 2 );
46
47 /**
48 * Finds a module ID for the selected block metadata field. It detects
49 * when a path to file was provided and finds a corresponding asset file
50 * with details necessary to register the module under an automatically
51 * generated module ID.
52 *
53 * This is analogous to the `register_block_script_handle` in WordPress Core.
54 *
55 * @param array $metadata Block metadata.
56 * @param string $field_name Field name to pick from metadata.
57 * @param int $index Optional. Index of the script to register when multiple items passed.
58 * Default 0.
59 * @return string|false Module ID.
60 */
61 function gutenberg_register_block_module_id( $metadata, $field_name, $index = 0 ) {
62 if ( empty( $metadata[ $field_name ] ) ) {
63 return false;
64 }
65
66 $module_id = $metadata[ $field_name ];
67 if ( is_array( $module_id ) ) {
68 if ( empty( $module_id[ $index ] ) ) {
69 return false;
70 }
71 $module_id = $module_id[ $index ];
72 }
73
74 $module_path = remove_block_asset_path_prefix( $module_id );
75 if ( $module_id === $module_path ) {
76 return $module_id;
77 }
78
79 $path = dirname( $metadata['file'] );
80 $module_asset_raw_path = $path . '/' . substr_replace( $module_path, '.asset.php', - strlen( '.js' ) );
81 $module_id = gutenberg_generate_block_asset_module_id( $metadata['name'], $field_name, $index );
82 $module_asset_path = wp_normalize_path( realpath( $module_asset_raw_path ) );
83
84 $module_path_norm = wp_normalize_path( realpath( $path . '/' . $module_path ) );
85 $module_uri = get_block_asset_url( $module_path_norm );
86 $module_asset = ! empty( $module_asset_path ) ? require $module_asset_path : array();
87 $module_dependencies = $module_asset['dependencies'] ?? array();
88
89 wp_register_script_module(
90 $module_id,
91 $module_uri,
92 $module_dependencies,
93 $module_asset['version'] ?? false
94 );
95
96 return $module_id;
97 }
98
99 /**
100 * Generates the module ID for an asset based on the name of the block
101 * and the field name provided.
102 *
103 * This is analogous to the `generate_block_asset_handle` in WordPress Core.
104 *
105 * @param string $block_name Name of the block.
106 * @param string $field_name Name of the metadata field.
107 * @param int $index Optional. Index of the asset when multiple items passed.
108 * Default 0.
109 * @return string Generated module ID for the block's field.
110 */
111 function gutenberg_generate_block_asset_module_id( $block_name, $field_name, $index = 0 ) {
112 if ( str_starts_with( $block_name, 'core/' ) ) {
113 $asset_handle = str_replace( 'core/', 'wp-block-', $block_name );
114 if ( str_starts_with( $field_name, 'editor' ) ) {
115 $asset_handle .= '-editor';
116 }
117 if ( str_starts_with( $field_name, 'view' ) ) {
118 $asset_handle .= '-view';
119 }
120 if ( $index > 0 ) {
121 $asset_handle .= '-' . ( $index + 1 );
122 }
123 return $asset_handle;
124 }
125
126 $field_mappings = array(
127 // @todo remove viewModule support in Gutenberg >= 17.8 (replaced by viewScriptModule).
128 'viewModule' => 'view-script-module',
129 'viewScriptModule' => 'view-script-module',
130 );
131 $asset_handle = str_replace( '/', '-', $block_name ) .
132 '-' . $field_mappings[ $field_name ];
133 if ( $index > 0 ) {
134 $asset_handle .= '-' . ( $index + 1 );
135 }
136 return $asset_handle;
137 }
138
139 /**
140 * Registers a REST field for block types to provide view module IDs.
141 *
142 * Adds the `view_script_module_ids` and `view_module_ids` (deprecated) field to block type objects in the REST API, which
143 * lists the script module IDs for any script modules associated with the
144 * block's viewScriptModule key.
145 */
146 function gutenberg_register_view_module_ids_rest_field() {
147 // @todo remove view_module_ids support in Gutenberg >= 17.8 (replaced by view_script_module_ids).
148 register_rest_field(
149 'block-type',
150 'view_module_ids',
151 array(
152 'get_callback' => function ( $item ) {
153 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $item['name'] );
154 return $block_type->view_script_module_ids ?? array();
155 },
156 )
157 );
158 }
159
160 add_action( 'rest_api_init', 'gutenberg_register_view_module_ids_rest_field' );
161
162 /**
163 * Registers the module if no module with that module identifier has already
164 * been registered.
165 *
166 * @deprecated 17.6.0 gutenberg_register_module is deprecated. Please use wp_register_script_module instead.
167 *
168 * @param string $module_identifier The identifier of the module. Should be unique. It will be used in the final import map.
169 * @param string $src Full URL of the module, or path of the script relative to the WordPress root directory.
170 * @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.
171 * @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.
172 */
173 function gutenberg_register_module( $module_identifier, $src = '', $dependencies = array(), $version = false ) {
174 _deprecated_function( __FUNCTION__, 'Gutenberg 17.6.0', 'wp_register_script_module' );
175 wp_script_modules()->register( $module_identifier, $src, $dependencies, $version );
176 }
177
178 /**
179 * Marks the module to be enqueued in the page.
180 *
181 * @deprecated 17.6.0 gutenberg_enqueue_module is deprecated. Please use wp_enqueue_script_module instead.
182 *
183 * @param string $module_identifier The identifier of the module.
184 */
185 function gutenberg_enqueue_module( $module_identifier ) {
186 _deprecated_function( __FUNCTION__, 'Gutenberg 17.6.0', 'wp_enqueue_script_module' );
187 wp_script_modules()->enqueue( $module_identifier );
188 }
189
190 /**
191 * Unmarks the module so it is not longer enqueued in the page.
192 *
193 * @deprecated 17.6.0 gutenberg_dequeue_module is deprecated. Please use wp_dequeue_script_module instead.
194 *
195 * @param string $module_identifier The identifier of the module.
196 */
197 function gutenberg_dequeue_module( $module_identifier ) {
198 _deprecated_function( __FUNCTION__, 'Gutenberg 17.6.0', 'wp_dequeue_script_module' );
199 wp_script_modules()->dequeue( $module_identifier );
200 }
201