Skip to content

Commit 53b42f4

Browse files
authored
Merge branch '2.4-develop' into AC-2649-remove-elasticsearch-6-module
2 parents abb6759 + 56a161a commit 53b42f4

File tree

5 files changed

+388
-13
lines changed

5 files changed

+388
-13
lines changed

app/code/Magento/Customer/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,4 +567,7 @@
567567
</argument>
568568
</arguments>
569569
</type>
570+
<type name="Magento\Customer\Model\EmailNotificationInterface">
571+
<plugin name="saveWishlistDataAndAddReferenceKeyToBackUrl" type="Magento\Wishlist\Plugin\SaveWishlistDataAndAddReferenceKeyToBackUrl"/>
572+
</type>
570573
</config>

app/code/Magento/Wishlist/Controller/Index/Add.php

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
use Magento\Framework\App\Response\RedirectInterface;
2121
use Magento\Framework\Controller\ResultInterface;
2222
use Magento\Wishlist\Controller\WishlistProviderInterface;
23+
use Magento\Framework\App\Action\HttpGetActionInterface;
24+
use Magento\Wishlist\Model\DataSerializer;
25+
use Magento\Framework\Data\Form\FormKey;
2326

2427
/**
2528
* Wish list Add controller
2629
*
2730
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2831
*/
29-
class Add extends \Magento\Wishlist\Controller\AbstractIndex implements HttpPostActionInterface
32+
class Add extends \Magento\Wishlist\Controller\AbstractIndex implements HttpPostActionInterface, HttpGetActionInterface
3033
{
3134
/**
3235
* @var WishlistProviderInterface
@@ -58,6 +61,16 @@ class Add extends \Magento\Wishlist\Controller\AbstractIndex implements HttpPost
5861
*/
5962
private $urlBuilder;
6063

64+
/**
65+
* @var DataSerializer
66+
*/
67+
private $dataSerializer;
68+
69+
/**
70+
* @var FormKey
71+
*/
72+
private $formKey;
73+
6174
/**
6275
* @param Context $context
6376
* @param Session $customerSession
@@ -66,22 +79,28 @@ class Add extends \Magento\Wishlist\Controller\AbstractIndex implements HttpPost
6679
* @param Validator $formKeyValidator
6780
* @param RedirectInterface|null $redirect
6881
* @param UrlInterface|null $urlBuilder
82+
* @param DataSerializer|null $dataSerializer
83+
* @param FormKey|null $formKey
6984
*/
7085
public function __construct(
7186
Context $context,
7287
Session $customerSession,
7388
WishlistProviderInterface $wishlistProvider,
7489
ProductRepositoryInterface $productRepository,
7590
Validator $formKeyValidator,
76-
RedirectInterface $redirect = null,
77-
UrlInterface $urlBuilder = null
91+
?RedirectInterface $redirect = null,
92+
?UrlInterface $urlBuilder = null,
93+
?DataSerializer $dataSerializer = null,
94+
?FormKey $formKey = null
7895
) {
7996
$this->_customerSession = $customerSession;
8097
$this->wishlistProvider = $wishlistProvider;
8198
$this->productRepository = $productRepository;
8299
$this->formKeyValidator = $formKeyValidator;
83100
$this->redirect = $redirect ?: ObjectManager::getInstance()->get(RedirectInterface::class);
84101
$this->urlBuilder = $urlBuilder ?: ObjectManager::getInstance()->get(UrlInterface::class);
102+
$this->dataSerializer = $dataSerializer ?: ObjectManager::getInstance()->get(DataSerializer::class);
103+
$this->formKey = $formKey ?: ObjectManager::getInstance()->get(FormKey::class);
85104
parent::__construct($context);
86105
}
87106

@@ -93,11 +112,28 @@ public function __construct(
93112
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
94113
* @SuppressWarnings(PHPMD.NPathComplexity)
95114
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
115+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
96116
*/
97117
public function execute()
98118
{
99119
/** @var Redirect $resultRedirect */
100120
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
121+
$session = $this->_customerSession;
122+
$requestParams = $this->getRequest()->getParams();
123+
124+
if ($session->getBeforeWishlistRequest()) {
125+
$requestParams = $session->getBeforeWishlistRequest();
126+
$session->unsBeforeWishlistRequest();
127+
$this->getRequest()->setParam('form_key', $requestParams['form_key']);
128+
}
129+
130+
if (isset($requestParams['token'])) {
131+
$wishlistRequestBeforeLogin = $this->dataSerializer->unserialize($requestParams['token']);
132+
$requestParams['product'] = isset($wishlistRequestBeforeLogin['product']) ?
133+
(int)$wishlistRequestBeforeLogin['product'] : null;
134+
$this->getRequest()->setParam('form_key', $this->formKey->getFormKey());
135+
}
136+
101137
if (!$this->formKeyValidator->validate($this->getRequest())) {
102138
return $resultRedirect->setPath('*/');
103139
}
@@ -107,15 +143,6 @@ public function execute()
107143
throw new NotFoundException(__('Page not found.'));
108144
}
109145

110-
$session = $this->_customerSession;
111-
112-
$requestParams = $this->getRequest()->getParams();
113-
114-
if ($session->getBeforeWishlistRequest()) {
115-
$requestParams = $session->getBeforeWishlistRequest();
116-
$session->unsBeforeWishlistRequest();
117-
}
118-
119146
$productId = isset($requestParams['product']) ? (int)$requestParams['product'] : null;
120147
if (!$productId) {
121148
$resultRedirect->setPath('*/');
@@ -153,6 +180,7 @@ public function execute()
153180
if ($referer) {
154181
$session->setBeforeWishlistUrl(null);
155182
} else {
183+
// phpcs:ignore
156184
$referer = $this->_redirect->getRefererUrl();
157185
}
158186

@@ -178,7 +206,12 @@ public function execute()
178206
}
179207

180208
if ($this->getRequest()->isAjax()) {
181-
$url = $this->urlBuilder->getUrl('*', $this->redirect->updatePathParams(['wishlist_id' => $wishlist->getId()]));
209+
$url = $this->urlBuilder->getUrl(
210+
'*',
211+
$this->redirect->updatePathParams(
212+
['wishlist_id' => $wishlist->getId()]
213+
)
214+
);
182215
/** @var Json $resultJson */
183216
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
184217
$resultJson->setData(['backUrl' => $url]);
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Magento\Wishlist\Model;
11+
12+
use Magento\Framework\Serialize\SerializerInterface;
13+
use Magento\Framework\App\CacheInterface;
14+
use Magento\Framework\Math\Random;
15+
use Psr\Log\LoggerInterface;
16+
use Throwable;
17+
18+
/**
19+
* Service class to Serialize, Cache & Unserialize the data
20+
*/
21+
class DataSerializer
22+
{
23+
/**
24+
* constant for Cache Key Prefix
25+
*/
26+
private const CACHE_KEY_PREFIX = 'wishlist_';
27+
28+
/**
29+
* constant for Cache Life time
30+
*/
31+
private const CACHE_LIFE_TIME = 604800; //7 Days
32+
33+
/**
34+
* constant for Cache Id length
35+
*/
36+
private const CACHE_ID_LENGTH = 32;
37+
38+
/**
39+
* @var SerializerInterface
40+
*/
41+
private $serializer;
42+
43+
/**
44+
* @var CacheInterface
45+
*/
46+
private $cache;
47+
48+
/**
49+
* @var Random
50+
*/
51+
private $random;
52+
53+
/**
54+
* @var LoggerInterface
55+
*/
56+
private $logger;
57+
58+
/**
59+
* Initialize dependencies.
60+
*
61+
* @param SerializerInterface $serializer
62+
* @param CacheInterface $cache
63+
* @param Random $random
64+
* @param LoggerInterface $logger
65+
*/
66+
public function __construct(
67+
SerializerInterface $serializer,
68+
CacheInterface $cache,
69+
Random $random,
70+
LoggerInterface $logger
71+
) {
72+
$this->serializer = $serializer;
73+
$this->cache = $cache;
74+
$this->random = $random;
75+
$this->logger = $logger;
76+
}
77+
78+
/**
79+
* Create Cache key, Serialize & Cache the provided data
80+
*
81+
* @param array $data
82+
* @return string
83+
*/
84+
public function serialize(array $data): string
85+
{
86+
$token = $this->random->getRandomString(self::CACHE_ID_LENGTH);
87+
$cacheKey = self::CACHE_KEY_PREFIX.$token;
88+
89+
$this->cache->save(
90+
$this->serializer->serialize($data),
91+
$cacheKey,
92+
[],
93+
self::CACHE_LIFE_TIME
94+
);
95+
96+
return $token;
97+
}
98+
99+
/**
100+
* Unserialize data for given Token
101+
*
102+
* @param string $token
103+
* @return array
104+
* @throws Throwable if unpredictable error occurred.
105+
*/
106+
public function unserialize(string $token): array
107+
{
108+
$result = [];
109+
if (strlen($token) === self::CACHE_ID_LENGTH) {
110+
$cacheKey = self::CACHE_KEY_PREFIX.$token;
111+
$json = $this->cache->load($cacheKey);
112+
if ($json) {
113+
$result = $this->serializer->unserialize($json);
114+
}
115+
116+
try {
117+
$this->cache->remove($token);
118+
} catch (Throwable $exception) {
119+
$this->logger->error('Unable to remove cache: '.$exception);
120+
}
121+
} else {
122+
$this->logger->error("Invalid Token '$token' supplied.");
123+
}
124+
125+
return $result;
126+
}
127+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Magento\Wishlist\Plugin;
11+
12+
use Magento\Wishlist\Model\DataSerializer;
13+
use Magento\Customer\Api\Data\CustomerInterface;
14+
use Magento\Customer\Model\Session as CustomerSession;
15+
use Magento\Customer\Model\EmailNotificationInterface;
16+
use Magento\Framework\UrlInterface;
17+
use Magento\Framework\Url\Helper\Data as UrlHelper;
18+
19+
/**
20+
* Cache wishlist data & Modify back Url
21+
*
22+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
23+
*/
24+
class SaveWishlistDataAndAddReferenceKeyToBackUrl
25+
{
26+
/**
27+
* @var DataSerializer
28+
*/
29+
private $dataSerializer;
30+
31+
/**
32+
* @var CustomerSession
33+
*/
34+
private $customerSession;
35+
36+
/**
37+
* @var UrlInterface
38+
*/
39+
private $urlBuilder;
40+
41+
/**
42+
* @var UrlHelper
43+
*/
44+
private $urlHelper;
45+
46+
/**
47+
* @param DataSerializer $dataSerializer
48+
* @param CustomerSession $customerSession
49+
* @param UrlInterface $urlBuilder
50+
* @param UrlHelper $urlHelper
51+
*/
52+
public function __construct(
53+
DataSerializer $dataSerializer,
54+
CustomerSession $customerSession,
55+
UrlInterface $urlBuilder,
56+
UrlHelper $urlHelper
57+
) {
58+
$this->dataSerializer = $dataSerializer;
59+
$this->customerSession = $customerSession;
60+
$this->urlBuilder = $urlBuilder;
61+
$this->urlHelper = $urlHelper;
62+
}
63+
64+
/**
65+
* Before sending New Account Email, Cache wishlist data & Modify back Url
66+
*
67+
* @param EmailNotificationInterface $subject
68+
* @param CustomerInterface $customer
69+
* @param string $type
70+
* @param string $backUrl
71+
* @param int $storeId
72+
* @param string $sendemailStoreId
73+
* @return array
74+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
75+
*/
76+
public function beforeNewAccount(
77+
EmailNotificationInterface $subject,
78+
CustomerInterface $customer,
79+
$type = EmailNotificationInterface::NEW_ACCOUNT_EMAIL_REGISTERED,
80+
$backUrl = '',
81+
$storeId = null,
82+
$sendemailStoreId = null
83+
): array {
84+
if (($this->customerSession->getBeforeWishlistRequest() != null)
85+
&& ($customer->getConfirmation() != null)
86+
&& ($backUrl !== null && strpos($backUrl, 'wishlist/index/add') !== false)
87+
) {
88+
$token = $this->dataSerializer->serialize($this->customerSession->getBeforeWishlistRequest());
89+
$backUrl = $this->urlHelper->addRequestParam($backUrl, ['token' => $token]);
90+
}
91+
92+
return [$customer, $type, $backUrl, $storeId, $sendemailStoreId];
93+
}
94+
}

0 commit comments

Comments
 (0)