-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Expand interface-driven datasource configs (#39)
* yolo * bluh bluh bluh * fix elden * lint * hhhhii * remove airtable stuff
Showing
24 changed files
with
228 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/** | ||
* DatasourceInterface | ||
* | ||
* @package remote-data-blocks | ||
* @since 0.1.0 | ||
*/ | ||
|
||
namespace RemoteDataBlocks\Config; | ||
|
||
/** | ||
* Interface used to define a Remote Data Blocks Datasource. It defines the | ||
* properties of a datasource that will be shared by queries against that | ||
* datasource. | ||
* | ||
* If you are a WPVIP customer, datasources are automatically provided by VIP. | ||
* Only implement this interface if you have additional custom datasources. | ||
*/ | ||
interface DatasourceInterface { | ||
/** | ||
* Get a human-readable name for this datasource. | ||
* | ||
* This method should return a display name for the datasource that can be | ||
* used in user interfaces or for identification purposes. | ||
* | ||
* @return string The display name of the datasource. | ||
*/ | ||
public function get_display_name(): string; | ||
|
||
/** | ||
* An optional image URL that can represent the datasource in the block editor | ||
* (e.g., in modals or in the block inspector). | ||
* | ||
* @return string|null The image URL or null if not set. | ||
*/ | ||
public function get_image_url(): ?string; | ||
} |
16 changes: 0 additions & 16 deletions
16
inc/config/http-datasource-config/http-datasource-config.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/** | ||
* HttpDatasourceInterface | ||
* | ||
* @package remote-data-blocks | ||
* @since 0.1.0 | ||
*/ | ||
|
||
namespace RemoteDataBlocks\Config; | ||
|
||
/** | ||
* Interface used to define a Remote Data Blocks Datasource for an HTTP API. It | ||
* defines the properties of an API that will be shared by queries against that | ||
* API. | ||
* | ||
* Assumptions: | ||
* - The API speaks valid JSON, both for request and response bodies. | ||
* - The API returns 2XX for successful requests. | ||
* - The API returns 3XX for redirects with a maximum of 5 redirects. | ||
* - The API returns 4XX or 5XX for unrecoverable errors, in which case the | ||
* response should be ignored. | ||
* | ||
* If you are a WPVIP customer, datasources are automatically provided by VIP. | ||
* Only implement this interface if you have custom datasources not provided by VIP. | ||
*/ | ||
interface HttpDatasourceInterface { | ||
/** | ||
* Get the endpoint for the query. Note that the query configuration has an | ||
* opportunity to change / override the endpoint at request time. For REST | ||
* APIs, a useful pattern is for the datasource to define a base endpoint and | ||
* the query config to target a specific resource. | ||
* | ||
* @return string The endpoint for the query. | ||
*/ | ||
public function get_endpoint(): string; | ||
|
||
/** | ||
* Get the request headers. Override this method to provide authorization or | ||
* other custom request headers. Note that the query configuration can override | ||
* or extend these headers at request time. | ||
* | ||
* @return array Associative array of request headers. | ||
*/ | ||
public function get_request_headers(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,35 @@ | ||
<?php | ||
|
||
/** | ||
* HttpDatasource class | ||
* | ||
* @package remote-data-blocks | ||
* @since 0.1.0 | ||
*/ | ||
|
||
namespace RemoteDataBlocks\Config; | ||
|
||
defined( 'ABSPATH' ) || exit(); | ||
|
||
/** | ||
* Base class used to define a Remote Data Blocks Datasource for an HTTP API. It | ||
* defines the properties of an API that will be shared by queries against that | ||
* API. | ||
* HttpDatasource class | ||
* | ||
* Assumptions: | ||
* - The API speaks valid JSON, both for request and response bodies. | ||
* - The API returns 2XX for successful requests. | ||
* - The API returns 3XX for redirects with a maximum of 5 redirects. | ||
* - The API returns 4XX or 5XX for unrecoverable errors, in which case the | ||
* response should be ignored. | ||
* Implements the HttpDatasourceInterface to define a generic HTTP datasource. | ||
* | ||
* If you are a WPVIP customer, datasources are automatically provided by VIP. | ||
* Only extend this class if you have custom datasources not provided by VIP. | ||
* @package remote-data-blocks | ||
* @since 0.1.0 | ||
*/ | ||
abstract class HttpDatasource implements HttpDatasourceConfig { | ||
abstract class HttpDatasource implements DatasourceInterface, HttpDatasourceInterface { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
abstract public function get_display_name(): string; | ||
|
||
/** | ||
* Get the endpoint for the query. Note that the query configuration has an | ||
* opportunity to change / override the endpoint at request time. For REST | ||
* APIs, a useful pattern is for the datasource to define a base endpoint and | ||
* the query config to target a specific resource. | ||
* | ||
* @return string The endpoint for the query. | ||
* @inheritDoc | ||
*/ | ||
abstract public function get_endpoint(): string; | ||
|
||
/** | ||
* Get the request headers. Override this method to provide authorization or | ||
* other custom request headers. Note that the query configuration can override | ||
* or extend these headers at request time. | ||
* | ||
* @return array Associative array of request headers. | ||
* @inheritDoc | ||
*/ | ||
abstract public function get_request_headers(): array; | ||
|
||
/** | ||
* An optional image URL that can represent the datasource in the block editor | ||
* (e.g., in modals or in the block inspector). | ||
* @inheritDoc | ||
*/ | ||
public function get_image_url(): string|null { | ||
public function get_image_url(): ?string { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 13 additions & 4 deletions
17
.../inc/queries/class-shopify-datasource.php → ...ration/datasources/shopify-datasource.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
4 changes: 1 addition & 3 deletions
4
...eries/class-shopify-get-product-query.php → ...ion/queries/shopify-get-product-query.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
...s/class-shopify-search-products-query.php → ...queries/shopify-search-products-query.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
inc/integrations/shopify-integration/shopify-integration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace RemoteDataBlocks\Integrations; | ||
|
||
use RemoteDataBlocks\Config\ShopifyDatasource; | ||
use RemoteDataBlocks\Config\ShopifyGetProductQuery; | ||
use RemoteDataBlocks\Config\ShopifySearchProductsQuery; | ||
use RemoteDataBlocks\Editor\ConfigurationLoader; | ||
use RemoteDataBlocks\Logging\LoggerManager; | ||
use RemoteDataBlocks\REST\DatasourceCRUD; | ||
|
||
require_once __DIR__ . '/datasources/shopify-datasource.php'; | ||
require_once __DIR__ . '/queries/shopify-get-product-query.php'; | ||
require_once __DIR__ . '/queries/shopify-search-products-query.php'; | ||
|
||
class ShopifyIntegration { | ||
public static function init(): void { | ||
self::register_dynamic_data_source_blocks(); | ||
} | ||
|
||
private static function register_dynamic_data_source_blocks(): void { | ||
$data_sources = DatasourceCRUD::get_data_sources( REMOTE_DATA_BLOCKS_SHOPIFY_SERVICE ); | ||
|
||
foreach ( $data_sources as $config ) { | ||
// Transform data to our experimental format, which is all array based | ||
$config = array_map( | ||
function ( $value ) { | ||
return is_object( $value ) ? (array) $value : $value; | ||
}, | ||
(array) $config | ||
); | ||
self::register_blocks_for_shopify_data_source( $config ); | ||
} | ||
} | ||
|
||
private static function register_blocks_for_shopify_data_source( array $config ): void { | ||
$shopify_datasource = new ShopifyDatasource( $config['token'], $config['store'] ); | ||
$shopify_search_products_query = new ShopifySearchProductsQuery( $shopify_datasource ); | ||
$shopify_get_product_query = new ShopifyGetProductQuery( $shopify_datasource ); | ||
|
||
$block_name = $shopify_datasource->get_display_name(); | ||
$block_pattern = file_get_contents( __DIR__ . '/patterns/product-teaser.html' ); | ||
|
||
ConfigurationLoader::register_block( $block_name, $shopify_get_product_query ); | ||
ConfigurationLoader::register_search_query( $block_name, $shopify_search_products_query ); | ||
ConfigurationLoader::register_block_pattern( $block_name, 'remote-data-blocks/shopify-product-teaser', $block_pattern, [ 'title' => 'Shopify Product Teaser' ] ); | ||
|
||
LoggerManager::instance()->info( 'Registered Shopify block', [ 'block_name' => $block_name ] ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters