66
77namespace 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 ;
913use Magento \Framework \View \Layout \ProcessorInterface ;
1014use 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
1219class 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