11<?php
2- declare (strict_types= 1 );
2+
33/**
44 * @copyright Copyright (c) Vendic B.V https://vendic.nl/
55 */
66
7+ declare (strict_types=1 );
8+
79namespace Vendic \OptimizeCacheSize \Plugin ;
810
9- use Magento \Framework \App \ScopeInterface ;
1011use Magento \Framework \View \DesignInterface ;
12+ use Magento \Store \Model \Store ;
1113use Magento \Store \Model \StoreManagerInterface ;
1214use Magento \Framework \Exception \LocalizedException ;
1315use Magento \Framework \View \Layout \ProcessorInterface ;
16+ use Magento \Widget \Model \ResourceModel \Layout \Update \CollectionFactory ;
1417use Vendic \OptimizeCacheSize \Model \Config ;
15- use Magento \Widget \Model \ResourceModel \Layout \Update ;
1618use Magento \Framework \Exception \NoSuchEntityException ;
17- use Vendic \OptimizeCacheSize \Model \LayoutUpdateFetcher ;
1819
19- class RemoveHandlersPlugin
20+ class RemoveHandlers
2021{
21-
2222 private const string PRODUCT_ID_HANDLER_STRING = 'catalog_product_view_id_ ' ;
2323 private const string PRODUCT_SKU_HANDLER_STRING = 'catalog_product_view_sku_ ' ;
2424 private const string CATEGORY_ID_HANDLER_STRING = 'catalog_category_view_id_ ' ;
2525
26+ private array $ dbLayoutHandlers = [];
27+
2628 public function __construct (
2729 private readonly Config $ config ,
28- private readonly LayoutUpdateFetcher $ layoutUpdateFetcher ,
2930 private readonly StoreManagerInterface $ storeManager ,
30- private readonly DesignInterface $ design
31+ private readonly DesignInterface $ design ,
32+ private readonly CollectionFactory $ layoutUpdateCollectionFactory
3133 ) {
3234 }
3335
@@ -44,17 +46,11 @@ public function afterAddHandle(
4446 return $ result ;
4547 }
4648
47- $ store = (string )$ this ->storeManager ->getStore ()->getId ();
48- $ theme = (string )$ this ->design ->getDesignTheme ()->getId ();
49- $ handlers = $ result ->getHandles ();
50-
51- $ dbLayoutHandlers = $ this ->layoutUpdateFetcher ->fetchDbLayoutHandlers ($ handlers , $ theme , $ store );
52-
53- foreach ($ handlers as $ handler ) {
49+ foreach ($ result ->getHandles () as $ handler ) {
5450 if (
5551 $ this ->config ->isRemoveCategoryIdHandlers ()
5652 && str_contains ($ handler , self ::CATEGORY_ID_HANDLER_STRING )
57- && !in_array ($ handler, $ dbLayoutHandlers )
53+ && !$ this -> hasDbLayoutUpdate ($ handler )
5854 ) {
5955 $ result ->removeHandle ($ handler );
6056 continue ;
@@ -63,7 +59,7 @@ public function afterAddHandle(
6359 if (
6460 $ this ->config ->isRemoveProductIdHandlers ()
6561 && str_contains ($ handler , self ::PRODUCT_ID_HANDLER_STRING )
66- && !in_array ($ handler, $ dbLayoutHandlers )
62+ && !$ this -> hasDbLayoutUpdate ($ handler )
6763 ) {
6864 $ result ->removeHandle ($ handler );
6965 continue ;
@@ -72,11 +68,27 @@ public function afterAddHandle(
7268 if (
7369 $ this ->config ->isRemoveProductSkuHandlers ()
7470 && str_contains ($ handler , self ::PRODUCT_SKU_HANDLER_STRING )
75- && !in_array ($ handler, $ dbLayoutHandlers )
71+ && !$ this -> hasDbLayoutUpdate ($ handler )
7672 ) {
7773 $ result ->removeHandle ($ handler );
7874 }
7975 }
8076 return $ result ;
8177 }
78+
79+ private function hasDbLayoutUpdate (string $ handler ): bool
80+ {
81+ $ storeId = (int )$ this ->storeManager ->getStore ()->getId ();
82+ $ themeId = (int )$ this ->design ->getDesignTheme ()->getId ();
83+
84+ if (!isset ($ this ->dbLayoutHandlers [$ storeId ][$ themeId ][$ handler ])) {
85+ $ updateCollection = $ this ->layoutUpdateCollectionFactory ->create ();
86+ $ updateCollection ->addThemeFilter ($ themeId );
87+ $ updateCollection ->addFieldToFilter ('store_id ' , [$ storeId , Store::DEFAULT_STORE_ID ]);
88+ $ updateCollection ->addFieldToFilter ('handle ' , $ handler );
89+ $ this ->dbLayoutHandlers [$ storeId ][$ themeId ][$ handler ] = $ updateCollection ->getSize () > 0 ;
90+ }
91+
92+ return $ this ->dbLayoutHandlers [$ storeId ][$ themeId ][$ handler ];
93+ }
8294}
0 commit comments