reCAPTCHA - Version 1.5

Version Description

  • Support for bypassing the captcha for specific IP addresses. Props to Daniel Mann
Download this release

Release Info

Developer ash.matadeen
Plugin Icon wp plugin reCAPTCHA
Version 1.5
Comparing to
See all releases

Code changes from version 1.4.1 to 1.5

Files changed (2) hide show
  1. no-captcha.php +67 -15
  2. readme.txt +4 -1
no-captcha.php CHANGED
@@ -3,9 +3,11 @@
3
  * Plugin Name: reCAPTCHA
4
  * Plugin URI: http://ashmatadeen.com
5
  * Description: Adds Google's reCAPTCHA to WP's login form
 
 
6
  * Author: Ash Matadeen
7
  * Author URI: http://ashmatadeen.com
8
- * Version: 1.4.1
9
  */
10
 
11
  add_action( 'admin_menu', 'wr_no_captcha_menu' );
@@ -15,9 +17,8 @@ add_action( 'login_enqueue_scripts', 'wr_no_captcha_css' );
15
  add_action( 'login_form', 'wr_no_captcha_render_login_captcha' );
16
  add_filter( 'wp_authenticate_user', 'wr_no_captcha_verify_login_captcha', 10, 2 );
17
 
18
- // Specific support for WooCommerce login form
19
- // Using WooCommerce specific hooks because
20
- // WooCommerce's login form does not use the expected wp_login_form()
21
  if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
22
  add_action( 'woocommerce_login_form', 'wr_no_captcha_render_login_captcha' );
23
  add_action( 'wp_enqueue_scripts', 'wr_no_captcha_login_form_script' );
