Skip to content

Commit

Permalink
Fix register_block_configuration() to persist block configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
alecgeatches committed Jan 2, 2025
1 parent b078b92 commit 6d19460
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions inc/Editor/BlockManagement/BlockRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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,
Expand All @@ -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 ];
}
}

0 comments on commit 6d19460

Please sign in to comment.