Skip to content

Commit b7289bc

Browse files
Merge branch 'develop' into PB-718
2 parents 38316dd + 6476826 commit b7289bc

File tree

11 files changed

+522
-15
lines changed

11 files changed

+522
-15
lines changed

app/code/Magento/PageBuilder/Block/WysiwygSetup.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88

99
namespace Magento\PageBuilder\Block;
1010

11+
use Magento\Framework\App\ObjectManager;
1112
use Magento\Framework\Cache\FrontendInterface;
1213
use Magento\Framework\DataObject;
1314
use Magento\Framework\View\Element\Template;
15+
use Magento\Framework\View\Element\Template\Context;
16+
use Magento\PageBuilder\Model\Session\RandomKey;
17+
use Magento\Ui\Component\Wysiwyg\ConfigInterface;
1418

1519
/**
1620
* @api
@@ -20,7 +24,7 @@ class WysiwygSetup extends Template
2024
private const WYSIWYG_CONFIG_CACHE_ID = 'WYSIWYG_CONFIG';
2125

2226
/**
23-
* @var \Magento\Ui\Component\Wysiwyg\ConfigInterface
27+
* @var ConfigInterface
2428
*/
2529
private $config;
2630

@@ -30,19 +34,28 @@ class WysiwygSetup extends Template
3034
private $cache;
3135

3236
/**
33-
* @param Template\Context $context
34-
* @param \Magento\Ui\Component\Wysiwyg\ConfigInterface $config
37+
* @var RandomKey
38+
*/
39+
private $sessionRandomKey;
40+
41+
/**
42+
* @param Context $context
43+
* @param ConfigInterface $config
3544
* @param array $data
3645
* @param FrontendInterface|null $cache
46+
* @param RandomKey|null $sessionRandomKey
3747
*/
3848
public function __construct(
39-
\Magento\Framework\View\Element\Template\Context $context,
40-
\Magento\Ui\Component\Wysiwyg\ConfigInterface $config,
49+
Context $context,
50+
ConfigInterface $config,
4151
array $data = [],
42-
FrontendInterface $cache = null
52+
FrontendInterface $cache = null,
53+
?RandomKey $sessionRandomKey = null
4354
) {
4455
$this->config = $config;
45-
$this->cache = $cache ?: \Magento\Framework\App\ObjectManager::getInstance()->get(FrontendInterface::class);
56+
$this->cache = $cache ?: ObjectManager::getInstance()->get(FrontendInterface::class);
57+
$this->sessionRandomKey = $sessionRandomKey
58+
?: ObjectManager::getInstance()->get(RandomKey::class);
4659
parent::__construct($context, $data);
4760
}
4861

