Skip to content

Commit 710b381

Browse files
committed
Code refactor
1 parent 4526c8e commit 710b381

File tree

17 files changed

+518
-55
lines changed

17 files changed

+518
-55
lines changed

Api/ContainerInterface.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magefan\GoogleTagManager\Api;
10+
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
13+
interface ContainerInterface
14+
{
15+
/**
16+
* Generate JSON container
17+
*
18+
* @param string|null $storeId
19+
* @return array
20+
* @throws NoSuchEntityException
21+
*/
22+
public function generate(string $storeId = null): array;
23+
}

Block/Adminhtml/System/Config/Form/Attention.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Attention extends \Magefan\Community\Block\Adminhtml\System\Config\Form\In
2525
public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
2626
{
2727
return '<div style="padding:10px;background-color:#ffe5e5;border:1px solid #ddd;margin-bottom:7px;">
28-
<strong>Attention!</strong> Once you change and save the "Web Container" or "Google Analytics 4" settings,
28+
<strong>Attention!</strong> Once you change and save the "Web/Server Container", "Google Analytics 4" or "Google Ads" settings,
2929
please don\'t forget to scroll down to the "Export Container" section
3030
and click the "Generate JSON Container & Download File" button to export container data.
3131
After you save the file, <a target="_blank" title="Create GTM tags" href="https://magefan.com/blog/add-google-tag-manager-to-magento-2#5-create-gtm-tags">
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form;
10+
11+
class ExportServerContainerButton extends ExportWebContainerButton
12+
{
13+
/**
14+
* @var string
15+
*/
16+
protected $conteinerType = 'server';
17+
18+
/**
19+
* @return string
20+
*/
21+
public function getContainerGenerateUrl()
22+
{
23+
return $this->getUrl(
24+
'mfgoogletagmanagerextra/serverContainer/generate',
25+
[
26+
'store_id' => (int)$this->getRequest()->getParam('store') ?: null,
27+
'website_id' => (int)$this->getRequest()->getParam('website') ?: null
28+
]
29+
);
30+
}
31+
}

Block/Adminhtml/System/Config/Form/Button.php renamed to Block/Adminhtml/System/Config/Form/ExportWebContainerButton.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,19 @@
1111
use Magento\Config\Block\System\Config\Form\Field;
1212
use Magento\Framework\Data\Form\Element\AbstractElement;
1313

14-
class Button extends Field
14+
class ExportWebContainerButton extends Field
1515
{
16-
public const BUTTON_TEMPLATE = 'system/config/button/button.phtml';
17-
1816
/**
19-
* Set template to itself
17+
* Path to template file in theme.
2018
*
21-
* @return $this
19+
* @var string
2220
*/
23-
protected function _prepareLayout(): Button
24-
{
25-
parent::_prepareLayout();
26-
if (!$this->getTemplate()) {
27-
$this->setTemplate(static::BUTTON_TEMPLATE);
28-
}
29-
return $this;
30-
}
21+
protected $_template = 'Magefan_GoogleTagManager::system/config/button/export-container-button.phtml';
22+
23+
/**
24+
* @var string
25+
*/
26+
protected $conteinerType = 'web';
3127

3228
/**
3329
* Render button
@@ -52,4 +48,26 @@ protected function _getElementHtml(AbstractElement $element): string
5248
{
5349
return $this->_toHtml();
5450
}
51+
52+
/**
53+
* @return string
54+
*/
55+
public function getConteinderType()
56+
{
57+
return $this->conteinerType;
58+
}
59+
60+
/**
61+
* @return string
62+
*/
63+
public function getContainerGenerateUrl()
64+
{
65+
return $this->getUrl(
66+
'mfgoogletagmanager/webContainer/generate',
67+
[
68+
'store_id' => (int)$this->getRequest()->getParam('store') ?: null,
69+
'website_id' => (int)$this->getRequest()->getParam('website') ?: null
70+
]
71+
);
72+
}
5573
}

Block/Adminhtml/System/Config/Form/Info.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Info extends \Magefan\Community\Block\Adminhtml\System\Config\Form\Info
1818
protected function getModuleUrl(): string
1919
{
2020
return 'https://mage' .
21-
'fan.com/magento2-extensions?utm_source=gtm_config&utm_medium=link&utm_campaign=regular';
21+
'fan.com/magento-2-google-tag-manager?utm_source=gtm_config&utm_medium=link&utm_campaign=regular';
2222
}
2323

2424
/**
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form;
10+
11+
class InfoGoogleAds extends InfoPlan
12+
{
13+
14+
/**
15+
* @return string
16+
*/
17+
protected function getMinPlan(): string
18+
{
19+
return 'Plus';
20+
}
21+
22+
/**
23+
* @return string
24+
*/
25+
protected function getSectionId(): string
26+
{
27+
return 'mfgoogletagmanager_ads';
28+
}
29+
30+
/**
31+
* @return string
32+
*/
33+
protected function getText(): string
34+
{
35+
return 'Google Ads options are available in <strong>Plus or Extra</strong> plans only.';
36+
}
37+
38+
39+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form;
10+
11+
class InfoMeasurementProtocol extends InfoPlan
12+
{
13+
14+
/**
15+
* @return string
16+
*/
17+
protected function getMinPlan(): string
18+
{
19+
return 'Extra';
20+
}
21+
22+
/**
23+
* @return string
24+
*/
25+
protected function getSectionId(): string
26+
{
27+
return 'mfgoogletagmanager_analytics_measurement_protocol';
28+
}
29+
30+
/**
31+
* @return string
32+
*/
33+
protected function getText(): string
34+
{
35+
return 'GA4 Measurement Protocol option is available in <strong>Extra</strong> plan only.';
36+
}
37+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form;
10+
11+
abstract class InfoPlan extends \Magefan\Community\Block\Adminhtml\System\Config\Form\Info
12+
{
13+
/**
14+
* @return string
15+
*/
16+
protected abstract function getMinPlan(): string;
17+
18+
/**
19+
* @return string
20+
*/
21+
protected abstract function getSectionId(): string;
22+
23+
/**
24+
* @return string
25+
*/
26+
protected abstract function getText(): string;
27+
28+
29+
/**
30+
* Return info block html
31+
* @param \Magento\Framework\Data\Form\Element\AbstractElement $element
32+
* @return string
33+
*/
34+
public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
35+
{
36+
if ($this->getModuleVersion->execute($this->getModuleName() . $this->getMinPlan())) {
37+
return '';
38+
}
39+
40+
$html = '';
41+
$html .= '<div style="padding:10px;background-color:#f8f8f8;border:1px solid #ddd;margin-bottom:7px;">';
42+
$html .= $this->getText() . ' <a style="color: #ef672f; text-decoration: underline;" href="https://magefan.com/magento-2-google-tag-manager/pricing?utm_source=gtm_config&utm_medium=link&utm_campaign=regular" target="_blank">Read more</a>.';
43+
$html .= '</div>';
44+
45+
$html .= '<script>
46+
require(["jquery", "Magento_Ui/js/modal/alert", "domReady!"], function($, alert){
47+
setInterval(function(){
48+
var $plusSection = $("#' . $this->getSectionId() . '-state").parent(".section-config");
49+
$plusSection.find(".use-default").css("visibility", "hidden");
50+
$plusSection.find("input,select").each(function(){
51+
$(this).attr("readonly", "readonly");
52+
$(this).removeAttr("disabled");
53+
if ($(this).data("gtmdisabled")) return;
54+
$(this).data("gtmdisabled", 1);
55+
$(this).click(function(){
56+
alert({
57+
title: "You cannot change this option.",
58+
content: "' .
59+
(
60+
($this->getMinPlan() == 'Extra')
61+
? 'This option is available in <strong>Extra</strong> plan only.'
62+
: 'This option is available in <strong>Plus or Extra</strong> plans only.'
63+
)
64+
. '",
65+
buttons: [{
66+
text: "Upgrade Plan Now",
67+
class: "action primary accept",
68+
click: function () {
69+
window.open("https://magefan.com/magento-2-google-tag-manager/pricing?utm_source=gtm_config&utm_medium=link&utm_campaign=regular");
70+
}
71+
}]
72+
});
73+
});
74+
});
75+
}, 1000);
76+
});
77+
</script>';
78+
79+
return $html;
80+
}
81+
82+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form;
10+
11+
class InfoServerContainer extends InfoPlan
12+
{
13+
14+
/**
15+
* @return string
16+
*/
17+
protected function getMinPlan(): string
18+
{
19+
return 'Extra';
20+
}
21+
22+
/**
23+
* @return string
24+
*/
25+
protected function getSectionId(): string
26+
{
27+
return 'mfgoogletagmanager_server_container';
28+
}
29+
30+
/**
31+
* @return string
32+
*/
33+
protected function getText(): string
34+
{
35+
return 'Server Container option is available in <strong>Extra</strong> plan only.';
36+
}
37+
38+
}

Controller/Adminhtml/Container/Generate.php renamed to Controller/Adminhtml/ContainerGenerate.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
declare(strict_types=1);
88

9-
namespace Magefan\GoogleTagManager\Controller\Adminhtml\Container;
9+
namespace Magefan\GoogleTagManager\Controller\Adminhtml;
1010

1111
use Magento\Backend\App\Action;
1212
use Magento\Backend\App\Action\Context;
@@ -20,10 +20,10 @@
2020
use Magento\Framework\Stdlib\DateTime\DateTime;
2121
use Psr\Log\LoggerInterface;
2222
use Magefan\GoogleTagManager\Model\Config;
23-
use Magefan\GoogleTagManager\Model\Container;
23+
use Magefan\GoogleTagManager\Api\ContainerInterface as Container;
2424
use Magento\Store\Model\StoreManagerInterface;
2525

26-
class Generate extends Action implements HttpGetActionInterface
26+
class ContainerGenerate extends Action implements HttpGetActionInterface
2727
{
2828
/**
2929
* Authorization level of a basic admin session
@@ -86,16 +86,17 @@ class Generate extends Action implements HttpGetActionInterface
8686
* @param StoreManagerInterface $storeManager
8787
*/
8888
public function __construct(
89-
Context $context,
90-
RawFactory $resultRawFactory,
91-
FileFactory $fileFactory,
92-
DateTime $dateTime,
93-
LoggerInterface $logger,
94-
RedirectInterface $redirect,
95-
Config $config,
96-
Container $container,
89+
Context $context,
90+
RawFactory $resultRawFactory,
91+
FileFactory $fileFactory,
92+
DateTime $dateTime,
93+
LoggerInterface $logger,
94+
RedirectInterface $redirect,
95+
Config $config,
96+
Container $container,
9797
StoreManagerInterface $storeManager
98-
) {
98+
)
99+
{
99100
$this->resultRawFactory = $resultRawFactory;
100101
$this->fileFactory = $fileFactory;
101102
$this->dateTime = $dateTime;
@@ -153,4 +154,4 @@ public function execute()
153154
return $resultRedirect->setPath($this->redirect->getRefererUrl());
154155
}
155156
}
156-
}
157+
}

0 commit comments

Comments
 (0)