PluginProbe ʕ •ᴥ•ʔ
reCaptcha by BestWebSoft / 1.84
reCaptcha by BestWebSoft v1.84
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 10 months ago css 10 months ago includes 10 months ago js 10 months ago languages 10 months ago google-captcha.php 10 months ago readme.txt 6 months ago screenshot-1.png 10 months ago screenshot-2.png 10 months ago screenshot-3.png 10 months ago screenshot-4.png 10 months ago screenshot-5.png 10 months ago screenshot-6.png 10 months ago screenshot-7.png 10 months ago screenshot-8.png 10 months ago
google-captcha.php
1479 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.84
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 'error_message' => __( 'The reCaptcha verification failed. Please try again.', 'google-captcha' ),
490 'empty_error_message' => __( 'The reCaptcha verification failed. Please try again.', 'google-captcha' ),
491 'public_key' => '',
492 'private_key' => '',
493 'login_form' => 0,
494 'registration_form' => 0,
495 'reset_pwd_form' => 1,
496 'password_form' => 0,
497 'comments_form' => 0,
498 'contact_form' => 0,
499 'testimonials' => 0,
500 'frm_contact_form' => 0,
501 'theme_v2' => 'light',
502 'recaptcha_version' => 'v2',
503 'plugin_option_version' => $gglcptch_plugin_info['Version'],
504 'first_install' => strtotime( 'now' ),
505 'display_settings_notice' => 1,
506 'suggest_feature_banner' => 1,
507 'score_v3' => 0.5,
508 'hide_badge' => 0,
509 'disable_submit_button' => 0,
510 'use_globally' => 0,
511 'weekdays' => array( 1, 2, 3, 4, 5, 6, 7 ),
512 'all_day' => array( 1, 2, 3, 4, 5, 6, 7 ),
513 'hours' => array(),
514 );
515
516 if ( function_exists( 'get_editable_roles' ) ) {
517 foreach ( get_editable_roles() as $role => $fields ) {
518 $default_options[ $role ] = '0';
519 }
520 }
521 return $default_options;
522 }
523 }
524
525 if ( ! function_exists( 'gglcptch_plugin_status' ) ) {
526 /**
527 * Status for plugin
528 *
529 * @param array $plugins BWS plugins.
530 * @param array $all_plugins All plugins on the site.
531 * @param bool $is_network Flag for network.
532 *
533 * @return array $result
534 */
535 function gglcptch_plugin_status( $plugins, $all_plugins, $is_network ) {
536 $result = array(
537 'status' => '',
538 'plugin' => '',
539 'plugin_info' => array(),
540 );
541 foreach ( (array) $plugins as $plugin ) {
542 if ( array_key_exists( $plugin, $all_plugins ) ) {
543 if (
544 ( $is_network && is_plugin_active_for_network( $plugin ) ) ||
545 ( ! $is_network && is_plugin_active( $plugin ) )
546 ) {
547 $result['status'] = 'activated';
548 $result['plugin'] = $plugin;
549 $result['plugin_info'] = $all_plugins[ $plugin ];
550 break;
551 } else {
552 $result['status'] = 'deactivated';
553 $result['plugin'] = $plugin;
554 $result['plugin_info'] = $all_plugins[ $plugin ];
555 }
556 }
557 }
558 if ( empty( $result['status'] ) ) {
559 $result['status'] = 'not_installed';
560 }
561 return $result;
562 }
563 }
564
565 if ( ! function_exists( 'gglcptch_allowlisted_ip' ) ) {
566 /**
567 * Check IP in allow table
568 */
569 function gglcptch_allowlisted_ip() {
570 global $wpdb, $gglcptch_options;
571 $checked = false;
572 if ( empty( $gglcptch_options ) ) {
573 $gglcptch_options = get_option( 'gglcptch_options' );
574 }
575 $allowlist_exist = $wpdb->query( "SHOW TABLES LIKE '{$wpdb->prefix}gglcptch_allowlist'" );
576 if ( 1 === $allowlist_exist ) {
577 $ip = gglcptch_get_ip();
578
579 if ( ! empty( $ip ) ) {
580 $ip_int = sprintf( '%u', ip2long( $ip ) );
581 $result = $wpdb->get_var(
582 $wpdb->prepare(
583 'SELECT `id`
584 FROM `' . $wpdb->prefix . 'gglcptch_allowlist`
585 WHERE ( `ip_from_int` <= %d AND `ip_to_int` >= %d ) OR `ip` LIKE %s LIMIT 1;',
586 $ip_int,
587 $ip_int,
588 $ip
589 )
590 );
591 $checked = is_null( $result ) || ! $result ? false : true;
592 }
593 }
594 return $checked;
595 }
596 }
597
598 if ( ! function_exists( 'gglcptch_add_settings_page' ) ) {
599 /**
600 * Display settings page
601 */
602 function gglcptch_add_settings_page() {
603 global $gglcptch_plugin_info;
604 /*pls */
605 require_once dirname( __FILE__ ) . '/includes/pro_banners.php';
606 /* pls*/
607 if ( 'google-captcha.php' === sanitize_text_field( wp_unslash( $_GET['page'] ) ) ) {
608 if ( ! class_exists( 'Bws_Settings_Tabs' ) ) {
609 require_once dirname( __FILE__ ) . '/bws_menu/class-bws-settings.php';
610 }
611 require_once dirname( __FILE__ ) . '/includes/class-gglcptch-settings-tabs.php';
612 $page = new Gglcptch_Settings_Tabs( plugin_basename( __FILE__ ) );
613 if ( method_exists( $page, 'add_request_feature' ) ) {
614 $page->add_request_feature();
615 }
616 } ?>
617 <div class="wrap">
618 <?php if ( 'google-captcha.php' === sanitize_text_field( wp_unslash( $_GET['page'] ) ) ) { ?>
619 <h1><?php esc_html_e( 'reCaptcha Settings', 'google-captcha' ); ?></h1>
620 <noscript><div class="error below-h2"><p><strong><?php esc_html_e( 'Please enable JavaScript in your browser.', 'google-captcha' ); ?></strong></p></div></noscript>
621 <?php
622 if ( function_exists( 'bws_plugin_promo_banner' ) ) {
623 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' );
624 }
625 ?>
626 <?php
627 $page->display_content();
628 } else {
629 require_once dirname( __FILE__ ) . '/includes/allowlist.php';
630 $page = new Gglcptch_Allowlist( plugin_basename( __FILE__ ) );
631 if ( is_object( $page ) ) {
632 $page->display_content();
633 }
634 bws_plugin_reviews_block( $gglcptch_plugin_info['Name'], 'google-captcha' );
635 }
636 ?>
637 </div>
638 <?php
639 }
640 }
641
642 if ( ! function_exists( 'gglcptch_is_recaptcha_required' ) ) {
643 /**
644 * Check the need for recaptcha for the form
645 *
646 * @param string $form_slug (Optional) Slug for form.
647 * @param bool $is_user_logged_in (Optional) Flag is user logged in.
648 *
649 * @return bool $result
650 */
651 function gglcptch_is_recaptcha_required( $form_slug = '', $is_user_logged_in = null ) {
652 global $gglcptch_options;
653
654 global $gglcptch_options;
655
656 if ( strstr( $_SERVER['REQUEST_URI'], '/jwt-auth' ) ) {
657 return false;
658 }
659
660 if ( is_null( $is_user_logged_in ) ) {
661 $is_user_logged_in = is_user_logged_in();
662 }
663
664 if ( empty( $gglcptch_options ) ) {
665 $gglcptch_options = get_option( 'gglcptch_options' );
666 if ( empty( $gglcptch_options ) ) {
667 register_gglcptch_settings();
668 }
669 }
670
671 $result = isset( $gglcptch_options[ $form_slug ] ) && (
672 ( ! empty( $gglcptch_options[ $form_slug ] ) && is_admin() && ( ! wp_doing_ajax() || ( isset( $_POST['action'] ) && 'wpforms_new_field_gglcptch' === $_POST['action'] ) ) ) ||
673 ( ! empty( $gglcptch_options[ $form_slug ] ) &&
674 ( ! $is_user_logged_in || ! gglcptch_is_hidden_for_role() )
675 )
676 );
677 return apply_filters( 'gglcptch_is_recaptcha_required', $result, $form_slug, $is_user_logged_in );
678 }
679 }
680
681 /* Checking current user role */
682 if ( ! function_exists( 'gglcptch_is_hidden_for_role' ) ) {
683 /**
684 * Check the need for recaptcha for the user role
685 */
686 function gglcptch_is_hidden_for_role() {
687 global $current_user, $gglcptch_options;
688
689 if ( ! is_user_logged_in() ) {
690 return false;
691 }
692
693 if ( ! empty( $current_user->roles[0] ) ) {
694 $role = $current_user->roles[0];
695 if ( empty( $gglcptch_options ) ) {
696 register_gglcptch_settings();
697 }
698 return ! empty( $gglcptch_options[ $role ] );
699 } else {
700 return false;
701 }
702 }
703 }
704
705 if ( ! function_exists( 'gglcptch_display' ) ) {
706 /**
707 * Display google captcha
708 *
709 * @param string $content (Optional) Post content.
710 *
711 * @return string $content
712 */
713 function gglcptch_display( $attr = array(), $content = false, $custom = false ) {
714 global $gglcptch_options, $gglcptch_count, $gglcptch_plugin_info;
715
716 if ( empty( $gglcptch_options ) ) {
717 register_gglcptch_settings();
718 }
719
720 $weekdays_flag = true;
721 if ( isset( $gglcptch_options['weekdays'] ) ) {
722 $week_day = date( 'N' );
723 $hour = date( 'G' );
724 if ( ! in_array( $week_day, $gglcptch_options['weekdays'] ) || ( ! in_array( $week_day, $gglcptch_options['all_day'] ) && ! in_array( $hour, $gglcptch_options['hours'][ $week_day ] ) ) ) {
725 $weekdays_flag = false;
726 }
727 }
728
729 if ( ! gglcptch_allowlisted_ip() && true === $weekdays_flag || ( isset( $_GET['action'] ) && 'gglcptch-test-keys' === sanitize_text_field( wp_unslash( $_GET['action'] ) ) ) ) {
730
731 if ( ! $gglcptch_count ) {
732 $gglcptch_count = 1;
733 }
734
735 if ( true === $custom ) {
736 $content .= '<div class="gglcptch gglcptch_' . $gglcptch_options['recaptcha_version'] . ' gglcptch_custom">';
737 } else {
738 $content .= '<div class="gglcptch gglcptch_' . $gglcptch_options['recaptcha_version'] . '">';
739 }
740
741 if ( $gglcptch_options['hide_badge'] && 'v2' !== $gglcptch_options['recaptcha_version'] ) {
742 $content .= sprintf(
743 '<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>',
744 esc_html__( 'This site is protected by reCAPTCHA and the Google ', 'google-captcha' ),
745 esc_html__( 'Privacy Policy', 'google-captcha' ),
746 esc_html__( ' and ', 'google-captcha' ),
747 esc_html__( 'Terms of Service', 'google-captcha' ),
748 esc_html__( ' apply.', 'google-captcha' )
749 );
750 }
751 if ( ! $gglcptch_options['private_key'] || ! $gglcptch_options['public_key'] ) {
752 if ( current_user_can( 'manage_options' ) ) {
753 $content .= sprintf(
754 '<strong>%s <a target="_blank" href="https://www.google.com/recaptcha/admin#list">%s</a> %s <a target="_blank" href="%s">%s</a>.</strong>',
755 esc_html__( 'To use reCaptcha you must get the keys from', 'google-captcha' ),
756 esc_html__( 'here', 'google-captcha' ),
757 esc_html__( 'and enter them on the', 'google-captcha' ),
758 esc_url( admin_url( '/admin.php?page=google-captcha.php' ) ),
759 esc_html__( 'plugin setting page', 'google-captcha' )
760 );
761 }
762 $content .= '</div>';
763 $gglcptch_count++;
764 return $content;
765 }
766
767 $api_url = gglcptch_get_api_url();
768
769 /* generating random id value in case of getting content with pagination plugin for not getting duplicate id values */
770 $id = wp_rand();
771 if ( isset( $gglcptch_options['recaptcha_version'] ) && in_array( $gglcptch_options['recaptcha_version'], array( 'v2', 'invisible' ) ) ) {
772 if ( true === $custom ) {
773 $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;">';
774 }
775 $content .= '<div id="gglcptch_recaptcha_' . esc_attr( $id ) . '" class="gglcptch_recaptcha"></div>
776 <noscript>
777 <div style="width: 302px;">
778 <div style="width: 302px; height: 422px; position: relative;">
779 <div style="width: 302px; height: 422px; position: absolute;">
780 <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>
781 </div>
782 </div>
783 <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;">
784 <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;">
785 </div>
786 </div>
787 </noscript>';
788
789 $deps = ( ! empty( $gglcptch_options['disable_submit'] ) ) ? array( 'gglcptch_pre_api' ) : array( 'jquery' );
790 } elseif ( isset( $gglcptch_options['recaptcha_version'] ) && 'v3' === $gglcptch_options['recaptcha_version'] ) {
791 $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>';
792 }
793 $content .= '</div>';
794 $gglcptch_count++;
795
796 /* register reCAPTCHA script */
797 if ( ! wp_script_is( 'gglcptch_api', 'registered' ) ) {
798
799 if ( isset( $gglcptch_options['recaptcha_version'] ) && 'v3' === $gglcptch_options['recaptcha_version'] ) {
800 wp_register_script( 'gglcptch_api', $api_url, false, null, false );
801 } else {
802 wp_register_script( 'gglcptch_api', $api_url, $deps, $gglcptch_plugin_info['Version'], true );
803 }
804 add_action( 'wp_footer', 'gglcptch_add_scripts' );
805 if (
806 $gglcptch_options['login_form'] ||
807 $gglcptch_options['reset_pwd_form'] ||
808 $gglcptch_options['registration_form']
809 ) {
810 add_action( 'login_footer', 'gglcptch_add_scripts' );
811 }
812 }
813 if (
814 ( ! isset( $_SERVER['REQUEST_URI'] ) ) ||
815 ( ! strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), '/wp-login.php' ) ) ||
816 ( '/wp-login.php?action=register' === sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) && $gglcptch_options['registration_form'] ) ||
817 ( '/wp-login.php?action=lostpassword' === sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) && $gglcptch_options['reset_pwd_form'] ) ||
818 ( '/wp-login.php' === sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) && $gglcptch_options['login_form'] ) ||
819 ( 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'] )
820 ) {
821 gglcptch_add_styles();
822 }
823 } elseif ( gglcptch_allowlisted_ip() && ! empty( $gglcptch_options['allowlist_message'] ) ) {
824 $content .= '<label class="gglcptch_allowlist_message" style="display: block;">' . esc_html( $gglcptch_options['allowlist_message'] ) . '</label>';
825 }
826
827 return $content;
828 }
829 }
830
831 if ( ! function_exists( 'gglcptch_display_custom' ) ) {
832 /**
833 * Return google captcha content for custom form
834 *
835 * @param string $content (Optional) Post content.
836 * @param string $form_slug (Optional) Form slug.
837 *
838 * @return string $content
839 */
840 function gglcptch_display_custom( $content = '', $form_slug = '' ) {
841 if ( gglcptch_is_recaptcha_required( $form_slug ) ) {
842 $content = gglcptch_display( array(), $content, true );
843 }
844
845 return $content;
846 }
847 }
848
849 if ( ! function_exists( 'gglcptch_get_api_url' ) ) {
850 /**
851 * Get the reCAPTCHA api js url that corresponds to the reCAPTCHA version.
852 */
853 function gglcptch_get_api_url() {
854 global $gglcptch_options;
855 $use_globally = $gglcptch_options['use_globally'] ? 'recaptcha.net' : 'google.com';
856
857 switch ( true ) {
858 case (
859 isset( $gglcptch_options['recaptcha_version'] ) &&
860 in_array( $gglcptch_options['recaptcha_version'], array( 'v2', 'invisible' ) )
861 ):
862 $callback = ( ! empty( $gglcptch_options['disable_submit'] ) ) ? 'onload=gglcptch_onload_callback&' : '';
863 $api_url = sprintf( 'https://www.' . $use_globally . '/recaptcha/api.js?%srender=explicit', $callback );
864 break;
865 case (
866 isset( $gglcptch_options['recaptcha_version'] ) &&
867 'v3' === $gglcptch_options['recaptcha_version']
868 ):
869 $api_url = sprintf( 'https://www.' . $use_globally . '/recaptcha/api.js?render=%s', $gglcptch_options['public_key'] );
870 break;
871 default:
872 $api_url = 'https://www.google.com/recaptcha/api/js/recaptcha_ajax.js';
873 }
874 return $api_url;
875 }
876 }
877
878 if ( ! function_exists( 'gglcptch_get_response' ) ) {
879 /**
880 * Get the reCAPTCHA response
881 *
882 * @param string $privatekey Private key for reCaptcha.
883 * @param string $remote_ip User IP.
884 *
885 * @return string $result
886 */
887 function gglcptch_get_response( $privatekey, $remote_ip ) {
888 $args = array(
889 'body' => array(
890 'secret' => $privatekey,
891 'response' => isset( $_POST['g-recaptcha-response'] ) ? sanitize_text_field( wp_unslash( $_POST['g-recaptcha-response'] ) ) : '',
892 'remoteip' => $remote_ip,
893 ),
894 'sslverify' => false,
895 );
896 $resp = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', $args );
897 return json_decode( wp_remote_retrieve_body( $resp ), true );
898 }
899 }
900
901 if ( ! function_exists( 'gglcptch_check' ) ) {
902 /**
903 * Check google captcha
904 *
905 * @param string $form (Optional) Form slug.
906 * @param bool $debug (Optional) Flag for debug mode.
907 *
908 * @return array $result
909 */
910 function gglcptch_check( $form = 'general', $debug = false ) {
911 global $gglcptch_options;
912
913 if ( 'reset_pwd_form' === $form && empty( $_REQUEST ) && empty( $_SERVER['REQUEST_URI'] ) ) {
914 $result = array(
915 'response' => true,
916 'reason' => '',
917 );
918 return $result;
919 }
920
921 $weekdays_flag = true;
922 if ( isset( $gglcptch_options['weekdays'] ) ) {
923 $week_day = date( 'N' );
924 $hour = date( 'G' );
925 if ( ! in_array( $week_day, $gglcptch_options['weekdays'] ) || ( ! in_array( $week_day, $gglcptch_options['all_day'] ) && ! in_array( $hour, $gglcptch_options['hours'][ $week_day ] ) ) ) {
926 $weekdays_flag = false;
927 }
928 }
929
930 if ( ( gglcptch_allowlisted_ip() && 'gglcptch_test' !== $form ) || false === $weekdays_flag ) {
931 $result = array(
932 'response' => true,
933 'reason' => '',
934 );
935 return $result;
936 }
937
938 if ( empty( $gglcptch_options ) ) {
939 register_gglcptch_settings();
940 }
941
942 if ( ! $gglcptch_options['public_key'] || ! $gglcptch_options['private_key'] ) {
943 $errors = new WP_Error();
944 $errors->add( 'gglcptch_error', gglcptch_get_message() );
945 return array(
946 'response' => false,
947 'reason' => 'ERROR_NO_KEYS',
948 'errors' => $errors,
949 );
950 }
951
952 $gglcptch_remote_addr = filter_var( sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ), FILTER_VALIDATE_IP );
953
954 if (
955 isset( $gglcptch_options['recaptcha_version'] ) &&
956 in_array( $gglcptch_options['recaptcha_version'], array( 'v2', 'invisible', 'v3' ) )
957 ) {
958 if ( ! isset( $_POST['g-recaptcha-response'] ) ) {
959 $result = array(
960 'response' => false,
961 'reason' => 'RECAPTCHA_NO_RESPONSE',
962 );
963 } elseif ( empty( $_POST['g-recaptcha-response'] ) ) {
964 $result = array(
965 'response' => false,
966 'reason' => 'RECAPTCHA_EMPTY_RESPONSE',
967 );
968 } else {
969 $response = gglcptch_get_response( $gglcptch_options['private_key'], $gglcptch_remote_addr );
970 if ( empty( $response ) ) {
971 $result = array(
972 'response' => false,
973 'reason' => $debug ? __( 'Response is empty', 'google-captcha' ) : 'VERIFICATION_FAILED',
974 );
975 } elseif ( isset( $response['success'] ) && ! ! $response['success'] ) {
976 if ( 'v3' === $gglcptch_options['recaptcha_version'] && $response['score'] < $gglcptch_options['score_v3'] ) {
977 $result = array(
978 'response' => false,
979 'reason' => 'RECAPTCHA_SMALL_SCORE',
980 );
981 } else {
982 $result = array(
983 'response' => true,
984 'reason' => '',
985 );
986 }
987 } else {
988 if (
989 ! $debug &&
990 (
991 in_array( 'missing-input-secret', $response['error-codes'] ) ||
992 in_array( 'invalid-input-secret', $response['error-codes'] )
993 )
994 ) {
995 $result = array(
996 'response' => false,
997 'reason' => 'ERROR_WRONG_SECRET',
998 );
999 } else {
1000 $result = array(
1001 'response' => false,
1002 'reason' => $debug ? $response['error-codes'] : 'VERIFICATION_FAILED',
1003 );
1004 }
1005 }
1006 }
1007 }
1008
1009 if ( ! $result['response'] ) {
1010 $result['errors'] = new WP_Error();
1011 if ( ! $debug && ! in_array( $result['reason'], array( 'ERROR_WRONG_SECRET', 'ERROR_NO_KEYS' ) ) ) {
1012 $result['errors']->add( 'gglcptch_error', gglcptch_get_message( $result['reason'] ) );
1013 }
1014 }
1015 $result = apply_filters( 'gglcptch_limit_attempts_check', $result, $form );
1016 return $result;
1017 }
1018 }
1019
1020 if ( ! function_exists( 'gglcptch_check_custom' ) ) {
1021 /**
1022 * Check google captcha for custom form
1023 *
1024 * @since 1.32
1025 * @param bool $allow (Optional) initial value wheter the previous verification is passed.
1026 * @param string $return_format (Optional) The type of variable to be returned when recaptcha is failed.
1027 * @param string $form_slug (Optional) The slug of the form to check.
1028 * Default is empty string. When specified, the reCAPTCHA check may be skipped if the form is disabled on the plugin settings page.
1029 * @return mixed $allow True if ReCapthca is successfully completed, error message string, WP_Error object or false otherwise, depending on the $return_format value.
1030 */
1031 function gglcptch_check_custom( $allow = true, $return_format = 'bool', $form_slug = '' ) {
1032
1033 if ( true !== $allow ) {
1034 return $allow;
1035 }
1036
1037 if ( gglcptch_is_recaptcha_required( $form_slug ) ) {
1038 $gglcptch_check = gglcptch_check();
1039
1040 if ( ! $gglcptch_check['response'] && 'ERROR_NO_KEYS' === $gglcptch_check['reason'] ) {
1041 return $allow;
1042 }
1043
1044 $la_result = ( ! empty( $form_slug ) ) ? gglcptch_handle_by_limit_attempts( $gglcptch_check['response'], $form_slug ) : true;
1045
1046 if ( ! $gglcptch_check['response'] || true !== $la_result ) {
1047 if ( ! in_array( $return_format, array( 'bool', 'string', 'wp_error' ) ) ) {
1048 $return_format = 'bool';
1049 }
1050
1051 switch ( $return_format ) {
1052 case 'string':
1053 $allow = '';
1054 if ( true !== $la_result ) {
1055 if ( is_wp_error( $la_result ) ) {
1056 $allow .= $la_result->get_error_message();
1057 } elseif ( is_string( $la_result ) ) {
1058 $allow .= $la_result;
1059 }
1060 }
1061 if ( ! $gglcptch_check['response'] ) {
1062 $allow .= ( ( '' !== $allow ) ? '&nbsp;' : '' ) . gglcptch_get_message();
1063 }
1064 break;
1065 case 'wp_error':
1066 $allow = new WP_Error();
1067 if ( true !== $la_result ) {
1068 if ( is_wp_error( $la_result ) ) {
1069 $allow = $la_result;
1070 } elseif ( is_string( $la_result ) ) {
1071 $allow->add( 'gglcptch_la_error', $la_result );
1072 }
1073 }
1074 if ( ! $gglcptch_check['response'] ) {
1075 $error_message = sprintf( '<strong>%s</strong>:&nbsp;%s', __( 'Error', 'google-captcha' ), gglcptch_get_message() );
1076 $allow->add( 'gglcptch_error', $error_message );
1077 }
1078 break;
1079 case 'bool':
1080 default:
1081 $allow = false;
1082 break;
1083 }
1084 }
1085 }
1086
1087 return $allow;
1088 }
1089 }
1090
1091 if ( ! function_exists( 'gglcptch_limit_attempts_check' ) ) {
1092 /**
1093 * Limit Attempts plugin check
1094 *
1095 * @param array $gglcptch_check reCaptcha response.
1096 * @param string $form Form slug.
1097 *
1098 * @return array $gglcptch_check
1099 */
1100 function gglcptch_limit_attempts_check( $gglcptch_check, $form ) {
1101
1102 $result = gglcptch_handle_by_limit_attempts( $gglcptch_check['response'], $form );
1103
1104 if ( true !== $result ) {
1105 $gglcptch_check['response'] = false;
1106 if ( 'login_form' !== $form ) {
1107 if ( is_wp_error( $result ) ) {
1108 $gglcptch_check['errors'] = $result;
1109 } elseif ( is_string( $result ) ) {
1110 $gglcptch_check['errors']->add( 'lmttmpts_error', $result );
1111 }
1112 }
1113 return $gglcptch_check;
1114 } else {
1115 if ( 'contact_form' === $form ) {
1116 $gglcptch_check['response'] = true;
1117 }
1118 return $gglcptch_check;
1119 }
1120 }
1121 }
1122
1123 /**
1124 *
1125 * @since 1.32
1126 */
1127 if ( ! function_exists( 'gglcptch_handle_by_limit_attempts' ) ) {
1128 /**
1129 * Limit Attempts plugin
1130 *
1131 * @param object $check_result reCaptcha check result.
1132 * @param string $form_slug Form slug.
1133 *
1134 * @return object $check_result
1135 */
1136 function gglcptch_handle_by_limit_attempts( $check_result, $form_slug = 'login_form' ) {
1137 global $gglcptch_forms;
1138
1139 if ( ! has_filter( 'lmtttmpts_check_ip' ) ) {
1140 return $check_result;
1141 }
1142
1143 if ( empty( $gglcptch_forms ) ) {
1144 $gglcptch_forms = gglcptch_get_forms();
1145 }
1146
1147 $la_form_slug = "{$form_slug}_captcha_check";
1148
1149 /* if reCAPTCHA answer is right */
1150 if ( true === $check_result ) {
1151 /* check if user IP is blocked in the Limit Attempts plugin lists */
1152 $check_result = apply_filters( 'lmtttmpts_check_ip', $check_result );
1153 do_action( 'lmtttmpts_form_success', $la_form_slug, gglcptch_get_ip(), array( 'form_name' => $gglcptch_forms[ $form_slug ]['form_name'] ) );
1154 } else {
1155 /* if reCAPTCHA answer is wrong */
1156 $form_data = array( 'form_name' => $gglcptch_forms[ $form_slug ]['form_name'] );
1157
1158 if ( 'login_form_captcha_check' !== $form_slug ) {
1159 $la_error = apply_filters( 'lmtttmpts_form_fail', $la_form_slug, '', $form_data );
1160 }
1161
1162 if ( ! empty( $la_error ) && $la_form_slug !== $la_error ) {
1163 if ( is_wp_error( $check_result ) ) {
1164 $check_result->add( 'gglcptch_error_lmttmpts', $la_error );
1165 } elseif ( is_string( $check_result ) ) {
1166 $check_result .= '<br />' . $la_error;
1167 } else {
1168 $check_result = $la_error;
1169 }
1170 }
1171 }
1172
1173 return $check_result;
1174 }
1175 }
1176
1177 if ( ! function_exists( 'gglcptch_get_ip' ) ) {
1178 /**
1179 * Get IP from server vars
1180 */
1181 function gglcptch_get_ip() {
1182 $ip = '';
1183 if ( isset( $_SERVER ) ) {
1184 $server_vars = array( 'HTTP_X_REAL_IP', 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR' );
1185 foreach ( $server_vars as $var ) {
1186 if ( ! empty( $_SERVER[ $var ] ) ) {
1187 if ( filter_var( sanitize_text_field( wp_unslash( $_SERVER[ $var ] ) ), FILTER_VALIDATE_IP ) ) {
1188 $ip = sanitize_text_field( wp_unslash( $_SERVER[ $var ] ) );
1189 if ( 0 < sprintf( '%u', ip2long( $ip ) ) ) {
1190 break;
1191 }
1192 } else { /* if proxy */
1193 $ip_array = explode( ',', sanitize_text_field( wp_unslash( $_SERVER[ $var ] ) ) );
1194 if ( is_array( $ip_array ) && ! empty( $ip_array ) && filter_var( $ip_array[0], FILTER_VALIDATE_IP ) ) {
1195 $ip = $ip_array[0];
1196 if ( 0 < sprintf( '%u', ip2long( $ip ) ) ) {
1197 break;
1198 }
1199 }
1200 }
1201 }
1202 }
1203 }
1204 return $ip;
1205 }
1206 }
1207
1208 if ( ! function_exists( 'gglcptch_get_message' ) ) {
1209 /**
1210 * Retrieve the message that corresponds to its message code
1211 *
1212 * @since 1.29
1213 * @param string $message_code used to switch the corresponding message.
1214 * @param boolean $echo 'false' is default. If 'false' - returns a message, if 'true' - first, echo a message and then return it.
1215 * @return string $message Returned message.
1216 */
1217 function gglcptch_get_message( $message_code = 'incorrect', $echo = false ) {
1218 global $gglcptch_options;
1219 $message = '';
1220
1221 $messages = array(
1222 /* custom error */
1223 'RECAPTCHA_EMPTY_RESPONSE' => isset( $gglcptch_options['empty_error_message'] ) ? $gglcptch_options['empty_error_message'] : __( 'The reCaptcha verification failed. Please try again.', 'google-captcha' ),
1224 /* v2 error */
1225 'missing-input-secret' => __( 'Secret Key is missing.', 'google-captcha' ),
1226 'invalid-input-secret' => sprintf(
1227 '<strong>%s</strong> <a target="_blank" href="https://www.google.com/recaptcha/admin#list">%s</a> %s.',
1228 __( 'Secret Key is invalid.', 'google-captcha' ),
1229 __( 'Check your domain configurations', 'google-captcha' ),
1230 __( 'and enter it again', 'google-captcha' )
1231 ),
1232 'incorrect-captcha-sol' => __( 'User response is invalid', 'google-captcha' ),
1233 'incorrect' => isset( $gglcptch_options['error_message'] ) ? $gglcptch_options['error_message'] : __( 'The reCaptcha verification failed. Please try again.', 'google-captcha' ),
1234 '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' ),
1235 /* v3 error */
1236 'RECAPTCHA_SMALL_SCORE' => __( 'reCaptcha v3 test failed', 'google-captcha' ),
1237 );
1238
1239 if ( isset( $messages[ $message_code ] ) ) {
1240 $message = $messages[ $message_code ];
1241 } else {
1242 $message = $messages['incorrect'];
1243 }
1244
1245 if ( $echo ) {
1246 echo wp_kses_post( $message );
1247 }
1248
1249 return $message;
1250 }
1251 }
1252
1253 if ( ! function_exists( 'gglcptch_is_woocommerce_page' ) ) {
1254 /**
1255 * Check WC page
1256 */
1257 function gglcptch_is_woocommerce_page() {
1258 $traces = debug_backtrace();
1259
1260 foreach ( $traces as $trace ) {
1261 if ( isset( $trace['file'] ) && false !== strpos( $trace['file'], 'woocommerce' ) ) {
1262 return true;
1263 }
1264 }
1265 return false;
1266 }
1267 }
1268
1269 if ( ! function_exists( 'gglcptch_test_keys' ) ) {
1270 /**
1271 * Test reCaptcha key
1272 */
1273 function gglcptch_test_keys() {
1274 global $gglcptch_options;
1275 if ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) ) ) {
1276 header( 'Content-Type: text/html' );
1277 register_gglcptch_settings();
1278 ?>
1279 <p>
1280 <?php
1281 if ( 'invisible' === $gglcptch_options['recaptcha_version'] || 'v3' === $gglcptch_options['recaptcha_version'] ) {
1282 esc_html_e( 'Please submit "Test verification"', 'google-captcha' );
1283 } else {
1284 esc_html_e( 'Please complete the captcha and submit "Test verification"', 'google-captcha' );
1285 }
1286 ?>
1287 </p>
1288 <?php echo gglcptch_display(); ?>
1289 <p>
1290 <input type="hidden" name="gglcptch_test_keys_verification-nonce" value="<?php echo esc_attr( wp_create_nonce( 'gglcptch_test_keys_verification' ) ); ?>" />
1291 <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>
1292 </p>
1293 <?php
1294 }
1295 die();
1296 }
1297 }
1298
1299 if ( ! function_exists( 'gglcptch_test_keys_verification' ) ) {
1300 /**
1301 * Test reCaptcha verification
1302 */
1303 function gglcptch_test_keys_verification() {
1304 if ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) ) ) {
1305 $result = gglcptch_check( 'gglcptch_test', true );
1306
1307 if ( ! $result['response'] ) {
1308 if ( isset( $result['reason'] ) ) {
1309 foreach ( (array) $result['reason'] as $error ) {
1310 ?>
1311 <div class="error gglcptch-test-results"><p>
1312 <?php gglcptch_get_message( $error, true ); ?>
1313 </p></div>
1314 <?php
1315 }
1316 }
1317 } else {
1318 ?>
1319 <div class="updated gglcptch-test-results"><p><?php esc_html_e( 'The verification is successfully completed.', 'google-captcha' ); ?></p></div>
1320 <?php
1321 $gglcptch_options = get_option( 'gglcptch_options' );
1322 $gglcptch_options['keys_verified'] = true;
1323 unset( $gglcptch_options['need_keys_verified_check'] );
1324 update_option( 'gglcptch_options', $gglcptch_options );
1325 }
1326 }
1327 die();
1328 }
1329 }
1330
1331 if ( ! function_exists( 'gglcptch_action_links' ) ) {
1332 /**
1333 * Add action links
1334 *
1335 * @param array $links Action link array.
1336 * @param file $file Plugin file.
1337 * @return array $links Returned link array.
1338 */
1339 function gglcptch_action_links( $links, $file ) {
1340 if ( ! is_network_admin() ) {
1341 static $this_plugin;
1342 if ( ! $this_plugin ) {
1343 $this_plugin = plugin_basename( __FILE__ );
1344 }
1345
1346 if ( $file === $this_plugin ) {
1347 $settings_link = '<a href="admin.php?page=google-captcha.php">' . __( 'Settings', 'google-captcha' ) . '</a>';
1348 array_unshift( $links, $settings_link );
1349 }
1350 }
1351 return $links;
1352 }
1353 }
1354
1355 if ( ! function_exists( 'gglcptch_links' ) ) {
1356 /**
1357 * Add Settings and Support links
1358 *
1359 * @param array $links Action link array.
1360 * @param file $file Plugin file.
1361 * @return array $links Returned link array.
1362 */
1363 function gglcptch_links( $links, $file ) {
1364 $base = plugin_basename( __FILE__ );
1365 if ( $file === $base ) {
1366 if ( ! is_network_admin() ) {
1367 $links[] = '<a href="admin.php?page=google-captcha.php">' . __( 'Settings', 'google-captcha' ) . '</a>';
1368 }
1369 $links[] = '<a href="https://support.bestwebsoft.com/hc/en-us/sections/200538719" target="_blank">' . __( 'FAQ', 'google-captcha' ) . '</a>';
1370 $links[] = '<a href="https://support.bestwebsoft.com">' . __( 'Support', 'google-captcha' ) . '</a>';
1371 }
1372 return $links;
1373 }
1374 }
1375
1376 if ( ! function_exists( 'gglcptch_plugin_banner' ) ) {
1377 /**
1378 * Dispaly plugins banner
1379 */
1380 function gglcptch_plugin_banner() {
1381 global $hook_suffix, $gglcptch_plugin_info, $gglcptch_options;
1382 if ( 'plugins.php' === $hook_suffix ) {
1383 if ( empty( $gglcptch_options ) ) {
1384 register_gglcptch_settings();
1385 }
1386 bws_plugin_banner_to_settings( $gglcptch_plugin_info, 'gglcptch_options', 'google-captcha', 'admin.php?page=google-captcha.php' );
1387 if ( function_exists( 'bws_plugin_banner_to_promo' ) ) {
1388 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' ) ) );
1389 }
1390 }
1391
1392 if ( isset( $_GET['page'] ) && 'google-captcha.php' === sanitize_text_field( wp_unslash( $_GET['page'] ) ) ) {
1393 bws_plugin_suggest_feature_banner( $gglcptch_plugin_info, 'gglcptch_options', 'google-captcha' );
1394 }
1395 }
1396 }
1397
1398 if ( ! function_exists( 'gglcptch_add_tabs' ) ) {
1399 /**
1400 * Add help tab
1401 */
1402 function gglcptch_add_tabs() {
1403 $screen = get_current_screen();
1404 $args = array(
1405 'id' => 'gglcptch',
1406 'section' => '200538719',
1407 );
1408 bws_help_tab( $screen, $args );
1409 }
1410 }
1411
1412 if ( ! function_exists( 'gglcptch_delete_options' ) ) {
1413 /**
1414 * Delete option
1415 */
1416 function gglcptch_delete_options() {
1417 if ( ! function_exists( 'get_plugins' ) ) {
1418 require_once ABSPATH . 'wp-admin/includes/plugin.php';
1419 }
1420 $all_plugins = get_plugins();
1421
1422 if ( ! array_key_exists( 'google-captcha-pro/google-captcha-pro.php', $all_plugins ) ) {
1423 global $wpdb;
1424 if ( function_exists( 'is_multisite' ) && is_multisite() ) {
1425 $old_blog = $wpdb->blogid;
1426 /* Get all blog ids */
1427 $blogids = $wpdb->get_col( "SELECT `blog_id` FROM $wpdb->blogs" );
1428 foreach ( $blogids as $blog_id ) {
1429 switch_to_blog( $blog_id );
1430 $wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->prefix}gglcptch_whitelist`;" );
1431 delete_option( 'gglcptch_options' );
1432 }
1433 switch_to_blog( $old_blog );
1434 delete_site_option( 'gglcptch_options' );
1435 } else {
1436 $wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->prefix}gglcptch_whitelist`;" );
1437 delete_option( 'gglcptch_options' );
1438 }
1439 }
1440
1441 require_once dirname( __FILE__ ) . '/bws_menu/bws_include.php';
1442 bws_include_init( plugin_basename( __FILE__ ) );
1443 bws_delete_plugin( plugin_basename( __FILE__ ) );
1444 }
1445 }
1446
1447 register_activation_hook( __FILE__, 'gglcptch_plugin_activate' );
1448
1449 add_action( 'admin_menu', 'gglcptch_admin_menu' );
1450
1451 add_action( 'init', 'gglcptch_init' );
1452 add_action( 'admin_init', 'gglcptch_admin_init' );
1453
1454 add_action( 'plugins_loaded', 'gglcptch_plugins_loaded' );
1455
1456 add_action( 'admin_enqueue_scripts', 'gglcptch_add_admin_script_styles' );
1457 add_action( 'login_enqueue_scripts', 'gglcptch_add_login_styles' );
1458 add_filter( 'script_loader_tag', 'gglcptch_add_async_attribute', 10, 2 );
1459 add_action( 'admin_footer', 'gglcptch_admin_footer' );
1460 add_filter( 'pgntn_callback', 'gglcptch_pagination_callback' );
1461
1462 add_filter( 'lmtttmpts_plugin_forms', 'gglcptch_add_lmtttmpts_forms', 10, 1 );
1463
1464 add_shortcode( 'bws_google_captcha', 'gglcptch_display' );
1465 add_filter( 'widget_text', 'do_shortcode' );
1466
1467 add_filter( 'gglcptch_display_recaptcha', 'gglcptch_display_custom', 10, 2 );
1468 add_filter( 'gglcptch_verify_recaptcha', 'gglcptch_check_custom', 10, 3 );
1469
1470 add_filter( 'gglcptch_limit_attempts_check', 'gglcptch_limit_attempts_check', 10, 2 );
1471
1472 add_filter( 'plugin_action_links', 'gglcptch_action_links', 10, 2 );
1473 add_filter( 'plugin_row_meta', 'gglcptch_links', 10, 2 );
1474
1475 add_action( 'admin_notices', 'gglcptch_plugin_banner' );
1476
1477 add_action( 'wp_ajax_gglcptch-test-keys', 'gglcptch_test_keys' );
1478 add_action( 'wp_ajax_gglcptch_test_keys_verification', 'gglcptch_test_keys_verification' );
1479