@@ -53,14 +66,18 @@ public function __construct(
5366
*/
5467
public function getConfigJson() : string
5568
{
56-
$configJson = $this->cache->load(self::WYSIWYG_CONFIG_CACHE_ID);
69+
$cacheKey = self::WYSIWYG_CONFIG_CACHE_ID;
70+
if ($this->_urlBuilder->useSecretKey()) {
71+
$cacheKey .= '_' . $this->sessionRandomKey->getValue();
72+
}
73+
$configJson = $this->cache->load($cacheKey);
5774
if (!$configJson) {
5875
$config = $this->config->getConfig();
5976
if (is_array($config)) {
6077
$config = new DataObject($config);
6178
}
6279
$configJson = $config->toJson();
63-
$this->cache->save($configJson, self::WYSIWYG_CONFIG_CACHE_ID);
80+
$this->cache->save($configJson, $cacheKey);
6481
}
6582

6683
return $configJson;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\PageBuilder\Model;
9+
10+
use Magento\Framework\Cache\FrontendInterface;
11+
12+
/**
13+
* Clean page builder config cache
14+
*/
15+
class EditorConfigCacheCleaner
16+
{
17+
/**
18+
* @var FrontendInterface
19+
*/
20+
private $cache;
21+
22+
/**
23+
* @param FrontendInterface $cache
24+
*/
25+
public function __construct(
26+
FrontendInterface $cache
27+
) {
28+
$this->cache = $cache;
29+
}
30+
31+
/**
32+
* Clean page builder config cache
33+
*/
34+
public function execute(): void
35+
{
36+
$this->cache->clean();
37+
}
38+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\PageBuilder\Model\Session;
9+
10+
use Magento\Framework\Math\Random;
11+
use Magento\Framework\Session\SessionManagerInterface;
12+
13+
/**
14+
* Generate random key and save it in current session
15+
*
16+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
17+
*/
18+
class RandomKey
19+
{
20+
/**
21+
* Default key length to generate
22+
*/
23+
private const DEFAULT_KEY_LENGTH = 16;
24+
25+
/**
26+
* Default key name
27+
*/
28+
private const DEFAULT_NAME = '_pb_config_cache_key_suffix';
29+
30+
/**
31+
* @var Random
32+
*/
33+
private $random;
34+
35+
/**
36+
* @var SessionManagerInterface
37+
*/
38+
private $session;
39+
40+
/**
41+
* @var string
42+
*/
43+
private $keyName;
44+
45+
/**
46+
* @var int
47+
*/
48+
private $keyLength;
49+
50+
/**
51+
* @param Random $random
52+
* @param SessionManagerInterface $session
53+
* @param string $keyName
54+
* @param int $keyLength
55+
*/
56+
public function __construct(
57+
Random $random,
58+
SessionManagerInterface $session,
59+
string $keyName = self::DEFAULT_NAME,
60+
int $keyLength = self::DEFAULT_KEY_LENGTH
61+
) {
62+
$this->random = $random;
63+
$this->session = $session;
64+
$this->keyName = $keyName;
65+
$this->keyLength = $keyLength;
66+
}
67+
68+
/**
69+
* Retrieve generated random key from session
70+
*
71+
* @return string
72+
* @throws \Magento\Framework\Exception\LocalizedException
73+
*/
74+
public function getValue(): string
75+
{
76+
$randomKey = $this->session->getData($this->keyName);
77+
if (!$randomKey) {
78+
$randomKey = $this->random->getRandomString($this->keyLength);
79+
$this->session->setData($this->keyName, $randomKey);
80+
}
81+
return $randomKey;
82+
}
83+
}

app/code/Magento/PageBuilder/Model/Stage/Config.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\AuthorizationInterface;
1414
use Magento\Framework\Cache\FrontendInterface;
1515
use Magento\Framework\Serialize\Serializer\Json;
16+
use Magento\PageBuilder\Model\Session\RandomKey;
1617

1718
/**
1819
* Provide configuration to the admin JavaScript app
@@ -119,6 +120,11 @@ class Config
119120
*/
120121
private $serializer;
121122

123+
/**
124+
* @var RandomKey
125+
*/
126+
private $sessionRandomKey;
127+
122128
/**
123129
* @param \Magento\PageBuilder\Model\ConfigInterface $config
124130
* @param Config\UiComponentConfig $uiComponentConfig
@@ -136,6 +142,7 @@ class Config
136142
* @param AuthorizationInterface|null $authorization
137143
* @param FrontendInterface|null $cache
138144
* @param Json|null $serializer
145+
* @param RandomKey|null $sessionRandomKey
139146
*
140147
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
141148
*/
@@ -155,7 +162,8 @@ public function __construct(
155162
\Magento\Variable\Model\Variable\Config $variableConfig = null,
156163
AuthorizationInterface $authorization = null,
157164
FrontendInterface $cache = null,
158-
Json $serializer = null
165+
Json $serializer = null,
166+
?RandomKey $sessionRandomKey = null
159167
) {
160168
$this->config = $config;
161169
$this->uiComponentConfig = $uiComponentConfig;
@@ -175,6 +183,8 @@ public function __construct(
175183
$this->authorization = $authorization ?: ObjectManager::getInstance()->get(AuthorizationInterface::class);
176184
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Json::class);
177185
$this->cache = $cache ?: \Magento\Framework\App\ObjectManager::getInstance()->get(FrontendInterface::class);
186+
$this->sessionRandomKey = $sessionRandomKey
187+
?: \Magento\Framework\App\ObjectManager::getInstance()->get(RandomKey::class);
178188
}
179189

180190
/**
@@ -437,6 +447,9 @@ private function getCachedWidgetBreakpoints(): array
437447
*/
438448
private function getCache(string $cacheIdentifier): array
439449
{
450+
if ($this->urlBuilder->useSecretKey()) {
451+
$cacheIdentifier .= '_' . $this->sessionRandomKey->getValue();
452+
}
440453
$serializedData = $this->cache->load($cacheIdentifier);
441454
$cache = $serializedData
442455
? $this->serializer->unserialize($serializedData)
@@ -453,6 +466,9 @@ private function getCache(string $cacheIdentifier): array
453466
*/
454467
private function saveCache(array $data, string $cacheIdentifier): void
455468
{
469+
if ($this->urlBuilder->useSecretKey()) {
470+
$cacheIdentifier .= '_' . $this->sessionRandomKey->getValue();
471+
}
456472
$this->cache->save($this->serializer->serialize($data), $cacheIdentifier);
457473
}
458474

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\PageBuilder\Plugin;
9+
10+
use Magento\Backend\Model\UrlInterface;
11+
use Magento\PageBuilder\Model\EditorConfigCacheCleaner;
12+
13+
/**
14+
* Clear page builder editor config cache after login
15+
*/
16+
class ClearEditorConfigCache
17+
{
18+
/**
19+
* @var EditorConfigCacheCleaner
20+
*/
21+
private $cacheCleaner;
22+
23+
/**
24+
* @var UrlInterface
25+
*/
26+
private $backendUrl;
27+
28+
/**
29+
* @param UrlInterface $backendUrl
30+
* @param EditorConfigCacheCleaner $cacheCleaner
31+
*/
32+
public function __construct(
33+
UrlInterface $backendUrl,
34+
EditorConfigCacheCleaner $cacheCleaner
35+
) {
36+
$this->cacheCleaner = $cacheCleaner;
37+
$this->backendUrl = $backendUrl;
38+
}
39+
40+
/**
41+
* Clear page builder editor config cache after login
42+
*
43+
* @param \Magento\Backend\Model\Auth $subject
44+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
45+
*/
46+
public function afterLogin(\Magento\Backend\Model\Auth $subject): void
47+
{
48+
if ($this->backendUrl->useSecretKey()) {
49+
$this->cacheCleaner->execute();
50+
}
51+
}
52+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\PageBuilder\Test\Unit\Model;
9+
10+
use Magento\Framework\Cache\FrontendInterface;
11+
use Magento\PageBuilder\Model\EditorConfigCacheCleaner;
12+
use PHPUnit\Framework\MockObject\MockObject;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/***
16+
* Test for page builder cache config cleaner
17+
*/
18+
class EditorConfigCacheCleanerTest extends TestCase
19+
{
20+
/**
21+
* @var EditorConfigCacheCleaner
22+
*/
23+
private $model;
24+
25+
/**
26+
* @var FrontendInterface|MockObject
27+
*/
28+
private $cache;
29+
30+
/**
31+
* @inheritDoc
32+
*/
33+
protected function setUp(): void
34+
{
35+
parent::setUp();
36+
$this->cache = $this->createMock(FrontendInterface::class);
37+
$this->model = new EditorConfigCacheCleaner(
38+
$this->cache
39+
);
40+
}
41+
42+
/**
43+
* Test that the supplied cache is clean up
44+
*/
45+
public function testExecute(): void
46+
{
47+
$this->cache->expects($this->once())
48+
->method('clean');
49+
$this->model->execute();
50+
}
51+
}

0 commit comments

Comments
 (0)