PluginProbe ʕ •ᴥ•ʔ
reCaptcha by BestWebSoft / 1.80
reCaptcha by BestWebSoft v1.80
1.79 1.80 1.82 1.83 1.84 1.85 1.86 1.87 trunk 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.10 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.18 1.19 1.20 1.21 1.22 1.23 1.24 1.25 1.26 1.27 1.28 1.29 1.30 1.31 1.32 1.33 1.34 1.35 1.36 1.37 1.38 1.39 1.40 1.41 1.42 1.43 1.44 1.45 1.46 1.47 1.48 1.49 1.50 1.51 1.52 1.53 1.54 1.55 1.56 1.57 1.58 1.59 1.60 1.61 1.62 1.63 1.64 1.65 1.66 1.67 1.68 1.70 1.71 1.72 1.73 1.74 1.75 1.78
google-captcha / google-captcha.php
google-captcha Last commit date
bws_menu 1 year ago css 1 year ago includes 1 year ago js 1 year ago languages 1 year ago google-captcha.php 1 year ago readme.txt 1 year ago screenshot-1.png 1 year ago screenshot-2.png 1 year ago screenshot-3.png 1 year ago screenshot-4.png 1 year ago screenshot-5.png 1 year ago screenshot-6.png 1 year ago screenshot-7.png 1 year ago screenshot-8.png 1 year ago
google-captcha.php
1477 lines
1 <?php
2 /**
3 Plugin Name: reCaptcha by BestWebSoft
4 Plugin URI: https://bestwebsoft.com/products/wordpress/plugins/google-captcha/
5 Description: Protect WordPress website forms from spam entries with Google Captcha (reCaptcha).
6 Author: BestWebSoft
7 Text Domain: google-captcha
8 Domain Path: /languages
9 Version: 1.80
10 Author URI: https://bestwebsoft.com/
11 License: GPLv3 or later
12 */
13
14 /**
15 © Copyright 2022 BestWebSoft ( https://support.bestwebsoft.com )
16
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License, version 2, as
19 published by the Free Software Foundation.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 */
30
31 if ( ! defined( 'ABSPATH' ) ) exit;
32
33 require_once dirname( __FILE__ ) . '/includes/forms.php';
34
35 if ( ! function_exists( 'gglcptch_admin_menu' ) ) {
36 /**
37 * Add menu page
38 */
39 function gglcptch_admin_menu() {
40 global $submenu, $wp_version, $gglcptch_plugin_info;
41
42 if ( ! is_plugin_active( 'google-captcha-pro/google-captcha-pro.php' ) ) {
43 $settings_page = add_menu_page(
44 __( 'reCaptcha Settings', 'google-captcha' ),
45 'reCaptcha',
46 'manage_options',
47 'google-captcha.php',
48 'gglcptch_add_settings_page',
49 'none'
50 );
51
52 add_submenu_page(
53 'google-captcha.php',
54 __( 'reCaptcha Settings', 'google-captcha' ),
55 __( 'Settings', 'google-captcha' ),
56 'manage_options',
57 'google-captcha.php',
58 'gglcptch_add_settings_page'
59 );
60
61 $allowlist_page = add_submenu_page(
62 'google-captcha.php',
63 __( 'reCaptcha Allow List', 'google-captcha' ),
64 __( 'Allow List', 'google-captcha' ),
65 'manage_options',
66 'google-captcha-allowlist.php',
67 'gglcptch_add_settings_page'
68 );
69
70 add_submenu_page(
71 'google-captcha.php',
72 'BWS Panel',
73 'BWS Panel',
74 'manage_options',
75 'gglcptch-bws-panel',
76 'bws_add_menu_render'
77 );
78 if ( isset( $submenu['google-captcha.php'] ) ) {
79 $submenu['google-captcha.php'][] = array(
80 '<span style="color:#d86463"> ' . __( 'Upgrade to Pro', 'google-captcha' ) . '</span>',
81 'manage_options',
82 'https://bestwebsoft.com/products/wordpress/plugins/google-captcha/?k=b850d949ccc1239cab0da315c3c822ab&pn=109&v=' . $gglcptch_plugin_info['Version'] . '&wp_v=' . $wp_version,
83 );
84 }
85 add_action( "load-{$settings_page}", 'gglcptch_add_tabs' );
86 add_action( "load-{$allowlist_page}", 'gglcptch_add_tabs' );
87 }
88 }
89 }
90
91 if ( ! function_exists( 'gglcptch_plugins_loaded' ) ) {
92 /**
93 * Load textdomain
94 */
95 function gglcptch_plugins_loaded() {
96 load_plugin_textdomain( 'google-captcha', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
97
98 include_once ABSPATH . 'wp-admin/includes/plugin.php';
99 $is_user_logged_in = is_user_logged_in();
100
101 if ( ( is_plugin_active( 'formidable/formidable.php' ) || is_plugin_active( 'formidable-pro/formidable-pro.php' ) ) ) {
102 require_once( dirname( __FILE__ ) . '/includes/captcha-for-formidable.php' );
103 }
104
105 }
106 }
107
108 if ( ! function_exists( 'gglcptch_init' ) ) {
109 /**
110 * Main init function
111 */
112 function gglcptch_init() {
113 global $gglcptch_plugin_info, $gglcptch_options, $pagenow;
114
115 require_once dirname( __FILE__ ) . '/bws_menu/bws_include.php';
116 bws_include_init( plugin_basename( __FILE__ ) );
117
118 if ( empty( $gglcptch_plugin_info ) ) {
119 if ( ! function_exists( 'get_plugin_data' ) ) {
120 require_once ABSPATH . 'wp-admin/includes/plugin.php';
121 }
122 $gglcptch_plugin_info = get_plugin_data( __FILE__ );
123 }
124
125 /* Function check if plugin is compatible with current WP version */
126 bws_wp_min_version_check( plugin_basename( __FILE__ ), $gglcptch_plugin_info, '4.5' );
127
128 $is_user_edit_page = isset( $pagenow ) && 'user-edit.php' === $pagenow;
129 $is_admin = is_admin() && ( ! defined( 'DOING_AJAX' ) || ! $is_user_edit_page );
130
131 /* Call register settings function */
132 if ( ! $is_admin || ( isset( $_GET['page'] ) && 'google-captcha.php' === sanitize_text_field( wp_unslash( $_GET['page'] ) ) ) ) {
133 register_gglcptch_settings();
134 }
135
136 /* Add hooks */
137 if ( ! $is_admin && ! empty( $gglcptch_options['public_key'] ) && ! empty( $gglcptch_options['private_key'] ) ) {
138 gglcptch_add_actions();
139 }
140 }
141 }
142
143 if ( ! function_exists( 'gglcptch_plugin_activate' ) ) {
144 /**
145 * Activation plugin function
146 */
147 function gglcptch_plugin_activate() {
148 /* Activation function for network, check if it is a network activation - if so, run the activation function for each blog id */
149 if ( is_multisite() ) {
150 switch_to_blog( 1 );
151 register_uninstall_hook( __FILE__, 'gglcptch_delete_options' );
152 restore_current_blog();
153 } else {
154 register_uninstall_hook( __FILE__, 'gglcptch_delete_options' );
155 }
156 }
157 }
158
159 if ( ! function_exists( 'gglcptch_admin_init' ) ) {
160 /**
161 * Init for dashboard
162 */
163 function gglcptch_admin_init() {
164 global $pagenow, $bws_plugin_info, $gglcptch_plugin_info, $gglcptch_options;
165
166 if ( empty( $bws_plugin_info ) ) {
167 $bws_plugin_info = array(
168 'id' => '109',
169 'version' => $gglcptch_plugin_info['Version'],
170 );
171 }
172
173 if ( 'plugins.php' === $pagenow ) {
174 if ( empty( $gglcptch_options ) ) {
175 register_gglcptch_settings();
176 }
177
178 if ( function_exists( 'bws_plugin_banner_go_pro' ) ) {
179 bws_plugin_banner_go_pro( $gglcptch_options, $gglcptch_plugin_info, 'gglcptch', 'google-captcha', '676d9558f9786ab41d7de35335cf5c4d', '109', 'google-captcha' );
180 }
181 }
182 }
183 }
184
185 if ( ! function_exists( 'gglcptch_add_admin_script_styles' ) ) {
186 /**
187 * Add google captcha styles
188 */
189 function gglcptch_add_admin_script_styles() {
190 global $gglcptch_plugin_info;
191
192 /* Css for displaing an icon */
193 wp_enqueue_style( 'gglcptch_admin_page_stylesheet', plugins_url( 'css/admin_page.css', __FILE__ ), false, $gglcptch_plugin_info['Version'] );
194
195 if ( isset( $_REQUEST['page'] ) && ( 'google-captcha.php' === sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ) || 'google-captcha-allowlist.php' === sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ) ) ) {
196 wp_enqueue_style( 'gglcptch_stylesheet', plugins_url( 'css/style.css', __FILE__ ), array(), $gglcptch_plugin_info['Version'] );
197 wp_enqueue_script( 'gglcptch_admin_script', plugins_url( 'js/admin_script.js', __FILE__ ), array( 'jquery', 'jquery-ui-accordion' ), $gglcptch_plugin_info['Version'], true );
198
199 bws_enqueue_settings_scripts();
200 bws_plugins_include_codemirror();
201 }
202 }
203 }
204 if ( ! function_exists( 'gglcptch_add_login_styles' ) ) {
205 /**
206 * Add reCaptcha styles for login page
207 */
208 function gglcptch_add_login_styles() {
209 global $gglcptch_plugin_info, $gglcptch_options;
210
211 wp_enqueue_style( 'gglcptch_stylesheet', plugins_url( 'css/login-style.css', __FILE__ ), array(), $gglcptch_plugin_info['Version'] );
212 }
213 }
214
215 if ( ! function_exists( 'gglcptch_admin_footer' ) ) {
216 /**
217 * Add google captcha admin styles for test key
218 */
219 function gglcptch_admin_footer() {
220 global $gglcptch_plugin_info, $gglcptch_options;
221 if ( isset( $_REQUEST['page'] ) && 'google-captcha.php' === sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ) ) {
222
223 /* update $gglcptch_options */
224 register_gglcptch_settings();
225
226 $api_url = gglcptch_get_api_url();
227
228 /* for gglcptch test key */
229 if ( isset( $gglcptch_options['recaptcha_version'] ) && in_array( $gglcptch_options['recaptcha_version'], array( 'v2', 'invisible' ) ) ) {
230 $deps = ( ! empty( $gglcptch_options['disable_submit'] ) ) ? array( 'gglcptch_pre_api' ) : array( 'jquery' );
231 } else {
232 $deps = array();
233 }
234 wp_register_script( 'gglcptch_api', $api_url, $deps, $gglcptch_plugin_info['Version'], true );
235 gglcptch_add_scripts();
236 }
237 }
238 }
239
240 if ( ! function_exists( 'gglcptch_remove_dublicate_scripts' ) ) {
241 /**
242 * Remove dublicate scripts
243 */
244 function gglcptch_remove_dublicate_scripts() {
245 global $wp_scripts;
246
247 if ( ! is_object( $wp_scripts ) || empty( $wp_scripts ) ) {
248 return false;
249 }
250
251 foreach ( $wp_scripts->registered as $script_name => $args ) {
252 if ( preg_match( '|google\.com/recaptcha/api\.js|', $args->src ) && 'gglcptch_api' !== $script_name ) {
253 /* remove a previously enqueued script */
254 wp_dequeue_script( $script_name );
255 }
256 }
257 }
258 }
259
260
261 if ( ! function_exists( 'gglcptch_add_styles' ) ) {
262 /**
263 * Add google captcha styles
264 */
265 function gglcptch_add_styles() {
266 global $gglcptch_plugin_info, $gglcptch_options;
267 wp_enqueue_style( 'gglcptch', plugins_url( 'css/gglcptch.css', __FILE__ ), array(), $gglcptch_plugin_info['Version'] );
268
269 if ( defined( 'BWS_ENQUEUE_ALL_SCRIPTS' ) && BWS_ENQUEUE_ALL_SCRIPTS ) {
270 if ( ! wp_script_is( 'gglcptch_api', 'registered' ) ) {
271 $api_url = gglcptch_get_api_url();
272 if ( isset( $gglcptch_options['recaptcha_version'] ) && in_array( $gglcptch_options['recaptcha_version'], array( 'v2', 'invisible' ) ) ) {
273 $deps = ( ! empty( $gglcptch_options['disable_submit'] ) ) ? array( 'gglcptch_pre_api' ) : array( 'jquery' );
274 } else {
275 $deps = array();
276 }
277
278 wp_register_script( 'gglcptch_api', $api_url, $deps, $gglcptch_plugin_info['Version'], true );
279
280 add_action( 'wp_footer', 'gglcptch_add_scripts' );
281 if (
282 $gglcptch_options['login_form'] ||
283 $gglcptch_options['reset_pwd_form'] ||
284 $gglcptch_options['registration_form']
285 ) {
286 add_action( 'login_footer', 'gglcptch_add_scripts' );
287 }
288 }
289 }
290 }
291 }
292
293 if ( ! function_exists( 'gglcptch_add_scripts' ) ) {
294 /**
295 * Add google captcha js scripts
296 */
297 function gglcptch_add_scripts() {
298 global $gglcptch_options, $gglcptch_plugin_info;
299
300 if ( empty( $gglcptch_options ) ) {
301 register_gglcptch_settings();
302 }
303
304 if ( isset( $gglcptch_options['recaptcha_version'] ) ) {
305 gglcptch_remove_dublicate_scripts();
306 if ( ! empty( $gglcptch_options['disable_submit'] ) ) {
307 wp_enqueue_script( 'gglcptch_pre_api', plugins_url( 'js/pre-api-script.js', __FILE__ ), array( 'jquery' ), $gglcptch_plugin_info['Version'], true );
308 wp_localize_script(
309 'gglcptch_pre_api',
310 'gglcptch_pre',
311 array(
312 'messages' => array(
313 'in_progress' => __( 'Please wait until Google reCAPTCHA is loaded.', 'google-captcha' ),
314 'timeout' => __( 'Failed to load Google reCAPTCHA. Please check your internet connection and reload this page.', 'google-captcha' ),
315 ),
316 'custom_callback' => apply_filters( 'gglcptch_custom_callback', '' ),
317 )
318 );
319 }
320 }
321
322 wp_enqueue_script( 'gglcptch_script', plugins_url( 'js/script.js', __FILE__ ), array( 'jquery', 'gglcptch_api' ), $gglcptch_plugin_info['Version'], true );
323
324 do_action( 'gglcptch_custom_enqueue_script' );
325
326 $options = array(
327 'version' => $gglcptch_options['recaptcha_version'],
328 'sitekey' => $gglcptch_options['public_key'],
329 'error' => sprintf( '<strong>%s</strong>:&nbsp;%s', __( 'Warning', 'google-captcha' ), gglcptch_get_message( 'multiple_blocks' ) ),
330 'disable' => $gglcptch_options['disable_submit_button'],
331 );
332
333 if ( 'v2' === $gglcptch_options['recaptcha_version'] ) {
334 $options['theme'] = $gglcptch_options['theme_v2'];
335 }
336
337 wp_localize_script(
338 'gglcptch_script',
339 'gglcptch',
340 array(
341 'options' => $options,
342 'vars' => array(
343 'visibility' => ( 'login_footer' === current_filter() ),
344 ),
345 )
346 );
347
348 if ( $gglcptch_options['hide_badge'] ) {
349 wp_enqueue_style( 'gglcptch_hide', plugins_url( 'css/hide_badge.css', __FILE__ ), array(), $gglcptch_plugin_info['Version'] );
350 }
351 }
352 }
353
354 if ( ! function_exists( 'gglcptch_pagination_callback' ) ) {
355 /**
356 * Callback function
357 *
358 * @param string $content Content from for pagination plugin.
359 *
360 * @return string $content
361 */
362 function gglcptch_pagination_callback( $content ) {
363 $content .= "if ( typeof Recaptcha != 'undefined' || typeof grecaptcha != 'undefined' ) { gglcptch.prepare(); }";
364 return $content;
365 }
366 }
367
368
369 if ( ! function_exists( 'gglcptch_add_async_attribute' ) ) {
370 /**
371 * Add the "async" attribute to our registered script.
372 *
373 * @param string $tag Tag for link.
374 * @param string $handle Id for link.
375 *
376 * @return string $tag
377 */
378 function gglcptch_add_async_attribute( $tag, $handle ) {
379 if ( 'gglcptch_api' === $handle ) {
380 $tag = str_replace( ' src', ' data-cfasync="false" async="async" defer="defer" src', $tag );
381 }
382 return $tag;
383 }
384 }
385
386 if ( ! function_exists( 'gglcptch_create_table' ) ) {
387 /**
388 * Create table in db
389 */
390 function gglcptch_create_table() {
391 global $wpdb;
392 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
393
394 $sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}gglcptch_allowlist` (
395 `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
396 `ip` CHAR(31) NOT NULL,
397 `ip_from_int` BIGINT,
398 `ip_to_int` BIGINT,
399 `add_time` DATETIME,
400 PRIMARY KEY (`id`)
401 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
402 dbDelta( $sql );
403
404 /* add unique key */
405 if ( ! $wpdb->query( "SHOW KEYS FROM `{$wpdb->prefix}gglcptch_allowlist` WHERE Key_name='ip'" ) ) {
406 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}gglcptch_allowlist` ADD UNIQUE(`ip`);" );
407 }
408 }
409 }
410
411 if ( ! function_exists( 'register_gglcptch_settings' ) ) {
412 /**
413 * Google catpcha settings
414 */
415 function register_gglcptch_settings() {
416 global $wpdb, $gglcptch_options, $gglcptch_plugin_info;
417
418 if ( empty( $gglcptch_plugin_info ) ) {
419 if ( ! function_exists( 'get_plugin_data' ) ) {
420 require_once ABSPATH . 'wp-admin/includes/plugin.php';
421 }
422 $gglcptch_plugin_info = get_plugin_data( __FILE__ );
423 }
424
425 $plugin_db_version = '0.2';
426
427 /* Install the option defaults */
428 if ( ! get_option( 'gglcptch_options' ) ) {
429 add_option( 'gglcptch_options', gglcptch_get_default_options() );
430 }
431 /* Get options from the database */
432 $gglcptch_options = get_option( 'gglcptch_options' );
433
434 /* Update tables when update plugin and tables changes*/
435 if ( ! isset( $gglcptch_options['plugin_db_version'] ) || $gglcptch_options['plugin_db_version'] !== $plugin_db_version ) {
436
437 if ( ! isset( $gglcptch_options['plugin_db_version'] ) ) {
438 gglcptch_create_table();
439 }
440
441 /**
442 * @deprecated since 1.59
443 * @todo remove after 01.05.2021
444 */
445 if ( isset( $gglcptch_options['plugin_option_version'] ) && version_compare( $gglcptch_options['plugin_option_version'], '1.59', '<' ) ) {
446 $prefix = $wpdb->prefix . 'gglcptch_';
447 /* Renaming a table */
448 $wpdb->query( 'RENAME TABLE `' . $prefix . 'whitelist` TO `' . $prefix . 'allowlist`' );
449
450 /* Renaming old options to DB */
451 $gglcptch_options['allowlist_is_empty'] = $gglcptch_options['whitelist_is_empty'];
452 $gglcptch_options['allowlist_message'] = $gglcptch_options['whitelist_message'];
453 }
454 /* end deprecated */
455
456 $gglcptch_options['plugin_db_version'] = $plugin_db_version;
457 update_option( 'gglcptch_options', $gglcptch_options );
458 }
459
460 /* Array merge incase this version has added new options */
461 if ( ! isset( $gglcptch_options['plugin_option_version'] ) || $gglcptch_options['plugin_option_version'] !== $gglcptch_plugin_info['Version'] ) {
462 $gglcptch_options = array_merge( gglcptch_get_default_options(), $gglcptch_options );
463 $gglcptch_options['plugin_option_version'] = $gglcptch_plugin_info['Version'];
464
465 /* show pro features */
466 $gglcptch_options['hide_premium_options'] = array();
467
468 if ( is_multisite() ) {
469 switch_to_blog( 1 );
470 register_uninstall_hook( __FILE__, 'gglcptch_delete_options' );
471 restore_current_blog();
472 } else {
473 register_uninstall_hook( __FILE__, 'gglcptch_delete_options' );
474 }
475 update_option( 'gglcptch_options', $gglcptch_options );
476 }
477 }
478 }
479
480 if ( ! function_exists( 'gglcptch_get_default_options' ) ) {
481 /**
482 * Default options for plugin
483 */
484 function gglcptch_get_default_options() {
485 global $gglcptch_plugin_info;
486
487 $default_options = array(
488 'allowlist_message' => __( 'You are in the allow list', 'google-captcha' ),
489 'public_key' => '',
490 'private_key' => '',
491 'login_form' => 0,
492 'registration_form' => 0,
493 'reset_pwd_form' => 1,
494 'password_form' => 0,
495 'comments_form' => 0,
496 'contact_form' => 0,
497 'testimonials' => 0,
498 'frm_contact_form' => 0,
499 'theme_v2' => 'light',
500 'recaptcha_version' => 'v2',
501 'plugin_option_version' => $gglcptch_plugin_info['Version'],
502 'first_install' => strtotime( 'now' ),
503 'display_settings_notice' => 1,
504 'suggest_feature_banner' => 1,
505 'score_v3' => 0.5,
506 'hide_badge' => 0,
507 'disable_submit_button' => 0,
508 'use_globally' => 0,
509 'weekdays' => array( 1, 2, 3, 4, 5, 6, 7 ),
510 'all_day' => array( 1, 2, 3, 4, 5, 6, 7 ),
511 'hours' => array(),
512 );
513
514 if ( function_exists( 'get_editable_roles' ) ) {
515 foreach ( get_editable_roles() as $role => $fields ) {
516 $default_options[ $role ] = '0';
517 }
518 }
519 return $default_options;
520 }
521 }
522
523 if ( ! function_exists( 'gglcptch_plugin_status' ) ) {
524 /**
525 * Status for plugin
526 *
527 * @param array $plugins BWS plugins.
528 * @param array $all_plugins All plugins on the site.
529 * @param bool $is_network Flag for network.
530 *
531 * @return array $result
532 */
533 function gglcptch_plugin_status( $plugins, $all_plugins, $is_network ) {
534 $result = array(
535 'status' => '',
536 'plugin' => '',
537 'plugin_info' => array(),
538 );
539 foreach ( (array) $plugins as $plugin ) {
540 if ( array_key_exists( $plugin, $all_plugins ) ) {
541 if (
542 ( $is_network && is_plugin_active_for_network( $plugin ) ) ||
543 ( ! $is_network && is_plugin_active( $plugin ) )
544 ) {
545 $result['status'] = 'activated';
546 $result['plugin'] = $plugin;
547 $result['plugin_info'] = $all_plugins[ $plugin ];
548 break;
549 } else {
550 $result['status'] = 'deactivated';
551 $result['plugin'] = $plugin;
552 $result['plugin_info'] = $all_plugins[ $plugin ];
553 }
554 }
555 }
556 if ( empty( $result['status'] ) ) {
557 $result['status'] = 'not_installed';
558 }
559 return $result;
560 }
561 }
562
563 if ( ! function_exists( 'gglcptch_allowlisted_ip' ) ) {
564 /**
565 * Check IP in allow table
566 */
567 function gglcptch_allowlisted_ip() {
568 global $wpdb, $gglcptch_options;
569 $checked = false;
570 if ( empty( $gglcptch_options ) ) {
571 $gglcptch_options = get_option( 'gglcptch_options' );
572 }
573 $allowlist_exist = $wpdb->query( "SHOW TABLES LIKE '{$wpdb->prefix}gglcptch_allowlist'" );
574 if ( 1 === $allowlist_exist ) {
575 $ip = gglcptch_get_ip();
576
577 if ( ! empty( $ip ) ) {
578 $ip_int = sprintf( '%u', ip2long( $ip ) );
579 $result = $wpdb->get_var(
580 $wpdb->prepare(
581 'SELECT `id`
582 FROM `' . $wpdb->prefix . 'gglcptch_allowlist`
583 WHERE ( `ip_from_int` <= %d AND `ip_to_int` >= %d ) OR `ip` LIKE %s LIMIT 1;',
584 $ip_int,
585 $ip_int,
586 $ip
587 )
588 );
589 $checked = is_null( $result ) || ! $result ? false : true;
590 }
591 }
592 return $checked;
593 }
594 }
595
596 if ( ! function_exists( 'gglcptch_add_settings_page' ) ) {
597 /**
598 * Display settings page
599 */
600 function gglcptch_add_settings_page() {
601 global $gglcptch_plugin_info;
602 /*pls */
603 require_once dirname( __FILE__ ) . '/includes/pro_banners.php';
604 /* pls*/
605 if ( 'google-captcha.php' === sanitize_text_field( wp_unslash( $_GET['page'] ) ) ) {
606 if ( ! class_exists( 'Bws_Settings_Tabs' ) ) {
607 require_once dirname( __FILE__ ) . '/bws_menu/class-bws-settings.php';
608 }
609 require_once dirname( __FILE__ ) . '/includes/class-gglcptch-settings-tabs.php';
610 $page = new Gglcptch_Settings_Tabs( plugin_basename( __FILE__ ) );
611 if ( method_exists( $page, 'add_request_feature' ) ) {
612 $page->add_request_feature();
613 }
614 } ?>
615 <div class="wrap">
616 <?php if ( 'google-captcha.php' === sanitize_text_field( wp_unslash( $_GET['page'] ) ) ) { ?>
617 <h1><?php esc_html_e( 'reCaptcha Settings', 'google-captcha' ); ?></h1>
618 <noscript><div class="error below-h2"><p><strong><?php esc_html_e( 'Please enable JavaScript in your browser.', 'google-captcha' ); ?></strong></p></div></noscript>
619 <?php
620 if ( function_exists( 'bws_plugin_promo_banner' ) ) {
621 echo bws_plugin_promo_banner( $gglcptch_plugin_info, 'gglcptch_options', 'google-captcha', 'https://bestwebsoft.com/products/wordpress/plugins/google-captcha/?utm_source=wordpress&utm_medium=plugin_banner&utm_campaign=upgrade' );
622 }
623 ?>
624 <?php
625 $page->display_content();
626 } else {
627 require_once dirname( __FILE__ ) . '/includes/allowlist.php';
628 $page = new Gglcptch_Allowlist( plugin_basename( __FILE__ ) );
629 if ( is_object( $page ) ) {
630 $page->display_content();
631 }
632 bws_plugin_reviews_block( $gglcptch_plugin_info['Name'], 'google-captcha' );
633 }
634 ?>
635 </div>
636 <?php
637 }
638 }
639
640 if ( ! function_exists( 'gglcptch_is_recaptcha_required' ) ) {
641 /**
642 * Check the need for recaptcha for the form
643 *
644 * @param string $form_slug (Optional) Slug for form.
645 * @param bool $is_user_logged_in (Optional) Flag is user logged in.
646 *
647 * @return bool $result
648 */
649 function gglcptch_is_recaptcha_required( $form_slug = '', $is_user_logged_in = null ) {
650 global $gglcptch_options;
651
652 global $gglcptch_options;
653
654 if ( strstr( $_SERVER['REQUEST_URI'], '/jwt-auth' ) ) {
655 return false;
656 }
657
658 if ( is_null( $is_user_logged_in ) ) {
659 $is_user_logged_in = is_user_logged_in();
660 }
661
662 if ( empty( $gglcptch_options ) ) {
663 $gglcptch_options = get_option( 'gglcptch_options' );
664 if ( empty( $gglcptch_options ) ) {
665 register_gglcptch_settings();
666 }
667 }
668
669 $result = isset( $gglcptch_options[ $form_slug ] ) && (
670 ( ! empty( $gglcptch_options[ $form_slug ] ) && is_admin() && ( ! wp_doing_ajax() || ( isset( $_POST['action'] ) && 'wpforms_new_field_gglcptch' === $_POST['action'] ) ) ) ||
671 ( ! empty( $gglcptch_options[ $form_slug ] ) &&
672 ( ! $is_user_logged_in || ! gglcptch_is_hidden_for_role() )
673 )
674 );
675 return apply_filters( 'gglcptch_is_recaptcha_required', $result, $form_slug, $is_user_logged_in );
676 }
677 }
678
679 /* Checking current user role */
680 if ( ! function_exists( 'gglcptch_is_hidden_for_role' ) ) {
681 /**
682 * Check the need for recaptcha for the user role
683 */
684 function gglcptch_is_hidden_for_role() {
685 global $current_user, $gglcptch_options;
686
687 if ( ! is_user_logged_in() ) {
688 return false;
689 }
690
691 if ( ! empty( $current_user->roles[0] ) ) {
692 $role = $current_user->roles[0];
693 if ( empty( $gglcptch_options ) ) {
694 register_gglcptch_settings();
695 }
696 return ! empty( $gglcptch_options[ $role ] );
697 } else {
698 return false;
699 }
700 }
701 }
702
703 if ( ! function_exists( 'gglcptch_display' ) ) {
704 /**
705 * Display google captcha
706 *
707 * @param string $content (Optional) Post content.
708 *
709 * @return string $content
710 */
711 function gglcptch_display( $attr = array(), $content = false, $custom = false ) {
712 global $gglcptch_options, $gglcptch_count, $gglcptch_plugin_info;
713
714 if ( empty( $gglcptch_options ) ) {
715 register_gglcptch_settings();
716 }
717
718 $weekdays_flag = true;
719 if ( isset( $gglcptch_options['weekdays'] ) ) {
720 $week_day = date( 'N' );
721 $hour = date( 'G' );
722 if ( ! in_array( $week_day, $gglcptch_options['weekdays'] ) || ( ! in_array( $week_day, $gglcptch_options['all_day'] ) && ! in_array( $hour, $gglcptch_options['hours'][ $week_day ] ) ) ) {
723 $weekdays_flag = false;
724 }
725 }
726
727 if ( ! gglcptch_allowlisted_ip() && true === $weekdays_flag || ( isset( $_GET['action'] ) && 'gglcptch-test-keys' === sanitize_text_field( wp_unslash( $_GET['action'] ) ) ) ) {
728
729 if ( ! $gglcptch_count ) {
730 $gglcptch_count = 1;
731 }
732
733 if ( true === $custom ) {
734 $content .= '<div class="gglcptch gglcptch_' . $gglcptch_options['recaptcha_version'] . ' gglcptch_custom">';
735 } else {
736 $content .= '<div class="gglcptch gglcptch_' . $gglcptch_options['recaptcha_version'] . '">';
737 }
738
739 if ( $gglcptch_options['hide_badge'] && 'v2' !== $gglcptch_options['recaptcha_version'] ) {
740 $content .= sprintf(
741 '<div class="google-captcha-notice">%s<a href="https://policies.google.com/privacy" target="_blank">%s</a>%s<a href="https://policies.google.com/terms" target="_blank">%s</a>%s</div>',
742 esc_html__( 'This site is protected by reCAPTCHA and the Google ', 'google-captcha' ),
743 esc_html__( 'Privacy Policy', 'google-captcha' ),
744 esc_html__( ' and ', 'google-captcha' ),
745 esc_html__( 'Terms of Service', 'google-captcha' ),
746 esc_html__( ' apply.', 'google-captcha' )
747 );
748 }
749 if ( ! $gglcptch_options['private_key'] || ! $gglcptch_options['public_key'] ) {
750 if ( current_user_can( 'manage_options' ) ) {
751 $content .= sprintf(
752 '<strong>%s <a target="_blank" href="https://www.google.com/recaptcha/admin#list">%s</a> %s <a target="_blank" href="%s">%s</a>.</strong>',
753 esc_html__( 'To use reCaptcha you must get the keys from', 'google-captcha' ),
754 esc_html__( 'here', 'google-captcha' ),
755 esc_html__( 'and enter them on the', 'google-captcha' ),
756 esc_url( admin_url( '/admin.php?page=google-captcha.php' ) ),
757 esc_html__( 'plugin setting page', 'google-captcha' )
758 );
759 }
760 $content .= '</div>';
761 $gglcptch_count++;
762 return $content;
763 }
764
765 $api_url = gglcptch_get_api_url();
766
767 /* generating random id value in case of getting content with pagination plugin for not getting duplicate id values */
768 $id = wp_rand();
769 if ( isset( $gglcptch_options['recaptcha_version'] ) && in_array( $gglcptch_options['recaptcha_version'], array( 'v2', 'invisible' ) ) ) {
770 if ( true === $custom ) {
771 $content .= '<input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px !important; height: 40px !important; border: 1px solid #c1c1c1 !important; margin: 10px 25px !important; padding: 0px !important; resize: none !important;">';
772 }
773 $content .= '<div id="gglcptch_recaptcha_' . esc_attr( $id ) . '" class="gglcptch_recaptcha"></div>
774 <noscript>
775 <div style="width: 302px;">
776 <div style="width: 302px; height: 422px; position: relative;">
777 <div style="width: 302px; height: 422px; position: absolute;">
778 <iframe src="https://www.google.com/recaptcha/api/fallback?k=' . esc_html( $gglcptch_options['public_key'] ) . '" frameborder="0" scrolling="no" style="width: 302px; height:422px; border-style: none;"></iframe>
779 </div>
780 </div>
781 <div style="border-style: none; bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px; height: 60px; width: 300px;">
782 <input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px !important; height: 40px !important; border: 1px solid #c1c1c1 !important; margin: 10px 25px !important; padding: 0px !important; resize: none !important;">
783 </div>
784 </div>
785 </noscript>';
786
787 $deps = ( ! empty( $gglcptch_options['disable_submit'] ) ) ? array( 'gglcptch_pre_api' ) : array( 'jquery' );
788 } elseif ( isset( $gglcptch_options['recaptcha_version'] ) && 'v3' === $gglcptch_options['recaptcha_version'] ) {
789 $content .= '<input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response" /><br /><div class="gglcptch_error_text">' . esc_html__( 'The reCAPTCHA verification period has expired. Please reload the page.', 'google-captcha' ) . '</div>';
790 }
791 $content .= '</div>';
792 $gglcptch_count++;
793
794 /* register reCAPTCHA script */
795 if ( ! wp_script_is( 'gglcptch_api', 'registered' ) ) {
796
797 if ( isset( $gglcptch_options['recaptcha_version'] ) && 'v3' === $gglcptch_options['recaptcha_version'] ) {
798 wp_register_script( 'gglcptch_api', $api_url, false, null, false );
799 } else {
800 wp_register_script( 'gglcptch_api', $api_url, $deps, $gglcptch_plugin_info['Version'], true );
801 }
802 add_action( 'wp_footer', 'gglcptch_add_scripts' );
803 if (
804 $gglcptch_options['login_form'] ||
805 $gglcptch_options['reset_pwd_form'] ||
806 $gglcptch_options['registration_form']
807 ) {
808 add_action( 'login_footer', 'gglcptch_add_scripts' );
809 }
810 }
811 if (
812 ( ! isset( $_SERVER['REQUEST_URI'] ) ) ||
813 ( ! strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), '/wp-login.php' ) ) ||
814 ( '/wp-login.php?action=register' === sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) && $gglcptch_options['registration_form'] ) ||
815 ( '/wp-login.php?action=lostpassword' === sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) && $gglcptch_options['reset_pwd_form'] ) ||
816 ( '/wp-login.php' === sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) && $gglcptch_options['login_form'] ) ||
817 ( strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), '/wp-login.php' ) && strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'loggedout' ) && $gglcptch_options['login_form'] )
818 ) {
819 gglcptch_add_styles();
820 }
821 } elseif ( gglcptch_allowlisted_ip() && ! empty( $gglcptch_options['allowlist_message'] ) ) {
822 $content .= '<label class="gglcptch_allowlist_message" style="display: block;">' . esc_html( $gglcptch_options['allowlist_message'] ) . '</label>';
823 }
824
825 return $content;
826 }
827 }
828
829 if ( ! function_exists( 'gglcptch_display_custom' ) ) {
830 /**
831 * Return google captcha content for custom form
832 *
833 * @param string $content (Optional) Post content.
834 * @param string $form_slug (Optional) Form slug.
835 *
836 * @return string $content
837 */
838 function gglcptch_display_custom( $content = '', $form_slug = '' ) {
839 if ( gglcptch_is_recaptcha_required( $form_slug ) ) {
840 $content = gglcptch_display( array(), $content, true );
841 }
842
843 return $content;
844 }
845 }
846
847 if ( ! function_exists( 'gglcptch_get_api_url' ) ) {
848 /**
849 * Get the reCAPTCHA api js url that corresponds to the reCAPTCHA version.
850 */
851 function gglcptch_get_api_url() {
852 global $gglcptch_options;
853 $use_globally = $gglcptch_options['use_globally'] ? 'recaptcha.net' : 'google.com';
854
855 switch ( true ) {
856 case (
857 isset( $gglcptch_options['recaptcha_version'] ) &&
858 in_array( $gglcptch_options['recaptcha_version'], array( 'v2', 'invisible' ) )
859 ):
860 $callback = ( ! empty( $gglcptch_options['disable_submit'] ) ) ? 'onload=gglcptch_onload_callback&' : '';
861 $api_url = sprintf( 'https://www.' . $use_globally . '/recaptcha/api.js?%srender=explicit', $callback );
862 break;
863 case (
864 isset( $gglcptch_options['recaptcha_version'] ) &&
865 'v3' === $gglcptch_options['recaptcha_version']
866 ):
867 $api_url = sprintf( 'https://www.' . $use_globally . '/recaptcha/api.js?render=%s', $gglcptch_options['public_key'] );
868 break;
869 default:
870 $api_url = 'https://www.google.com/recaptcha/api/js/recaptcha_ajax.js';
871 }
872 return $api_url;
873 }
874 }
875
876 if ( ! function_exists( 'gglcptch_get_response' ) ) {
877 /**
878 * Get the reCAPTCHA response
879 *
880 * @param string $privatekey Private key for reCaptcha.
881 * @param string $remote_ip User IP.
882 *
883 * @return string $result
884 */
885 function gglcptch_get_response( $privatekey, $remote_ip ) {
886 $args = array(
887 'body' => array(
888 'secret' => $privatekey,
889 'response' => isset( $_POST['g-recaptcha-response'] ) ? sanitize_text_field( wp_unslash( $_POST['g-recaptcha-response'] ) ) : '',
890 'remoteip' => $remote_ip,
891 ),
892 'sslverify' => false,
893 );
894 $resp = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', $args );
895 return json_decode( wp_remote_retrieve_body( $resp ), true );
896 }
897 }
898
899 if ( ! function_exists( 'gglcptch_check' ) ) {
900 /**
901 * Check google captcha
902 *
903 * @param string $form (Optional) Form slug.
904 * @param bool $debug (Optional) Flag for debug mode.
905 *
906 * @return array $result
907 */
908 function gglcptch_check( $form = 'general', $debug = false ) {
909 global $gglcptch_options;
910
911 if ( 'reset_pwd_form' === $form && empty( $_REQUEST ) && empty( $_SERVER['REQUEST_URI'] ) ) {
912 $result = array(
913 'response' => true,
914 'reason' => '',
915 );
916 return $result;
917 }
918
919 $weekdays_flag = true;
920 if ( isset( $gglcptch_options['weekdays'] ) ) {
921 $week_day = date( 'N' );
922 $hour = date( 'G' );
923 if ( ! in_array( $week_day, $gglcptch_options['weekdays'] ) || ( ! in_array( $week_day, $gglcptch_options['all_day'] ) && ! in_array( $hour, $gglcptch_options['hours'][ $week_day ] ) ) ) {
924 $weekdays_flag = false;
925 }
926 }
927
928 if ( ( gglcptch_allowlisted_ip() && 'gglcptch_test' !== $form ) || false === $weekdays_flag ) {
929 $result = array(
930 'response' => true,
931 'reason' => '',
932 );
933 return $result;
934 }
935
936 if ( empty( $gglcptch_options ) ) {
937 register_gglcptch_settings();
938 }
939
940 if ( ! $gglcptch_options['public_key'] || ! $gglcptch_options['private_key'] ) {
941 $errors = new WP_Error();
942 $errors->add( 'gglcptch_error', gglcptch_get_message() );
943 return array(
944 'response' => false,
945 'reason' => 'ERROR_NO_KEYS',
946 'errors' => $errors,
947 );
948 }
949
950 $gglcptch_remote_addr = filter_var( sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ), FILTER_VALIDATE_IP );
951
952 if (
953 isset( $gglcptch_options['recaptcha_version'] ) &&
954 in_array( $gglcptch_options['recaptcha_version'], array( 'v2', 'invisible', 'v3' ) )
955 ) {
956 if ( ! isset( $_POST['g-recaptcha-response'] ) ) {
957 $result = array(
958 'response' => false,
959 'reason' => 'RECAPTCHA_NO_RESPONSE',
960 );
961 } elseif ( empty( $_POST['g-recaptcha-response'] ) ) {
962 $result = array(
963 'response' => false,
964 'reason' => 'RECAPTCHA_EMPTY_RESPONSE',
965 );
966 } else {
967 $response = gglcptch_get_response( $gglcptch_options['private_key'], $gglcptch_remote_addr );
968 if ( empty( $response ) ) {
969 $result = array(
970 'response' => false,
971 'reason' => $debug ? __( 'Response is empty', 'google-captcha' ) : 'VERIFICATION_FAILED',
972 );
973 } elseif ( isset( $response['success'] ) && ! ! $response['success'] ) {
974 if ( 'v3' === $gglcptch_options['recaptcha_version'] && $response['score'] < $gglcptch_options['score_v3'] ) {
975 $result = array(
976 'response' => false,
977 'reason' => 'RECAPTCHA_SMALL_SCORE',
978 );
979 } else {
980 $result = array(
981 'response' => true,
982 'reason' => '',
983 );
984 }
985 } else {
986 if (
987 ! $debug &&
988 (
989 in_array( 'missing-input-secret', $response['error-codes'] ) ||
990 in_array( 'invalid-input-secret', $response['error-codes'] )
991 )
992 ) {
993 $result = array(
994 'response' => false,
995 'reason' => 'ERROR_WRONG_SECRET',
996 );
997 } else {
998 $result = array(
999 'response' => false,
1000 'reason' => $debug ? $response['error-codes'] : 'VERIFICATION_FAILED',
1001 );
1002 }
1003 }
1004 }
1005 }
1006
1007 if ( ! $result['response'] ) {
1008 $result['errors'] = new WP_Error();
1009 if ( ! $debug && ! in_array( $result['reason'], array( 'ERROR_WRONG_SECRET', 'ERROR_NO_KEYS' ) ) ) {
1010 $result['errors']->add( 'gglcptch_error', gglcptch_get_message( $result['reason'] ) );
1011 }
1012 }
1013 $result = apply_filters( 'gglcptch_limit_attempts_check', $result, $form );
1014 return $result;
1015 }
1016 }
1017
1018 if ( ! function_exists( 'gglcptch_check_custom' ) ) {
1019 /**
1020 * Check google captcha for custom form
1021 *
1022 * @since 1.32
1023 * @param bool $allow (Optional) initial value wheter the previous verification is passed.
1024 * @param string $return_format (Optional) The type of variable to be returned when recaptcha is failed.
1025 * @param string $form_slug (Optional) The slug of the form to check.
1026 * Default is empty string. When specified, the reCAPTCHA check may be skipped if the form is disabled on the plugin settings page.
1027 * @return mixed $allow True if ReCapthca is successfully completed, error message string, WP_Error object or false otherwise, depending on the $return_format value.
1028 */
1029 function gglcptch_check_custom( $allow = true, $return_format = 'bool', $form_slug = '' ) {
1030
1031 if ( true !== $allow ) {
1032 return $allow;
1033 }
1034
1035 if ( gglcptch_is_recaptcha_required( $form_slug ) ) {
1036 $gglcptch_check = gglcptch_check();
1037
1038 if ( ! $gglcptch_check['response'] && 'ERROR_NO_KEYS' === $gglcptch_check['reason'] ) {
1039 return $allow;
1040 }
1041
1042 $la_result = ( ! empty( $form_slug ) ) ? gglcptch_handle_by_limit_attempts( $gglcptch_check['response'], $form_slug ) : true;
1043
1044 if ( ! $gglcptch_check['response'] || true !== $la_result ) {
1045 if ( ! in_array( $return_format, array( 'bool', 'string', 'wp_error' ) ) ) {
1046 $return_format = 'bool';
1047 }
1048
1049 switch ( $return_format ) {
1050 case 'string':
1051 $allow = '';
1052 if ( true !== $la_result ) {
1053 if ( is_wp_error( $la_result ) ) {
1054 $allow .= $la_result->get_error_message();
1055 } elseif ( is_string( $la_result ) ) {
1056 $allow .= $la_result;
1057 }
1058 }
1059 if ( ! $gglcptch_check['response'] ) {
1060 $allow .= ( ( '' !== $allow ) ? '&nbsp;' : '' ) . gglcptch_get_message();
1061 }
1062 break;
1063 case 'wp_error':
1064 $allow = new WP_Error();
1065 if ( true !== $la_result ) {
1066 if ( is_wp_error( $la_result ) ) {
1067 $allow = $la_result;
1068 } elseif ( is_string( $la_result ) ) {
1069 $allow->add( 'gglcptch_la_error', $la_result );
1070 }
1071 }
1072 if ( ! $gglcptch_check['response'] ) {
1073 $error_message = sprintf( '<strong>%s</strong>:&nbsp;%s', __( 'Error', 'google-captcha' ), gglcptch_get_message() );
1074 $allow->add( 'gglcptch_error', $error_message );
1075 }
1076 break;
1077 case 'bool':
1078 default:
1079 $allow = false;
1080 break;
1081 }
1082 }
1083 }
1084
1085 return $allow;
1086 }
1087 }
1088
1089 if ( ! function_exists( 'gglcptch_limit_attempts_check' ) ) {
1090 /**
1091 * Limit Attempts plugin check
1092 *
1093 * @param array $gglcptch_check reCaptcha response.
1094 * @param string $form Form slug.
1095 *
1096 * @return array $gglcptch_check
1097 */
1098 function gglcptch_limit_attempts_check( $gglcptch_check, $form ) {
1099
1100 $result = gglcptch_handle_by_limit_attempts( $gglcptch_check['response'], $form );
1101
1102 if ( true !== $result ) {
1103 $gglcptch_check['response'] = false;
1104 if ( 'login_form' !== $form ) {
1105 if ( is_wp_error( $result ) ) {
1106 $gglcptch_check['errors'] = $result;
1107 } elseif ( is_string( $result ) ) {
1108 $gglcptch_check['errors']->add( 'lmttmpts_error', $result );
1109 }
1110 }
1111 return $gglcptch_check;
1112 } else {
1113 if ( 'contact_form' === $form ) {
1114 $gglcptch_check['response'] = true;
1115 }
1116 return $gglcptch_check;
1117 }
1118 }
1119 }
1120
1121 /**
1122 *
1123 * @since 1.32
1124 */
1125 if ( ! function_exists( 'gglcptch_handle_by_limit_attempts' ) ) {
1126 /**
1127 * Limit Attempts plugin
1128 *
1129 * @param object $check_result reCaptcha check result.
1130 * @param string $form_slug Form slug.
1131 *
1132 * @return object $check_result
1133 */
1134 function gglcptch_handle_by_limit_attempts( $check_result, $form_slug = 'login_form' ) {
1135 global $gglcptch_forms;
1136
1137 if ( ! has_filter( 'lmtttmpts_check_ip' ) ) {
1138 return $check_result;
1139 }
1140
1141 if ( empty( $gglcptch_forms ) ) {
1142 $gglcptch_forms = gglcptch_get_forms();
1143 }
1144
1145 $la_form_slug = "{$form_slug}_captcha_check";
1146
1147 /* if reCAPTCHA answer is right */
1148 if ( true === $check_result ) {
1149 /* check if user IP is blocked in the Limit Attempts plugin lists */
1150 $check_result = apply_filters( 'lmtttmpts_check_ip', $check_result );
1151 do_action( 'lmtttmpts_form_success', $la_form_slug, gglcptch_get_ip(), array( 'form_name' => $gglcptch_forms[ $form_slug ]['form_name'] ) );
1152 } else {
1153 /* if reCAPTCHA answer is wrong */
1154 $form_data = array( 'form_name' => $gglcptch_forms[ $form_slug ]['form_name'] );
1155
1156 if ( 'login_form_captcha_check' !== $form_slug ) {
1157 $la_error = apply_filters( 'lmtttmpts_form_fail', $la_form_slug, '', $form_data );
1158 }
1159
1160 if ( ! empty( $la_error ) && $la_form_slug !== $la_error ) {
1161 if ( is_wp_error( $check_result ) ) {
1162 $check_result->add( 'gglcptch_error_lmttmpts', $la_error );
1163 } elseif ( is_string( $check_result ) ) {
1164 $check_result .= '<br />' . $la_error;
1165 } else {
1166 $check_result = $la_error;
1167 }
1168 }
1169 }
1170
1171 return $check_result;
1172 }
1173 }
1174
1175 if ( ! function_exists( 'gglcptch_get_ip' ) ) {
1176 /**
1177 * Get IP from server vars
1178 */
1179 function gglcptch_get_ip() {
1180 $ip = '';
1181 if ( isset( $_SERVER ) ) {
1182 $server_vars = array( 'HTTP_X_REAL_IP', 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR' );
1183 foreach ( $server_vars as $var ) {
1184 if ( ! empty( $_SERVER[ $var ] ) ) {
1185 if ( filter_var( sanitize_text_field( wp_unslash( $_SERVER[ $var ] ) ), FILTER_VALIDATE_IP ) ) {
1186 $ip = sanitize_text_field( wp_unslash( $_SERVER[ $var ] ) );
1187 if ( 0 < sprintf( '%u', ip2long( $ip ) ) ) {
1188 break;
1189 }
1190 } else { /* if proxy */
1191 $ip_array = explode( ',', sanitize_text_field( wp_unslash( $_SERVER[ $var ] ) ) );
1192 if ( is_array( $ip_array ) && ! empty( $ip_array ) && filter_var( $ip_array[0], FILTER_VALIDATE_IP ) ) {
1193 $ip = $ip_array[0];
1194 if ( 0 < sprintf( '%u', ip2long( $ip ) ) ) {
1195 break;
1196 }
1197 }
1198 }
1199 }
1200 }
1201 }
1202 return $ip;
1203 }
1204 }
1205
1206 if ( ! function_exists( 'gglcptch_get_message' ) ) {
1207 /**
1208 * Retrieve the message that corresponds to its message code
1209 *
1210 * @since 1.29
1211 * @param string $message_code used to switch the corresponding message.
1212 * @param boolean $echo 'false' is default. If 'false' - returns a message, if 'true' - first, echo a message and then return it.
1213 * @return string $message Returned message.
1214 */
1215 function gglcptch_get_message( $message_code = 'incorrect', $echo = false ) {
1216
1217 $message = '';
1218
1219 $messages = array(
1220 /* custom error */
1221 'RECAPTCHA_EMPTY_RESPONSE' => __( 'The reCaptcha verification failed. Please try again.', 'google-captcha' ),
1222 /* v2 error */
1223 'missing-input-secret' => __( 'Secret Key is missing.', 'google-captcha' ),
1224 'invalid-input-secret' => sprintf(
1225 '<strong>%s</strong> <a target="_blank" href="https://www.google.com/recaptcha/admin#list">%s</a> %s.',
1226 __( 'Secret Key is invalid.', 'google-captcha' ),
1227 __( 'Check your domain configurations', 'google-captcha' ),
1228 __( 'and enter it again', 'google-captcha' )
1229 ),
1230 'incorrect-captcha-sol' => __( 'User response is invalid', 'google-captcha' ),
1231 'incorrect' => __( 'The reCaptcha verification failed. Please try again.', 'google-captcha' ),
1232 'multiple_blocks' => __( 'More than one reCAPTCHA has been found in the current form. Please remove all unnecessary reCAPTCHA fields to make it work properly.', 'google-captcha' ),
1233 /* v3 error */
1234 'RECAPTCHA_SMALL_SCORE' => __( 'reCaptcha v3 test failed', 'google-captcha' ),
1235 );
1236
1237 if ( isset( $messages[ $message_code ] ) ) {
1238 $message = $messages[ $message_code ];
1239 } else {
1240 $message = $messages['incorrect'];
1241 }
1242
1243 if ( $echo ) {
1244 echo wp_kses_post( $message );
1245 }
1246
1247 return $message;
1248 }
1249 }
1250
1251 if ( ! function_exists( 'gglcptch_is_woocommerce_page' ) ) {
1252 /**
1253 * Check WC page
1254 */
1255 function gglcptch_is_woocommerce_page() {
1256 $traces = debug_backtrace();
1257
1258 foreach ( $traces as $trace ) {
1259 if ( isset( $trace['file'] ) && false !== strpos( $trace['file'], 'woocommerce' ) ) {
1260 return true;
1261 }
1262 }
1263 return false;
1264 }
1265 }
1266
1267 if ( ! function_exists( 'gglcptch_test_keys' ) ) {
1268 /**
1269 * Test reCaptcha key
1270 */
1271 function gglcptch_test_keys() {
1272 global $gglcptch_options;
1273 if ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) ) ) {
1274 header( 'Content-Type: text/html' );
1275 register_gglcptch_settings();
1276 ?>
1277 <p>
1278 <?php
1279 if ( 'invisible' === $gglcptch_options['recaptcha_version'] || 'v3' === $gglcptch_options['recaptcha_version'] ) {
1280 esc_html_e( 'Please submit "Test verification"', 'google-captcha' );
1281 } else {
1282 esc_html_e( 'Please complete the captcha and submit "Test verification"', 'google-captcha' );
1283 }
1284 ?>
1285 </p>
1286 <?php echo gglcptch_display(); ?>
1287 <p>
1288 <input type="hidden" name="gglcptch_test_keys_verification-nonce" value="<?php echo esc_attr( wp_create_nonce( 'gglcptch_test_keys_verification' ) ); ?>" />
1289 <button id="gglcptch_test_keys_verification" name="action" class="button-primary cptch_loading" value="gglcptch_test_keys_verification" disabled="disabled"><?php esc_html_e( 'Test verification', 'google-captcha' ); ?></button>
1290 </p>
1291 <?php
1292 }
1293 die();
1294 }
1295 }
1296
1297 if ( ! function_exists( 'gglcptch_test_keys_verification' ) ) {
1298 /**
1299 * Test reCaptcha verification
1300 */
1301 function gglcptch_test_keys_verification() {
1302 if ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) ) ) {
1303 $result = gglcptch_check( 'gglcptch_test', true );
1304
1305 if ( ! $result['response'] ) {
1306 if ( isset( $result['reason'] ) ) {
1307 foreach ( (array) $result['reason'] as $error ) {
1308 ?>
1309 <div class="error gglcptch-test-results"><p>
1310 <?php gglcptch_get_message( $error, true ); ?>
1311 </p></div>
1312 <?php
1313 }
1314 }
1315 } else {
1316 ?>
1317 <div class="updated gglcptch-test-results"><p><?php esc_html_e( 'The verification is successfully completed.', 'google-captcha' ); ?></p></div>
1318 <?php
1319 $gglcptch_options = get_option( 'gglcptch_options' );
1320 $gglcptch_options['keys_verified'] = true;
1321 unset( $gglcptch_options['need_keys_verified_check'] );
1322 update_option( 'gglcptch_options', $gglcptch_options );
1323 }
1324 }
1325 die();
1326 }
1327 }
1328
1329 if ( ! function_exists( 'gglcptch_action_links' ) ) {
1330 /**
1331 * Add action links
1332 *
1333 * @param array $links Action link array.
1334 * @param file $file Plugin file.
1335 * @return array $links Returned link array.
1336 */
1337 function gglcptch_action_links( $links, $file ) {
1338 if ( ! is_network_admin() ) {
1339 static $this_plugin;
1340 if ( ! $this_plugin ) {
1341 $this_plugin = plugin_basename( __FILE__ );
1342 }
1343
1344 if ( $file === $this_plugin ) {
1345 $settings_link = '<a href="admin.php?page=google-captcha.php">' . __( 'Settings', 'google-captcha' ) . '</a>';
1346 array_unshift( $links, $settings_link );
1347 }
1348 }
1349 return $links;
1350 }
1351 }
1352
1353 if ( ! function_exists( 'gglcptch_links' ) ) {
1354 /**
1355 * Add Settings and Support links
1356 *
1357 * @param array $links Action link array.
1358 * @param file $file Plugin file.
1359 * @return array $links Returned link array.
1360 */
1361 function gglcptch_links( $links, $file ) {
1362 $base = plugin_basename( __FILE__ );
1363 if ( $file === $base ) {
1364 if ( ! is_network_admin() ) {
1365 $links[] = '<a href="admin.php?page=google-captcha.php">' . __( 'Settings', 'google-captcha' ) . '</a>';
1366 }
1367 $links[] = '<a href="https://support.bestwebsoft.com/hc/en-us/sections/200538719" target="_blank">' . __( 'FAQ', 'google-captcha' ) . '</a>';
1368 $links[] = '<a href="https://support.bestwebsoft.com">' . __( 'Support', 'google-captcha' ) . '</a>';
1369 }
1370 return $links;
1371 }
1372 }
1373
1374 if ( ! function_exists( 'gglcptch_plugin_banner' ) ) {
1375 /**
1376 * Dispaly plugins banner
1377 */
1378 function gglcptch_plugin_banner() {
1379 global $hook_suffix, $gglcptch_plugin_info, $gglcptch_options;
1380 if ( 'plugins.php' === $hook_suffix ) {
1381 if ( empty( $gglcptch_options ) ) {
1382 register_gglcptch_settings();
1383 }
1384 bws_plugin_banner_to_settings( $gglcptch_plugin_info, 'gglcptch_options', 'google-captcha', 'admin.php?page=google-captcha.php' );
1385 if ( function_exists( 'bws_plugin_banner_to_promo' ) ) {
1386 bws_plugin_banner_to_promo( $gglcptch_plugin_info, 'gglcptch_options', 'google-captcha', 'admin.php?page=google-captcha.php', array( __( 'Double-Check Your reCAPTCHA Keys', 'bestwebsoft' ), __( "Ensure your keys are correctly configured for maximum protection. Click “Test Keys” button. Check your settings now!", 'bestwebsoft' ) ) );
1387 }
1388 }
1389
1390 if ( isset( $_GET['page'] ) && 'google-captcha.php' === sanitize_text_field( wp_unslash( $_GET['page'] ) ) ) {
1391 bws_plugin_suggest_feature_banner( $gglcptch_plugin_info, 'gglcptch_options', 'google-captcha' );
1392 }
1393 }
1394 }
1395
1396 if ( ! function_exists( 'gglcptch_add_tabs' ) ) {
1397 /**
1398 * Add help tab
1399 */
1400 function gglcptch_add_tabs() {
1401 $screen = get_current_screen();
1402 $args = array(
1403 'id' => 'gglcptch',
1404 'section' => '200538719',
1405 );
1406 bws_help_tab( $screen, $args );
1407 }
1408 }
1409
1410 if ( ! function_exists( 'gglcptch_delete_options' ) ) {
1411 /**
1412 * Delete option
1413 */
1414 function gglcptch_delete_options() {
1415 if ( ! function_exists( 'get_plugins' ) ) {
1416 require_once ABSPATH . 'wp-admin/includes/plugin.php';
1417 }
1418 $all_plugins = get_plugins();
1419
1420 if ( ! array_key_exists( 'google-captcha-pro/google-captcha-pro.php', $all_plugins ) ) {
1421 global $wpdb;
1422 if ( function_exists( 'is_multisite' ) && is_multisite() ) {
1423 $old_blog = $wpdb->blogid;
1424 /* Get all blog ids */
1425 $blogids = $wpdb->get_col( "SELECT `blog_id` FROM $wpdb->blogs" );
1426 foreach ( $blogids as $blog_id ) {
1427 switch_to_blog( $blog_id );
1428 $wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->prefix}gglcptch_whitelist`;" );
1429 delete_option( 'gglcptch_options' );
1430 }
1431 switch_to_blog( $old_blog );
1432 delete_site_option( 'gglcptch_options' );
1433 } else {
1434 $wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->prefix}gglcptch_whitelist`;" );
1435 delete_option( 'gglcptch_options' );
1436 }
1437 }
1438
1439 require_once dirname( __FILE__ ) . '/bws_menu/bws_include.php';
1440 bws_include_init( plugin_basename( __FILE__ ) );
1441 bws_delete_plugin( plugin_basename( __FILE__ ) );
1442 }
1443 }
1444
1445 register_activation_hook( __FILE__, 'gglcptch_plugin_activate' );
1446
1447 add_action( 'admin_menu', 'gglcptch_admin_menu' );
1448
1449 add_action( 'init', 'gglcptch_init' );
1450 add_action( 'admin_init', 'gglcptch_admin_init' );
1451
1452 add_action( 'plugins_loaded', 'gglcptch_plugins_loaded' );
1453
1454 add_action( 'admin_enqueue_scripts', 'gglcptch_add_admin_script_styles' );
1455 add_action( 'login_enqueue_scripts', 'gglcptch_add_login_styles' );
1456 add_filter( 'script_loader_tag', 'gglcptch_add_async_attribute', 10, 2 );
1457 add_action( 'admin_footer', 'gglcptch_admin_footer' );
1458 add_filter( 'pgntn_callback', 'gglcptch_pagination_callback' );
1459
1460 add_filter( 'lmtttmpts_plugin_forms', 'gglcptch_add_lmtttmpts_forms', 10, 1 );
1461
1462 add_shortcode( 'bws_google_captcha', 'gglcptch_display' );
1463 add_filter( 'widget_text', 'do_shortcode' );
1464
1465 add_filter( 'gglcptch_display_recaptcha', 'gglcptch_display_custom', 10, 2 );
1466 add_filter( 'gglcptch_verify_recaptcha', 'gglcptch_check_custom', 10, 3 );
1467
1468 add_filter( 'gglcptch_limit_attempts_check', 'gglcptch_limit_attempts_check', 10, 2 );
1469
1470 add_filter( 'plugin_action_links', 'gglcptch_action_links', 10, 2 );
1471 add_filter( 'plugin_row_meta', 'gglcptch_links', 10, 2 );
1472
1473 add_action( 'admin_notices', 'gglcptch_plugin_banner' );
1474
1475 add_action( 'wp_ajax_gglcptch-test-keys', 'gglcptch_test_keys' );
1476 add_action( 'wp_ajax_gglcptch_test_keys_verification', 'gglcptch_test_keys_verification' );
1477