From f04ad62fa72ee202b6d687b023c8da7ab98955e8 Mon Sep 17 00:00:00 2001 From: Dennis Lindeboom Date: Tue, 7 Dec 2021 12:16:43 +0100 Subject: [PATCH 01/11] Added code splitting for resolving page html --- .idea/.gitignore | 8 +++++ Plugin/Model/PagePlugin.php | 50 +++++++------------------- Resolver/RemotePageResolver.php | 63 +++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 38 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 Resolver/RemotePageResolver.php diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/Plugin/Model/PagePlugin.php b/Plugin/Model/PagePlugin.php index 1f7f6ca..fb57f7b 100644 --- a/Plugin/Model/PagePlugin.php +++ b/Plugin/Model/PagePlugin.php @@ -7,62 +7,36 @@ use Magento\Cms\Model\Page; use Magento\Framework\Exception\LocalizedException; use Mooore\WordpressIntegrationCms\Model\RemotePageRepository; +use Mooore\WordpressIntegrationCms\Resolver\RemotePageResolver; class PagePlugin { - /** - * @var RemotePageRepository - */ - private $pageRepository; /** * @var array */ private $remotePageContentCache = []; + /** + * @var RemotePageResolver + */ + 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) { $remotePageId = $subject->getData('wordpress_page_id'); + + $html = $this->remotePageResolver->resolve($remotePageId); - if (empty($remotePageId)) { - 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(); - } - - 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; } } diff --git a/Resolver/RemotePageResolver.php b/Resolver/RemotePageResolver.php new file mode 100644 index 0000000..f70d035 --- /dev/null +++ b/Resolver/RemotePageResolver.php @@ -0,0 +1,63 @@ +pageRepository = $pageRepository; + } + + public function resolve(string $remotePageId) + { + if (empty($remotePageId)) { + return null; + } + + 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 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[$remotePageId] = $html; + + return $this->remotePageContentCache[$remotePageId]; + } +} From 8db48ccdb90db55855422fab398dd9ca6bf0f9b6 Mon Sep 17 00:00:00 2001 From: Dennis Lindeboom Date: Tue, 7 Dec 2021 12:21:41 +0100 Subject: [PATCH 02/11] Remove .idea files --- .idea/.gitignore | 8 -------- .idea/workspace.xml | 6 ++++++ 2 files changed, 6 insertions(+), 8 deletions(-) delete mode 100644 .idea/.gitignore create mode 100644 .idea/workspace.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 73f69e0..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..8235f21 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file From c6bf6fc49de85f4f408f069ae5aaf0f54b9d22f2 Mon Sep 17 00:00:00 2001 From: Dennis Lindeboom Date: Tue, 7 Dec 2021 12:24:30 +0100 Subject: [PATCH 03/11] Added .idea to .gitignore --- .gitignore | 1 + .idea/workspace.xml | 6 ------ 2 files changed, 1 insertion(+), 6 deletions(-) delete mode 100644 .idea/workspace.xml diff --git a/.gitignore b/.gitignore index 07e6e47..551af95 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /node_modules +.idea diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index 8235f21..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 5a48ae07eda4f4b4f8d8f86cee9be9d60bb8b5da Mon Sep 17 00:00:00 2001 From: Dennis Lindeboom Date: Tue, 7 Dec 2021 12:25:58 +0100 Subject: [PATCH 04/11] Removed property --- Plugin/Model/PagePlugin.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Plugin/Model/PagePlugin.php b/Plugin/Model/PagePlugin.php index fb57f7b..4405ff5 100644 --- a/Plugin/Model/PagePlugin.php +++ b/Plugin/Model/PagePlugin.php @@ -11,16 +11,11 @@ class PagePlugin { - /** - * @var array - */ - private $remotePageContentCache = []; /** * @var RemotePageResolver */ private $remotePageResolver; - - + public function __construct( RemotePageResolver $remotePageResolver ) { From 4ca196a15a6c06d81e6f7e841c041f182f51f241 Mon Sep 17 00:00:00 2001 From: Sean van Zuidam Date: Tue, 7 Dec 2021 12:49:02 +0100 Subject: [PATCH 05/11] @dlindeboom please also add .vscode to be consistent --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 551af95..4f51d55 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /node_modules .idea +.vscode From c7c2cefd13870ab8a9b4d6d432de9d0870604786 Mon Sep 17 00:00:00 2001 From: Dennis Lindeboom Date: Tue, 7 Dec 2021 13:25:31 +0100 Subject: [PATCH 06/11] Change parameters --- Plugin/Model/PagePlugin.php | 8 +++++++- Resolver/RemotePageResolver.php | 18 +++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Plugin/Model/PagePlugin.php b/Plugin/Model/PagePlugin.php index 4405ff5..309b8af 100644 --- a/Plugin/Model/PagePlugin.php +++ b/Plugin/Model/PagePlugin.php @@ -26,7 +26,13 @@ public function aroundGetContent(Page $subject, callable $proceed) { $remotePageId = $subject->getData('wordpress_page_id'); - $html = $this->remotePageResolver->resolve($remotePageId); + if (empty($remotePageId)) { + return $proceed(); + } + + [$siteId, $pageId] = explode('_', $remotePageId); + + $html = $this->remotePageResolver->resolve((int) $siteId, (int)$pageId); if ($html === null) { return $proceed(); diff --git a/Resolver/RemotePageResolver.php b/Resolver/RemotePageResolver.php index f70d035..737a0ea 100644 --- a/Resolver/RemotePageResolver.php +++ b/Resolver/RemotePageResolver.php @@ -22,17 +22,13 @@ public function __construct(RemotePageRepository $pageRepository) $this->pageRepository = $pageRepository; } - public function resolve(string $remotePageId) + public function resolve(int $siteId, int $pageId) { - if (empty($remotePageId)) { - return null; - } - - if (isset($this->remotePageContentCache[$remotePageId])) { - return $this->remotePageContentCache[$remotePageId]; - } + $cacheKey = sprintf('page_%s_%s', $siteId, $pageId); - [$siteId, $pageId] = explode('_', $remotePageId); + if (isset($this->remotePageContentCache[$cacheKey])) { + return $this->remotePageContentCache[$cacheKey]; + } try { $remotePage = $this->pageRepository->get((int) $siteId, (int) $pageId); @@ -56,8 +52,8 @@ public function resolve(string $remotePageId) return str_replace('“', '``', $match); // Double quotes }, $html); - $this->remotePageContentCache[$remotePageId] = $html; + $this->remotePageContentCache[$cacheKey] = $html; - return $this->remotePageContentCache[$remotePageId]; + return $this->remotePageContentCache[$cacheKey]; } } From 723dd13cbe8958fe0b22a536cc4391b1d6dbf7ab Mon Sep 17 00:00:00 2001 From: Dennis Lindeboom Date: Tue, 7 Dec 2021 13:29:58 +0100 Subject: [PATCH 07/11] Change double casting --- Resolver/RemotePageResolver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resolver/RemotePageResolver.php b/Resolver/RemotePageResolver.php index 737a0ea..b991994 100644 --- a/Resolver/RemotePageResolver.php +++ b/Resolver/RemotePageResolver.php @@ -31,7 +31,7 @@ public function resolve(int $siteId, int $pageId) } try { - $remotePage = $this->pageRepository->get((int) $siteId, (int) $pageId); + $remotePage = $this->pageRepository->get($siteId, $pageId); } catch (LocalizedException $e) { return null; } From 6e1660a9f20f3135201db4b1f0da314e614bc7d4 Mon Sep 17 00:00:00 2001 From: Dennis Lindeboom Date: Tue, 7 Dec 2021 13:32:12 +0100 Subject: [PATCH 08/11] Improved formatting --- Plugin/Model/PagePlugin.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Plugin/Model/PagePlugin.php b/Plugin/Model/PagePlugin.php index 309b8af..f3f0e37 100644 --- a/Plugin/Model/PagePlugin.php +++ b/Plugin/Model/PagePlugin.php @@ -15,7 +15,7 @@ class PagePlugin * @var RemotePageResolver */ private $remotePageResolver; - + public function __construct( RemotePageResolver $remotePageResolver ) { @@ -31,8 +31,8 @@ public function aroundGetContent(Page $subject, callable $proceed) } [$siteId, $pageId] = explode('_', $remotePageId); - - $html = $this->remotePageResolver->resolve((int) $siteId, (int)$pageId); + + $html = $this->remotePageResolver->resolve((int)$siteId, (int)$pageId); if ($html === null) { return $proceed(); From fc795d5865e3b431b82c794dad4254ab3cbb7eee Mon Sep 17 00:00:00 2001 From: Dennis Lindeboom Date: Tue, 7 Dec 2021 13:32:40 +0100 Subject: [PATCH 09/11] Removed spaces --- Plugin/Model/PagePlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Plugin/Model/PagePlugin.php b/Plugin/Model/PagePlugin.php index f3f0e37..3579549 100644 --- a/Plugin/Model/PagePlugin.php +++ b/Plugin/Model/PagePlugin.php @@ -25,7 +25,7 @@ public function __construct( public function aroundGetContent(Page $subject, callable $proceed) { $remotePageId = $subject->getData('wordpress_page_id'); - + if (empty($remotePageId)) { return $proceed(); } From eeac13de3ac5c10d957231cabc5b0e5fe5bcd81e Mon Sep 17 00:00:00 2001 From: Dennis Lindeboom Date: Tue, 7 Dec 2021 13:39:25 +0100 Subject: [PATCH 10/11] Added return type --- Resolver/RemotePageResolver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resolver/RemotePageResolver.php b/Resolver/RemotePageResolver.php index b991994..96fa721 100644 --- a/Resolver/RemotePageResolver.php +++ b/Resolver/RemotePageResolver.php @@ -22,7 +22,7 @@ public function __construct(RemotePageRepository $pageRepository) $this->pageRepository = $pageRepository; } - public function resolve(int $siteId, int $pageId) + public function resolve(int $siteId, int $pageId): ?string { $cacheKey = sprintf('page_%s_%s', $siteId, $pageId); From bb4847d58c086e2d4e8f1c96a567ec31bfb59930 Mon Sep 17 00:00:00 2001 From: Dennis Lindeboom Date: Tue, 7 Dec 2021 13:57:08 +0100 Subject: [PATCH 11/11] Updated CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9dfb0c1..b33f10c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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