Skip to content

Commit

Permalink
auto-register blocks for google sheets data sources (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
shekharnwagh authored Jan 6, 2025
1 parent f601686 commit 1de044c
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 35 deletions.
35 changes: 4 additions & 31 deletions example/google-sheets/westeros-houses/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,8 @@ function register_westeros_houses_block(): void {
],
],
],
'preprocess_response' => function ( mixed $response_data ) use ( $columns ): array {
if ( isset( $response_data['values'] ) && is_array( $response_data['values'] ) ) {
$values = $response_data['values'];
array_shift( $values ); // Drop the first row

$response_data['values'] = array_map(
function ( $row, $index ) use ( $columns ) {
$combined = array_combine( $columns, $row );
$combined['RowId'] = $index + 1; // Add row_id field, starting from 1
return $combined;
},
$values,
array_keys( $values )
);
}

return $response_data;
'preprocess_response' => function ( mixed $response_data ): array {
return GoogleSheetsDataSource::preprocess_list_response( $response_data );
},
] );

Expand Down Expand Up @@ -139,20 +124,8 @@ function ( $row, $index ) use ( $columns ) {
],
],
],
'preprocess_response' => function ( mixed $response_data, array $input_variables ) use ( $columns ): array {
$selected_row = null;
$row_id = $input_variables['row_id'];

if ( isset( $response_data['values'] ) && is_array( $response_data['values'] ) ) {
$raw_selected_row = $response_data['values'][ $row_id ];
if ( is_array( $raw_selected_row ) ) {
$selected_row = array_combine( $columns, $raw_selected_row );
$selected_row = array_combine( $columns, $selected_row );
$selected_row['RowId'] = $row_id;
}
}

return $selected_row;
'preprocess_response' => function ( mixed $response_data, array $input_variables ): array {
return GoogleSheetsDataSource::preprocess_get_response( $response_data, $input_variables );
},
] );

Expand Down
14 changes: 11 additions & 3 deletions inc/Integrations/Airtable/AirtableIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ public static function init(): void {
}

public static function register_blocks(): void {
$data_source_configs = DataSourceCrud::get_configs_by_service( REMOTE_DATA_BLOCKS_AIRTABLE_SERVICE );
$data_source_configs = DataSourceCrud::get_configs_by_service(
REMOTE_DATA_BLOCKS_AIRTABLE_SERVICE
);

foreach ( $data_source_configs as $config ) {
$data_source = AirtableDataSource::from_array( $config );
self::register_block_for_airtable_data_source( $data_source );
}
}

