From 17c93287adb9d21c56dcc16b45a47e7541ffc6b4 Mon Sep 17 00:00:00 2001 From: Magefan - e-Commerce solutions you can trust Date: Mon, 10 Nov 2025 17:49:02 +0200 Subject: [PATCH] 14232-compatibiility-with-mageplaza-layered-naivigation --- .../Magento/Framework/App/ActionInterface.php | 75 +++++++++++++------ 1 file changed, 54 insertions(+), 21 deletions(-) diff --git a/Plugin/Frontend/Magento/Framework/App/ActionInterface.php b/Plugin/Frontend/Magento/Framework/App/ActionInterface.php index 4f1bdf6..94ffde0 100644 --- a/Plugin/Frontend/Magento/Framework/App/ActionInterface.php +++ b/Plugin/Frontend/Magento/Framework/App/ActionInterface.php @@ -25,12 +25,12 @@ class ActionInterface protected $config; /** - * @var RequestInterface + * @var RequestInterface */ private $request; /** - * @var ResponseInterface + * @var ResponseInterface */ private $response; @@ -55,42 +55,75 @@ public function __construct( /** * @param $subject - * @param $response + * @param $result * @return mixed */ - public function afterExecute($subject, $response) + public function afterExecute($subject, $result) { - if (!$this->canProcess($response)) { - return $response; + if (!$this->request->isAjax() || !$this->config->isEnabled()) { + return $result; } + $response = $result ?: ($subject->getResponse() ?? null); + if (!is_object($response) || !method_exists($response, 'getBody')) { + return $result; + } + + $body = (string) $response->getBody(); + + if ($this->looksLikeJson($body)) { + $decoded = json_decode($body, true); + + if (is_array($decoded) && isset($decoded['products']) && is_string($decoded['products'])) { + $html = $decoded['products']; - $html = $response->getBody(); + if ($this->containsMarkers($html)) { + $decoded['products'] = $this->htmlParser->execute($html); + $newBody = json_encode($decoded, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); - if ( - $html - && ( - false !== strpos($html, Html::COMMENT_PREFIX) || - false !== strpos($html, Html::COMMENT_PREFIX_GALLERY) - ) - ) { + if (method_exists($response, 'setBody')) { + $response->setBody($newBody); + return $response; + } + + return $newBody; + } + } + + return $result; + } - $response->setBody($this->htmlParser->execute($html)); + // If not JSON, treat as HTML + if ($this->containsMarkers($body) && method_exists($response, 'setBody')) { + $response->setBody($this->htmlParser->execute($body)); } return $response; } /** + * @param string $body * @return bool */ - private function canProcess($response): bool + private function looksLikeJson(string $body): bool { + $trim = ltrim($body); + return (strlen($trim) > 0 && ($trim[0] === '{' || $trim[0] === '[')) && (false !== strpos($body, '"products"') || false !== strpos($body, 'products')); + } + + /** + * @param string|null $html + * @return bool + */ + private function containsMarkers(?string $html): bool + { + if ($html === null || $html === '') { + return false; + } + return - $this->request->isAjax() - && $this->config->isEnabled() - && is_object($response) - && get_class($response) == 'Magento\Framework\App\Response\Http\Interceptor'; + (false !== strpos($html, Html::COMMENT_PREFIX)) || + (false !== strpos($html, Html::COMMENT_PREFIX_GALLERY)); } -} \ No newline at end of file +}