@@ -55,13 +56,21 @@ function wr_no_captcha_options_page() {
55
  ?>
56
  </form>
57
 
58
- <form method="post" action="options.php">
59
  <?php
60
  settings_fields( 'messages_section' );
61
  do_settings_sections( 'recaptcha-text-options' );
62
  submit_button();
63
  ?>
64
  </form>
 
 
 
 
 
 
 
 
65
  </div>
66
 
67
  </div>
@@ -129,14 +138,26 @@ function wr_no_captcha_display_options() {
129
  add_settings_section( 'messages_section', 'Custom error message', 'wr_no_captcha_display_recaptcha_error_message_content', 'recaptcha-text-options' );
130
  add_settings_field( 'wr_no_captcha_error_message_text', 'Custom error message text', 'wr_no_captcha_error_message_input', 'recaptcha-text-options', 'messages_section' );
131
  register_setting( 'messages_section', 'wr_no_captcha_error_message_text' );
 
 
 
 
132
  }
133
 
134
  function wr_no_captcha_display_recaptcha_error_message_content() {
135
- echo "<p>You can set your own error message here for when the bot test fails:</p>";
 
 
 
 
136
  }
137
 
138
  function wr_no_captcha_error_message_input() {
139
- echo '<input size="60" type="text" name="wr_no_captcha_error_message_text" id="wr_no_captcha_error_message_text" value="'. get_option( 'wr_no_captcha_error_message_text' ) . '" />';
 
 
 
 
140
  }
141
 
142
  function wr_no_captcha_display_recaptcha_api_content() {
@@ -144,7 +165,7 @@ function wr_no_captcha_display_recaptcha_api_content() {
144
  }
145
 
146
  function wr_no_captcha_key_input() {
147
- echo '<input type="text" name="wr_no_captcha_site_key" id="captcha_site_key" value="'. get_option( 'wr_no_captcha_site_key' ) . '" />';
148
  }
149
 
150
  function wr_no_captcha_secret_key_input() {
@@ -152,18 +173,20 @@ function wr_no_captcha_secret_key_input() {
152
  }
153
 
154
  function wr_no_captcha_login_form_script() {
155
- wp_register_script( 'no_captcha_login', 'https://www.google.com/recaptcha/api.js' );
156
- wp_enqueue_script( 'no_captcha_login' );
 
 
157
  }
158
 
159
  function wr_no_captcha_render_login_captcha() {
160
- if ( wr_no_captcha_api_keys_set() ) {
161
  echo '<div class="g-recaptcha" data-sitekey="' . get_option( 'wr_no_captcha_site_key' ) . '"></div>';
162
- require_once( plugin_dir_path( __FILE__ ) . 'noscript/noscript.php');
163
  }
164
  }
165
 
166
- function wr_no_captcha_verify_login_captcha($user, $password) {
167
  if ( isset( $_POST['g-recaptcha-response'] ) ) {
168
  $no_captcha_secret = get_option( 'wr_no_captcha_secret_key' );
169
  $response = wp_remote_get( 'https://www.google.com/recaptcha/api/siteverify?secret=' . $no_captcha_secret . '&response=' . $_POST['g-recaptcha-response'] );
@@ -173,7 +196,7 @@ function wr_no_captcha_verify_login_captcha($user, $password) {
173
  } else {
174
  return new WP_Error( 'Captcha Invalid', wr_no_captcha_get_error_message() );
175
  }
176
- } else if ( ! wr_no_captcha_api_keys_set() ) {
177
  return $user;
178
  }
179
  }
@@ -186,7 +209,7 @@ function wr_no_captcha_css() {
186
  function wr_no_captcha_get_error_message() {
187
  $custom_error = get_option( 'wr_no_captcha_error_message_text' );
188
  if ( $custom_error ) {
189
- return __( $custom_error );
190
  } else {
191
  return __( '<strong>Robot test error</strong>: I suggest a new strategy, R2, let the Wookie win.' );
192
  }
@@ -199,3 +222,32 @@ function wr_no_captcha_api_keys_set() {
199
  return false;
200
  }
201
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  * Plugin Name: reCAPTCHA
4
  * Plugin URI: http://ashmatadeen.com
5
  * Description: Adds Google's reCAPTCHA to WP's login form
6
+ *
7
+ * @package login-form-recaptcha
8
  * Author: Ash Matadeen
9
  * Author URI: http://ashmatadeen.com
10
+ * Version: 1.5
11
  */
12
 
13
  add_action( 'admin_menu', 'wr_no_captcha_menu' );
17
  add_action( 'login_form', 'wr_no_captcha_render_login_captcha' );
18
  add_filter( 'wp_authenticate_user', 'wr_no_captcha_verify_login_captcha', 10, 2 );
19
 
20
+ // Specific support for WooCommerce login form!
21
+ // Using WooCommerce specific hooks because WooCommerce's login form does not use the expected wp_login_form().
 
22
  if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
23
  add_action( 'woocommerce_login_form', 'wr_no_captcha_render_login_captcha' );
24
  add_action( 'wp_enqueue_scripts', 'wr_no_captcha_login_form_script' );
56
  ?>
57
  </form>
58
 
59
+ <form method="post" action="options.php">
60
  <?php
61
  settings_fields( 'messages_section' );
62
  do_settings_sections( 'recaptcha-text-options' );
63
  submit_button();
64
  ?>
65
  </form>
66
+
67
+ <form method="post" action="options.php">
68
+ <?php
69
+ settings_fields( 'exlude_ips_section' );
70
+ do_settings_sections( 'recaptcha-exlude_ips-options' );
71
+ submit_button();
72
+ ?>
73
+ </form>
74
  </div>
75
 
76
  </div>
138
  add_settings_section( 'messages_section', 'Custom error message', 'wr_no_captcha_display_recaptcha_error_message_content', 'recaptcha-text-options' );
139
  add_settings_field( 'wr_no_captcha_error_message_text', 'Custom error message text', 'wr_no_captcha_error_message_input', 'recaptcha-text-options', 'messages_section' );
140
  register_setting( 'messages_section', 'wr_no_captcha_error_message_text' );
141
+
142
+ add_settings_section( 'exlude_ips_section', 'Exclude IP addresses', 'wr_no_captcha_display_recaptcha_exlude_ips_content', 'recaptcha-exlude_ips-options' );
143
+ add_settings_field( 'wr_no_captcha_exlude_ips', 'Exclude IP addresses', 'wr_no_captcha_exlude_ips_input', 'recaptcha-exlude_ips-options', 'exlude_ips_section' );
144
+ register_setting( 'exlude_ips_section', 'wr_no_captcha_exlude_ips' );
145
  }
146
 
147
  function wr_no_captcha_display_recaptcha_error_message_content() {
148
+ echo '<p>You can set your own error message here for when the bot test fails:</p>';
149
+ }
150
+
151
+ function wr_no_captcha_display_recaptcha_exlude_ips_content() {
152
+ echo '<p>You can exclude specific IP addresses (separated by comma) from displaying the captcha:</p>';
153
  }
154
 
155
  function wr_no_captcha_error_message_input() {
156
+ echo '<input size="60" type="text" name="wr_no_captcha_error_message_text" id="wr_no_captcha_error_message_text" value="' . get_option( 'wr_no_captcha_error_message_text' ) . '" />';
157
+ }
158
+
159
+ function wr_no_captcha_exlude_ips_input() {
160
+ echo '<input size="60" type="text" name="wr_no_captcha_exlude_ips" id="wr_no_captcha_exlude_ips" value="' . get_option( 'wr_no_captcha_exlude_ips' ) . '" />';
161
  }
162
 
163
  function wr_no_captcha_display_recaptcha_api_content() {
165
  }
166
 
167
  function wr_no_captcha_key_input() {
168
+ echo '<input type="text" name="wr_no_captcha_site_key" id="captcha_site_key" value="' . get_option( 'wr_no_captcha_site_key' ) . '" />';
169
  }
170
 
171
  function wr_no_captcha_secret_key_input() {
173
  }
174
 
175
  function wr_no_captcha_login_form_script() {
176
+ if ( ! wr_no_captcha_is_ip_excluded() ) {
177
+ wp_register_script( 'no_captcha_login', 'https://www.google.com/recaptcha/api.js' );
178
+ wp_enqueue_script( 'no_captcha_login' );
179
+ }
180
  }
181
 
182
  function wr_no_captcha_render_login_captcha() {
183
+ if ( wr_no_captcha_api_keys_set() && ! wr_no_captcha_is_ip_excluded() ) {
184
  echo '<div class="g-recaptcha" data-sitekey="' . get_option( 'wr_no_captcha_site_key' ) . '"></div>';
185
+ require_once( plugin_dir_path( __FILE__ ) . 'noscript/noscript.php' );
186
  }
187
  }
188
 
189
+ function wr_no_captcha_verify_login_captcha( $user, $password ) {
190
  if ( isset( $_POST['g-recaptcha-response'] ) ) {
191
  $no_captcha_secret = get_option( 'wr_no_captcha_secret_key' );
192
  $response = wp_remote_get( 'https://www.google.com/recaptcha/api/siteverify?secret=' . $no_captcha_secret . '&response=' . $_POST['g-recaptcha-response'] );
196
  } else {
197
  return new WP_Error( 'Captcha Invalid', wr_no_captcha_get_error_message() );
198
  }
199
+ } elseif ( ! wr_no_captcha_api_keys_set() || wr_no_captcha_is_ip_excluded() ) {
200
  return $user;
201
  }
202
  }
209
  function wr_no_captcha_get_error_message() {
210
  $custom_error = get_option( 'wr_no_captcha_error_message_text' );
211
  if ( $custom_error ) {
212
+ return $custom_error;
213
  } else {
214
  return __( '<strong>Robot test error</strong>: I suggest a new strategy, R2, let the Wookie win.' );
215
  }
222
  return false;
223
  }
224
  }
225
+
226
+ function wr_no_captcha_get_exlude_ips() {
227
+ $exlude_ips = get_option( 'wr_no_captcha_exlude_ips' );
228
+ if ( $exlude_ips ) {
229
+ return array_map( 'trim', explode( ',', $exlude_ips ) );
230
+ } else {
231
+ return array();
232
+ }
233
+ }
234
+
235
+ function wr_no_captcha_get_client_ip() {
236
+ $ipaddress = '';
237
+ if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) {
238
+ $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
239
+ } elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
240
+ $ipaddress = $_SERVER['REMOTE_ADDR'];
241
+ } else {
242
+ $ipaddress = 'UNKNOWN';
243
+ }
244
+ return $ipaddress;
245
+ }
246
+
247
+ function wr_no_captcha_is_ip_excluded() {
248
+ if ( wr_no_captcha_get_client_ip() === 'UNKNOWN' ) {
249
+ return false;
250
+ } else {
251
+ return in_array( wr_no_captcha_get_client_ip(), wr_no_captcha_get_exlude_ips() );
252
+ }
253
+ }
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: ash.matadeen
3
  Donate link: http://ashmatadeen.com/donate
4
  Tags: security, bots, recaptcha, nocaptcha, google, login
5
  Requires at least: 4.2.2
6
- Tested up to: 4.6.1
7
  Stable tag: trunk
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -31,6 +31,9 @@ This plugin prevents brute force logins on your WordPress website by adding Goog
31
 
32
  == Changelog ==
33
 
 
 
 
34
  = 1.4.1 =
35
  * Support for WooCommerce's login form
36
 
3
  Donate link: http://ashmatadeen.com/donate
4
  Tags: security, bots, recaptcha, nocaptcha, google, login
5
  Requires at least: 4.2.2
6
+ Tested up to: 4.7.4
7
  Stable tag: trunk
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
31
 
32
  == Changelog ==
33
 
34
+ = 1.5 =
35
+ * Support for bypassing the captcha for specific IP addresses. Props to [Daniel Mann](https://github.com/dnlm)
36
+
37
  = 1.4.1 =
38
  * Support for WooCommerce's login form
39