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