1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2019. Volodymyr Hryvinskyi. All rights reserved. |
4
|
|
|
* @author: <mailto:[email protected]> |
5
|
|
|
* @github: <https://github.com/hryvinskyi> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Hryvinskyi\InvisibleCaptcha\Block; |
11
|
|
|
|
12
|
|
|
use Hryvinskyi\Base\Helper\Json; |
13
|
|
|
use Hryvinskyi\InvisibleCaptcha\Helper\Config\Frontend; |
14
|
|
|
use Hryvinskyi\InvisibleCaptcha\Helper\Config\General; |
15
|
|
|
use Hryvinskyi\InvisibleCaptcha\Model\LayoutSettings; |
16
|
|
|
use Magento\Framework\View\Element\Template; |
17
|
|
|
use Magento\Framework\View\Element\Template\Context; |
18
|
|
|
|
19
|
|
|
class Captcha extends Template |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
private static $widgetId = 0; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $widgetIdClass = ''; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var General |
33
|
|
|
*/ |
34
|
|
|
private $generalConfig; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Frontend |
38
|
|
|
*/ |
39
|
|
|
private $frontendConfig; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var LayoutSettings |
43
|
|
|
*/ |
44
|
|
|
private $layoutSettings; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Captcha constructor. |
48
|
|
|
* |
49
|
|
|
* @param Context $context |
50
|
|
|
* @param General $generalConfig |
51
|
|
|
* @param Frontend $frontendConfig |
52
|
|
|
* @param LayoutSettings $layoutSettings |
53
|
|
|
* @param array $data |
54
|
|
|
*/ |
55
|
|
|
public function __construct( |
56
|
|
|
Context $context, |
57
|
|
|
General $generalConfig, |
58
|
|
|
Frontend $frontendConfig, |
59
|
|
|
LayoutSettings $layoutSettings, |
60
|
|
|
array $data = [] |
61
|
|
|
) { |
62
|
|
|
parent::__construct($context, $data); |
63
|
|
|
|
64
|
|
|
$this->generalConfig = $generalConfig; |
65
|
|
|
$this->frontendConfig = $frontendConfig; |
66
|
|
|
$this->layoutSettings = $layoutSettings; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Constructor |
71
|
|
|
*/ |
72
|
|
|
public function _construct() |
73
|
|
|
{ |
74
|
|
|
parent::_construct(); |
75
|
|
|
|
76
|
|
|
$this->widgetIdClass = 'invisible-captcha-container-' . ++self::$widgetId; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getWidgetId(): string |
83
|
|
|
{ |
84
|
|
|
return $this->widgetIdClass; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
*/ |
90
|
|
|
public function getJsLayout() |
91
|
|
|
{ |
92
|
|
|
$layout = Json::decode(parent::getJsLayout()); |
93
|
|
|
|
94
|
|
|
if ($this->frontendConfig->hasEnabled() && $this->isModuleOn()) { |
95
|
|
|
$layout['components']['invisible-captcha']['config'] = $this->layoutSettings->getCaptchaSettings(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ( |
99
|
|
|
(!$this->frontendConfig->hasEnabled() || !$this->isModuleOn()) |
100
|
|
|
&& isset($layout['components']['invisible-captcha']) |
101
|
|
|
) { |
102
|
|
|
unset($layout['components']['invisible-captcha']); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return Json::encode($layout); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
public function isModuleOn(): bool |
112
|
|
|
{ |
113
|
|
|
return $this->generalConfig->hasEnabled(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function toHtml() |
120
|
|
|
{ |
121
|
|
|
if (!$this->isModuleOn()) { |
122
|
|
|
return ''; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return parent::toHtml(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|