PluginProbe
Gutenberg / 22.3.0
Gutenberg v22.3.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 / script-modules.php

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

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