Skip to content

Commit e48a1c1

Browse files
authored
Merge branch 'trunk' into add/wp-phpunit-integration-tests
2 parents 6aa3dae + 77efde3 commit e48a1c1

File tree

11 files changed

+231
-66
lines changed

11 files changed

+231
-66
lines changed

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/extending/query-runner.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The `get_raw_response_data` method dispatches the HTTP request and assembles the
2828

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

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

3333
## Custom query execution
3434

docs/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ For plugin overview and getting started guide, see [README](../README.md).
2020

2121
- [Workflows](workflows/index.md)
2222

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

2727
- [Development](local-development.md)
2828
- [Troubleshooting](troubleshooting.md)

docs/workflows/google-sheets.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Create a Google Sheets remote data block
22

3-
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/).
3+
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/).
44

55
## Google Sheets API Access
66

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

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

3131
## Code Reference
3232

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

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

39-
- [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.
39+
- [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.
4040
- Add sheet named `Houses` inside the newly created Google Sheet with columns with headers as
4141
- House
4242
- Seat

example/google-sheets/westeros-houses/register.php

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,8 @@ function register_westeros_houses_block(): void {
7676
],
7777
],
7878
],
79-
'preprocess_response' => function ( mixed $response_data ) use ( $columns ): array {
80-
if ( isset( $response_data['values'] ) && is_array( $response_data['values'] ) ) {
81-
$values = $response_data['values'];
82-
array_shift( $values ); // Drop the first row
83-
84-
$response_data['values'] = array_map(
85-
function ( $row, $index ) use ( $columns ) {
86-
$combined = array_combine( $columns, $row );
87-
$combined['RowId'] = $index + 1; // Add row_id field, starting from 1
88-
return $combined;
89-
},
90-
$values,
91-
array_keys( $values )
92-
);
93-
}
94-
95-
return $response_data;
79+
'preprocess_response' => function ( mixed $response_data ): array {
80+
return GoogleSheetsDataSource::preprocess_list_response( $response_data );
9681
},
9782
] );
9883

@@ -139,20 +124,8 @@ function ( $row, $index ) use ( $columns ) {
139124
],
140125
],
141126
],
142-
'preprocess_response' => function ( mixed $response_data, array $input_variables ) use ( $columns ): array {
143-
$selected_row = null;
144-
$row_id = $input_variables['row_id'];
145-
146-
if ( isset( $response_data['values'] ) && is_array( $response_data['values'] ) ) {
147-
$raw_selected_row = $response_data['values'][ $row_id ];
148-
if ( is_array( $raw_selected_row ) ) {
149-
$selected_row = array_combine( $columns, $raw_selected_row );
150-
$selected_row = array_combine( $columns, $selected_row );
151-
$selected_row['RowId'] = $row_id;
152-
}
153-
}
154-
155-
return $selected_row;
127+
'preprocess_response' => function ( mixed $response_data, array $input_variables ): array {
128+
return GoogleSheetsDataSource::preprocess_get_response( $response_data, $input_variables );
156129
},
157130
] );
158131

inc/Integrations/Airtable/AirtableIntegration.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@ public static function init(): void {
1010
}
1111

1212
public static function register_blocks(): void {
13-
$data_source_configs = DataSourceCrud::get_configs_by_service( REMOTE_DATA_BLOCKS_AIRTABLE_SERVICE );
13+
$data_source_configs = DataSourceCrud::get_configs_by_service(
14+
REMOTE_DATA_BLOCKS_AIRTABLE_SERVICE
15+
);
1416

1517
foreach ( $data_source_configs as $config ) {
1618
$data_source = AirtableDataSource::from_array( $config );
1719
self::register_block_for_airtable_data_source( $data_source );
1820
}
1921
}
2022

