Skip to content

Commit 70d0862

Browse files
author
Prabhu Ram
committed
Merge remote-tracking branch 'mainline/2.4-develop' into honey-pr-bundle
2 parents 89bb27d + f7759d8 commit 70d0862

File tree

52 files changed

+2369
-191
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2369
-191
lines changed

app/code/Magento/Catalog/Model/ImageUploader.php

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,114 +5,116 @@
55
*/
66
namespace Magento\Catalog\Model;
77

8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\Exception\LocalizedException;
810
use Magento\Framework\File\Uploader;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\File\Name;
13+
use Magento\Framework\Filesystem;
14+
use Magento\Framework\Filesystem\Directory\WriteInterface;
15+
use Magento\MediaStorage\Helper\File\Storage\Database;
16+
use Magento\MediaStorage\Model\File\UploaderFactory;
17+
use Magento\Store\Model\StoreManagerInterface;
18+
use Psr\Log\LoggerInterface;
919

1020
/**
1121
* Catalog image uploader
1222
*/
1323
class ImageUploader
1424
{
1525
/**
16-
* Core file storage database
17-
*
18-
* @var \Magento\MediaStorage\Helper\File\Storage\Database
26+
* @var Database
1927
*/
2028
protected $coreFileStorageDatabase;
2129

2230
/**
23-
* Media directory object (writable).
24-
*
25-
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
31+
* @var WriteInterface
2632
*/
2733
protected $mediaDirectory;
2834

2935
/**
30-
* Uploader factory
31-
*
32-
* @var \Magento\MediaStorage\Model\File\UploaderFactory
36+
* @var UploaderFactory
3337
*/
3438
private $uploaderFactory;
3539

3640
/**
37-
* Store manager
38-
*
39-
* @var \Magento\Store\Model\StoreManagerInterface
41+
* @var StoreManagerInterface
4042
*/
4143
protected $storeManager;
4244

4345
/**
44-
* @var \Psr\Log\LoggerInterface
46+
* @var LoggerInterface
4547
*/
4648
protected $logger;
4749

4850
/**
49-
* Base tmp path
50-
*
5151
* @var string
5252
*/
5353
protected $baseTmpPath;
5454

5555
/**
56-
* Base path
57-
*
5856
* @var string
5957
*/
6058
protected $basePath;
6159

6260
/**
63-
* Allowed extensions
64-
*
6561
* @var string
6662
*/
6763
protected $allowedExtensions;
6864

6965
/**
70-
* List of allowed image mime types
71-
*
7266
* @var string[]
7367
*/
7468
private $allowedMimeTypes;
7569

7670
/**
77-
* ImageUploader constructor
71+
* @var Name
72+
*/
73+
private $fileNameLookup;
74+
75+
/**
76+
* ImageUploader constructor.
7877
*
79-
* @param \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase
80-
* @param \Magento\Framework\Filesystem $filesystem
81-
* @param \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory
82-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
83-
* @param \Psr\Log\LoggerInterface $logger
78+
* @param Database $coreFileStorageDatabase
79+
* @param Filesystem $filesystem
80+
* @param UploaderFactory $uploaderFactory
81+
* @param StoreManagerInterface $storeManager
82+
* @param LoggerInterface $logger
8483
* @param string $baseTmpPath
8584
* @param string $basePath
8685
* @param string[] $allowedExtensions
8786
* @param string[] $allowedMimeTypes
87+
* @param Name|null $fileNameLookup
88+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8889
*/
8990
public function __construct(
90-
\Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase,
91-
\Magento\Framework\Filesystem $filesystem,
92-
\Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
93-
\Magento\Store\Model\StoreManagerInterface $storeManager,
94-
\Psr\Log\LoggerInterface $logger,
91+
Database $coreFileStorageDatabase,
92+
Filesystem $filesystem,
93+
UploaderFactory $uploaderFactory,
94+
StoreManagerInterface $storeManager,
95+
LoggerInterface $logger,
9596
$baseTmpPath,
9697
$basePath,
9798
$allowedExtensions,
98-
$allowedMimeTypes = []
99+
$allowedMimeTypes = [],
100+
Name $fileNameLookup = null
99101
) {
100102
$this->coreFileStorageDatabase = $coreFileStorageDatabase;
101-
$this->mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
103+
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
102104
$this->uploaderFactory = $uploaderFactory;
103105
$this->storeManager = $storeManager;
104106
$this->logger = $logger;
105107
$this->baseTmpPath = $baseTmpPath;
106108
$this->basePath = $basePath;
107109
$this->allowedExtensions = $allowedExtensions;
108110
$this->allowedMimeTypes = $allowedMimeTypes;
111+
$this->fileNameLookup = $fileNameLookup ?? ObjectManager::getInstance()->get(Name::class);
109112
}
110113

111114
/**
112115
* Set base tmp path
113116
*
114117
* @param string $baseTmpPath
115-
*
116118
* @return void
117119
*/
118120
public function setBaseTmpPath($baseTmpPath)
@@ -124,7 +126,6 @@ public function setBaseTmpPath($baseTmpPath)
124126
* Set base path
125127
*
126128
* @param string $basePath
127-
*
128129
* @return void
129130
*/
130131
public function setBasePath($basePath)
@@ -136,7 +137,6 @@ public function setBasePath($basePath)
136137
* Set allowed extensions
137138
*
138139
* @param string[] $allowedExtensions
139-
*
140140
* @return void
141141
*/
142142
public function setAllowedExtensions($allowedExtensions)
@@ -179,7 +179,6 @@ public function getAllowedExtensions()
179179
*
180180
* @param string $path
181181
* @param string $imageName
182-
*
183182
* @return string
184183
*/
185184
public function getFilePath($path, $imageName)
@@ -194,7 +193,7 @@ public function getFilePath($path, $imageName)
194193
* @param bool $returnRelativePath
195194
* @return string
196195
*
197-
* @throws \Magento\Framework\Exception\LocalizedException
196+
* @throws LocalizedException
198197
*/
199198
public function moveFileFromTmp($imageName, $returnRelativePath = false)
200199
{
@@ -203,7 +202,7 @@ public function moveFileFromTmp($imageName, $returnRelativePath = false)
203202

204203
$baseImagePath = $this->getFilePath(
205204
$basePath,
206-
Uploader::getNewFileName(
205+
$this->fileNameLookup->getNewFileName(
207206
$this->mediaDirectory->getAbsolutePath(
208207
$this->getFilePath($basePath, $imageName)
209208
)
@@ -222,10 +221,7 @@ public function moveFileFromTmp($imageName, $returnRelativePath = false)
222221
);
223222
} catch (\Exception $e) {
224223
$this->logger->critical($e);
225-
throw new \Magento\Framework\Exception\LocalizedException(
226-
__('Something went wrong while saving the file(s).'),
227-
$e
228-
);
224+
throw new LocalizedException(__('Something went wrong while saving the file(s).'), $e);
229225
}
230226

231227
return $returnRelativePath ? $baseImagePath : $imageName;
@@ -235,10 +231,9 @@ public function moveFileFromTmp($imageName, $returnRelativePath = false)
235231
* Checking file for save and save it to tmp dir
236232
*
237233
* @param string $fileId
238-
*
239234
* @return string[]
240235
*
241-
* @throws \Magento\Framework\Exception\LocalizedException
236+
* @throws LocalizedException
242237
*/
243238
public function saveFileToTmpDir($fileId)
244239
{
@@ -249,15 +244,13 @@ public function saveFileToTmpDir($fileId)
249244
$uploader->setAllowedExtensions($this->getAllowedExtensions());
250245
$uploader->setAllowRenameFiles(true);
251246
if (!$uploader->checkMimeType($this->allowedMimeTypes)) {
252-
throw new \Magento\Framework\Exception\LocalizedException(__('File validation failed.'));
247+
throw new LocalizedException(__('File validation failed.'));
253248
}
254249
$result = $uploader->save($this->mediaDirectory->getAbsolutePath($baseTmpPath));
255250
unset($result['path']);
256251

257252
if (!$result) {
258-
throw new \Magento\Framework\Exception\LocalizedException(
259-
__('File can not be saved to the destination folder.')
260-
);
253+
throw new LocalizedException(__('File can not be saved to the destination folder.'));
261254
}
262255

263256
/**
@@ -277,7 +270,7 @@ public function saveFileToTmpDir($fileId)
277270
$this->coreFileStorageDatabase->saveFile($relativePath);
278271
} catch (\Exception $e) {
279272
$this->logger->critical($e);
280-
throw new \Magento\Framework\Exception\LocalizedException(
273+
throw new LocalizedException(
281274
__('Something went wrong while saving the file(s).'),
282275
$e
283276
);

app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\CatalogUrlRewrite\Model;
77

88
use Magento\Catalog\Api\CategoryRepositoryInterface;
9+
use Magento\Catalog\Api\Data\CategoryInterface;
910
use Magento\Catalog\Model\Category;
1011
use Magento\Catalog\Model\Product;
1112
use Magento\CatalogUrlRewrite\Model\Product\AnchorUrlRewriteGenerator;
@@ -15,12 +16,14 @@
1516
use Magento\CatalogUrlRewrite\Service\V1\StoreViewService;
1617
use Magento\Framework\App\Config\ScopeConfigInterface;
1718
use Magento\Framework\App\ObjectManager;
19+
use Magento\Framework\Exception\NoSuchEntityException;
1820
use Magento\Store\Model\Store;
1921
use Magento\Store\Model\StoreManagerInterface;
2022
use Magento\UrlRewrite\Model\MergeDataProviderFactory;
2123

2224
/**
23-
* Class ProductScopeRewriteGenerator
25+
* Generates Product/Category URLs for different scopes
26+
*
2427
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2528
*/
2629
class ProductScopeRewriteGenerator
@@ -174,7 +177,6 @@ public function generateForSpecificStoreView($storeId, $productCategories, Produ
174177
continue;
175178
}
176179

177-
// category should be loaded per appropriate store if category's URL key has been changed
178180
$categories[] = $this->getCategoryWithOverriddenUrlKey($storeId, $category);
179181
}
180182

@@ -240,9 +242,15 @@ public function isCategoryProperForGenerating(Category $category, $storeId)
240242
* Checks if URL key has been changed for provided category and returns reloaded category,
241243
* in other case - returns provided category.
242244
*
245+
* Category should be loaded per appropriate store at all times. This is because whilst the URL key on the
246+
* category in focus might be unchanged, parent category URL keys might be. If the category store ID
247+
* and passed store ID are the same then return current category as it is correct but may have changed in memory
248+
*
243249
* @param int $storeId
244250
* @param Category $category
245-
* @return Category
251+
*
252+
* @return CategoryInterface
253+
* @throws NoSuchEntityException
246254
*/
247255
private function getCategoryWithOverriddenUrlKey($storeId, Category $category)
248256
{
@@ -252,9 +260,10 @@ private function getCategoryWithOverriddenUrlKey($storeId, Category $category)
252260
Category::ENTITY
253261
);
254262

255-
if (!$isUrlKeyOverridden) {
263+
if (!$isUrlKeyOverridden && $storeId === $category->getStoreId()) {
256264
return $category;
257265
}
266+
258267
return $this->categoryRepository->get($category->getEntityId(), $storeId);
259268
}
260269

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogUrlRewrite\Plugin\Model;
9+
10+
use Magento\Catalog\Model\Category;
11+
use Magento\Framework\Webapi\Rest\Request as RestRequest;
12+
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
13+
14+
class CategorySetSaveRewriteHistory
15+
{
16+
private const SAVE_REWRITES_HISTORY = 'save_rewrites_history';
17+
18+
/**
19+
* @var RestRequest
20+
*/
21+
private $request;
22+
23+
/**
24+
* @param RestRequest $request
25+
*/
26+
public function __construct(RestRequest $request)
27+
{
28+
$this->request = $request;
29+
}
30+
31+
/**
32+
* Add 'save_rewrites_history' param to the category for list
33+
*
34+
* @param CategoryUrlRewriteGenerator $subject
35+
* @param Category $category
36+
* @param bool $overrideStoreUrls
37+
* @param int|null $rootCategoryId
38+
* @return array
39+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
40+
*/
41+
public function beforeGenerate(
42+
CategoryUrlRewriteGenerator $subject,
43+
Category $category,
44+
bool $overrideStoreUrls = false,
45+
?int $rootCategoryId = null
46+
) {
47+
$requestBodyParams = $this->request->getBodyParams();
48+
49+
if ($this->isCustomAttributesExists($requestBodyParams, CategoryUrlRewriteGenerator::ENTITY_TYPE)) {
50+
foreach ($requestBodyParams[CategoryUrlRewriteGenerator::ENTITY_TYPE]['custom_attributes'] as $attribute) {
51+
if ($attribute['attribute_code'] === self::SAVE_REWRITES_HISTORY) {
52+
$category->setData(self::SAVE_REWRITES_HISTORY, (bool)$attribute['value']);
53+
}
54+
}
55+
}
56+
57+
return [$category, $overrideStoreUrls, $rootCategoryId];
58+
}
59+
60+
/**
61+
* Check is any custom options exists in data
62+
*
63+
* @param array $requestBodyParams
64+
* @param string $entityCode
65+
* @return bool
66+
*/
67+
private function isCustomAttributesExists(array $requestBodyParams, string $entityCode): bool
68+
{
69+
return !empty($requestBodyParams[$entityCode]['custom_attributes']);
70+
}
71+
}

0 commit comments

Comments
 (0)