Skip to content

Commit e56b3a2

Browse files
authored
Merge pull request #12 from magefan/12223-convert-labels-in-ajax-requests
12223 replace label comments on labels in ajax requests
2 parents 008230d + 3c26515 commit e56b3a2

File tree

3 files changed

+107
-16
lines changed

3 files changed

+107
-16
lines changed

Model/Parser/Html.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace Magefan\ProductLabel\Model\Parser;
1010

11-
use Magento\Framework\App\RequestInterface;
1211
use Magefan\ProductLabel\Model\GetLabels;
1312

1413
class Html
@@ -17,29 +16,15 @@ class Html
1716
const COMMENT_PREFIX_GALLERY = '<!--mf_product_label_gallery_comment_';
1817
const COMMENT_SUFFIX = '-->';
1918

20-
/**
21-
* @var RequestInterface
22-
*/
23-
protected $request;
24-
25-
/**
26-
* @var
27-
*/
28-
protected $fan;
29-
3019
/**
3120
* @var GetLabels
3221
*/
3322
protected $getLabels;
3423

3524
public function __construct(
36-
RequestInterface $request,
3725
GetLabels $getLabels
3826
) {
39-
$this->request = $request;
4027
$this->getLabels = $getLabels;
41-
42-
$this->fan = $this->request->getFullActionName();
4328
}
4429

4530
/**
@@ -51,7 +36,7 @@ public function execute(string $output): string
5136
$isOutputIsJson = $this->json_validate($output);
5237

5338
$productIds = $this->getProductIds($output);
54-
$currentPageProductId = $this->fan == 'catalog_product_view' ? $this->getCurrentPageProductId($output) : 0;
39+
$currentPageProductId = $this->getCurrentPageProductId($output);
5540
$productIdsForProductPage = [];
5641

5742
if ($currentPageProductId) {
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magefan\ProductLabel\Plugin\Frontend\Magento\Framework\App;
9+
10+
use Magento\Framework\App\RequestInterface;
11+
use Magento\Framework\App\ResponseInterface;
12+
use Magefan\ProductLabel\Model\Parser\Html;
13+
use Magefan\ProductLabel\Model\Config;
14+
15+
class ActionInterface
16+
{
17+
/**
18+
* @var Html
19+
*/
20+
protected $htmlParser;
21+
22+
/**
23+
* @var Config
24+
*/
25+
protected $config;
26+
27+
/**
28+
* @var RequestInterface
29+
*/
30+
private $request;
31+
32+
/**
33+
* @var ResponseInterface
34+
*/
35+
private $response;
36+
37+
/**
38+
* ActionInterface constructor.
39+
* @param Html $htmlParser
40+
* @param Config $config
41+
* @param RequestInterface $request
42+
* @param ResponseInterface $response
43+
*/
44+
public function __construct(
45+
Html $htmlParser,
46+
Config $config,
47+
RequestInterface $request,
48+
ResponseInterface $response
49+
) {
50+
$this->htmlParser = $htmlParser;
51+
$this->config = $config;
52+
$this->request = $request;
53+
$this->response = $response;
54+
}
55+
56+
/**
57+
* @param $subject
58+
* @param $response
59+
* @return mixed
60+
*/
61+
public function afterExecute($subject, $response)
62+
{
63+
if (!$this->canProcess($response)) {
64+
return $response;
65+
}
66+
67+
68+
69+
$html = $response->getBody();
70+
71+
if (
72+
$html
73+
&& (
74+
false !== strpos($html, Html::COMMENT_PREFIX) ||
75+
false !== strpos($html, Html::COMMENT_PREFIX_GALLERY)
76+
)
77+
) {
78+
79+
$response->setBody($this->htmlParser->execute($html));
80+
}
81+
82+
return $response;
83+
}
84+
85+
/**
86+
* @return bool
87+
*/
88+
private function canProcess($response): bool
89+
{
90+
return
91+
$this->request->isAjax()
92+
&& $this->config->isEnabled()
93+
&& is_object($response)
94+
&& get_class($response) == 'Magento\Framework\App\Response\Http\Interceptor';
95+
}
96+
}

etc/frontend/di.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,19 @@
1010
<type name="Magento\Catalog\Block\Product\Image">
1111
<plugin name="wrap_image_with_mf_comments" type="Magefan\ProductLabel\Plugin\Frontend\Magento\Catalog\Block\Product\Image" sortOrder="10" disabled="false"/>
1212
</type>
13+
14+
15+
1316
<type name="Magento\Framework\Controller\ResultInterface">
1417
<plugin name="replace_mf_comments_on_product_labels" type="Magefan\ProductLabel\Plugin\Frontend\Magento\Framework\Controller\ResultInterface" sortOrder="-21" disabled="false"/>
1518
</type>
19+
20+
<type name="Magento\Framework\App\ActionInterface">
21+
<plugin name="replace_mf_comments_on_product_labels_in_ajax_requests" type="Magefan\ProductLabel\Plugin\Frontend\Magento\Framework\App\ActionInterface" sortOrder="10001"/>
22+
</type>
23+
24+
25+
1626
<type name="Magento\Catalog\Block\Product\View\Gallery">
1727
<plugin name="wrap_gallery_image_with_mf_comments" type="Magefan\ProductLabel\Plugin\Frontend\Magento\Catalog\Block\Product\View\Gallery" sortOrder="10" disabled="false"/>
1828
</type>

0 commit comments

Comments
 (0)