Skip to content

Commit

Permalink
Revert "Merge branch 'trunk' into add/wp-phpunit-integration-tests"
Browse files Browse the repository at this point in the history
This reverts commit e48a1c1, reversing
changes made to 6aa3dae.
  • Loading branch information
maxschmeling committed Jan 6, 2025
1 parent e48a1c1 commit 123b55f
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 231 deletions.
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/extending/query-runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The `get_raw_response_data` method dispatches the HTTP request and assembles the

### get_response_metadata( HttpQueryInterface $query, array $response_metadata, array $query_results ): array

The `get_response_metadata` method returns the response metadata for the query, which are available as bindings for [field shortcodes](../concepts/field-shortcodes.md).
The `get_response_metadata` method returns the response metadata for the query, which are available as bindings for [field shortcodes](field-shortcodes.md).

## Custom query execution

Expand Down
6 changes: 3 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ For plugin overview and getting started guide, see [README](../README.md).

- [Workflows](workflows/index.md)

- [Airtable Integration](workflows/airtable.md)
- [Google Sheets Integration](workflows/google-sheets.md)
- [REST API Integration](workflows/rest-api.md)
- [Airtable Integration](workflows/airtable-with-code.md)
- [Google Sheets Integration](workflows/google-sheets-with-code.md)
- [ZIP Code Integration](workflows/zip-code-with-contract.md)

- [Development](local-development.md)
- [Troubleshooting](troubleshooting.md)
Expand Down
8 changes: 4 additions & 4 deletions docs/workflows/google-sheets.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Create a Google Sheets remote data block

This page will walk you through connecting a [Google Sheets](https://workspace.google.com/products/sheets/) data source. The remote block registration to display sheet records and the styling of that block is similar to the [Airtable workflow](./airtable.md). If you have not yet installed and activated the Remote Data Blocks plugin, visit [Getting Started](https://remotedatablocks.com/getting-started/).
This page will walk you through connecting a [Google Sheets](https://workspace.google.com/products/sheets/) data source. The remote block registration to display sheet records and the styling of that block is similar to the [Airtable workflow](./airtable-with-code.md). If you have not yet installed and activated the Remote Data Blocks plugin, visit [Getting Started](https://remotedatablocks.com/getting-started/).

## Google Sheets API Access

Expand All @@ -25,8 +25,8 @@ The Service Account Keys JSON should be provided to your application securely. O
This would be similar to the [Airtable workflow](airtable.md). Refer the following sections from that workflow:

- [Create the data source](./airtable.md#create-the-data-source)
- [Insert the block](./airtable.md#insert-the-block)
- [Custom patterns and styling](./airtable.md#custom-patterns-and-styling)
- [Insert the block](./airtable-with-code.md#insert-the-block)
- [Custom patterns and styling](./airtable-with-code.md#custom-patterns-and-styling)

## Code Reference

Expand All @@ -36,7 +36,7 @@ Check out [a working example](https://github.com/Automattic/remote-data-blocks/t

Follow following setup steps to get the Westeros Houses example working:

- [Configure the Google Sheet API Access](./google-sheets.md#google-sheets-api-access) and [Create a new Google Sheet](./google-sheets.md#setting-up-the-google-sheet) by following the steps above.
- [Configure the Google Sheet API Access](./google-sheets-with-code.md#google-sheets-api-access) and [Create a new Google Sheet](./google-sheets-with-code.md#setting-up-the-google-sheet) by following the steps above.
- Add sheet named `Houses` inside the newly created Google Sheet with columns with headers as
- House
- Seat
Expand Down
35 changes: 31 additions & 4 deletions example/google-sheets/westeros-houses/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,23 @@ function register_westeros_houses_block(): void {
],
],
],
'preprocess_response' => function ( mixed $response_data ): array {
return GoogleSheetsDataSource::preprocess_list_response( $response_data );
'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;
},
] );

Expand Down Expand Up @@ -124,8 +139,20 @@ function register_westeros_houses_block(): void {
],
],
],
'preprocess_response' => function ( mixed $response_data, array $input_variables ): array {
return GoogleSheetsDataSource::preprocess_get_response( $response_data, $input_variables );
'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;
},
] );

Expand Down
14 changes: 3 additions & 11 deletions inc/Integrations/Airtable/AirtableIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,15 @@ 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 @@ -43,10 +38,7 @@ public static function register_block_for_airtable_data_source(
);
}

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: 1 addition & 121 deletions inc/Integrations/Google/Sheets/GoogleSheetsDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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 @@ -52,10 +51,7 @@ 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 @@ -69,120 +65,4 @@ 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: 0 additions & 64 deletions inc/Integrations/Google/Sheets/GoogleSheetsIntegration.php

This file was deleted.

Loading

0 comments on commit 123b55f

Please sign in to comment.