@@ -35,26 +35,34 @@ class HCaptcha
35
35
* @var array
36
36
*/
37
37
protected $ verifiedResponses = [];
38
-
38
+
39
39
/**
40
40
* @var null
41
41
* lastScore
42
42
*/
43
43
protected $ lastScore = null ;
44
-
45
-
44
+
45
+ /**
46
+ * Whether to use hCaptcha or not.
47
+ *
48
+ * @var bool
49
+ */
50
+ protected $ enabled ;
51
+
46
52
/**
47
53
* HCaptcha.
48
54
*
49
55
* @param string $secret
50
56
* @param string $sitekey
51
57
* @param array $options
58
+ * @param bool $enabled
52
59
*/
53
- public function __construct ($ secret , $ sitekey , $ options = [])
60
+ public function __construct ($ secret , $ sitekey , $ options = [], $ enabled = true )
54
61
{
55
62
$ this ->secret = $ secret ;
56
63
$ this ->sitekey = $ sitekey ;
57
64
$ this ->http = new Client ($ options );
65
+ $ this ->enabled = $ enabled ;
58
66
}
59
67
60
68
/**
@@ -66,6 +74,10 @@ public function __construct($secret, $sitekey, $options = [])
66
74
*/
67
75
public function display ($ attributes = [])
68
76
{
77
+ if (!$ this ->enabled ) {
78
+ return '' ;
79
+ }
80
+
69
81
$ attributes = $ this ->prepareAttributes ($ attributes );
70
82
return '<div ' . $ this ->buildAttributes ($ attributes ) . '></div> ' ;
71
83
}
@@ -89,6 +101,10 @@ public function displayWidget($attributes = [])
89
101
*/
90
102
public function displaySubmit ($ formIdentifier , $ text = 'submit ' , $ attributes = [])
91
103
{
104
+ if (!$ this ->enabled ) {
105
+ return sprintf ('<button%s><span>%s</span></button> ' , $ this ->buildAttributes ($ attributes ), $ text );
106
+ }
107
+
92
108
$ javascript = '' ;
93
109
if (!isset ($ attributes ['data-callback ' ])) {
94
110
$ functionName = 'onSubmit ' . str_replace (['- ' , '= ' , '\'' , '" ' , '< ' , '> ' , '` ' ], '' , $ formIdentifier );
@@ -118,6 +134,10 @@ public function displaySubmit($formIdentifier, $text = 'submit', $attributes = [
118
134
*/
119
135
public function renderJs ($ lang = null , $ callback = false , $ onLoadClass = 'onloadCallBack ' )
120
136
{
137
+ if (!$ this ->enabled ) {
138
+ return '' ;
139
+ }
140
+
121
141
return '<script src=" ' . $ this ->getJsLink ($ lang , $ callback , $ onLoadClass ) . '" async defer></script> ' . "\n" ;
122
142
}
123
143
@@ -131,11 +151,15 @@ public function renderJs($lang = null, $callback = false, $onLoadClass = 'onload
131
151
*/
132
152
public function verifyResponse ($ response , $ clientIp = null )
133
153
{
154
+ if (!$ this ->enabled ) {
155
+ return true ; // Always true if hCaptcha is disabled
156
+ }
157
+
134
158
if (empty ($ response )) {
135
159
return false ;
136
160
}
137
161
138
- // Return true if response already verfied before.
162
+ // Return true if response already verified before.
139
163
if (in_array ($ response , $ this ->verifiedResponses )) {
140
164
return true ;
141
165
}
@@ -194,6 +218,10 @@ public function verifyRequest(Request $request)
194
218
*/
195
219
public function getJsLink ($ lang = null , $ callback = false , $ onLoadClass = 'onloadCallBack ' )
196
220
{
221
+ if (!$ this ->enabled ) {
222
+ return '' ;
223
+ }
224
+
197
225
$ client_api = static ::CLIENT_API ;
198
226
$ params = [];
199
227
@@ -202,7 +230,7 @@ public function getJsLink($lang = null, $callback = false, $onLoadClass = 'onloa
202
230
203
231
return $ client_api . '? ' . http_build_query ($ params );
204
232
}
205
-
233
+
206
234
/**
207
235
* Get the score from the last successful hCaptcha verification.
208
236
*
0 commit comments