Skip to content

Commit

Permalink
add shopify block registration snippet generation api
Browse files Browse the repository at this point in the history
  • Loading branch information
shekharnwagh committed Jan 22, 2025
1 parent ca13313 commit 8056c5c
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
23 changes: 23 additions & 0 deletions inc/REST/DataSourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use RemoteDataBlocks\Analytics\TracksAnalytics;
use RemoteDataBlocks\Editor\BlockManagement\ConfigStore;
use RemoteDataBlocks\WpdbStorage\DataSourceCrud;
use RemoteDataBlocks\Snippets\Snippets;
use WP_REST_Controller;
use WP_REST_Request;
use WP_REST_Response;
Expand Down Expand Up @@ -47,6 +48,23 @@ public function register_routes(): void {
]
);

// get_snippet
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/snippets/(?P<uuid>[\w-]+)',
[
'methods' => 'GET',
'callback' => [ $this, 'get_snippets' ],
'permission_callback' => [ $this, 'get_item_permissions_check' ],
'args' => [
'uuid' => [
'type' => 'string',
'required' => true,
],
],
]
);

// create_item
register_rest_route(
$this->namespace,
Expand Down Expand Up @@ -195,6 +213,11 @@ public function get_item( mixed $request ): WP_REST_Response|WP_Error {
return rest_ensure_response( $response );
}

public function get_snippets( mixed $request ): WP_REST_Response|WP_Error {
$code_snippets = Snippets::get_snippets( $request->get_param( 'uuid' ) );
return rest_ensure_response( [ 'snippets' => $code_snippets ] );
}

/**
* Updates a single item.
*
Expand Down
43 changes: 43 additions & 0 deletions inc/Snippets/Snippets.php
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 ) ];
}
}
46 changes: 46 additions & 0 deletions inc/Snippets/Templates/Shopify.template
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' );

0 comments on commit 8056c5c

Please sign in to comment.