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