6
6
7
7
namespace Vendic \OptimizeCacheSize \Plugin ;
8
8
9
+ use Magento \Framework \App \ScopeInterface ;
10
+ use Magento \Framework \View \DesignInterface ;
11
+ use Magento \Store \Model \StoreManagerInterface ;
12
+ use Magento \Framework \Exception \LocalizedException ;
9
13
use Magento \Framework \View \Layout \ProcessorInterface ;
10
14
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 ;
11
18
12
19
class RemoveHandlersPlugin
13
20
{
14
21
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_ ' ;
18
25
19
26
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
21
31
) {
22
32
}
23
33
34
+ /**
35
+ * @throws NoSuchEntityException
36
+ * @throws LocalizedException
37
+ */
24
38
public function afterAddHandle (
25
39
ProcessorInterface $ subject ,
26
40
ProcessorInterface $ result ,
@@ -29,20 +43,37 @@ public function afterAddHandle(
29
43
if (!$ this ->config ->isModuleEnabled ()) {
30
44
return $ result ;
31
45
}
46
+
47
+ $ store = (string )$ this ->storeManager ->getStore ()->getId ();
48
+ $ theme = (string )$ this ->design ->getDesignTheme ()->getId ();
32
49
$ handlers = $ result ->getHandles ();
50
+
51
+ $ dbLayoutHandlers = $ this ->layoutUpdateFetcher ->fetchDbLayoutHandlers ($ handlers , $ theme , $ store );
52
+
33
53
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
+ ) {
36
59
$ result ->removeHandle ($ handler );
37
60
continue ;
38
61
}
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
+ ) {
41
68
$ result ->removeHandle ($ handler );
42
69
continue ;
43
70
}
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
+ ) {
46
77
$ result ->removeHandle ($ handler );
47
78
}
48
79
}
0 commit comments