-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add export CMS feature with basic config blacklist pages
- Loading branch information
Showing
11 changed files
with
301 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Factfinder\Export\Model\Config; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
|
||
class CmsConfig | ||
{ | ||
private const PATH_DISABLE_CMS_PAGES = 'factfinder_export/cms_export/ff_export_cms_blacklist'; | ||
|
||
public function __construct(private readonly ScopeConfigInterface $scopeConfig) | ||
{ | ||
} | ||
|
||
public function getCmsBlacklist(int $scopeCode = null): array | ||
{ | ||
$pages = (string) $this->scopeConfig->getValue(self::PATH_DISABLE_CMS_PAGES, 'store', $scopeCode); | ||
|
||
return array_filter(explode(',', $pages)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Factfinder\Export\Model\Export\Cms; | ||
|
||
use Factfinder\Export\Api\Export\DataProviderInterface; | ||
use Factfinder\Export\Api\Export\ExportEntityInterface; | ||
|
||
class DataProvider implements DataProviderInterface | ||
{ | ||
public function __construct( | ||
private readonly Pages $pages, | ||
private readonly PageFactory $pageFactory, | ||
private readonly array $fields, | ||
) { | ||
} | ||
|
||
/** | ||
* @return ExportEntityInterface[] | ||
*/ | ||
public function getEntities(): iterable | ||
{ | ||
yield from []; | ||
|
||
foreach ($this->pages as $page) { | ||
yield $this->pageFactory->create(['page' => $page, 'pageFields' => $this->fields]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Factfinder\Export\Model\Export\Cms\Field; | ||
|
||
use Factfinder\Export\Api\Export\FieldInterface; | ||
use Magento\Cms\Api\Data\PageInterface; | ||
use Magento\Email\Model\Template\Filter; | ||
use Magento\Framework\Model\AbstractModel; | ||
|
||
class Content implements FieldInterface | ||
{ | ||
public function __construct(private readonly Filter $filter) | ||
{ | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'Content'; | ||
} | ||
|
||
/** | ||
* @param PageInterface $page | ||
* | ||
* @return string | ||
*/ | ||
public function getValue(AbstractModel $page): string | ||
{ | ||
$filteredContent = $this->filter->filter($page->getContent()); | ||
$stylesAndScripts = '#\<(?:style|script)[^\>]*\>[^\<]*\</(?:style|script)\>#siU'; | ||
$variables = '#{{[^}]*}}#siU'; | ||
$returns = '#<br\s?\/?>#'; | ||
$whitespaces = '#(\s| )+#s'; | ||
|
||
return preg_replace([$stylesAndScripts, $variables, $returns, $whitespaces], ' ', $filteredContent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Factfinder\Export\Model\Export\Cms\Field; | ||
|
||
use Factfinder\Export\Api\Export\FieldInterface; | ||
use Magento\Cms\Api\Data\PageInterface; | ||
use Magento\Framework\UrlInterface; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
use Magento\Framework\Model\AbstractModel; | ||
|
||
class Deeplink implements FieldInterface | ||
{ | ||
public function __construct( | ||
private readonly UrlInterface $urlBuilder, | ||
private readonly StoreManagerInterface $storeManager, | ||
) { | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'Deeplink'; | ||
} | ||
|
||
/** | ||
* @param PageInterface $page | ||
* | ||
* @return string | ||
*/ | ||
public function getValue(AbstractModel $page): string | ||
{ | ||
$this->urlBuilder->setScope($this->storeManager->getStore()->getId()); | ||
|
||
return $this->urlBuilder->getUrl(null, ['_direct' => $page->getIdentifier()]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Factfinder\Export\Model\Export\Cms\Field; | ||
|
||
use Factfinder\Export\Api\Export\FieldInterface; | ||
use Magento\Cms\Api\Data\PageInterface; | ||
use Magento\Email\Model\Template\Filter; | ||
use Magento\Framework\Model\AbstractModel; | ||
|
||
class Image implements FieldInterface | ||
{ | ||
public function __construct(private readonly Filter $filter) | ||
{ | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'Image'; | ||
} | ||
|
||
/** | ||
* @param PageInterface $page | ||
* | ||
* @return string | ||
*/ | ||
public function getValue(AbstractModel $page): string | ||
{ | ||
$pattern = '#https?://[^/\s]+/\S+\.(jpe?g|png|gif)#i'; | ||
preg_match($pattern, $this->filter->filter((string) $page->getContent()), $result); | ||
|
||
return $result[0] ?? ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Factfinder\Export\Model\Export\Cms; | ||
|
||
use Factfinder\Export\Api\Export\ExportEntityInterface; | ||
use Factfinder\Export\Api\Export\FieldInterface; | ||
use Magento\Cms\Api\Data\PageInterface; | ||
|
||
class Page implements ExportEntityInterface | ||
{ | ||
public function __construct( | ||
private readonly PageInterface $page, | ||
private readonly array $pageFields = [], | ||
) { | ||
} | ||
|
||
public function getId(): int | ||
{ | ||
return (int) $this->page->getId(); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
$data = [ | ||
'PageId' => (string) $this->getId(), | ||
'Master' => (string) $this->getId(), | ||
'Identifier' => (string) $this->page->getIdentifier(), | ||
'Title' => (string) $this->page->getTitle(), | ||
'ContentHeading' => (string) $this->page->getContentHeading(), | ||
'MetaKeywords' => (string) $this->page->getMetaKeywords(), | ||
'MetaDescription' => (string) $this->page->getMetaDescription(), | ||
]; | ||
|
||
return array_reduce( | ||
$this->pageFields, | ||
fn (array $result, FieldInterface $field): array => | ||
[$field->getName() => $field->getValue($this->page)] + $result, | ||
$data | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Factfinder\Export\Model\Export\Cms; | ||
|
||
use Factfinder\Export\Model\Config\CmsConfig; | ||
use Magento\Cms\Api\Data\PageInterface; | ||
use Magento\Cms\Api\PageRepositoryInterface; | ||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Store\Model\Store; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
use Traversable; | ||
|
||
class Pages implements \IteratorAggregate | ||
{ | ||
public function __construct( | ||
private readonly PageRepositoryInterface $pageRepository, | ||
private readonly SearchCriteriaBuilder $searchCriteriaBuilder, | ||
private readonly CmsConfig $cmsConfig, | ||
private readonly StoreManagerInterface $storeManager, | ||
) { | ||
} | ||
|
||
/** | ||
* @return Traversable|PageInterface[] | ||
* @throws LocalizedException | ||
*/ | ||
public function getIterator(): Traversable | ||
{ | ||
$query = $this->getQuery()->create(); | ||
yield from $this->pageRepository->getList($query)->getItems(); | ||
} | ||
|
||
protected function getQuery(): SearchCriteriaBuilder | ||
{ | ||
$blacklist = $this->cmsConfig->getCmsBlacklist(); | ||
|
||
if ($blacklist) { | ||
$this->searchCriteriaBuilder->addFilter('identifier', $blacklist, 'nin'); | ||
} | ||
|
||
$inStores = [Store::DEFAULT_STORE_ID, (int) $this->storeManager->getStore()->getId()]; | ||
|
||
return $this->searchCriteriaBuilder->addFilter('store_id', $inStores, 'in'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0"?> | ||
<include xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_include.xsd"> | ||
<group id="cms_export" translate="label" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1"> | ||
<label>CMS Export Settings</label> | ||
<field id="ff_export_cms_blacklist" translate="label comment" type="multiselect" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="5"> | ||
<label>Pages Blacklist</label> | ||
<comment>Selected pages will not be exported</comment> | ||
<source_model>Magento\Cms\Model\Config\Source\Page</source_model> | ||
<can_be_empty>1</can_be_empty> | ||
</field> | ||
</group> | ||
</include> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters