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