Skip to content
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
97db04b
add theme actions
enmaboya Nov 7, 2024
f5afea8
remove theme helper
enmaboya Nov 7, 2024
bc93798
update tests
enmaboya Nov 7, 2024
3bb2e3f
clean asset data json
enmaboya Nov 20, 2024
917cb23
set default asset content
enmaboya Nov 30, 2024
2aab761
upgrade PHP requirement: 8.0 → 8.1, code adjustments
enmaboya Jun 30, 2025
f35990d
normalize composer json
enmaboya Jun 30, 2025
fb92729
update phpstan
enmaboya Jun 30, 2025
1f0a5b6
add optional baseline
enmaboya Jun 30, 2025
1a1bbb5
Merge branch 'chore/bump-php-min-8.1' of github.com:enmaboya/laravel-…
enmaboya Jul 17, 2025
f145bc2
Merge branch 'master' into replace_rest_with_graphql_for_theme_level_…
enmaboya Jul 17, 2025
8c755c5
Merge branch 'master' into replace_rest_with_graphql_for_theme_level_…
enmaboya Dec 17, 2025
6ec7f20
Merge remote-tracking branch 'upstream/master' into replace_rest_with…
enmaboya Dec 17, 2025
112041f
Merge remote-tracking branch 'upstream/master'
enmaboya Apr 30, 2026
a05fdb0
Merge branch 'master' into replace_rest_with_graphql_for_theme_level_…
enmaboya Apr 30, 2026
b5f3e41
Merge remote-tracking branch 'upstream/master'
enmaboya Jul 2, 2026
8353fa0
Merge branch 'master' into replace_rest_with_graphql_for_theme_level_…
enmaboya Jul 2, 2026
a258f72
simplification of the ThemeSupportLevel configuration
enmaboya Jul 2, 2026
9ec56c5
add test for store with only product template
enmaboya Jul 6, 2026
ede1a66
add store theme level support tests
enmaboya Jul 6, 2026
a8f550d
update tests, add app extensions test with two assets
enmaboya Jul 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/Actions/FetchMainTheme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Osiset\ShopifyApp\Actions;

use Illuminate\Support\Facades\Log;
use Osiset\ShopifyApp\Contracts\ShopModel;

final class FetchMainTheme
{
/**
* @return array{id: ?string, name: ?string}
*/
public function handle(ShopModel $shop): array
Comment thread
enmaboya marked this conversation as resolved.
{
$response = $shop->api()->graph('{
themes(first: 1, roles: MAIN) {
nodes {
id
name
}
}
}');

if ($response['errors']) {
Log::error('Fetching main theme error: '.json_encode($response['errors']));

return [];
}

return data_get($response['body']->toArray(), 'data.themes.nodes.0', []);
}
}
61 changes: 61 additions & 0 deletions src/Actions/FetchThemeAssets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Osiset\ShopifyApp\Actions;

use Illuminate\Support\Facades\Log;
use Osiset\ShopifyApp\Contracts\ShopModel;

final class FetchThemeAssets
{
/**
* @param string[] $filenames
*/
public function handle(ShopModel $shop, string $mainThemeId, array $filenames): array
{
$response = $shop->api()->graph('query ($id: ID!, $filenames: [String!]) {
theme(id: $id) {
id
name
role
files(filenames: $filenames) {
nodes {
filename
body {
... on OnlineStoreThemeFileBodyText {
content
}
}
}
}
}
}', [
'id' => $mainThemeId,
'filenames' => array_values($filenames),
]);

if ($response['errors']) {
Log::error('Fetching settings data error: '.json_encode($response['errors']));

return [];
}

$nodes = data_get($response['body']->toArray(), 'data.theme.files.nodes', []);

return array_values(array_filter(array_map(function (array $data) {
$content = data_get($data, 'body.content');

if ($content === null) {
Log::warning('Theme file body is not text, skipping: '.data_get($data, 'filename', '?'));

return null;
}

return [
'filename' => $data['filename'],
'content' => $content,
];
}, $nodes)));
}
}
116 changes: 95 additions & 21 deletions src/Actions/VerifyThemeSupport.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,119 @@
<?php

declare(strict_types=1);

namespace Osiset\ShopifyApp\Actions;

use Osiset\ShopifyApp\Contracts\Queries\Shop as IShopQuery;
use Osiset\ShopifyApp\Contracts\ShopModel;
use Osiset\ShopifyApp\Objects\Enums\ThemeSupportLevel;
use Osiset\ShopifyApp\Objects\Values\ShopId;
use Osiset\ShopifyApp\Services\ThemeHelper;

