Skip to content

Commit

Permalink
Merge pull request #61 from mooore-digital/feature/code-split-page-re…
Browse files Browse the repository at this point in the history
…solving

Added code splitting for resolving page html
  • Loading branch information
poespas authored Dec 7, 2021
2 parents 5350280 + bb4847d commit 52c68d1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/node_modules
.idea
.vscode
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.8.5] - 2021-12-07
### Changed
- Code split the PagePlugin so the remote page resolving becomes reusable

## [0.8.4] - 2021-09-02
### Fixed
- Potential security issue with path-parse from @wordpress/block-library #57
Expand Down
43 changes: 9 additions & 34 deletions Plugin/Model/PagePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@
use Magento\Cms\Model\Page;
use Magento\Framework\Exception\LocalizedException;
use Mooore\WordpressIntegrationCms\Model\RemotePageRepository;
use Mooore\WordpressIntegrationCms\Resolver\RemotePageResolver;

class PagePlugin
{
/**
* @var RemotePageRepository
* @var RemotePageResolver
*/
private $pageRepository;
/**
* @var array
*/
private $remotePageContentCache = [];

private $remotePageResolver;

public function __construct(RemotePageRepository $pageRepository)
{
$this->pageRepository = $pageRepository;
public function __construct(
RemotePageResolver $remotePageResolver
) {
$this->remotePageResolver = $remotePageResolver;
}

public function aroundGetContent(Page $subject, callable $proceed)
Expand All @@ -33,36 +30,14 @@ public function aroundGetContent(Page $subject, callable $proceed)
return $proceed();
}

if (isset($this->remotePageContentCache[$remotePageId])) {
return $this->remotePageContentCache[$remotePageId];
}

[$siteId, $pageId] = explode('_', $remotePageId);

try {
$remotePage = $this->pageRepository->get((int) $siteId, (int) $pageId);
} catch (LocalizedException $e) {
return $proceed();
}
$html = $this->remotePageResolver->resolve((int)$siteId, (int)$pageId);

if ($remotePage === null || empty($remotePage['content'])) {
if ($html === null) {
return $proceed();
}

$html = $remotePage['content']['rendered'];

$html = preg_replace_callback("{{(.*)}}", function ($matches) {
$match = $matches[0];

$match = html_entity_decode($match);
$match = str_replace('', '"', $match); // Opening quote
$match = str_replace('', '"', $match); // Ending quote
$match = str_replace('', '``', $match); // Double quotes
return $match;
}, $html);

$this->remotePageContentCache[$remotePageId] = $html;

return $html;
}
}
59 changes: 59 additions & 0 deletions Resolver/RemotePageResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Mooore\WordpressIntegrationCms\Resolver;

use Mooore\WordpressIntegrationCms\Model\RemotePageRepository;

class RemotePageResolver
{
/**
* @var RemotePageRepository
*/
private $pageRepository;
/**
* @var array
*/
private $remotePageContentCache = [];

public function __construct(RemotePageRepository $pageRepository)
{
$this->pageRepository = $pageRepository;
}

public function resolve(int $siteId, int $pageId): ?string
{
$cacheKey = sprintf('page_%s_%s', $siteId, $pageId);

if (isset($this->remotePageContentCache[$cacheKey])) {
return $this->remotePageContentCache[$cacheKey];
}

try {
$remotePage = $this->pageRepository->get($siteId, $pageId);
} catch (LocalizedException $e) {
return null;
}

if ($remotePage === null || empty($remotePage['content'])) {
return null;
}

/** @var string $html */
$html = $remotePage['content']['rendered'];

$html = preg_replace_callback("{{(.*)}}", function ($matches) {
$match = $matches[0];

$match = html_entity_decode($match);
$match = str_replace('', '"', $match); // Opening quote
$match = str_replace('', '"', $match); // Ending quote
return str_replace('', '``', $match); // Double quotes
}, $html);

$this->remotePageContentCache[$cacheKey] = $html;

return $this->remotePageContentCache[$cacheKey];
}
}

0 comments on commit 52c68d1

Please sign in to comment.