Skip to content

Commit 1d5a136

Browse files
Merge pull request #11 from Vendic/bugfix/MAXFEA-160-check-existing-layout-updates
Exclude db layout updates from remove
2 parents ec1845c + ad0c4c8 commit 1d5a136

File tree

2 files changed

+88
-10
lines changed

2 files changed

+88
-10
lines changed

Model/LayoutUpdateFetcher.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (c) Vendic B.V https://vendic.nl/
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Vendic\OptimizeCacheSize\Model;
10+
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\DB\Select;
13+
14+
readonly class LayoutUpdateFetcher
15+
{
16+
public function __construct(
17+
private ResourceConnection $resourceConnection
18+
) {
19+
}
20+
21+
public function fetchDbLayoutHandlers(array $handler, string $themeId, string $storeId): array
22+
{
23+
$connection = $this->resourceConnection->getConnection();
24+
25+
$select = $connection->select()->from(
26+
['layout_update' => $connection->getTableName('layout_update')],
27+
['handle']
28+
)->join(
29+
['link' => $connection->getTableName('layout_link')],
30+
'link.layout_update_id=layout_update.layout_update_id',
31+
''
32+
)->where(
33+
'link.store_id IN (0, ?)',
34+
$storeId
35+
)->where(
36+
'link.theme_id = ?',
37+
$themeId
38+
)->where(
39+
'handle IN (?)',
40+
$handler
41+
)->order(
42+
'layout_update.sort_order ' . Select::SQL_ASC
43+
);
44+
45+
return $connection->fetchCol($select);
46+
}
47+
}

Plugin/RemoveHandlersPlugin.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,35 @@
66

77
namespace Vendic\OptimizeCacheSize\Plugin;
88

9+
use Magento\Framework\App\ScopeInterface;
10+
use Magento\Framework\View\DesignInterface;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
use Magento\Framework\Exception\LocalizedException;
913
use Magento\Framework\View\Layout\ProcessorInterface;
1014
use Vendic\OptimizeCacheSize\Model\Config;
15+
use Magento\Widget\Model\ResourceModel\Layout\Update;
16+
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Vendic\OptimizeCacheSize\Model\LayoutUpdateFetcher;
1118

1219
class RemoveHandlersPlugin
1320
{
1421

15-
private const PRODUCT_ID_HANDLER_STRING = 'catalog_product_view_id_';
16-
private const PRODUCT_SKU_HANDLER_STRING = 'catalog_product_view_sku_';
17-
private const CATEGORY_ID_HANDLER_STRING = 'catalog_category_view_id_';
22+
private const string PRODUCT_ID_HANDLER_STRING = 'catalog_product_view_id_';
23+
private const string PRODUCT_SKU_HANDLER_STRING = 'catalog_product_view_sku_';
24+
private const string CATEGORY_ID_HANDLER_STRING = 'catalog_category_view_id_';
1825

1926
public function __construct(
20-
private Config $config
27+
private readonly Config $config,
28+
private readonly LayoutUpdateFetcher $layoutUpdateFetcher,
29+
private readonly StoreManagerInterface $storeManager,
30+
private readonly DesignInterface $design
2131
) {
2232
}
2333

34+
/**
35+
* @throws NoSuchEntityException
36+
* @throws LocalizedException
37+
*/
2438
public function afterAddHandle(
2539
ProcessorInterface $subject,
2640
ProcessorInterface $result,
@@ -29,20 +43,37 @@ public function afterAddHandle(
2943
if (!$this->config->isModuleEnabled()) {
3044
return $result;
3145
}
46+
47+
$store = (string)$this->storeManager->getStore()->getId();
48+
$theme = (string)$this->design->getDesignTheme()->getId();
3249
$handlers = $result->getHandles();
50+
51+
$dbLayoutHandlers = $this->layoutUpdateFetcher->fetchDbLayoutHandlers($handlers, $theme, $store);
52+
3353
foreach ($handlers as $handler) {
34-
if ($this->config->isRemoveCategoryIdHandlers()
35-
&& str_contains($handler, self::CATEGORY_ID_HANDLER_STRING)) {
54+
if (
55+
$this->config->isRemoveCategoryIdHandlers()
56+
&& str_contains($handler, self::CATEGORY_ID_HANDLER_STRING)
57+
&& !in_array($handler, $dbLayoutHandlers)
58+
) {
3659
$result->removeHandle($handler);
3760
continue;
3861
}
39-
if ($this->config->isRemoveProductIdHandlers()
40-
&& str_contains($handler, self::PRODUCT_ID_HANDLER_STRING)) {
62+
63+
if (
64+
$this->config->isRemoveProductIdHandlers()
65+
&& str_contains($handler, self::PRODUCT_ID_HANDLER_STRING)
66+
&& !in_array($handler, $dbLayoutHandlers)
67+
) {
4168
$result->removeHandle($handler);
4269
continue;
4370
}
44-
if ($this->config->isRemoveProductSkuHandlers()
45-
&& str_contains($handler, self::PRODUCT_SKU_HANDLER_STRING)) {
71+
72+
if (
73+
$this->config->isRemoveProductSkuHandlers()
74+
&& str_contains($handler, self::PRODUCT_SKU_HANDLER_STRING)
75+
&& !in_array($handler, $dbLayoutHandlers)
76+
) {
4677
$result->removeHandle($handler);
4778
}
4879
}

0 commit comments

Comments
 (0)