Skip to content

Commit c6def81

Browse files
Merge pull request #378 from magento-obsessive-owls/PB-370-Introduce-upgrade-mechanism
[Owls] PB-370: Introduce upgrade mechanism for Page Builder content
2 parents 3f1ddd9 + 8b26546 commit c6def81

File tree

18 files changed

+614
-32
lines changed

18 files changed

+614
-32
lines changed

app/code/Magento/PageBuilder/Model/Dom/Adapter/DocumentInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,11 @@ public function getContents(): string;
181181
* value is found, or null if the key is not found.
182182
*/
183183
public function getMetadata($key = null);
184+
185+
/**
186+
* Strips the HTML doctype, body, etc. tags that are automatically wrapped around the content.
187+
*
188+
* @return string
189+
*/
190+
public function stripHtmlWrapperTags(): string;
184191
}

app/code/Magento/PageBuilder/Model/Dom/Adapter/ElementInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,20 @@ public function querySelector(string $selector): ElementInterface;
6969
* @return HtmlCollectionInterface
7070
*/
7171
public function querySelectorAll(string $selector): HtmlCollectionInterface;
72+
73+
/**
74+
* Removes the specified style property from the style attribute.
75+
*
76+
* @param string $styleProperty
77+
*/
78+
public function removeStyle(string $styleProperty): string;
79+
80+
/**
81+
* Adds the specified property & value to the style attribute.
82+
*
83+
* @param string $styleProperty
84+
* @param string $value
85+
* @return string
86+
*/
87+
public function addStyle(string $styleProperty, string $value): string;
7288
}

app/code/Magento/PageBuilder/Model/Dom/Document.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ class Document implements DocumentInterface
2323
/**
2424
* @var ObjectManagerInterface
2525
*/
26-
private $objectManager;
26+
protected $objectManager;
2727

2828
/**
2929
* @var GtDomDocument
3030
*/
31-
private $document;
31+
protected $document;
3232

3333
/**
3434
* Document constructor.
@@ -41,7 +41,6 @@ public function __construct(
4141
) {
4242
$this->objectManager = $objectManager;
4343
$this->document = $this->objectManager->create(GtDomDocument::class, [ 'document' => $document ]);
44-
$this->document->createDocumentFragment();
4544
}
4645

4746
/**
@@ -204,4 +203,19 @@ public function getMetadata($key = null)
204203
{
205204
return $this->document->getMetadata($key);
206205
}
206+
207+
/**
208+
* @inheritDoc
209+
*/
210+
public function stripHtmlWrapperTags(): string
211+
{
212+
preg_match('/<body>(.*)<\/body>/s', $this->saveHTML(), $matches);
213+
return preg_replace_callback(
214+
'/=\"(%7B%7B[^"]*%7D%7D)\"/m',
215+
function ($matches) {
216+
return urldecode($matches[0]);
217+
},
218+
$matches[1]
219+
);
220+
}
207221
}

app/code/Magento/PageBuilder/Model/Dom/Element.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,24 @@ public function querySelectorAll(string $selector): HtmlCollectionInterface
111111
[ 'collection' => $this->element->querySelectorAll($selector) ]
112112
);
113113
}
114+
115+
/**
116+
* @inheritDoc
117+
*/
118+
public function removeStyle(string $styleProperty): string
119+
{
120+
$style = $this->getAttribute('style');
121+
$this->setAttribute('style', preg_replace("/${styleProperty}:(.*?);/", '', $style ?? ''));
122+
return $this->getAttribute('style') ?? '';
123+
}
124+
125+
/**
126+
* @inheritDoc
127+
*/
128+
public function addStyle(string $styleProperty, string $value): string
129+
{
130+
$this->removeStyle($styleProperty);
131+
$this->setAttribute('style', "${styleProperty}: $value; " . $this->getAttribute('style') ?? '');
132+
return $this->getAttribute('style') ?? '';
133+
}
114134
}

app/code/Magento/PageBuilder/Model/Dom/HtmlDocument.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use Gt\Dom\HTMLDocument as GtDomHTMLDocument;
1111
use Magento\Framework\ObjectManagerInterface;
12-
use Magento\PageBuilder\Model\Dom\Adapter\ElementInterface;
1312
use Magento\PageBuilder\Model\Dom\Adapter\HtmlCollectionInterface;
1413
use Magento\PageBuilder\Model\Dom\Adapter\HtmlDocumentInterface;
1514

