@@ -82,6 +82,14 @@ class NocaptchaField extends FormField {
82
82
*/
83
83
private static $ default_size ='normal ' ;
84
84
85
+ /**
86
+ * Whether form submit events are handled directly by this module.
87
+ * If false, a function is provided that can be called by user code submit handlers.
88
+ * @var boolean
89
+ * @default true
90
+ */
91
+ private static $ default_handle_submit = true ;
92
+
85
93
/**
86
94
* Recaptcha Site Key
87
95
* Configurable via Injector config
@@ -150,6 +158,13 @@ class NocaptchaField extends FormField {
150
158
*/
151
159
protected $ minimumScore ;
152
160
161
+ /**
162
+ * Whether form submit events are handled directly by this module.
163
+ * If false, a function is provided that can be called by user code submit handlers.
164
+ * @var boolean
165
+ */
166
+ private $ handleSubmitEvents ;
167
+
153
168
/**
154
169
* Creates a new Recaptcha 2 field.
155
170
* @param string $name The internal field name, passed to forms.
@@ -165,6 +180,7 @@ public function __construct($name, $title=null, $value=null) {
165
180
$ this ->_captchaType =self ::config ()->default_type ;
166
181
$ this ->_captchaSize =self ::config ()->default_size ;
167
182
$ this ->_captchaBadge =self ::config ()->default_badge ;
183
+ $ this ->handleSubmitEvents = self ::config ()->default_handle_submit ;
168
184
}
169
185
170
186
/**
@@ -180,44 +196,71 @@ public function Field($properties=array()) {
180
196
user_error ('You must configure Nocaptcha.site_key and Nocaptcha.secret_key, you can retrieve these at https://google.com/recaptcha ' , E_USER_ERROR );
181
197
}
182
198
183
- $ form = $ this ->getForm ();
184
-
185
199
if ($ this ->config ()->get ('recaptcha_version ' ) == 2 ) {
186
- $ exemptActionsString = implode ("' , ' " , $ form ->getValidationExemptActions ());
200
+ $ this ->configureRequirementsForV2 ();
201
+ } else {
202
+ $ this ->configureRequirementsForV3 ();
203
+ }
187
204
205
+ return parent ::Field ($ properties );
206
+ }
207
+
208
+ /**
209
+ * Configure any javascript and css requirements that are specific for recaptcha v2.
210
+ */
211
+ protected function configureRequirementsForV2 ()
212
+ {
213
+ Requirements::customScript (
214
+ "(function() { \n" .
215
+ "var gr = document.createElement('script'); gr.type = 'text/javascript'; gr.async = true; \n" .
216
+ "gr.src = ('https:' == document.location.protocol ? 'https://www' : 'http://www') + " .
217
+ "'.google.com/recaptcha/api.js?render=explicit&hl= " .
218
+ Locale::getPrimaryLanguage (i18n::get_locale ()) .
219
+ "&onload=noCaptchaFieldRender'; \n" .
220
+ "var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gr, s); \n" .
221
+ "})(); \n" ,
222
+ 'NocaptchaField-lib '
223
+ );
224
+ if ($ this ->getHandleSubmitEvents ()) {
225
+ $ exemptActionsString = implode ("' , ' " , $ this ->getForm ()->getValidationExemptActions ());
188
226
Requirements::javascript ('undefinedoffset/silverstripe-nocaptcha:javascript/NocaptchaField.js ' );
189
227
Requirements::customScript (
190
228
"var _noCaptchaFields=_noCaptchaFields || [];_noCaptchaFields.push(' " .$ this ->ID ()."'); " .
191
229
"var _noCaptchaValidationExemptActions=_noCaptchaValidationExemptActions || []; " .
192
230
"_noCaptchaValidationExemptActions.push(' " . $ exemptActionsString . "'); " ,
193
231
"NocaptchaField- " . $ this ->ID ()
194
232
);
233
+ } else {
195
234
Requirements::customScript (
196
- "(function() { \n" .
197
- "var gr = document.createElement('script'); gr.type = 'text/javascript'; gr.async = true; \n" .
198
- "gr.src = ('https:' == document.location.protocol ? 'https://www' : 'http://www') + " .
199
- "'.google.com/recaptcha/api.js?render=explicit&hl= " .
200
- Locale::getPrimaryLanguage (i18n::get_locale ()) .
201
- "&onload=noCaptchaFieldRender'; \n" .
202
- "var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gr, s); \n" .
203
- "})(); \n" ,
204
- 'NocaptchaField-lib '
235
+ "var _noCaptchaFields=_noCaptchaFields || [];_noCaptchaFields.push(' " .$ this ->ID ()."'); " ,
236
+ "NocaptchaField- " . $ this ->ID ()
205
237
);
206
- } else {
207
- Requirements::javascript ('https://www.google.com/recaptcha/api.js?render= ' . urlencode ($ siteKey ) . '&onload=noCaptchaFormRender ' );
238
+ Requirements::javascript ('undefinedoffset/silverstripe-nocaptcha:javascript/NocaptchaField_noHandler_v2.js ' );
239
+ }
240
+ }
241
+
242
+ /**
243
+ * Configure any javascript and css requirements that are specific for recaptcha v3.
244
+ */
245
+ protected function configureRequirementsForV3 ()
246
+ {
247
+ Requirements::customCSS ('.nocaptcha { display: none !important; } ' , self ::class);
248
+ if ($ this ->getHandleSubmitEvents ()) {
249
+ Requirements::javascript ('https://www.google.com/recaptcha/api.js?render= ' . urlencode ($ this ->getSiteKey ()) . '&onload=noCaptchaFormRender ' );
208
250
Requirements::javascript ('undefinedoffset/silverstripe-nocaptcha:javascript/NocaptchaField_v3.js ' );
209
- Requirements::customCSS ('.nocaptcha { display: none !important; } ' , self ::class);
210
251
252
+ $ form = $ this ->getForm ();
211
253
$ helper = $ form ->getTemplateHelper ();
212
254
$ id = $ helper ->generateFormID ($ form );
213
255
214
256
Requirements::customScript (
215
257
"var _noCaptchaForms=_noCaptchaForms || [];_noCaptchaForms.push(' " . $ id . "'); " ,
216
258
'NocaptchaForm- ' . $ id
217
259
);
260
+ } else {
261
+ Requirements::javascript ('https://www.google.com/recaptcha/api.js?render= ' . urlencode ($ this ->getSiteKey ()));
262
+ Requirements::javascript ('undefinedoffset/silverstripe-nocaptcha:javascript/NocaptchaField_noHandler_v3.js ' );
218
263
}
219
-
220
- return parent ::Field ($ properties );
221
264
}
222
265
223
266
/**
@@ -294,6 +337,28 @@ public function validate($validator) {
294
337
return true ;
295
338
}
296
339
340
+ /**
341
+ * Sets whether form submit events are handled directly by this module.
342
+ *
343
+ * @param boolean $value
344
+ * @return NocaptchaField
345
+ */
346
+ public function setHandleSubmitEvents (bool $ value )
347
+ {
348
+ $ this ->handleSubmitEvents = $ value ;
349
+ return $ this ;
350
+ }
351
+
352
+ /**
353
+ * Get whether form submit events are handled directly by this module.
354
+ *
355
+ * @return boolean
356
+ */
357
+ public function getHandleSubmitEvents (): bool
358
+ {
359
+ return $ this ->handleSubmitEvents ;
360
+ }
361
+
297
362
/**
298
363
* Sets the theme for this captcha
299
364
* @param string $value Theme to set it to, currently the api supports light and dark
0 commit comments