-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add shopify block registration snippet generation api
- Loading branch information
1 parent
ca13313
commit 8056c5c
Showing
3 changed files
with
112 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace RemoteDataBlocks\Snippets; | ||
|
||
use WP_Error; | ||
use RemoteDataBlocks\WpdbStorage\DataSourceCrud; | ||
|
||
class Snippets { | ||
public static function get_snippets( string $uuid ): array|WP_Error { | ||
$data_source_config = DataSourceCrud::get_config_by_uuid( $uuid ); | ||
|
||
if ( is_wp_error( $data_source_config ) ) { | ||
return $data_source_config; | ||
} | ||
|
||
$service = $data_source_config['service']; | ||
|
||
|
||
switch ( $service ) { | ||
case 'shopify': | ||
return self::get_shopify_snippets( $data_source_config ); | ||
default: | ||
return new WP_Error( 'invalid_service', __( 'Invalid service', 'remote-data-blocks' ) ); | ||
} | ||
} | ||
|
||
private static function strip_template_comments( string $content ): string { | ||
// Match PHPDoc blocks that contain @template tags and any preceding blank lines | ||
return preg_replace( | ||
'/\n*\/\*\*\s*\n\s*\*\s*@template-.*?\*\/\n*/s', | ||
"\n\n", | ||
$content | ||
); | ||
} | ||
|
||
private static function get_shopify_snippets( array $data_source_config ): array { | ||
$raw_snippet = file_get_contents( __DIR__ . '/Templates/Shopify.template' ); | ||
$snippet = strtr( $raw_snippet, [ | ||
'{{DATA_SOURCE_UUID}}' => $data_source_config['uuid'], | ||
] ); | ||
return [ self::strip_template_comments( $snippet ) ]; | ||
} | ||
} |
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 declare(strict_types = 1); | ||
|
||
/** | ||
* @template-name Shopify Block Registration | ||
* @template-version 1.0.0 | ||
* @template-description Registers a Shopify block for a given data source. | ||
* | ||
* Template variables: | ||
* - DATA_SOURCE_UUID: The UUID of the data source to register the block for. | ||
*/ | ||
|
||
/** | ||
* Update to the actual namespace which would used for the block registration. | ||
*/ | ||
namespace RemoteDataBlocks\Snippets\Shopify; | ||
|
||
use RemoteDataBlocks\Integrations\Shopify\ShopifyDataSource; | ||
use RemoteDataBlocks\Integrations\Shopify\ShopifyIntegration; | ||
use RemoteDataBlocks\WpdbStorage\DataSourceCrud; | ||
|
||
function register_shopify_block(): void { | ||
$data_source_config = DataSourceCrud::get_config_by_uuid( '{{DATA_SOURCE_UUID}}' ); | ||
|
||
if ( is_wp_error( $data_source_config ) ) { | ||
return; | ||
} | ||
|
||
$service_config = $data_source_config['service_config']; | ||
|
||
$access_token = $service_config['access_token']; | ||
$store_slug = $service_config['store_name']; | ||
$display_name = $service_config['display_name']; | ||
|
||
$shopify_data_source = ShopifyDataSource::from_array( [ | ||
'service_config' => [ | ||
'__version' => 1, | ||
'access_token' => $access_token, | ||
'display_name' => $display_name, | ||
'store_name' => $store_slug, | ||
], | ||
] ); | ||
|
||
ShopifyIntegration::register_blocks_for_shopify_data_source( $shopify_data_source ); | ||
} | ||
|
||
add_action( 'init', __NAMESPACE__ . '\\register_shopify_block' ); |