Skip to content

Commit 690825a

Browse files
Exclude db layout updates from remove
1 parent ec1845c commit 690825a

File tree

2 files changed

+87
-10
lines changed

2 files changed

+87
-10
lines changed

Model/LayoutUpdateFetcher.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 isDbLayoutHandler(string $handler, string $themeId, string $storeId): bool
22+
{
23+
$connection = $this->resourceConnection->getConnection();
24+
25+
$bind = [
26+
'theme_id' => $themeId,
27+
'store_id' => $storeId,
28+
'handle' => $handler,
29+
];
30+
31+
$select = $connection->select()->from(
32+
['layout_update' => $connection->getTableName('layout_update')],
33+
['handle']
34+
)->join(
35+
['link' => $connection->getTableName('layout_link')],
36+
'link.layout_update_id=layout_update.layout_update_id',
37+
''
38+
)->where(
39+
'link.store_id IN (0, :store_id)'
40+
)->where(
41+
'link.theme_id = :theme_id'
42+
)->where(
43+
'handle = :handle'
44+
)->order(
45+
'layout_update.sort_order ' . Select::SQL_ASC
46+
);
47+
48+
return (bool)$connection->fetchOne($select, $bind);
49+
}
50+
}

Plugin/RemoveHandlersPlugin.php

Lines changed: 37 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,33 @@ 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();
3350
foreach ($handlers as $handler) {
34-
if ($this->config->isRemoveCategoryIdHandlers()
35-
&& str_contains($handler, self::CATEGORY_ID_HANDLER_STRING)) {
51+
if (
52+
$this->config->isRemoveCategoryIdHandlers()
53+
&& str_contains($handler, self::CATEGORY_ID_HANDLER_STRING)
54+
&& !$this->layoutUpdateFetcher->isDbLayoutHandler($handler, $theme, $store)
55+
) {
3656
$result->removeHandle($handler);
3757
continue;
3858
}
39-
if ($this->config->isRemoveProductIdHandlers()
40-
&& str_contains($handler, self::PRODUCT_ID_HANDLER_STRING)) {
59+
60+
if (
61+
$this->config->isRemoveProductIdHandlers()
62+
&& str_contains($handler, self::PRODUCT_ID_HANDLER_STRING)
63+
&& !$this->layoutUpdateFetcher->isDbLayoutHandler($handler, $theme, $store)
64+
) {
4165
$result->removeHandle($handler);
4266
continue;
4367
}
44-
if ($this->config->isRemoveProductSkuHandlers()
45-
&& str_contains($handler, self::PRODUCT_SKU_HANDLER_STRING)) {
68+
69+
if (
70+
$this->config->isRemoveProductSkuHandlers()
71+
&& str_contains($handler, self::PRODUCT_SKU_HANDLER_STRING)
72+
) {
4673
$result->removeHandle($handler);
4774
}
4875
}

0 commit comments

Comments
 (0)