class VerifyThemeSupport
final class VerifyThemeSupport
{
private const ASSET_FILE_NAMES = ['templates/product.json', 'templates/collection.json', 'templates/index.json'];
Comment thread
enmaboya marked this conversation as resolved.

private const MAIN_ROLE = 'main';

public function __construct(
protected IShopQuery $shopQuery,
protected ThemeHelper $themeHelper
private IShopQuery $shopQuery,
private FetchMainTheme $fetchMainTheme,
private FetchThemeAssets $fetchThemeAssets,
) {
}

public function __invoke(ShopId $shopId): int
{
$this->themeHelper->extractStoreMainTheme($shopId);

if ($this->themeHelper->themeIsReady()) {
$templateJSONFiles = $this->themeHelper->templateJSONFiles();
$templateMainSections = $this->themeHelper->mainSections($templateJSONFiles);
$sectionsWithAppBlock = $this->themeHelper->sectionsWithAppBlock($templateMainSections);
$shop = $this->shopQuery->getById($shopId);

$hasTemplates = count($templateJSONFiles) > 0;
$allTemplatesHasRightType = count($templateJSONFiles) === count($sectionsWithAppBlock);
$templatesСountWithRightType = count($sectionsWithAppBlock);
/** @var array{id: string, name: string} */
$mainTheme = $this->fetchMainTheme->handle($shop);

switch (true) {
case $hasTemplates && $allTemplatesHasRightType:
return ThemeSupportLevel::FULL;
if (isset($mainTheme['id'])) {
/** @var array<int, array{filename: string, content: string}> */
$assets = $this->fetchThemeAssets->handle(
shop: $shop,
mainThemeId: $mainTheme['id'],
filenames: self::ASSET_FILE_NAMES
);
$templateMainSections = $this->mainSections(
shop: $shop,
Comment thread
enmaboya marked this conversation as resolved.
mainTheme: $mainTheme,
assets: $assets
);
$sectionsWithAppBlock = $this->sectionsWithAppBlock($templateMainSections);

case $templatesСountWithRightType:
return ThemeSupportLevel::PARTIAL;
$hasTemplates = count($assets) > 0;
$allTemplatesHasRightType = count($assets) === count($sectionsWithAppBlock);
$hasTemplatesCountWithRightType = count($sectionsWithAppBlock) > 0;

default:
return ThemeSupportLevel::UNSUPPORTED;
}
return match (true) {
$hasTemplates && $allTemplatesHasRightType => ThemeSupportLevel::FULL,
$hasTemplatesCountWithRightType => ThemeSupportLevel::PARTIAL,
default => ThemeSupportLevel::UNSUPPORTED
};
}

return ThemeSupportLevel::UNSUPPORTED;
}

/**
* @param array{id: string} $mainTheme
* @param array{content: string} $assets
*
* @return array<int, array{filename: string, content: string}>
*/
private function mainSections(ShopModel $shop, array $mainTheme, array $assets): array
{
$filenamesForMainSections = array_filter(
array_map(function ($asset) {
$content = $asset['content'];

if (! $this->json_validate($content)) {
$content = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $content);
}

$assetContent = json_decode($content, true);


$mainAsset = array_filter($assetContent['sections'], function ($value, $key) {
return $key == self::MAIN_ROLE || str_starts_with($value['type'], self::MAIN_ROLE);
}, ARRAY_FILTER_USE_BOTH);

if ($mainAsset) {
return 'sections/'.end($mainAsset)['type'].'.liquid';
}
}, $assets)
);

return $this->fetchThemeAssets->handle(
shop: $shop,
mainThemeId: $mainTheme['id'],
filenames: $filenamesForMainSections
);
}

private function sectionsWithAppBlock(array $templateMainSections): array
{
return array_filter(array_map(function ($file) {
$acceptsAppBlock = false;

preg_match('/\{\%-?\s+schema\s+-?\%\}([\s\S]*?)\{\%-?\s+endschema\s+-?\%\}/m', $file['content'], $matches);
$schema = json_decode($matches[1] ?? '{}', true);

if ($schema && isset($schema['blocks'])) {
$acceptsAppBlock = in_array('@app', array_column($schema['blocks'], 'type'));
}

return $acceptsAppBlock ? $file : null;
}, $templateMainSections));
}


private function json_validate(string $string): bool
{
json_decode($string);

return json_last_error() === JSON_ERROR_NONE;
}
}
4 changes: 2 additions & 2 deletions src/Http/Middleware/VerifyScopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private function currentScopes(ShopModel $shop): array
}')
);

if (! $response['errors'] && blank(data_get($response['body']->toArray(), 'data.currentAppInstallation.userErrors'))) {
if (! $response['errors']) {
return [
'hasErrors' => false,
'result' => array_column(
Expand All @@ -78,7 +78,7 @@ private function currentScopes(ShopModel $shop): array
];
}

Log::error('Fetch current app installation access scopes error: '.json_encode(data_get($response['body']->toArray(), 'data.currentAppInstallation.userErrors')));
Log::error('Fetch current app installation access scopes error: '.json_encode($response['errors']));

return [
'hasErrors' => true,
Expand Down
96 changes: 0 additions & 96 deletions src/Objects/Values/MainTheme.php

This file was deleted.

Loading
Loading