@@ -18,16 +17,6 @@
1817
*/
1918
class HtmlDocument extends Document implements HtmlDocumentInterface
2019
{
21-
/**
22-
* @var ObjectManagerInterface
23-
*/
24-
private $objectManager;
25-
26-
/**
27-
* @var GtDomHTMLDocument
28-
*/
29-
private $document;
30-
3120
/**
3221
* HtmlDocument constructor.
3322
* @param ObjectManagerInterface $objectManager
@@ -37,7 +26,7 @@ public function __construct(
3726
ObjectManagerInterface $objectManager,
3827
string $document = ""
3928
) {
40-
$this->objectManager = $objectManager;
29+
parent::__construct($objectManager, $document);
4130
$this->document = $this->objectManager->create(GtDomHTMLDocument::class, [ 'document' => $document ]);
4231
}
4332

app/code/Magento/PageBuilder/Model/Dom/XmlDocument.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@
1616
*/
1717
class XmlDocument extends Document implements XmlDocumentInterface
1818
{
19-
/**
20-
* @var ObjectManagerInterface
21-
*/
22-
private $objectManager;
23-
24-
/**
25-
* @var GtDomXmlDocument
26-
*/
27-
private $document;
28-
2919
/**
3020
* XmlDocument constructor.
3121
*
@@ -36,7 +26,7 @@ public function __construct(
3626
ObjectManagerInterface $objectManager,
3727
string $document = ""
3828
) {
39-
$this->objectManager = $objectManager;
29+
parent::__construct($objectManager, $document);
4030
$this->document = $this->objectManager->create(GtDomXmlDocument::class, [ 'document' => $document ]);
4131
}
4232
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\PageBuilder\Model;
7+
8+
/**
9+
* Class EntityPool
10+
*
11+
* Pool of entities
12+
*/
13+
class UpgradableEntitiesPool
14+
{
15+
/**
16+
* @var array
17+
*/
18+
private $entities;
19+
20+
/**
21+
* @param array $entities
22+
*/
23+
public function __construct(array $entities = [])
24+
{
25+
$this->entities = $entities;
26+
}
27+
28+
/**
29+
* Retrieve entities
30+
*
31+
* @return array
32+
*/
33+
public function getEntities()
34+
{
35+
return $this->entities;
36+
}
37+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageBuilder\Setup\Converters;
10+
11+
use Magento\Framework\DB\DataConverter\DataConverterInterface;
12+
use Magento\PageBuilder\Model\Dom\Adapter\ElementInterface;
13+
use Magento\PageBuilder\Model\Dom\HtmlDocument;
14+
use Magento\PageBuilder\Model\Dom\HtmlDocumentFactory;
15+
16+
/**
17+
* Converter to move padding in full width columns from the main row element to the inner element
18+
*/
19+
class FixFullWidthRowPadding implements DataConverterInterface
20+
{
21+
/**
22+
* @var HtmlDocumentFactory
23+
*/
24+
private $htmlDocumentFactory;
25+
26+
/**
27+
* @param HtmlDocumentFactory $htmlDocumentFactory
28+
*/
29+
public function __construct(HtmlDocumentFactory $htmlDocumentFactory)
30+
{
31+
$this->htmlDocumentFactory = $htmlDocumentFactory;
32+
}
33+
34+
/**
35+
* @inheritDoc
36+
*/
37+
public function convert($value)
38+
{
39+
/** @var HtmlDocument $document */
40+
$document = $this->htmlDocumentFactory->create([ 'document' => $value ]);
41+
$fullWidthRows = $document->querySelectorAll("div[data-content-type='row'][data-appearance='full-width']");
42+
/** @var ElementInterface $row */
43+
foreach ($fullWidthRows as $row) {
44+
$style = $row->getAttribute("style");
45+
preg_match("/padding:(.*?);/", $style, $results);
46+
$padding = isset($results[1]) ? trim($results[1]) : '';
47+
if (!$padding) {
48+
continue;
49+
}
50+
// remove padding from main row element
51+
$row->removeStyle("padding");
52+
// add padding to inner row element
53+
$innerDiv = $row->querySelector(".row-full-width-inner");
54+
$innerDiv->addStyle("padding", $padding);
55+
}
56+
return $fullWidthRows->count() > 0 ? $document->stripHtmlWrapperTags() : $value;
57+
}
58+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\PageBuilder\Setup\Patch\Data;
7+
8+
use Magento\Framework\DB\FieldDataConversionException;
9+
use Magento\Framework\Setup\Patch\DataPatchInterface;
10+
use Magento\PageBuilder\Setup\Converters\FixFullWidthRowPadding;
11+
use Magento\PageBuilder\Setup\UpgradeContentHelper;
12+
13+
/**
14+
* Patch upgrade mechanism allows us to do atomic data changes
15+
*/
16+
class UpgradeFullWidthPadding implements DataPatchInterface
17+
{
18+
/**
19+
* @var UpgradeContentHelper
20+
*/
21+
private $helper;
22+
23+
/**
24+
* @param UpgradeContentHelper $helper
25+
*/
26+
public function __construct(
27+
UpgradeContentHelper $helper
28+
) {
29+
$this->helper = $helper;
30+
}
31+
32+
/**
33+
* Do upgrade
34+
*
35+
* @return void
36+
* @throws FieldDataConversionException
37+
*/
38+
public function apply()
39+
{
40+
$this->helper->upgrade([
41+
FixFullWidthRowPadding::class
42+
]);
43+
}
44+
45+
/**
46+
* @inheritdoc
47+
*/
48+
public function getAliases()
49+
{
50+
return [];
51+
}
52+
53+
/**
54+
* @inheritdoc
55+
*/
56+
public static function getDependencies()
57+
{
58+
return [];
59+
}
60+
}

0 commit comments

Comments
 (0)