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