class-recaptcha.php
22.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
<?php
if ( ! defined( 'ABSPATH' ) )
exit; // Exit if accessed directly
/**
* @package Password Protected
* @subpackage reCAPTCHA
*
* @since 2.6
*/
class Password_Protected_reCAPTCHA {
public $options_group = 'password-protected-recaptcha';
public $options_name = 'password_protected_recaptcha';
public $tab = 'password-protected&tab=advanced';
public $settings = array();
/**
* Constructor
*
* @since 2.6
*
* @internal public. This class should only be instantiated once by the plugin.
*/
public function __construct() {
$options = get_option( $this->options_name );
if( empty($options) || !$options )
$this->settings = $this->get_default_reCAPTCHA_settings();
else
$this->settings = get_option( $this->options_name );
add_action( 'admin_init', array( $this, 'register_reCAPTCHA_settings' ), 6 );
add_action( 'password_protected_after_password_field', array( $this, 'add_recaptcha' ) );
add_filter( 'password_protected_verify_recaptcha', array( $this, 'verify_recaptcha' ) );
}
/**
* reCAPTCHA Default Settings
*
* @return array
* @since 2.6
*/
private function get_default_reCAPTCHA_settings(): array {
return array(
'enable' => 0,
'version' => 'google_recaptcha_v2',
'v2_site_key' => null,
'v3_site_key' => null,
'v2_secret_key' => null,
'v3_secret_key' => null,
'v3_score' => 0.3,
'v3_badge' => 'bottomright',
'v2_theme' => 'light'
);
}
/**
* reCAPTCHA Settings Info
*
* Displays information on the settings page for helping
* to configure Password Protected to work with Google reCAPTCHA v2 and v3.
*
* @since 2.6
*/
public function register_reCAPTCHA_settings() {
// reCAPTCHA Section
add_settings_section(
$this->options_group,
__( 'Google reCAPTCHA', 'password-protected' ),
array( $this, 'reCAPTCHA_section' ),
$this->tab
);
// Enable reCAPTCHA
add_settings_field(
'password_protected_enable_recaptcha',
__( 'Enable reCAPTCHA ', 'password-protected' ),
array( $this, 'reCAPTCHA_enable' ),
$this->tab,
$this->options_group
);
// reCAPTCHA version v2/v3
add_settings_field(
'password_protected_recaptcha_settings',
__( 'Captcha Settings', 'password-protected' ),
array( $this, 'reCAPTCHA_setting' ),
$this->tab,
$this->options_group
);
// reCAPTCHA v2/v3 sitekey
add_settings_field(
'password_protected_recaptcha_v2_site_key',
__( 'Site Key', 'password-protected' ),
array( $this, 'reCAPTCHA_site_key' ),
$this->tab,
$this->options_group
);
// reCAPTCHA v2/v3 secretkey
add_settings_field(
'password_protected_recaptcha_v2_secret_key',
__( 'Secret Key', 'password-protected' ),
array( $this, 'reCAPTCHA_secret_key' ),
$this->tab,
$this->options_group
);
// reCAPTCHA v3 score
add_settings_field(
'password_protected_recaptcha_score',
__( 'Score', 'password-protected' ),
array( $this, 'reCAPTCHA_score' ),
$this->tab,
$this->options_group
);
// reCAPTCHA v3 badgeposition
add_settings_field(
'password_protected_recaptcha_badge_position',
__( 'Badge Position', 'password-protected' ),
array( $this, 'reCAPTCHA_badge_position' ),
$this->tab,
$this->options_group
);
// reCAPTCHA v2 theme
add_settings_field(
'password_protected_recaptcha_theme',
__( 'Theme', 'password-protected' ),
array( $this, 'reCAPTCHA_theme' ),
$this->tab,
$this->options_group
);
// register settings in an array group.
register_setting( 'password-protected-advanced', $this->options_name, array( 'type' => 'array') );
}
/**
* reCAPTCHA Screen
*
*@since 2.6
*
* @return void password protected reCAPTCHA settings
*/
public static function recpatcha_screen() {
do_settings_sections( 'password-protected&tab=advanced' );
submit_button();
}
/**
* reCAPTCHA Section
*
* @return void password protected reCAPTCHA section
*/
public function reCAPTCHA_section() {
return 1;
}
/**
* ENable reCAPTCHA
*
* @since 2.6
*
* @return void password protected reCAPTCHA status field
*/
public function reCAPTCHA_enable() {
echo '<label>
<input
name="'.esc_attr( $this->options_name ) .'[enable]"
id="pp_enable_recaptcha"
type="checkbox"
value="1" ' . checked( 1, @$this->settings['enable'], false ) . '
/> ' .
__( 'Enabled', 'password-protected' ) . '
</label>';
}
/**
* reCAPTCHA Version
*
* @since 2.6
*
* @return void password protected reCAPTCHA version field
*/
public function reCAPTCHA_setting() {
echo '<label>
<input
' . checked( 'google_recaptcha_v2', $this->settings['version'], false ) . '
type="radio"
name="'.esc_attr( $this->options_name ) . '[version]"
value="google_recaptcha_v2"
/>
<span>Google reCAPTCHA Version 2</span>
</label>
<br />
<label>
<input
' . checked( 'google_recaptcha_v3', $this->settings['version'], false ) . '
type="radio"
name="'.esc_attr( $this->options_name ) . '[version]"
value="google_recaptcha_v3"
/>
<span>Google reCAPTCHA Version 3</span>
</label>';
}
/**
* reCAPTCHA Site Key
*
* @since 2.6
*
* @return void password protected v2/v3 sitekey field
*/
public function reCAPTCHA_site_key() {
echo '<div>
<input
name="'.esc_attr( $this->options_name ).'[v2_site_key]"
type="text"
id="pp_google_recaptcha_v2_site_key"
value="' . $this->settings['v2_site_key'] . '"
class="regular-text"
/>
<p class="description">
Enter Google reCAPTCHA v2 Site Key.
<a target="_blank" href="https://developers.google.com/recaptcha/intro#recaptcha-overview">
Click Here
</a>
</p>
</div>';
echo '<div>
<input
name="' . esc_attr( $this->options_name ).'[v3_site_key]"
type="text"
id="pp_google_recaptcha_v3_site_key"
value="' . $this->settings['v3_site_key'] . '"
class="regular-text"
/>
<p class="description">
Enter Google reCAPTCHA v3 Site Key.
<a target="_blank" href="https://developers.google.com/recaptcha/intro#recaptcha-overview">
Click Here
</a>
</p>
</div>';
}
/**
* reCAPTCHA Secret Key
*
* @since 2.6
*
* @return void password protected v2/v3 secretkey field
*/
public function reCAPTCHA_secret_key() {
echo '<div>
<input
name="' . esc_attr( $this->options_name ).'[v2_secret_key]"
type="text"
id="pp_google_recaptcha_v2_secret_key"
value="' . $this->settings['v2_secret_key'] . '"
class="regular-text"
/>
<p class="description">
Enter Google reCAPTCHA v2 Secret Key.
<a target="_blank" href="https://developers.google.com/recaptcha/intro#recaptcha-overview">
Click Here
</a>
</p>
</div>';
echo '<div>
<input
name="' . esc_attr( $this->options_name ).'[v3_secret_key]"
type="text"
id="pp_google_recaptcha_v3_secret_key"
value="' . $this->settings['v3_secret_key'] . '"
class="regular-text"
/>
<p class="description">
Enter Google reCAPTCHA v3 Secret Key.
<a target="_blank" href="https://developers.google.com/recaptcha/intro#recaptcha-overview">
Click Here
</a>
</p>
</div>';
}
/**
* reCAPTCHA V3 Score
*
* @since 2.6
*
* @return void password protected v3 score field
*/
public function reCAPTCHA_score() {
echo '<fieldset id="pp_google_recpatcha_v3_score">
<label>
<input
type="radio"
name="'.esc_attr( $this->options_name ).'[v3_score]"
value="0.1"
' . checked( 0.1, $this->settings['v3_score'], false ) . '
/>
<span>0.1</span>
</label>
<label>
<input
type="radio"
name="'.esc_attr( $this->options_name ).'[v3_score]"
value="0.2"
' . checked( 0.2, $this->settings['v3_score'], false ) . '
/>
<span>0.2</span>
</label>
<label>
<input
type="radio"
name="'.esc_attr( $this->options_name ).'[v3_score]"
value="0.3"
' . checked( 0.3, $this->settings['v3_score'], false ) . '
/>
<span>0.3</span>
</label>
<label>
<input
type="radio"
name="'.esc_attr( $this->options_name ).'[v3_score]"
value="0.4"
' . checked( 0.4, $this->settings['v3_score'], false ) . '
/>
<span>0.4</span>
</label>
<label>
<input
type="radio"
name="'.esc_attr( $this->options_name ).'[v3_score]"
value="0.5"
' . checked( 0.5, $this->settings['v3_score'], false ) . '
/>
<span>0.5</span>
</label>
<p class="description">Select Google Version 3 Score.</p>
</fieldset>';
}
/**
* reCAPTCHA V3 Badge Position
*
* @since 2.6
*
* @return void password protected v3 badgeposition field
*/
public function reCAPTCHA_badge_position() {
echo '<fieldset id="pp_google_recpatcha_v3_badge">
<label>
<input
type="radio"
name="'.esc_attr( $this->options_name ).'[v3_badge]"
value="inline"
' . checked( 'inline', $this->settings['v3_badge'], false ) . '
/>
<span>Inline</span>
</label>
<label>
<input
type="radio"
name="'.esc_attr( $this->options_name ).'[v3_badge]"
value="bottomleft"
' . checked( 'bottomleft', $this->settings['v3_badge'], false ) . '
/>
<span>Bottom - Left</span>
</label>
<label>
<input
type="radio"
name="'.esc_attr( $this->options_name ).'[v3_badge]"
value="bottomright"
' . checked( 'bottomright', $this->settings['v3_badge'], false ) . '
/>
<span>Bottom - Right</span>
</label>
</fieldset>';
}
/**
* reCAPTCHA V2 Theme
*
* @since 2.6
*
* @return void password protected v2 theme field
*/
public function reCAPTCHA_theme() {
echo '<label>
<input
checked="checked"
type="radio"
name="'.esc_attr( $this->options_name ).'[v2_theme]"
value="light"
' . checked( 'light', $this->settings['v2_theme'], false ) . '
/>
<span>Light</span>
</label>
<br />
<label>
<input
type="radio"
name="'.esc_attr( $this->options_name ).'[v2_theme]"
value="dark"
' . checked( 'dark', $this->settings['v2_theme'], false ) . '
/>
<span>Dark</span>
</label>
<p class="description">Select Google reCAPTCHA Version 2 Theme.</p>';
}
/**
* Add reCAPTCHA on Password Protected Form
*
* @since 2.6
*
* @return void password protected reCAPTCHA v2 OR v3
*/
public function add_recaptcha() {
if( ! @$this->settings['enable'] )
return; //recpatcha is disabled
if( $this->settings['version'] === 'google_recaptcha_v2' )
$this->display_recaptcha_v2();
if( $this->settings['version'] === 'google_recaptcha_v3' )
$this->display_recaptcha_v3();
}
/**
* Diaplay reCAPTCHA V2
*
* @since 2.6
*
* @return void password protected reCAPTCHA v2 field
*/
public function display_recaptcha_v2() {
wp_enqueue_style( 'pp-recaptcha-style', plugin_dir_url( __DIR__ ) . "assets/css/recaptcha.css", array(), '2.6.2' );
wp_enqueue_script( 'pp-recaptcha-api-v2', esc_url( 'https://www.google.com/recaptcha/api.js' ), array(), null );
echo '<div
class="g-recaptcha"
data-sitekey="' . esc_attr( $this->settings['v2_site_key'] ) .'"
data-theme="' . esc_attr( $this->settings['v2_theme'] ) . '"
>
</div>';
}
/**
* Diaplay reCAPTCHA V3
*
* @since 2.6
*
* @return void password protected reCAPTCHA v3 field
*/
public function display_recaptcha_v3() {
$grecaptcha_v3_site_key = isset( $this->settings['v3_site_key'] ) ? esc_attr( $this->settings['v3_site_key'] ) : '';
$grecaptcha_v3_badge = isset( $this->settings['v3_badge'] ) ? esc_attr( $this->settings['v3_badge'] ) : 'bottomright';
$script = <<<EOT
if('function' !== typeof pprecaptcha) {
function pprecaptcha() {
grecaptcha.ready(function() {
[].forEach.call(document.querySelectorAll('.pp-g-recaptcha'), function(el) {
const action = el.getAttribute('data-action');
const form = el.form;
form.addEventListener('submit', function(e) {
e.preventDefault();
grecaptcha.execute('$grecaptcha_v3_site_key', {action: action}).then(function(token) {
el.setAttribute('value', token);
const button = form.querySelector('[type="submit"]');
if(button) {
const input = document.createElement('input');
input.type = 'hidden';
input.name = button.getAttribute('name');
input.value = button.value;
input.classList.add('pp-submit-input');
var inputEls = document.querySelectorAll('.pp-submit-input');
[].forEach.call(inputEls, function(inputEl) {
inputEl.remove();
});
form.appendChild(input);
}
HTMLFormElement.prototype.submit.call(form);
});
});
});
});
}
}
EOT;
wp_enqueue_script( 'recaptcha-api-v3', ( 'https://www.google.com/recaptcha/api.js?onload=pprecaptcha&render=' . esc_attr( $grecaptcha_v3_site_key ) . '&badge=' . esc_attr( $grecaptcha_v3_badge ) ), array(), null );
wp_add_inline_script( 'recaptcha-api-v3', $script ); ?>
<input type="hidden" name="g-recaptcha-response" id="g-recaptcha-response-password_protected" class="pp-g-recaptcha" data-action="password_protected">
<?php
}
/**
* Verify reCAPTCHA
* process reCAPTCHA
*
* @since 2.6
*
* @return object WP_Error
*/
public function verify_recaptcha( $errors ) {
if( ! @$this->settings['enable'] ) {
return $errors; // return errors
}
if( $this->settings['version'] === 'google_recaptcha_v2' ) {
$grecaptcha_v2_site_key = isset( $this->settings['v2_site_key'] ) ? esc_attr( $this->settings['v2_site_key'] ) : '';
$grecaptcha_v2_secret_key = isset( $this->settings['v2_secret_key'] ) ? esc_attr( $this->settings['v2_secret_key'] ) : '';
if ( empty( $grecaptcha_v2_site_key ) || empty( $grecaptcha_v2_secret_key ) ) {
$errors->add( 001, 'Google reCaptcha keys not found.' );
}
if ( isset( $_POST['g-recaptcha-response'] ) && ! empty( $_POST['g-recaptcha-response'] ) ) {
$response = wp_remote_post(
'https://www.google.com/recaptcha/api/siteverify',
array(
'body' => array(
'secret' => $grecaptcha_v2_secret_key,
'response' => sanitize_text_field($_POST['g-recaptcha-response'] ),
)
)
);
$data = wp_remote_retrieve_body( $response );
$data = json_decode( $data );
if ( isset( $data->{'error-codes'} ) && is_array( $data->{'error-codes'} ) && count( $data->{'error-codes'} ) ) {
foreach ( $data->{'error-codes'} as $index => $error_code ) {
$errors->add( $index, $error_code );
}
}
if ( isset( $data->success ) && true === $data->success ) {
return $errors;
}
}
$error_message = wp_kses( __( '<strong>ERROR:</strong> Please confirm you are not a robot.', 'password-protected' ), array( 'strong' => array() ) );
$errors->add( 'captcha_invalid', $error_message );
return $errors;
} else if( $this->settings['version'] === 'google_recaptcha_v3' ) {
$grecaptcha_v3_site_key = isset( $this->settings['v3_site_key'] ) ? esc_attr( $this->settings['v3_site_key'] ) : '';
$grecaptcha_v3_secret_key = isset( $this->settings['v3_secret_key'] ) ? esc_attr( $this->settings['v3_secret_key'] ) : '';
$grecaptcha_v3_score = isset( $this->settings['v3_score'] ) ? esc_attr( $this->settings['v3_score'] ) : '0.3';
if ( empty( $grecaptcha_v3_site_key ) || empty( $grecaptcha_v3_secret_key ) ) {
$errors->add( 001, 'Google reCaptcha keys not found.' );
}
if ( isset( $_POST['g-recaptcha-response'] ) && ! empty( $_POST['g-recaptcha-response'] ) ) {
$response = wp_remote_post(
'https://www.google.com/recaptcha/api/siteverify',
array(
'body' => array(
'secret' => $grecaptcha_v3_secret_key,
'response' => sanitize_text_field( $_POST['g-recaptcha-response'] ),
'remoteip' => self::get_ip_address(),
)
)
);
$data = wp_remote_retrieve_body( $response );
$data = json_decode( $data );
if ( isset( $data->{'error-codes'} ) && is_array( $data->{'error-codes'} ) && count( $data->{'error-codes'} ) ) {
foreach ( $data->{'error-codes'} as $index => $error_code ) {
$errors->add( $index, $error_code );
}
}
if ( isset( $data->success ) && true === $data->success ) {
$grecaptcha_v3_score = (float) $grecaptcha_v3_score;
if ( isset( $data->action ) && ( 'password_protected' === $data->action ) && isset( $data->score ) && $data->score >= $grecaptcha_v3_score ) {
return $errors;
} else {
$error_message = wp_kses( __( '<strong>ERROR:</strong> Low Score ', 'password-protected' ) . ':' . esc_html( $data->score ) , array( 'strong' => array() ) );
$errors->add( 002, $error_message );
}
}
}
return $errors;
}
}
/**
* Get IP Address
*
* @since 2.6
*
* @return string client IP address
*/
private static function get_ip_address() {
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = sanitize_text_field( $_SERVER['HTTP_CLIENT_IP'] );
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = sanitize_text_field( $_SERVER['HTTP_X_FORWARDED_FOR'] );
else if(isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = sanitize_text_field( $_SERVER['HTTP_X_FORWARDED'] );
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
$ipaddress = sanitize_text_field( $_SERVER['HTTP_FORWARDED_FOR'] );
else if(isset($_SERVER['HTTP_FORWARDED']))
$ipaddress = sanitize_text_field( $_SERVER['HTTP_FORWARDED'] );
else if(isset($_SERVER['REMOTE_ADDR']))
$ipaddress = sanitize_text_field( $_SERVER['REMOTE_ADDR'] );
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
}