Skip to content

Commit 4c1ecac

Browse files
Change data source slugs to an overridable auto-UUID (#229)
--------- Signed-off-by: brookewp <[email protected]> Co-authored-by: Max Schmeling <[email protected]>
1 parent 78e3755 commit 4c1ecac

37 files changed

+415
-456
lines changed

docs/workflows/zip-code-with-contract.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This page will walk you through building [Zippopotam.us](https://zippopotam.us/)
44

55
## The contract
66

7-
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.
7+
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.
88

99
## Define a query
1010

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

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

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

8989
```
9090

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

9393
We're done!
9494

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

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

108-
The slug _must_ match the slug defined by the developer in the previous section when creating the data source.
110+
The UUID _must_ match the UUID defined by the developer in the previous section when creating the data source.

example/github/remote-data-blocks/register.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function register_github_file_as_html_block() {
1515

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

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

example/rest-api/art-institute/register.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

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

example/rest-api/zip-code/register.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
require_once __DIR__ . '/inc/queries/class-get-zip-code-query.php';
99

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

1313
if ( ! $zipcode_data_source instanceof GenericHttpDataSource ) {
1414
LoggerManager::instance()->debug( 'Zip Code data source not found' );

inc/Config/DataSource/DataSourceInterface.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ interface DataSourceInterface {
2222
],
2323
'service' => [ 'type' => 'string' ],
2424
'service_schema_version' => [ 'type' => 'integer' ],
25-
'slug' => [
26-
'type' => 'string',
27-
'pattern' => '/^[a-z0-9-]+$/',
28-
],
2925
'uuid' => [
3026
'type' => 'string',
3127
'callback' => 'wp_is_uuid',

inc/Config/DataSource/HttpDataSource.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public function get_service(): ?string {
4343
return $this->config['service'] ?? null;
4444
}
4545

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

5050
/**
@@ -60,8 +60,8 @@ final public static function get_config_schema(): array {
6060
return $schema;
6161
}
6262

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

6666
if ( ! $config ) {
6767
return new WP_Error( 'data_source_not_found', __( 'Data source not found.', 'remote-data-blocks' ), [ 'status' => 404 ] );
@@ -105,7 +105,7 @@ public function to_ui_display(): array {
105105
// TODO: Implement remove from children and implement here in standardized way
106106
return [
107107
'display_name' => $this->get_display_name(),
108-
'slug' => $this->get_slug(),
108+
'uuid' => $this->get_uuid(),
109109
'service' => static::SERVICE_NAME,
110110
];
111111
}

inc/Editor/BlockManagement/ConfigStore.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ public static function get_data_sources_displayable(): array {
117117
$data_source = $query->get_data_source();
118118

119119
if ( $data_source instanceof UiDisplayableInterface ) {
120-
$data_sources[ $data_source->to_array()['slug'] ] = $data_source->to_ui_display();
120+
$data_source_array = $data_source->to_array();
121+
if ( isset( $data_source_array['uuid'] ) ) {
122+
$data_sources[ $data_source_array['uuid'] ] = $data_source->to_ui_display();
123+
}
121124
}
122125
}
123126
}

inc/ExampleApi/ExampleApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static function register_remote_data_block(): void {
3131
}
3232

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

inc/Integrations/Airtable/AirtableDataSource.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ class AirtableDataSource extends HttpDataSource {
6969
],
7070
'display_name' => [
7171
'type' => 'string',
72-
'required' => false,
72+
'required' => true,
7373
],
7474
],
7575
];
7676

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

8181
public function get_endpoint(): string {
@@ -96,20 +96,19 @@ public static function create( string $access_token, string $base_id, ?array $ta
9696
'base' => [ 'id' => $base_id ],
9797
'tables' => $tables,
9898
'display_name' => $display_name,
99-
'slug' => $display_name ? sanitize_title( $display_name ) : sanitize_title( 'Airtable ' . $base_id ),
10099
]);
101100
}
102101

103102
public function to_ui_display(): array {
104103
return [
105-
'slug' => $this->get_slug(),
106104
'service' => REMOTE_DATA_BLOCKS_AIRTABLE_SERVICE,
107105
'base' => [
108106
'id' => $this->config['base']['id'],
109107
'name' => $this->config['base']['name'] ?? null,
110108
],
111109
'tables' => $this->config['tables'] ?? [],
112110
'uuid' => $this->config['uuid'] ?? null,
111+
'display_name' => $this->config['display_name'] ?? null,
113112
];
114113
}
115114

inc/Integrations/GenericHttp/GenericHttpDataSource.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ class GenericHttpDataSource extends HttpDataSource {
4747
'callback' => '\RemoteDataBlocks\Validation\is_url',
4848
'sanitize' => 'sanitize_url',
4949
],
50+
'display_name' => [
51+
'type' => 'string',
52+
'required' => false,
53+
],
5054
],
5155
];
5256

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

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

6771
public static function create( string $url, string $auth, string $display_name ): self {
6872
return parent::from_array([
73+
'display_name' => $display_name,
6974
'service' => REMOTE_DATA_BLOCKS_GENERIC_HTTP_SERVICE,
7075
'url' => $url,
7176
'auth' => $auth,
72-
'slug' => sanitize_title( $display_name ),
7377
]);
7478
}
7579

7680
public function to_ui_display(): array {
7781
return [
78-
'slug' => $this->get_slug(),
82+
'display_name' => $this->get_display_name(),
7983
'service' => REMOTE_DATA_BLOCKS_GENERIC_HTTP_SERVICE,
8084
'url' => $this->config['url'],
8185
'auth_type' => $this->config['auth']['type'],

inc/Integrations/GitHub/GitHubDataSource.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ public function get_ref(): string {
6060
return $this->config['ref'];
6161
}
6262

63-
public static function create( string $repo_owner, string $repo_name, string $ref ): self {
63+
public static function create( string $repo_owner, string $repo_name, string $ref, string $uuid ): self {
6464
return parent::from_array([
65+
'display_name' => sprintf( 'GitHub: %s/%s (%s)', $repo_owner, $repo_name, $ref ),
6566
'service' => REMOTE_DATA_BLOCKS_GITHUB_SERVICE,
6667
'repo_owner' => $repo_owner,
6768
'repo_name' => $repo_name,
6869
'ref' => $ref,
69-
'slug' => sanitize_title( sprintf( '%s/%s/%s', $repo_owner, $repo_name, $ref ) ),
70+
'uuid' => $uuid,
7071
]);
7172
}
7273
}

inc/Integrations/Google/Sheets/GoogleSheetsDataSource.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,12 @@ public static function create( array $credentials, string $spreadsheet_id, strin
105105
'name' => '',
106106
'id' => 0,
107107
],
108-
'slug' => sanitize_title( $display_name ),
109108
]);
110109
}
111110

112111
public function to_ui_display(): array {
113112
return [
114-
'slug' => $this->get_slug(),
113+
'display_name' => $this->get_display_name(),
115114
'service' => REMOTE_DATA_BLOCKS_GOOGLE_SHEETS_SERVICE,
116115
'spreadsheet' => [ 'name' => $this->config['spreadsheet_id'] ],
117116
'sheet' => [ 'name' => '' ],

inc/Integrations/SalesforceB2C/SalesforceB2CDataSource.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ class SalesforceB2CDataSource extends HttpDataSource {
2727
'organization_id' => [ 'type' => 'string' ],
2828
'client_id' => [ 'type' => 'string' ],
2929
'client_secret' => [ 'type' => 'string' ],
30+
'display_name' => [
31+
'type' => 'string',
32+
'required' => true,
33+
],
3034
],
3135
];
3236

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

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

51-
public static function create( string $shortcode, string $organization_id, string $client_id, string $client_secret ): self {
55+
public static function create( string $shortcode, string $organization_id, string $client_id, string $client_secret, ?string $display_name = null ): self {
5256
return parent::from_array([
5357
'service' => REMOTE_DATA_BLOCKS_SALESFORCE_B2C_SERVICE,
5458
'shortcode' => $shortcode,
5559
'organization_id' => $organization_id,
5660
'client_id' => $client_id,
5761
'client_secret' => $client_secret,
58-
'slug' => sanitize_title( sprintf( '%s/%s', $shortcode, $organization_id ) ),
62+
'display_name' => $display_name,
5963
]);
6064
}
6165

6266
public function to_ui_display(): array {
6367
return [
64-
'slug' => $this->get_slug(),
6568
'service' => REMOTE_DATA_BLOCKS_SALESFORCE_B2C_SERVICE,
6669
'store_name' => $this->config['store_name'],
6770
'uuid' => $this->config['uuid'] ?? null,

inc/Integrations/Shopify/ShopifyDataSource.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ class ShopifyDataSource extends HttpDataSource {
2525
],
2626
'access_token' => [ 'type' => 'string' ],
2727
'store_name' => [ 'type' => 'string' ],
28+
'display_name' => [
29+
'type' => 'string',
30+
'required' => false,
31+
],
2832
],
2933
];
3034

3135
public function get_display_name(): string {
32-
return 'Shopify (' . $this->config['slug'] . ')';
36+
return 'Shopify (' . $this->config['display_name'] . ')';
3337
}
3438

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

50-
public static function create( string $access_token, string $store_name ): self {
54+
public static function create( string $access_token, string $store_name, ?string $display_name = null ): self {
5155
return parent::from_array([
56+
'display_name' => $display_name ?? 'Shopify (' . $store_name . ')',
5257
'service' => REMOTE_DATA_BLOCKS_SHOPIFY_SERVICE,
5358
'access_token' => $access_token,
5459
'store_name' => $store_name,
55-
'slug' => $store_name,
5660
]);
5761
}
5862

5963
public function to_ui_display(): array {
6064
return [
61-
'slug' => $this->get_slug(),
65+
'display_name' => $this->get_display_name(),
6266
'service' => REMOTE_DATA_BLOCKS_SHOPIFY_SERVICE,
6367
'store_name' => $this->config['store_name'],
6468
'uuid' => $this->config['uuid'] ?? null,

0 commit comments

Comments
 (0)