Skip to content

Commit

Permalink
Naively dynamically register blocks based on saved config
Browse files Browse the repository at this point in the history
  • Loading branch information
jblz committed Aug 26, 2024
1 parent cf1bf0b commit b06aba5
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions inc/editor/configuration-loader/configuration-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
defined( 'ABSPATH' ) || exit();

use Error;
use RemoteDataBlocks\Config\AirtableDatasource;
use RemoteDataBlocks\Config\HttpDatasourceConfig;
use RemoteDataBlocks\Config\QueryContext;
use RemoteDataBlocks\Config\ShopifyDatasource;
use RemoteDataBlocks\Logging\Logger;
use RemoteDataBlocks\Logging\LoggerManager;
use RemoteDataBlocks\REST\DatasourceCRUD;
use WP_Error;

use function add_action;
use function do_action;
Expand All @@ -28,6 +33,8 @@ public static function init() {
public static function register_remote_data_blocks() {
// Allow other plugins to register their blocks.
do_action( 'register_remote_data_blocks' );

self::register_blocks_for_dynamic_data_sources();
}

/**
Expand Down Expand Up @@ -245,4 +252,44 @@ public static function unregister_all(): void {

self::$configurations = [];
}

// TODO: Move everything below to its own class maybe...?
private static function get_datasource_from_config( object $source ): HttpDatasourceConfig|WP_Error {
try {
switch ( $source->service ) {
case 'airtable':
return new AirtableDatasource( $source->token, $source->base?->id, $source->table?->id );
case 'shopify':
return new ShopifyDatasource( $source->token, $source->store );
default:
return new WP_Error( 'unsupported_data_source', __( 'Unsupported data source.', 'remote-data-blocks' ), $source );
}
} catch ( Error $e ) {
return new WP_Error( 'invalid_data_source', $e->getMessage(), $source );
}
}

private static function register_blocks_for_dynamic_data_sources(): void {
$data_sources_from_config = DatasourceCRUD::get_data_sources();

foreach ( $data_sources_from_config as $_source ) {
$datasource = self::get_datasource_from_config( $_source );
if ( $datasource instanceof WP_Error ) {
self::$logger->error( $datasource->get_error_message() );
continue;
}

// TODO: Store these as settings and iteratively call these against each config:
$block_name = 'RDB ' . $_source->service . ' ' . $_source->slug; // TODO: more friendly name
$query_context = new QueryContext( $datasource );

self::register_block( $block_name, $query_context );

self::$logger->debug( sprintf( 'Registered "%s" block for dynamic data source: %s', $block_name, $_source->uuid ) );
// self::register_list_query( $block_name, $query_context );
// self::register_loop_block( $block_name . ' List', $query_context );
// self::register_page( $block_name, '...' );
}
}
}

0 comments on commit b06aba5

Please sign in to comment.