public static function register_block_for_airtable_data_source( AirtableDataSource $data_source, array $block_overrides = [] ): void {
public static function register_block_for_airtable_data_source(
AirtableDataSource $data_source,
array $block_overrides = []
): void {
register_remote_data_block(
array_merge(
[
Expand All @@ -38,7 +43,10 @@ public static function register_block_for_airtable_data_source( AirtableDataSour
);
}

public static function register_loop_block_for_airtable_data_source( AirtableDataSource $data_source, array $block_overrides = [] ): void {
public static function register_loop_block_for_airtable_data_source(
AirtableDataSource $data_source,
array $block_overrides = []
): void {
register_remote_data_block(
array_merge(
[
Expand Down
122 changes: 121 additions & 1 deletion inc/Integrations/Google/Sheets/GoogleSheetsDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace RemoteDataBlocks\Integrations\Google\Sheets;

use RemoteDataBlocks\Config\DataSource\HttpDataSource;
use RemoteDataBlocks\Config\Query\HttpQuery;
use RemoteDataBlocks\Integrations\Google\Auth\GoogleAuth;
use RemoteDataBlocks\Validation\Types;

Expand Down Expand Up @@ -51,7 +52,10 @@ protected static function get_service_config_schema(): array {
protected static function map_service_config( array $service_config ): array {
return [
'display_name' => $service_config['display_name'],
'endpoint' => sprintf( 'https://sheets.googleapis.com/v4/spreadsheets/%s', $service_config['spreadsheet']['id'] ),
'endpoint' => sprintf(
'https://sheets.googleapis.com/v4/spreadsheets/%s',
$service_config['spreadsheet']['id']
),
'request_headers' => function () use ( $service_config ): array {
$access_token = GoogleAuth::generate_token_from_service_account_key(
$service_config['credentials'],
Expand All @@ -65,4 +69,120 @@ protected static function map_service_config( array $service_config ): array {
},
];
}

public function ___temp_get_query(): HttpQuery {
$service_config = $this->config['service_config'];

$input_schema = [
'row_id' => [
'name' => 'Row ID',
'type' => 'id',
],
];

$output_schema = [
'is_collection' => false,
'type' => [
'row_id' => [
'name' => 'Row ID',
'path' => '$.RowId',
'type' => 'id',
],
],
];

foreach ( $service_config['sheets'][0]['output_query_mappings'] as $mapping ) {
$mapping_key = $mapping['key'];
$output_schema['type'][ $mapping_key ] = [
'name' => $mapping['name'] ?? $mapping_key,
'path' => $mapping['path'] ?? '$.fields["' . $mapping_key . '"]',
'type' => $mapping['type'] ?? 'string',
];
}

return HttpQuery::from_array( [
'data_source' => $this,
'endpoint' => function (): string {
return $this->get_endpoint() . '/values/' . $this->config['service_config']['sheets'][0]['name'];
},
'input_schema' => $input_schema,
'output_schema' => $output_schema,
'preprocess_response' => function ( mixed $response_data, array $input_variables ): array {
return GoogleSheetsDataSource::preprocess_get_response( $response_data, $input_variables );
},
] );
}

public function ___temp_get_list_query(): HttpQuery {
$service_config = $this->config['service_config'];

$output_schema = [
'is_collection' => true,
'path' => '$.values[*]',
'type' => [
'row_id' => [
'name' => 'Row ID',
'path' => '$.RowId',
'type' => 'id',
],
],
];

foreach ( $service_config['sheets'][0]['output_query_mappings'] as $mapping ) {
$mapping_key = $mapping['key'];
$output_schema['type'][ $mapping_key ] = [
'name' => $mapping['name'] ?? $mapping_key,
'path' => $mapping['path'] ?? '$.fields["' . $mapping_key . '"]',
'type' => $mapping['type'] ?? 'string',
];
}

return HttpQuery::from_array( [
'data_source' => $this,
'endpoint' => function (): string {
return $this->get_endpoint() . '/values/' . $this->config['service_config']['sheets'][0]['name'];
},
'input_schema' => [],
'output_schema' => $output_schema,
'preprocess_response' => function ( mixed $response_data ): array {
return GoogleSheetsDataSource::preprocess_list_response( $response_data );
},
] );
}

public static function preprocess_list_response( array $response_data ): array {
if ( isset( $response_data['values'] ) && is_array( $response_data['values'] ) ) {
$values = $response_data['values'];
$columns = array_shift( $values ); // Get column names from first row

$response_data['values'] = array_map(
function ( $row, $index ) use ( $columns ) {
$combined = array_combine( $columns, $row );
$combined['RowId'] = $index + 1; // Add row_id field, starting from 1
return $combined;
},
$values,
array_keys( $values )
);
}

return $response_data;
}

public static function preprocess_get_response( array $response_data, array $input_variables ): array {
$selected_row = null;
$row_id = $input_variables['row_id'];

if ( isset( $response_data['values'] ) && is_array( $response_data['values'] ) ) {
$values = $response_data['values'];
$columns = array_shift( $values ); // Get column names from first row
$raw_selected_row = $values[ $row_id - 1 ];
if ( is_array( $raw_selected_row ) ) {
$selected_row = array_combine( $columns, $raw_selected_row );
$selected_row['RowId'] = $row_id;
}
}

return $selected_row;
}
}
64 changes: 64 additions & 0 deletions inc/Integrations/Google/Sheets/GoogleSheetsIntegration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php declare(strict_types = 1);

namespace RemoteDataBlocks\Integrations\Google\Sheets;

use RemoteDataBlocks\WpdbStorage\DataSourceCrud;

class GoogleSheetsIntegration {
public static function init(): void {
add_action( 'init', [ __CLASS__, 'register_blocks' ], 10, 0 );
}

public static function register_blocks(): void {
$data_source_configs = DataSourceCrud::get_configs_by_service(
REMOTE_DATA_BLOCKS_GOOGLE_SHEETS_SERVICE
);

foreach ( $data_source_configs as $config ) {
$data_source = GoogleSheetsDataSource::from_array( $config );
self::register_block_for_google_sheets_data_source( $data_source );
self::register_loop_block_for_google_sheets_data_source( $data_source );
}
}

public static function register_block_for_google_sheets_data_source(
GoogleSheetsDataSource $data_source,
array $block_overrides = []
): void {
register_remote_data_block(
array_merge(
[
'title' => $data_source->get_display_name(),
'render_query' => [
'query' => $data_source->___temp_get_query(),
],
'selection_queries' => [
[
'query' => $data_source->___temp_get_list_query(),
'type' => 'list',
],
],
],
$block_overrides
)
);
}

public static function register_loop_block_for_google_sheets_data_source(
GoogleSheetsDataSource $data_source,
array $block_overrides = []
): void {
register_remote_data_block(
array_merge(
[
'title' => sprintf( '%s Loop', $data_source->get_display_name() ),
'render_query' => [
'loop' => true,
'query' => $data_source->___temp_get_list_query(),
],
],
$block_overrides
)
);
}
}
1 change: 1 addition & 0 deletions remote-data-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

// Integrations
Integrations\Airtable\AirtableIntegration::init();
Integrations\Google\Sheets\GoogleSheetsIntegration::init();
Integrations\Shopify\ShopifyIntegration::init();
Integrations\SalesforceB2C\SalesforceB2CIntegration::init();
Integrations\VipBlockDataApi\VipBlockDataApi::init();
Expand Down

0 comments on commit 1de044c

Please sign in to comment.