From 6d19460de72337498af70858f22ea4914686cb9e Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Thu, 2 Jan 2025 15:09:52 -0700 Subject: [PATCH] Fix register_block_configuration() to persist block configurations --- .../BlockManagement/BlockRegistration.php | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/inc/Editor/BlockManagement/BlockRegistration.php b/inc/Editor/BlockManagement/BlockRegistration.php index 9d837aba..924e0fa3 100644 --- a/inc/Editor/BlockManagement/BlockRegistration.php +++ b/inc/Editor/BlockManagement/BlockRegistration.php @@ -32,23 +32,27 @@ public static function add_block_category( array $block_categories ): array { } public static function register_blocks(): void { - $remote_data_blocks_config = []; + $all_remote_data_config = []; $scripts_to_localize = []; - foreach ( ConfigStore::get_block_configurations() as $config ) { - self::register_block_config( $config ); + foreach ( ConfigStore::get_block_configurations() as $block_configuration ) { + $block_name = $block_configuration['name']; + + [ $config, $script_handle ] = self::register_block_configuration( $block_configuration ); + $all_remote_data_config[ $block_name ] = $config; + $scripts_to_localize[] = $script_handle; } foreach ( array_unique( $scripts_to_localize ) as $script_handle ) { wp_localize_script( $script_handle, 'REMOTE_DATA_BLOCKS', [ - 'config' => $remote_data_blocks_config, + 'config' => $all_remote_data_config, 'rest_url' => RemoteDataController::get_url(), 'tracks_global_properties' => TracksAnalytics::get_global_properties(), ] ); } } - public static function register_block_config( array $config ): void { + public static function register_block_configuration( array $config ): array { $block_name = $config['name']; $block_path = REMOTE_DATA_BLOCKS__PLUGIN_DIRECTORY . '/build/blocks/remote-data-container'; @@ -74,7 +78,7 @@ public static function register_block_config( array $config ): void { } // Create the localized data that will be used by our block editor script. - $remote_data_blocks_config[ $block_name ] = [ + $block_config = [ 'availableBindings' => $available_bindings, 'loop' => $config['loop'], 'name' => $block_name, @@ -101,10 +105,12 @@ public static function register_block_config( array $config ): void { $block_type = register_block_type( $block_path, $block_options ); - $scripts_to_localize[] = $block_type->editor_script_handles[0]; + $script_handle = $block_type->editor_script_handles[0]; // Register a default pattern that simply displays the available data. $default_pattern_name = BlockPatterns::register_default_block_pattern( $block_name, $config['title'], $config['queries'][ ConfigRegistry::DISPLAY_QUERY_KEY ] ); - $remote_data_blocks_config[ $block_name ]['patterns']['default'] = $default_pattern_name; + $block_config['patterns']['default'] = $default_pattern_name; + + return [ $block_config, $script_handle ]; } }