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