Skip to content

Commit

Permalink
Change data source slugs to an overridable auto-UUID (#229)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: brookewp <[email protected]>
Co-authored-by: Max Schmeling <[email protected]>
  • Loading branch information
brookewp and maxschmeling authored Dec 12, 2024
1 parent 78e3755 commit 4c1ecac
Show file tree
Hide file tree
Showing 37 changed files with 415 additions and 456 deletions.
16 changes: 9 additions & 7 deletions docs/workflows/zip-code-with-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This page will walk you through building [Zippopotam.us](https://zippopotam.us/)

## The contract

Developers can code a "slug" to define a "contract" between the remote data block integration they build and the admins managing the Remote Data Blocks settings in WordPress.
Developers can code a version 4 UUID to define a "contract" between the remote data block integration they build and the admins managing the Remote Data Blocks settings in WordPress.

## Define a query

Expand Down Expand Up @@ -73,7 +73,7 @@ use RemoteDataBlocks\Logging\LoggerManager;
require_once __DIR__ . '/inc/queries/class-get-zip-code-query.php';

function register_zipcode_block() {
$zipcode_data_source = GenericHttpDataSource::from_slug( 'zip-code' );
$zipcode_data_source = GenericHttpDataSource::from_uuid( '0d8f9e74-5244-49b4-981b-e5374107aa5c' );

if ( ! $zipcode_data_source instanceof GenericHttpDataSource ) {
LoggerManager::instance()->debug( 'Zip Code data source not found' );
Expand All @@ -88,7 +88,7 @@ add_action( 'init', __NAMESPACE__ . '\\register_zipcode_block' );

```

Note the `zip-code` slug in the `GenericHttpDataSource::from_slug` call. That's the "contract" in our implementation.
Note the `0d8f9e74-5244-49b4-981b-e5374107aa5c` UUID in the `GenericHttpDataSource::from_uuid` call. That's the "contract" in our implementation.

We're done!

Expand All @@ -97,12 +97,14 @@ We're done!
An admin can seperately set up the data source via the following steps:

1. Go to the Remote Data Blocks settings page in your WordPress admin area.
2. Click on "Add Data Source".
3. Choose "Generic HTTP" as the data source type.
2. Click on the "Add" menu button.
3. Choose "HTTP" from the dropdown menu as the data source type.
4. Fill in the following details:
- Name: Zip Code API
- Slug: zip-code
- Endpoint: https://api.zippopotam.us/us/
5. Save the data source.
6. Click on the edit icon for the newly saved data source.
7. Click on the settings icon next to "Edit HTTP Data Source".
8. Update the UUID to match the one defined in the code or vice versa.

The slug _must_ match the slug defined by the developer in the previous section when creating the data source.
The UUID _must_ match the UUID defined by the developer in the previous section when creating the data source.
2 changes: 1 addition & 1 deletion example/github/remote-data-blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function register_github_file_as_html_block() {

$block_name = sprintf( 'GitHub File As HTML (%s/%s)', $repo_owner, $repo_name );

$github_data_source = GitHubDataSource::create( $repo_owner, $repo_name, $branch );
$github_data_source = GitHubDataSource::create( $repo_owner, $repo_name, $branch, '37fb2ac2-2399-4095-94d4-d58037d66c61' );
$github_get_file_as_html_query = new GitHubGetFileAsHtmlQuery( $github_data_source, '.md' );
$github_get_list_files_query = new GitHubListFilesQuery( $github_data_source, '.md' );

Expand Down
2 changes: 1 addition & 1 deletion example/rest-api/art-institute/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

function register_aic_block() {
$aic_data_source = ArtInstituteOfChicagoDataSource::from_array( [
'slug' => 'art-institute-of-chicago',
'uuid' => '7c979e0d-67ca-44f8-a835-3c6fbf0f01d0',
'service' => 'art-institute-of-chicago',
] );

Expand Down
2 changes: 1 addition & 1 deletion example/rest-api/zip-code/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
require_once __DIR__ . '/inc/queries/class-get-zip-code-query.php';

function register_zipcode_block() {
$zipcode_data_source = GenericHttpDataSource::from_slug( 'zip-code' );
$zipcode_data_source = GenericHttpDataSource::from_uuid( '0d8f9e74-5244-49b4-981b-e5374107aa5c' );

if ( ! $zipcode_data_source instanceof GenericHttpDataSource ) {
LoggerManager::instance()->debug( 'Zip Code data source not found' );
Expand Down
4 changes: 0 additions & 4 deletions inc/Config/DataSource/DataSourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ interface DataSourceInterface {
],
'service' => [ 'type' => 'string' ],
'service_schema_version' => [ 'type' => 'integer' ],
'slug' => [
'type' => 'string',
'pattern' => '/^[a-z0-9-]+$/',
],
'uuid' => [
'type' => 'string',
'callback' => 'wp_is_uuid',
Expand Down
10 changes: 5 additions & 5 deletions inc/Config/DataSource/HttpDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public function get_service(): ?string {
return $this->config['service'] ?? null;
}

public function get_slug(): string {
return $this->config['slug'];
public function get_uuid(): string {
return $this->config['uuid'];
}

/**
Expand All @@ -60,8 +60,8 @@ final public static function get_config_schema(): array {
return $schema;
}

public static function from_slug( string $slug ): DataSourceInterface|WP_Error {
$config = DataSourceCrud::get_by_slug( $slug );
public static function from_uuid( string $uuid ): DataSourceInterface|WP_Error {
$config = DataSourceCrud::get_by_uuid( $uuid );

if ( ! $config ) {
return new WP_Error( 'data_source_not_found', __( 'Data source not found.', 'remote-data-blocks' ), [ 'status' => 404 ] );
Expand Down Expand Up @@ -105,7 +105,7 @@ public function to_ui_display(): array {
// TODO: Implement remove from children and implement here in standardized way
return [
'display_name' => $this->get_display_name(),
'slug' => $this->get_slug(),
'uuid' => $this->get_uuid(),
'service' => static::SERVICE_NAME,
];
}
Expand Down
5 changes: 4 additions & 1 deletion inc/Editor/BlockManagement/ConfigStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ public static function get_data_sources_displayable(): array {
$data_source = $query->get_data_source();

if ( $data_source instanceof UiDisplayableInterface ) {
$data_sources[ $data_source->to_array()['slug'] ] = $data_source->to_ui_display();
$data_source_array = $data_source->to_array();
if ( isset( $data_source_array['uuid'] ) ) {
$data_sources[ $data_source_array['uuid'] ] = $data_source->to_ui_display();
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion inc/ExampleApi/ExampleApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function register_remote_data_block(): void {
}

$data_source = ExampleApiDataSource::from_array( [
'slug' => 'example-api',
'uuid' => 'bf4bc2b4-c06a-40d2-80f2-a682d81d63f5',
'service' => 'example_api',
] );

Expand Down
7 changes: 3 additions & 4 deletions inc/Integrations/Airtable/AirtableDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ class AirtableDataSource extends HttpDataSource {
],
'display_name' => [
'type' => 'string',
'required' => false,
'required' => true,
],
],
];

public function get_display_name(): string {
return sprintf( 'Airtable (%s)', $this->config['display_name'] ?? $this->config['slug'] ?? $this->config['base']['name'] );
return sprintf( 'Airtable (%s)', $this->config['display_name'] ?? $this->config['base']['name'] );
}

public function get_endpoint(): string {
Expand All @@ -96,20 +96,19 @@ public static function create( string $access_token, string $base_id, ?array $ta
'base' => [ 'id' => $base_id ],
'tables' => $tables,
'display_name' => $display_name,
'slug' => $display_name ? sanitize_title( $display_name ) : sanitize_title( 'Airtable ' . $base_id ),
]);
}

public function to_ui_display(): array {
return [
'slug' => $this->get_slug(),
'service' => REMOTE_DATA_BLOCKS_AIRTABLE_SERVICE,
'base' => [
'id' => $this->config['base']['id'],
'name' => $this->config['base']['name'] ?? null,
],
'tables' => $this->config['tables'] ?? [],
'uuid' => $this->config['uuid'] ?? null,
'display_name' => $this->config['display_name'] ?? null,
];
}

Expand Down
10 changes: 7 additions & 3 deletions inc/Integrations/GenericHttp/GenericHttpDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ class GenericHttpDataSource extends HttpDataSource {
'callback' => '\RemoteDataBlocks\Validation\is_url',
'sanitize' => 'sanitize_url',
],
'display_name' => [
'type' => 'string',
'required' => false,
],
],
];

public function get_display_name(): string {
return 'HTTP Connection (' . $this->config['slug'] . ')';
return 'HTTP Connection (' . $this->config['display_name'] . ')';
}

public function get_endpoint(): string {
Expand All @@ -66,16 +70,16 @@ public function get_request_headers(): array {

public static function create( string $url, string $auth, string $display_name ): self {
return parent::from_array([
'display_name' => $display_name,
'service' => REMOTE_DATA_BLOCKS_GENERIC_HTTP_SERVICE,
'url' => $url,
'auth' => $auth,
'slug' => sanitize_title( $display_name ),
]);
}

public function to_ui_display(): array {
return [
'slug' => $this->get_slug(),
'display_name' => $this->get_display_name(),
'service' => REMOTE_DATA_BLOCKS_GENERIC_HTTP_SERVICE,
'url' => $this->config['url'],
'auth_type' => $this->config['auth']['type'],
Expand Down
5 changes: 3 additions & 2 deletions inc/Integrations/GitHub/GitHubDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ public function get_ref(): string {
return $this->config['ref'];
}

public static function create( string $repo_owner, string $repo_name, string $ref ): self {
public static function create( string $repo_owner, string $repo_name, string $ref, string $uuid ): self {
return parent::from_array([
'display_name' => sprintf( 'GitHub: %s/%s (%s)', $repo_owner, $repo_name, $ref ),
'service' => REMOTE_DATA_BLOCKS_GITHUB_SERVICE,
'repo_owner' => $repo_owner,
'repo_name' => $repo_name,
'ref' => $ref,
'slug' => sanitize_title( sprintf( '%s/%s/%s', $repo_owner, $repo_name, $ref ) ),
'uuid' => $uuid,
]);
}
}
3 changes: 1 addition & 2 deletions inc/Integrations/Google/Sheets/GoogleSheetsDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,12 @@ public static function create( array $credentials, string $spreadsheet_id, strin
'name' => '',
'id' => 0,
],
'slug' => sanitize_title( $display_name ),
]);
}

public function to_ui_display(): array {
return [
'slug' => $this->get_slug(),
'display_name' => $this->get_display_name(),
'service' => REMOTE_DATA_BLOCKS_GOOGLE_SHEETS_SERVICE,
'spreadsheet' => [ 'name' => $this->config['spreadsheet_id'] ],
'sheet' => [ 'name' => '' ],
Expand Down
11 changes: 7 additions & 4 deletions inc/Integrations/SalesforceB2C/SalesforceB2CDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ class SalesforceB2CDataSource extends HttpDataSource {
'organization_id' => [ 'type' => 'string' ],
'client_id' => [ 'type' => 'string' ],
'client_secret' => [ 'type' => 'string' ],
'display_name' => [
'type' => 'string',
'required' => true,
],
],
];

public function get_display_name(): string {
return 'Salesforce B2C (' . $this->config['slug'] . ')';
return 'Salesforce B2C (' . $this->config['uuid'] . ')';
}

public function get_endpoint(): string {
Expand All @@ -48,20 +52,19 @@ public function get_image_url(): string {
return plugins_url( './assets/salesforce_commerce_cloud_logo.png', __FILE__ );
}

public static function create( string $shortcode, string $organization_id, string $client_id, string $client_secret ): self {
public static function create( string $shortcode, string $organization_id, string $client_id, string $client_secret, ?string $display_name = null ): self {
return parent::from_array([
'service' => REMOTE_DATA_BLOCKS_SALESFORCE_B2C_SERVICE,
'shortcode' => $shortcode,
'organization_id' => $organization_id,
'client_id' => $client_id,
'client_secret' => $client_secret,
'slug' => sanitize_title( sprintf( '%s/%s', $shortcode, $organization_id ) ),
'display_name' => $display_name,
]);
}

public function to_ui_display(): array {
return [
'slug' => $this->get_slug(),
'service' => REMOTE_DATA_BLOCKS_SALESFORCE_B2C_SERVICE,
'store_name' => $this->config['store_name'],
'uuid' => $this->config['uuid'] ?? null,
Expand Down
12 changes: 8 additions & 4 deletions inc/Integrations/Shopify/ShopifyDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ class ShopifyDataSource extends HttpDataSource {
],
'access_token' => [ 'type' => 'string' ],
'store_name' => [ 'type' => 'string' ],
'display_name' => [
'type' => 'string',
'required' => false,
],
],
];

public function get_display_name(): string {
return 'Shopify (' . $this->config['slug'] . ')';
return 'Shopify (' . $this->config['display_name'] . ')';
}

public function get_endpoint(): string {
Expand All @@ -47,18 +51,18 @@ public function get_image_url(): string {
return plugins_url( './assets/shopify_logo_black.png', __FILE__ );
}

public static function create( string $access_token, string $store_name ): self {
public static function create( string $access_token, string $store_name, ?string $display_name = null ): self {
return parent::from_array([
'display_name' => $display_name ?? 'Shopify (' . $store_name . ')',
'service' => REMOTE_DATA_BLOCKS_SHOPIFY_SERVICE,
'access_token' => $access_token,
'store_name' => $store_name,
'slug' => $store_name,
]);
}

public function to_ui_display(): array {
return [
'slug' => $this->get_slug(),
'display_name' => $this->get_display_name(),
'service' => REMOTE_DATA_BLOCKS_SHOPIFY_SERVICE,
'store_name' => $this->config['store_name'],
'uuid' => $this->config['uuid'] ?? null,
Expand Down
Loading

0 comments on commit 4c1ecac

Please sign in to comment.