88use Magento \Catalog \Model \Category ;
99use Magento \Catalog \Model \CategoryRepository ;
1010use Magento \Catalog \Model \Product ;
11+ use Magento \Framework \App \Config \ScopeConfigInterface ;
1112use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
1213use Magento \UrlRewrite \Model \OptionProvider ;
1314use Magento \CatalogUrlRewrite \Model \ObjectRegistry ;
1819use Magento \CatalogUrlRewrite \Model \Map \UrlRewriteFinder ;
1920use Magento \Framework \App \ObjectManager ;
2021use Magento \UrlRewrite \Model \MergeDataProviderFactory ;
22+ use Magento \CatalogUrlRewrite \Block \UrlKeyRenderer ;
23+ use Magento \Store \Model \ScopeInterface ;
2124
2225/**
2326 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -27,18 +30,21 @@ class CurrentUrlRewritesRegenerator
2730 /**
2831 * @var Product
2932 * @deprecated 100.1.0
33+ * @see not used
3034 */
3135 protected $ product ;
3236
3337 /**
3438 * @var ObjectRegistry
3539 * @deprecated 100.1.0
40+ * @see not used
3641 */
3742 protected $ productCategories ;
3843
3944 /**
4045 * @var UrlFinderInterface
4146 * @deprecated 100.1.0
47+ * @see not used
4248 */
4349 protected $ urlFinder ;
4450
@@ -72,21 +78,28 @@ class CurrentUrlRewritesRegenerator
7278 */
7379 private $ categoryRepository ;
7480
81+ /**
82+ * @var ScopeConfigInterface
83+ */
84+ private ScopeConfigInterface $ scopeConfig ;
85+
7586 /**
7687 * @param UrlFinderInterface $urlFinder
7788 * @param ProductUrlPathGenerator $productUrlPathGenerator
7889 * @param UrlRewriteFactory $urlRewriteFactory
7990 * @param UrlRewriteFinder|null $urlRewriteFinder
8091 * @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory
8192 * @param CategoryRepository|null $categoryRepository
93+ * @param ScopeConfigInterface|null $scopeConfig
8294 */
8395 public function __construct (
8496 UrlFinderInterface $ urlFinder ,
8597 ProductUrlPathGenerator $ productUrlPathGenerator ,
8698 UrlRewriteFactory $ urlRewriteFactory ,
8799 ?UrlRewriteFinder $ urlRewriteFinder = null ,
88100 ?MergeDataProviderFactory $ mergeDataProviderFactory = null ,
89- ?CategoryRepository $ categoryRepository = null
101+ ?CategoryRepository $ categoryRepository = null ,
102+ ?ScopeConfigInterface $ scopeConfig = null
90103 ) {
91104 $ this ->urlFinder = $ urlFinder ;
92105 $ this ->productUrlPathGenerator = $ productUrlPathGenerator ;
@@ -98,6 +111,7 @@ public function __construct(
98111 }
99112 $ this ->categoryRepository = $ categoryRepository ?: ObjectManager::getInstance ()->get (CategoryRepository::class);
100113 $ this ->mergeDataProviderPrototype = $ mergeDataProviderFactory ->create ();
114+ $ this ->scopeConfig = $ scopeConfig ?: ObjectManager::getInstance ()->get (ScopeConfigInterface::class);
101115 }
102116
103117 /**
@@ -119,6 +133,14 @@ public function generate($storeId, Product $product, ObjectRegistry $productCate
119133 $ rootCategoryId
120134 );
121135
136+ $ isSaveHistory = $ this ->scopeConfig ->isSetFlag (
137+ UrlKeyRenderer::XML_PATH_SEO_SAVE_HISTORY ,
138+ ScopeInterface::SCOPE_STORE ,
139+ $ storeId
140+ );
141+
142+ $ product ->setData ('save_rewrites_history ' , $ isSaveHistory );
143+
122144 foreach ($ currentUrlRewrites as $ currentUrlRewrite ) {
123145 $ category = $ this ->retrieveCategoryFromMetadata ($ currentUrlRewrite , $ productCategories );
124146 if ($ category === false ) {
@@ -158,10 +180,12 @@ public function generateAnchor(
158180 ProductUrlRewriteGenerator::ENTITY_TYPE ,
159181 $ rootCategoryId
160182 );
161-
162- foreach ($ productCategories ->getList () as $ productCategory ) {
163- $ anchorCategoryIds = array_merge ($ productCategory ->getAnchorsAbove (), $ anchorCategoryIds );
164- }
183+ $ anchorCategoryIds = array_merge (
184+ ...array_map (
185+ fn ($ productCategory ) => $ productCategory ->getAnchorsAbove (),
186+ $ productCategories ->getList ()
187+ )
188+ );
165189
166190 foreach ($ currentUrlRewrites as $ currentUrlRewrite ) {
167191 $ metadata = $ currentUrlRewrite ->getMetadata ();
@@ -181,6 +205,8 @@ public function generateAnchor(
181205 }
182206
183207 /**
208+ * Generate URL rewrites for autogenerated URLs
209+ *
184210 * @param UrlRewrite $url
185211 * @param int $storeId
186212 * @param Category|null $category
@@ -209,6 +235,8 @@ protected function generateForAutogenerated($url, $storeId, $category, $product
209235 }
210236
211237 /**
238+ * Generate URL rewrites for custom URLs
239+ *
212240 * @param UrlRewrite $url
213241 * @param int $storeId
214242 * @param Category|null $category
@@ -237,6 +265,8 @@ protected function generateForCustom($url, $storeId, $category, $product = null)
237265 }
238266
239267 /**
268+ * Retrieve category from URL metadata
269+ *
240270 * @param UrlRewrite $url
241271 * @param ObjectRegistry|null $productCategories
242272 * @return Category|null|bool
0 commit comments