diff --git a/composer.lock b/composer.lock index 0447f2ba..45675d4f 100644 --- a/composer.lock +++ b/composer.lock @@ -2279,16 +2279,16 @@ }, { "name": "phpunit/phpunit", - "version": "10.5.40", + "version": "9.6.22", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "e6ddda95af52f69c1e0c7b4f977cccb58048798c" + "reference": "f80235cb4d3caa59ae09be3adf1ded27521d1a9c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/e6ddda95af52f69c1e0c7b4f977cccb58048798c", - "reference": "e6ddda95af52f69c1e0c7b4f977cccb58048798c", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f80235cb4d3caa59ae09be3adf1ded27521d1a9c", + "reference": "f80235cb4d3caa59ae09be3adf1ded27521d1a9c", "shasum": "" }, "require": { @@ -2362,7 +2362,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/10.5.40" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.22" }, "funding": [ { @@ -2378,7 +2378,7 @@ "type": "tidelift" } ], - "time": "2024-12-21T05:49:06+00:00" + "time": "2024-12-05T13:48:26+00:00" }, { "name": "psalm/phar", diff --git a/docs/extending/query-runner.md b/docs/extending/query-runner.md index 8668147f..2b1cbb3b 100644 --- a/docs/extending/query-runner.md +++ b/docs/extending/query-runner.md @@ -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 diff --git a/docs/index.md b/docs/index.md index a3327979..98f67136 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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) diff --git a/docs/workflows/google-sheets.md b/docs/workflows/google-sheets.md index 3b2cb073..85c17fae 100644 --- a/docs/workflows/google-sheets.md +++ b/docs/workflows/google-sheets.md @@ -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 @@ -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 @@ -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 diff --git a/example/google-sheets/westeros-houses/register.php b/example/google-sheets/westeros-houses/register.php index 122759e6..63cd4a08 100644 --- a/example/google-sheets/westeros-houses/register.php +++ b/example/google-sheets/westeros-houses/register.php @@ -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; }, ] ); @@ -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; }, ] ); diff --git a/inc/Integrations/Airtable/AirtableIntegration.php b/inc/Integrations/Airtable/AirtableIntegration.php index 04e64ee3..e6a58830 100644 --- a/inc/Integrations/Airtable/AirtableIntegration.php +++ b/inc/Integrations/Airtable/AirtableIntegration.php @@ -10,9 +10,7 @@ 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 ); @@ -20,10 +18,7 @@ public static function register_blocks(): void { } } - 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( [ @@ -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( [ diff --git a/inc/Integrations/Google/Sheets/GoogleSheetsDataSource.php b/inc/Integrations/Google/Sheets/GoogleSheetsDataSource.php index fbe48e4e..43229040 100644 --- a/inc/Integrations/Google/Sheets/GoogleSheetsDataSource.php +++ b/inc/Integrations/Google/Sheets/GoogleSheetsDataSource.php @@ -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; @@ -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'], @@ -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; - } } diff --git a/inc/Integrations/Google/Sheets/GoogleSheetsIntegration.php b/inc/Integrations/Google/Sheets/GoogleSheetsIntegration.php deleted file mode 100644 index dbd39ee5..00000000 --- a/inc/Integrations/Google/Sheets/GoogleSheetsIntegration.php +++ /dev/null @@ -1,64 +0,0 @@ - $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 - ) - ); - } -} diff --git a/package-lock.json b/package-lock.json index 7854dcdd..361549ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -46,9 +46,9 @@ "@wordpress/url": "4.14.0", "eslint": "8.57.1", "fork-ts-checker-webpack-plugin": "9.0.2", - "happy-dom": "16.3.0", + "happy-dom": "15.11.7", "husky": "9.1.7", - "lint-staged": "15.3.0", + "lint-staged": "15.2.11", "prettier": "npm:wp-prettier@2.8.5", "react": "18.3.1", "stylelint": "16.11.0", @@ -16376,11 +16376,12 @@ "dev": true }, "node_modules/happy-dom": { - "version": "16.3.0", - "resolved": "https://registry.npmjs.org/happy-dom/-/happy-dom-16.3.0.tgz", - "integrity": "sha512-Q71RaIhyS21vhW17Tpa5W36yqQXIlE1TZ0A0Gguts8PShUSQE/7fBgxYGxgm3+5y0gF6afdlAVHLQqgrIcfRzg==", + "version": "15.11.7", + "resolved": "https://registry.npmjs.org/happy-dom/-/happy-dom-15.11.7.tgz", + "integrity": "sha512-KyrFvnl+J9US63TEzwoiJOQzZBJY7KgBushJA8X61DMbNsH+2ONkDuLDnCnwUiPTF42tLoEmrPyoqbenVA5zrg==", "dev": true, "dependencies": { + "entities": "^4.5.0", "webidl-conversions": "^7.0.0", "whatwg-mimetype": "^3.0.0" }, @@ -19086,12 +19087,12 @@ } }, "node_modules/lint-staged": { - "version": "15.3.0", - "resolved": "https://registry.npmjs.org/lint-staged/-/lint-staged-15.3.0.tgz", - "integrity": "sha512-vHFahytLoF2enJklgtOtCtIjZrKD/LoxlaUusd5nh7dWv/dkKQJY74ndFSzxCdv7g0ueGg1ORgTSt4Y9LPZn9A==", + "version": "15.2.11", + "resolved": "https://registry.npmjs.org/lint-staged/-/lint-staged-15.2.11.tgz", + "integrity": "sha512-Ev6ivCTYRTGs9ychvpVw35m/bcNDuBN+mnTeObCL5h+boS5WzBEC6LHI4I9F/++sZm1m+J2LEiy0gxL/R9TBqQ==", "dev": true, "dependencies": { - "chalk": "~5.4.1", + "chalk": "~5.3.0", "commander": "~12.1.0", "debug": "~4.4.0", "execa": "~8.0.1", @@ -19113,9 +19114,9 @@ } }, "node_modules/lint-staged/node_modules/chalk": { - "version": "5.4.1", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.4.1.tgz", - "integrity": "sha512-zgVZuo2WcZgfUEmsn6eO3kINexW8RAE4maiQ8QNs8CtpPCSyMiYsULR3HQYkm3w8FIA3SberyMJMSldGsW+U3w==", + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.3.0.tgz", + "integrity": "sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==", "dev": true, "engines": { "node": "^12.17.0 || ^14.13 || >=16.0.0" diff --git a/package.json b/package.json index 4cf4b3d0..57dfc527 100644 --- a/package.json +++ b/package.json @@ -89,9 +89,9 @@ "@wordpress/url": "4.14.0", "eslint": "8.57.1", "fork-ts-checker-webpack-plugin": "9.0.2", - "happy-dom": "16.3.0", + "happy-dom": "15.11.7", "husky": "9.1.7", - "lint-staged": "15.3.0", + "lint-staged": "15.2.11", "prettier": "npm:wp-prettier@2.8.5", "react": "18.3.1", "stylelint": "16.11.0", diff --git a/remote-data-blocks.php b/remote-data-blocks.php index 472ffa4e..05f23e33 100644 --- a/remote-data-blocks.php +++ b/remote-data-blocks.php @@ -7,7 +7,7 @@ * Author: WPVIP * Author URI: https://wpvip.com * Text Domain: remote-data-blocks - * Version: 0.5.0 + * Version: 0.3.3 * Requires at least: 6.7 * Requires PHP: 8.1 */ @@ -18,7 +18,7 @@ define( 'REMOTE_DATA_BLOCKS__PLUGIN_ROOT', __FILE__ ); define( 'REMOTE_DATA_BLOCKS__PLUGIN_DIRECTORY', untrailingslashit( plugin_dir_path( __FILE__ ) ) ); -define( 'REMOTE_DATA_BLOCKS__PLUGIN_VERSION', '0.5.0' ); +define( 'REMOTE_DATA_BLOCKS__PLUGIN_VERSION', '0.3.3' ); define( 'REMOTE_DATA_BLOCKS__REST_NAMESPACE', 'remote-data-blocks/v1' ); @@ -45,7 +45,6 @@ // Integrations Integrations\Airtable\AirtableIntegration::init(); -Integrations\Google\Sheets\GoogleSheetsIntegration::init(); Integrations\Shopify\ShopifyIntegration::init(); Integrations\SalesforceB2C\SalesforceB2CIntegration::init(); Integrations\VipBlockDataApi\VipBlockDataApi::init();