@@ -28,6 +28,34 @@ class WP_Scripts_Asset_Loader {
2828 */
2929 protected $ url ;
3030
31+ /**
32+ * Blocks for this asset set.
33+ *
34+ * @var array
35+ */
36+ protected $ blocks = [];
37+
38+ /**
39+ * Block paths list for this instance.
40+ *
41+ * @var array
42+ */
43+ protected $ block_paths = [];
44+
45+ /**
46+ * Class instances count.
47+ *
48+ * @var integer
49+ */
50+ protected static $ instances = 0 ;
51+
52+ /**
53+ * Class instance ID.
54+ *
55+ * @var integer
56+ */
57+ protected $ instance_id = 0 ;
58+
3159 /**
3260 * Enqueue a src directory assets and blocks.
3361 *
@@ -41,6 +69,9 @@ public function __construct( string $handle, string $path, string $url ) {
4169 $ this ->path = untrailingslashit ( $ path );
4270 $ this ->url = untrailingslashit ( $ url );
4371
72+ // Bump the class instance to distinguish asset handles.
73+ $ this ->instance_id = ++self ::$ instances ;
74+
4475 if ( did_action ( 'init ' ) ) {
4576 _doing_it_wrong ( __FUNCTION__ , 'new WP_Scripts_Asset_Loader() must be called before the init action, or on init priority 1. ' , '1.0.0 ' );
4677 }
@@ -110,10 +141,8 @@ public function enqueue_global_editor_assets() {
110141 * @return array
111142 */
112143 protected function get_blocks () : array {
113- static $ blocks ;
114-
115- if ( ! empty ( $ blocks ) ) {
116- return $ blocks ;
144+ if ( ! empty ( $ this ->blocks ) ) {
145+ return $ this ->blocks ;
117146 }
118147
119148 $ blocks_dir = $ this ->path . '/blocks ' ;
@@ -122,15 +151,15 @@ protected function get_blocks() : array {
122151 $ block_json_pattern = $ blocks_dir . '/*/*/block.json ' ;
123152 $ block_json_files = glob ( $ block_json_pattern );
124153
125- $ blocks = array_combine (
154+ $ this -> blocks = array_combine (
126155 $ block_json_files ,
127156 array_map ( function ( $ block_json_file ) {
128157 $ block_json_content = file_get_contents ( $ block_json_file );
129158 return json_decode ( $ block_json_content , true );
130159 }, $ block_json_files )
131160 );
132161
133- return $ blocks ;
162+ return $ this -> blocks ;
134163 }
135164
136165 /**
@@ -190,7 +219,7 @@ public function enqueue_block_assets() {
190219 }
191220
192221 // Create handle from block name.
193- $ handle = $ this ->handle . '- ' . str_replace ( '/ ' , '- ' , $ block_name );
222+ $ handle = $ this ->handle . '- ' . str_replace ( '/ ' , '- ' , $ block_name ) . ' - ' . $ this -> instance_id ;
194223
195224 $ style_file = remove_block_asset_path_prefix ( $ style );
196225
@@ -254,21 +283,21 @@ protected function get_asset_file( $filepath ) {
254283 * @return array Array of determined settings for registering a block type.
255284 */
256285 public function extend_block_type_metadata ( $ metadata ) {
257- static $ blocks , $ block_paths ;
286+ $ blocks = $ this ->get_blocks ();
287+ $ block_names = wp_list_pluck ( $ blocks , 'name ' );
258288
259- if ( empty ( $ blocks ) ) {
260- $ blocks = $ this ->get_blocks ();
261- $ block_names = wp_list_pluck ( $ blocks , 'name ' );
262- $ block_paths = array_combine (
289+ if ( empty ( $ this ->block_paths ) ) {
290+ $ this ->block_paths = array_combine (
263291 $ block_names ,
264292 array_keys ( $ blocks )
265293 );
266- $ blocks = array_combine (
267- $ block_names ,
268- $ blocks
269- );
270294 }
271295
296+ $ blocks = array_combine (
297+ $ block_names ,
298+ $ blocks
299+ );
300+
272301 $ block_type = $ metadata ['name ' ];
273302
274303 // Return early if we're not doing anything with this block type,
@@ -277,21 +306,26 @@ public function extend_block_type_metadata( $metadata ) {
277306 return $ metadata ;
278307 }
279308
280- $ block_path = $ block_paths [ $ block_type ];
309+ $ block_path = $ this ->block_paths [ $ block_type ];
310+
311+ $ instance_id = $ this ->instance_id ;
281312
282313 // Ensure our extended blocks view script handles start from a higher
283314 // index to avoid collisions.
284315 foreach ( [ 'editorScript ' , 'script ' , 'viewScript ' ] as $ script_type ) {
285316 if ( isset ( $ blocks [ $ block_type ][ $ script_type ] ) ) {
286- $ metadata [ $ script_type ] = array_values ( array_unique ( array_merge (
317+ $ metadata [ $ script_type ] = array_filter ( array_values ( array_unique ( array_merge (
287318 (array ) ( $ metadata [ $ script_type ] ?? [] ),
288- array_map ( function ( $ script ) use ( $ metadata , $ block_path , $ script_type ) {
319+ array_map ( function ( $ script ) use ( $ metadata , $ block_path , $ script_type , $ instance_id ) {
320+ if ( strpos ( $ script , '?skip_enqueue ' ) !== false ) {
321+ return '' ;
322+ }
289323 $ meta_for_path = $ metadata ;
290324 $ meta_for_path ['file ' ] = $ block_path ;
291325 $ meta_for_path [ $ script_type ] = $ script ;
292- return register_block_script_handle ( $ meta_for_path , $ script_type , 100 );
326+ return register_block_script_handle ( $ meta_for_path , $ script_type , 100 + $ instance_id );
293327 }, (array ) $ blocks [ $ block_type ][ $ script_type ] )
294- ) ) );
328+ ) ) ) ) ;
295329 }
296330 }
297331
0 commit comments