Skip to content

Commit 9fd6236

Browse files
author
Alex Zeng
committed
Remove jQuery
1 parent 4afedd2 commit 9fd6236

File tree

1 file changed

+104
-37
lines changed

1 file changed

+104
-37
lines changed

src/AlexStack/GoogleRecaptchaToAnyForm/GoogleRecaptcha.php

Lines changed: 104 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,56 +9,28 @@
99
class GoogleRecaptcha
1010
{
1111

12-
public static function show($site_key,$after_field_id='Form_ContactForm_Comment', $debug='no_debug', $extra_class="mt-4 mb-4", $please_tick_msg="Please tick the I'm not robot checkbox") {
13-
$debug_alert = ($debug=='no_debug') ? 'false' : 'true';
14-
$str = <<<EOF
15-
<!-- Start of the Google Recaptcha code -->
16-
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
17-
18-
<script src='https://www.google.com/recaptcha/api.js'></script>
19-
<script>
20-
21-
// Display google recaptcha
22-
$('#$after_field_id').parent().after('<div class="g-recaptcha $extra_class" data-sitekey="$site_key"></div> ');
23-
24-
// Validate google recaptcha before submit the form
25-
$('#$after_field_id').closest('form').submit(function(e) {
26-
var response = grecaptcha.getResponse();
27-
//recaptcha failed validation
28-
if(response.length == 0) {
29-
alert("$please_tick_msg");
30-
return $debug_alert;
31-
}
32-
return true;
33-
});
34-
35-
</script>
36-
<!-- End of the Google Recaptcha code -->
37-
EOF;
38-
return $str;
39-
}
40-
4112
/**
4213
* verify function
4314
*
4415
* @param [string] $secret_key
4516
* @param [string] $break_msg, if set, pop up as an javascript alert and exit
4617
* @return true or false
4718
*/
48-
public static function verify($secret_key, $break_msg = null) {
19+
public static function verify($secret_key, $break_msg = null)
20+
{
4921
$valid = false;
50-
if ( isset($_POST['g-recaptcha-response']) && strlen($_POST['g-recaptcha-response'])>20 ) {
22+
if (isset($_POST['g-recaptcha-response']) && strlen($_POST['g-recaptcha-response']) > 20) {
5123
$valid = Self::result($secret_key, $_POST['g-recaptcha-response']);
5224
}
53-
54-
if ( !$valid && $break_msg){
55-
if ( strlen($break_msg) < 10 ){
25+
26+
if (!$valid && $break_msg) {
27+
if (strlen($break_msg) < 10) {
5628
$break_msg = "Google Recaptcha Validation Failed!!";
5729
}
5830
echo '<script>alert("' . $break_msg . '");history.back();</script>';
5931
exit();
60-
}
61-
return $valid;
32+
}
33+
return $valid;
6234
}
6335

6436
/**
@@ -103,6 +75,101 @@ public static function result($secret_key, $g_recaptcha_response)
10375
} else {
10476
return true;
10577
}
78+
}
79+
80+
/**
81+
* show recaptcha function without jQuery
82+
*
83+
* @param string $site_key
84+
* @param string $after_field_id
85+
* @param string $debug
86+
* @param string $extra_class
87+
* @param string $please_tick_msg
88+
* @return void
89+
*/
90+
public static function show($site_key, $after_field_id = 'Form_ContactForm_Comment', $debug = 'no_debug', $extra_class = "mt-4 mb-4", $please_tick_msg = "Please tick the I'm not robot checkbox")
91+
{
92+
$debug_alert = ($debug == 'no_debug') ? 'false' : 'true';
93+
$str = <<<EOF
94+
<!-- Start of the Google Recaptcha code -->
95+
96+
<script src='https://www.google.com/recaptcha/api.js'></script>
97+
<script>
98+
99+
// Display google recaptcha
100+
var alexEL = document.getElementById('$after_field_id');
101+
alexEL.parentNode.insertAdjacentHTML('afterend', '<div class="g-recaptcha $extra_class" data-sitekey="$site_key"></div>');
102+
103+
function alexFindClosestNode(el, selector) {
104+
const matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;
106105
107-
}
106+
while (el) {
107+
if (matchesSelector.call(el, selector)) {
108+
return el;
109+
} else {
110+
el = el.parentElement;
111+
}
112+
}
113+
return null;
114+
}
115+
116+
// Validate google recaptcha before submit the form
117+
118+
alexFindClosestNode(alexEL,'form').addEventListener('submit',function(e){
119+
var response = grecaptcha.getResponse();
120+
//recaptcha failed validation
121+
if(response.length == 0) {
122+
alert("$please_tick_msg");
123+
e.preventDefault();
124+
return $debug_alert;
125+
}
126+
return true;
127+
});
128+
129+
</script>
130+
<!-- End of the Google Recaptcha code -->
131+
EOF;
132+
return $str;
133+
}
134+
135+
136+
/**
137+
* jqueryShow function
138+
*
139+
* @param string $site_key
140+
* @param string $after_field_id
141+
* @param string $debug
142+
* @param string $extra_class
143+
* @param string $please_tick_msg
144+
* @return void
145+
*/
146+
public static function jqueryShow($site_key, $after_field_id = 'Form_ContactForm_Comment', $debug = 'no_debug', $extra_class = "mt-4 mb-4", $please_tick_msg = "Please tick the I'm not robot checkbox")
147+
{
148+
$debug_alert = ($debug == 'no_debug') ? 'false' : 'true';
149+
$str = <<<EOF
150+
<!-- Start of the Google Recaptcha code -->
151+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
152+
153+
<script src='https://www.google.com/recaptcha/api.js'></script>
154+
<script>
155+
156+
// Display google recaptcha
157+
$('#$after_field_id').parent().after('<div class="g-recaptcha $extra_class" data-sitekey="$site_key"></div> ');
158+
159+
// Validate google recaptcha before submit the form
160+
$('#$after_field_id').closest('form').submit(function(e) {
161+
var response = grecaptcha.getResponse();
162+
//recaptcha failed validation
163+
if(response.length == 0) {
164+
alert("$please_tick_msg");
165+
return $debug_alert;
166+
}
167+
return true;
168+
});
169+
170+
</script>
171+
<!-- End of the Google Recaptcha code -->
172+
EOF;
173+
return $str;
174+
}
108175
}

0 commit comments

Comments
 (0)