21-
public static function register_block_for_airtable_data_source( AirtableDataSource $data_source, array $block_overrides = [] ): void {
23+
public static function register_block_for_airtable_data_source(
24+
AirtableDataSource $data_source,
25+
array $block_overrides = []
26+
): void {
2227
register_remote_data_block(
2328
array_merge(
2429
[
@@ -38,7 +43,10 @@ public static function register_block_for_airtable_data_source( AirtableDataSour
3843
);
3944
}
4045

41-
public static function register_loop_block_for_airtable_data_source( AirtableDataSource $data_source, array $block_overrides = [] ): void {
46+
public static function register_loop_block_for_airtable_data_source(
47+
AirtableDataSource $data_source,
48+
array $block_overrides = []
49+
): void {
4250
register_remote_data_block(
4351
array_merge(
4452
[

inc/Integrations/Google/Sheets/GoogleSheetsDataSource.php

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace RemoteDataBlocks\Integrations\Google\Sheets;
44

55
use RemoteDataBlocks\Config\DataSource\HttpDataSource;
6+
use RemoteDataBlocks\Config\Query\HttpQuery;
67
use RemoteDataBlocks\Integrations\Google\Auth\GoogleAuth;
78
use RemoteDataBlocks\Validation\Types;
89

@@ -51,7 +52,10 @@ protected static function get_service_config_schema(): array {
5152
protected static function map_service_config( array $service_config ): array {
5253
return [
5354
'display_name' => $service_config['display_name'],
54-
'endpoint' => sprintf( 'https://sheets.googleapis.com/v4/spreadsheets/%s', $service_config['spreadsheet']['id'] ),
55+
'endpoint' => sprintf(
56+
'https://sheets.googleapis.com/v4/spreadsheets/%s',
57+
$service_config['spreadsheet']['id']
58+
),
5559
'request_headers' => function () use ( $service_config ): array {
5660
$access_token = GoogleAuth::generate_token_from_service_account_key(
5761
$service_config['credentials'],
@@ -65,4 +69,120 @@ protected static function map_service_config( array $service_config ): array {
6569
},
6670
];
6771
}
72+
73+
public function ___temp_get_query(): HttpQuery {
74+
$service_config = $this->config['service_config'];
75+
76+
$input_schema = [
77+
'row_id' => [
78+
'name' => 'Row ID',
79+
'type' => 'id',
80+
],
81+
];
82+
83+
$output_schema = [
84+
'is_collection' => false,
85+
'type' => [
86+
'row_id' => [
87+
'name' => 'Row ID',
88+
'path' => '$.RowId',
89+
'type' => 'id',
90+
],
91+
],
92+
];
93+
94+
foreach ( $service_config['sheets'][0]['output_query_mappings'] as $mapping ) {
95+
$mapping_key = $mapping['key'];
96+
$output_schema['type'][ $mapping_key ] = [
97+
'name' => $mapping['name'] ?? $mapping_key,
98+
'path' => $mapping['path'] ?? '$.fields["' . $mapping_key . '"]',
99+
'type' => $mapping['type'] ?? 'string',
100+
];
101+
}
102+
103+
return HttpQuery::from_array( [
104+
'data_source' => $this,
105+
'endpoint' => function (): string {
106+
return $this->get_endpoint() . '/values/' . $this->config['service_config']['sheets'][0]['name'];
107+
},
108+
'input_schema' => $input_schema,
109+
'output_schema' => $output_schema,
110+
'preprocess_response' => function ( mixed $response_data, array $input_variables ): array {
111+
return GoogleSheetsDataSource::preprocess_get_response( $response_data, $input_variables );
112+
},
113+
] );
114+
}
115+
116+
public function ___temp_get_list_query(): HttpQuery {
117+
$service_config = $this->config['service_config'];
118+
119+
$output_schema = [
120+
'is_collection' => true,
121+
'path' => '$.values[*]',
122+
'type' => [
123+
'row_id' => [
124+
'name' => 'Row ID',
125+
'path' => '$.RowId',
126+
'type' => 'id',
127+
],
128+
],
129+
];
130+
131+
foreach ( $service_config['sheets'][0]['output_query_mappings'] as $mapping ) {
132+
$mapping_key = $mapping['key'];
133+
$output_schema['type'][ $mapping_key ] = [
134+
'name' => $mapping['name'] ?? $mapping_key,
135+
'path' => $mapping['path'] ?? '$.fields["' . $mapping_key . '"]',
136+
'type' => $mapping['type'] ?? 'string',
137+
];
138+
}
139+
140+
return HttpQuery::from_array( [
141+
'data_source' => $this,
142+
'endpoint' => function (): string {
143+
return $this->get_endpoint() . '/values/' . $this->config['service_config']['sheets'][0]['name'];
144+
},
145+
'input_schema' => [],
146+
'output_schema' => $output_schema,
147+
'preprocess_response' => function ( mixed $response_data ): array {
148+
return GoogleSheetsDataSource::preprocess_list_response( $response_data );
149+
},
150+
] );
151+
}
152+
153+
public static function preprocess_list_response( array $response_data ): array {
154+
if ( isset( $response_data['values'] ) && is_array( $response_data['values'] ) ) {
155+
$values = $response_data['values'];
156+
$columns = array_shift( $values ); // Get column names from first row
157+
158+
$response_data['values'] = array_map(
159+
function ( $row, $index ) use ( $columns ) {
160+
$combined = array_combine( $columns, $row );
161+
$combined['RowId'] = $index + 1; // Add row_id field, starting from 1
162+
return $combined;
163+
},
164+
$values,
165+
array_keys( $values )
166+
);
167+
}
168+
169+
return $response_data;
170+
}
171+
172+
public static function preprocess_get_response( array $response_data, array $input_variables ): array {
173+
$selected_row = null;
174+
$row_id = $input_variables['row_id'];
175+
176+
if ( isset( $response_data['values'] ) && is_array( $response_data['values'] ) ) {
177+
$values = $response_data['values'];
178+
$columns = array_shift( $values ); // Get column names from first row
179+
$raw_selected_row = $values[ $row_id - 1 ];
180+
if ( is_array( $raw_selected_row ) ) {
181+
$selected_row = array_combine( $columns, $raw_selected_row );
182+
$selected_row['RowId'] = $row_id;
183+
}
184+
}
185+
186+
return $selected_row;
187+
}
68188
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace RemoteDataBlocks\Integrations\Google\Sheets;
4+
5+
use RemoteDataBlocks\WpdbStorage\DataSourceCrud;
6+
7+
class GoogleSheetsIntegration {
8+
public static function init(): void {
9+
add_action( 'init', [ __CLASS__, 'register_blocks' ], 10, 0 );
10+
}
11+
12+
public static function register_blocks(): void {
13+
$data_source_configs = DataSourceCrud::get_configs_by_service(
14+
REMOTE_DATA_BLOCKS_GOOGLE_SHEETS_SERVICE
15+
);
16+
17+
foreach ( $data_source_configs as $config ) {
18+
$data_source = GoogleSheetsDataSource::from_array( $config );
19+
self::register_block_for_google_sheets_data_source( $data_source );
20+
self::register_loop_block_for_google_sheets_data_source( $data_source );
21+
}
22+
}
23+
24+
public static function register_block_for_google_sheets_data_source(
25+
GoogleSheetsDataSource $data_source,
26+
array $block_overrides = []
27+
): void {
28+
register_remote_data_block(
29+
array_merge(
30+
[
31+
'title' => $data_source->get_display_name(),
32+
'render_query' => [
33+
'query' => $data_source->___temp_get_query(),
34+
],
35+
'selection_queries' => [
36+
[
37+
'query' => $data_source->___temp_get_list_query(),
38+
'type' => 'list',
39+
],
40+
],
41+
],
42+
$block_overrides
43+
)
44+
);
45+
}
46+
47+
public static function register_loop_block_for_google_sheets_data_source(
48+
GoogleSheetsDataSource $data_source,
49+
array $block_overrides = []
50+
): void {
51+
register_remote_data_block(
52+
array_merge(
53+
[
54+
'title' => sprintf( '%s Loop', $data_source->get_display_name() ),
55+
'render_query' => [
56+
'loop' => true,
57+
'query' => $data_source->___temp_get_list_query(),
58+
],
59+
],
60+
$block_overrides
61+
)
62+
);
63+
}
64+
}

0 commit comments

Comments
 (0)