Skip to content

Commit

Permalink
Spaces to tabs
Browse files Browse the repository at this point in the history
Signed-off-by: brookewp <[email protected]>
  • Loading branch information
brookewp committed Dec 2, 2024
1 parent 7b1a76e commit 05ea99a
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 124 deletions.
8 changes: 4 additions & 4 deletions inc/Editor/BlockManagement/ConfigStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ public static function get_data_sources_displayable(): array {
$data_source = $query->get_data_source();

if ( $data_source instanceof UiDisplayableInterface ) {
$data_source_array = $data_source->to_array();
if (isset($data_source_array['uuid'])) {
$data_sources[$data_source_array['uuid']] = $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/Integrations/Airtable/AirtableDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function to_ui_display(): array {
],
'tables' => $this->config['tables'] ?? [],
'uuid' => $this->config['uuid'] ?? null,
'display_name' => $this->config['display_name'] ?? null,
'display_name' => $this->config['display_name'] ?? null,
];
}

Expand Down
4 changes: 2 additions & 2 deletions inc/Integrations/GenericHttp/GenericHttpDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ 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,
'display_name' => $display_name,
'service' => REMOTE_DATA_BLOCKS_GENERIC_HTTP_SERVICE,
'url' => $url,
'auth' => $auth,
Expand All @@ -75,7 +75,7 @@ public static function create( string $url, string $auth, string $display_name )

public function to_ui_display(): array {
return [
'display_name' => $this->get_display_name(),
'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
4 changes: 2 additions & 2 deletions inc/Integrations/GitHub/GitHubDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public function get_ref(): string {

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 ),
'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,
'uuid' => $uuid ?? wp_generate_uuid4(),
'uuid' => $uuid ?? wp_generate_uuid4(),
]);
}
}
2 changes: 1 addition & 1 deletion inc/Integrations/Google/Sheets/GoogleSheetsDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static function create( array $credentials, string $spreadsheet_id, strin

public function to_ui_display(): array {
return [
'display_name' => $this->get_display_name(),
'display_name' => $this->get_display_name(),
'service' => REMOTE_DATA_BLOCKS_GOOGLE_SHEETS_SERVICE,
'spreadsheet' => [ 'name' => $this->config['spreadsheet_id'] ],
'sheet' => [ 'name' => '' ],
Expand Down
6 changes: 3 additions & 3 deletions inc/Integrations/Shopify/ShopifyDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ShopifyDataSource extends HttpDataSource {
],
'access_token' => [ 'type' => 'string' ],
'store_name' => [ 'type' => 'string' ],
'display_name' => [
'display_name' => [
'type' => 'string',
'required' => false,
],
Expand Down Expand Up @@ -53,7 +53,7 @@ public function get_image_url(): string {

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 . ')',
'display_name' => $display_name ?? 'Shopify (' . $store_name . ')',
'service' => REMOTE_DATA_BLOCKS_SHOPIFY_SERVICE,
'access_token' => $access_token,
'store_name' => $store_name,
Expand All @@ -62,7 +62,7 @@ public static function create( string $access_token, string $store_name, ?string

public function to_ui_display(): array {
return [
'display_name' => $this->get_display_name(),
'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
144 changes: 72 additions & 72 deletions inc/REST/DataSourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,20 @@ public function get_items( $request ) {
* single source of truth for data source configurations
*/
$data_sources = array_values(array_reduce(
array_merge( $code_configured_data_sources, $ui_configured_data_sources ),
function ( $acc, $item ) {
// Check if item with the same UUID already exists
if ( isset( $acc[ $item['uuid'] ] ) ) {
// Merge the properties of the existing item with the new one
$acc[ $item['uuid'] ] = array_merge( $acc[ $item['uuid'] ], $item );
} else {
// Otherwise, add the new item
$acc[ $item['uuid'] ] = $item;
}
return $acc;
},
[]
));
array_merge( $code_configured_data_sources, $ui_configured_data_sources ),
function ( $acc, $item ) {
// Check if item with the same UUID already exists
if ( isset( $acc[ $item['uuid'] ] ) ) {
// Merge the properties of the existing item with the new one
$acc[ $item['uuid'] ] = array_merge( $acc[ $item['uuid'] ], $item );
} else {
// Otherwise, add the new item
$acc[ $item['uuid'] ] = $item;
}
return $acc;
},
[]
));

// Tracks Analytics. Only once per day to reduce noise.
$track_transient_key = 'remotedatablocks_view_data_sources_tracked';
Expand All @@ -166,66 +166,66 @@ function ( $acc, $item ) {
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function update_item( $request ) {
$current_uuid = $request->get_param( 'uuid' );
$data_source_properties = $request->get_json_params();

// Extract new UUID if provided
$new_uuid = $data_source_properties['newUUID'] ?? null;

// Retrieve the current data sources and the item by its current UUID
$data_sources = DataSourceCrud::get_data_sources();
$item = DataSourceCrud::get_item_by_uuid( $data_sources, $current_uuid );

// Handle item not found
if ( ! $item ) {
return new WP_Error(
'data_source_not_found',
__( 'Data source not found.', 'remote-data-blocks' ),
[ 'status' => 404 ]
);
}

// Handle new UUID conflicts
if ( $new_uuid && $new_uuid !== $current_uuid ) {
// Ensure no conflict with existing UUID
if ( DataSourceCrud::get_item_by_uuid( $data_sources, $new_uuid ) ) {
return new WP_Error(
'uuid_conflict',
__( 'The new UUID already exists.', 'remote-data-blocks' ),
[ 'status' => 409 ]
);
}

// Set the new UUID in the properties
$data_source_properties['uuid'] = $new_uuid;
}

// Merge the updated properties with the existing item
$updated_item = array_merge( $item, $data_source_properties );

// Pass the original UUID when updating the item to avoid duplication
$result = DataSourceCrud::update_item_by_uuid( $current_uuid, $updated_item, $current_uuid );

if ( is_wp_error( $result ) ) {
return $result; // Return WP_Error if update fails
}

// Log the update action
TracksAnalytics::record_event(
'remotedatablocks_data_source_interaction',
array_merge(
[
'data_source_type' => $data_source_properties['service'],
'action' => 'update',
],
$this->get_data_source_interaction_track_props( $data_source_properties )
)
);

return rest_ensure_response( $result );
$current_uuid = $request->get_param( 'uuid' );
$data_source_properties = $request->get_json_params();

// Extract new UUID if provided
$new_uuid = $data_source_properties['newUUID'] ?? null;

// Retrieve the current data sources and the item by its current UUID
$data_sources = DataSourceCrud::get_data_sources();
$item = DataSourceCrud::get_item_by_uuid( $data_sources, $current_uuid );

// Handle item not found
if ( ! $item ) {
return new WP_Error(
'data_source_not_found',
__( 'Data source not found.', 'remote-data-blocks' ),
[ 'status' => 404 ]
);
}

// Handle new UUID conflicts
if ( $new_uuid && $new_uuid !== $current_uuid ) {
// Ensure no conflict with existing UUID
if ( DataSourceCrud::get_item_by_uuid( $data_sources, $new_uuid ) ) {
return new WP_Error(
'uuid_conflict',
__( 'The new UUID already exists.', 'remote-data-blocks' ),
[ 'status' => 409 ]
);
}

// Set the new UUID in the properties
$data_source_properties['uuid'] = $new_uuid;
}

// Merge the updated properties with the existing item
$updated_item = array_merge( $item, $data_source_properties );

// Pass the original UUID when updating the item to avoid duplication
$result = DataSourceCrud::update_item_by_uuid( $current_uuid, $updated_item, $current_uuid );

if ( is_wp_error( $result ) ) {
return $result; // Return WP_Error if update fails
}

// Log the update action
TracksAnalytics::record_event(
'remotedatablocks_data_source_interaction',
array_merge(
[
'data_source_type' => $data_source_properties['service'],
'action' => 'update',
],
$this->get_data_source_interaction_track_props( $data_source_properties )
)
);

return rest_ensure_response( $result );
}



/**
Expand Down
76 changes: 38 additions & 38 deletions inc/WpdbStorage/DataSourceCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,43 +61,43 @@ public static function get_item_by_uuid( array $data_sources, string $uuid ): ar
}

public static function update_item_by_uuid( string $uuid, array $new_item ): HttpDataSourceInterface|WP_Error {
$data_sources = self::get_data_sources();
$item = self::get_item_by_uuid($data_sources, $uuid);
if (!$item) {
return new WP_Error('data_source_not_found', __('Data source not found.', 'remote-data-blocks'), ['status' => 404]);
}
// Check if new UUID is provided
$new_uuid = $new_item['uuid'] ?? null;
if ($new_uuid && $new_uuid !== $uuid) {
// Ensure the new UUID doesn't already exist
if (self::get_item_by_uuid($data_sources, $new_uuid)) {
return new WP_Error('uuid_conflict', __('The new UUID already exists.', 'remote-data-blocks'), ['status' => 409]);
}
// Remove the old item from data source array if UUID is being updated
unset($data_sources[$uuid]);
}
// Merge new item properties
$merged_item = array_merge($item, $new_item);
// Resolve and save the updated item
$resolved_data_source = self::resolve_data_source($merged_item);
if (is_wp_error($resolved_data_source)) {
return $resolved_data_source; // If resolving fails, return error
}
// Save the updated item
$result = self::save_data_source($resolved_data_source, $data_sources, $uuid); // Passing old UUID to remove it if changed
if (!$result) {
return new WP_Error('failed_to_update_data_source', __('Failed to update data source.', 'remote-data-blocks'));
}
return $resolved_data_source;
}
$data_sources = self::get_data_sources();
$item = self::get_item_by_uuid($data_sources, $uuid);
if (!$item) {
return new WP_Error('data_source_not_found', __('Data source not found.', 'remote-data-blocks'), ['status' => 404]);
}
// Check if new UUID is provided
$new_uuid = $new_item['uuid'] ?? null;
if ($new_uuid && $new_uuid !== $uuid) {
// Ensure the new UUID doesn't already exist
if (self::get_item_by_uuid($data_sources, $new_uuid)) {
return new WP_Error('uuid_conflict', __('The new UUID already exists.', 'remote-data-blocks'), ['status' => 409]);
}
// Remove the old item from data source array if UUID is being updated
unset($data_sources[$uuid]);
}
// Merge new item properties
$merged_item = array_merge($item, $new_item);
// Resolve and save the updated item
$resolved_data_source = self::resolve_data_source($merged_item);
if (is_wp_error($resolved_data_source)) {
return $resolved_data_source; // If resolving fails, return error
}
// Save the updated item
$result = self::save_data_source($resolved_data_source, $data_sources, $uuid); // Passing old UUID to remove it if changed
if (!$result) {
return new WP_Error('failed_to_update_data_source', __('Failed to update data source.', 'remote-data-blocks'));
}
return $resolved_data_source;
}

public static function delete_item_by_uuid( string $uuid ): WP_Error|bool {
$data_sources = self::get_data_sources();
Expand All @@ -109,7 +109,7 @@ public static function delete_item_by_uuid( string $uuid ): WP_Error|bool {
return true;
}

public static function get_by_uuid( string $uuid ): array|false {
public static function get_by_uuid( string $uuid ): array|false {
$data_sources = self::get_data_sources();
foreach ( $data_sources as $source ) {
if ( $source['uuid'] === $uuid ) {
Expand Down
2 changes: 1 addition & 1 deletion tests/inc/WpdbStorage/DataSourceCrudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function test_update_item_by_uuid_with_valid_uuid() {
'service' => REMOTE_DATA_BLOCKS_AIRTABLE_SERVICE,
'service_schema_version' => 1,
'uuid' => wp_generate_uuid4(),
'newUUID' => '2ced7a88-5a61-4e66-840e-26baefdfdfd5',
'newUUID' => '2ced7a88-5a61-4e66-840e-26baefdfdfd5',
'access_token' => 'token1',
'base' => [
'id' => 'base_id1',
Expand Down

0 comments on commit 05ea99a

Please sign in to comment.