Skip to content

Commit

Permalink
Resolved merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Riezebos committed Jan 10, 2022
2 parents ad20d6d + 52c68d1 commit e0639e3
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 21 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Sometimes double-dashes gets encoded to one from API

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

## [0.8.4] - 2021-09-02
Expand Down
29 changes: 9 additions & 20 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,19 +30,11 @@ 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();
}

Expand Down
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 e0639e3

Please sign in to comment.