Simple Google reCAPTCHA - Version 2.8

Version Description

  • Warning: New logic - Google reCAPTCHA js file will be loaded in the background on every page for non logged in users
  • Warning: If Google reCAPTCHA verification fail, response code is 403 instead of 500 now. Thank you for contribution, Sara Koziska!
  • Bugfix: WooCommerce problem (JSON.parse error) in checkout process has been fixed. I'm sorry for a really big delay!
Download this release

Release Info

Developer Minor
Plugin Icon 128x128 Simple Google reCAPTCHA
Version 2.8
Comparing to
See all releases

Code changes from version 2.7 to 2.8

Files changed (2) hide show
  1. readme.txt +7 -2
  2. simple-google-recaptcha.php +29 -41
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: Minor
3
  Tags: recaptcha, spam, block, captcha, bots, brute, force, protect, comments, secure, attack, registration, reset, form, buddypress, woocommerce, google
4
  Requires at least: 4.2.0
5
- Tested up to: 4.8.1
6
- Stable tag: 2.7
7
  License: GPLv3
8
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
9
  Donate link: https://www.paypal.me/NovaMi
@@ -47,6 +47,11 @@ Just use standard Plugin overview page in WordPress admin section and deactivate
47
  5. Simple Google reCAPTCHA - Settings
48
 
49
  == Changelog ==
 
 
 
 
 
50
  = 2.7 =
51
  * Bugfix: Loading of Google reCAPTCHA form failed in some rare cases
52
 
2
  Contributors: Minor
3
  Tags: recaptcha, spam, block, captcha, bots, brute, force, protect, comments, secure, attack, registration, reset, form, buddypress, woocommerce, google
4
  Requires at least: 4.2.0
5
+ Tested up to: 4.9.4
6
+ Stable tag: 2.8
7
  License: GPLv3
8
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
9
  Donate link: https://www.paypal.me/NovaMi
47
  5. Simple Google reCAPTCHA - Settings
48
 
49
  == Changelog ==
50
+ = 2.8 =
51
+ * Warning: New logic - Google reCAPTCHA js file will be loaded in the background on every page for non logged in users
52
+ * Warning: If Google reCAPTCHA verification fail, response code is 403 instead of 500 now. Thank you for contribution, Sara Kozińska!
53
+ * Bugfix: WooCommerce problem (JSON.parse error) in checkout process has been fixed. I'm sorry for a really big delay!
54
+
55
  = 2.7 =
56
  * Bugfix: Loading of Google reCAPTCHA form failed in some rare cases
57
 
simple-google-recaptcha.php CHANGED
@@ -2,7 +2,7 @@
2
  /*
3
  * Plugin Name: Simple Google reCAPTCHA
4
  * Description: Simply protect your WordPress against spam comments and brute-force attacks, thanks to Google reCAPTCHA!
5
- * Version: 2.7
6
  * Author: Michal Novák
7
  * Author URI: https://www.novami.cz
8
  * License: GPL3
@@ -16,7 +16,7 @@ function sgr_add_plugin_action_links($links) {
16
  add_filter("plugin_action_links_".plugin_basename(__FILE__), "sgr_add_plugin_action_links");
17
 
18
  function sgr_activation($plugin) {
19
- if ($plugin == plugin_basename(__FILE__) && (get_option("sgr_site_key") == "" || get_option("sgr_secret_key") == "")) {
20
  exit(wp_redirect(admin_url("options-general.php?page=sgr-options")));
21
  }
22
  }
@@ -43,11 +43,13 @@ function sgr_display_content() {
43
  }
44
 
45
  function sgr_display_site_key_element() {
46
- echo "<input type=\"text\" name=\"sgr_site_key\" class=\"regular-text\" id=\"sgr_site_key\" value=\"".get_option("sgr_site_key")."\" />";
 
47
  }
48
 
49
  function sgr_display_secret_key_element() {
50
- echo "<input type=\"text\" name=\"sgr_secret_key\" class=\"regular-text\" id=\"sgr_secret_key\" value=\"".get_option("sgr_secret_key")."\" />";
 
51
  }
52
 
53
  function sgr_display_options() {
@@ -67,25 +69,18 @@ function load_language_sgr() {
67
  add_action("plugins_loaded", "load_language_sgr");
68
 
69
  function frontend_sgr_script() {
70
- if (did_action("login_init") > 0 || function_exists("is_account_page") || function_exists("bp_get_signup_page") || (is_singular() && comments_open()) && !is_user_logged_in()) {
71
- wp_register_script("sgr_recaptcha_main", plugin_dir_url(__FILE__)."main.js");
72
- wp_enqueue_script("sgr_recaptcha_main");
73
- wp_localize_script("sgr_recaptcha_main", "sgr_recaptcha", array("site_key" => get_option("sgr_site_key")));
74
- wp_register_script("sgr_recaptcha", "https://www.google.com/recaptcha/api.js?hl=".get_locale()."&onload=sgr&render=explicit");
75
- wp_enqueue_script("sgr_recaptcha");
76
- wp_enqueue_style("style", plugin_dir_url(__FILE__)."style.css");
77
 
78
- add_action("comment_form_after_fields", "sgr_display");
79
- add_action("login_form", "sgr_display");
80
- add_action("register_form", "sgr_display");
81
- add_action("lost_password", "sgr_display");
82
- add_action("lostpassword_form", "sgr_display");
83
- add_action("retrieve_password", "sgr_display");
84
- add_action("resetpass_form", "sgr_display");
85
- add_action("woocommerce_login_form", "sgr_display");
86
- add_action("woocommerce_register_form", "sgr_display");
87
- add_action("woocommerce_lostpassword_form", "sgr_display");
88
- add_action("bp_after_signup_profile_fields", "sgr_display");
89
  }
90
  }
91
 
@@ -95,40 +90,33 @@ function sgr_display() {
95
 
96
  function sgr_verify($input) {
97
  if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["g-recaptcha-response"])) {
98
- $recaptcha_response = sanitize_text_field($_POST["g-recaptcha-response"]);
99
- $recaptcha_secret = get_option("sgr_secret_key");
100
- $response = wp_remote_get("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$recaptcha_response);
101
- $response = json_decode($response["body"], true);
102
 
103
- if ($response["success"] == true) {
104
  return $input;
105
  } else {
106
- wp_die("<p><strong>".__("ERROR:", "simple-google-recaptcha")."</strong> ".__("Google reCAPTCHA verification failed.", "simple-google-recaptcha")."</p>\n\n<p><a href=".wp_get_referer().">&laquo; ".__("Back", "simple-google-recaptcha")."</a>");
107
  return null;
108
  }
109
-
110
  } else {
111
- wp_die("<p><strong>".__("ERROR:", "simple-google-recaptcha")."</strong> ".__("Google reCAPTCHA verification failed.", "simple-google-recaptcha")." ".__("Do you have JavaScript enabled?", "simple-google-recaptcha")."</p>\n\n<p><a href=".wp_get_referer().">&laquo; ".__("Back", "simple-google-recaptcha")."</a>");
112
  return null;
113
  }
114
  }
115
 
116
  function sgr_check() {
117
- if (get_option("sgr_site_key") != "" && get_option("sgr_secret_key") != "") {
118
-
119
  add_action("login_enqueue_scripts", "frontend_sgr_script");
120
  add_action("wp_enqueue_scripts", "frontend_sgr_script");
121
 
122
- if (!is_user_logged_in()) {
123
- add_action("preprocess_comment", "sgr_verify");
124
- }
125
 
126
- add_action("wp_authenticate_user", "sgr_verify");
127
- add_action("registration_errors", "sgr_verify");
128
- add_action("lostpassword_post", "sgr_verify");
129
- add_action("resetpass_post", "sgr_verify");
130
- add_action("bp_signup_validate", "sgr_verify");
131
- add_action("woocommerce_register_post", "sgr_verify");
132
  }
133
  }
134
 
2
  /*
3
  * Plugin Name: Simple Google reCAPTCHA
4
  * Description: Simply protect your WordPress against spam comments and brute-force attacks, thanks to Google reCAPTCHA!
5
+ * Version: 2.8
6
  * Author: Michal Nov&aacute;k
7
  * Author URI: https://www.novami.cz
8
  * License: GPL3
16
  add_filter("plugin_action_links_".plugin_basename(__FILE__), "sgr_add_plugin_action_links");
17
 
18
  function sgr_activation($plugin) {
19
+ if ($plugin == plugin_basename(__FILE__) && (!get_option("sgr_site_key") || !get_option("sgr_secret_key"))) {
20
  exit(wp_redirect(admin_url("options-general.php?page=sgr-options")));
21
  }
22
  }
43
  }
44
 
45
  function sgr_display_site_key_element() {
46
+ $sgr_site_key = filter_var(get_option("sgr_site_key"), FILTER_SANITIZE_FULL_SPECIAL_CHARS);
47
+ echo "<input type=\"text\" name=\"sgr_site_key\" class=\"regular-text\" id=\"sgr_site_key\" value=\"{$sgr_site_key}\" />";
48
  }
49
 
50
  function sgr_display_secret_key_element() {
51
+ $sgr_secret_key = filter_var(get_option("sgr_secret_key"), FILTER_SANITIZE_FULL_SPECIAL_CHARS);
52
+ echo "<input type=\"text\" name=\"sgr_secret_key\" class=\"regular-text\" id=\"sgr_secret_key\" value=\"{$sgr_secret_key}\" />";
53
  }
54
 
55
  function sgr_display_options() {
69
  add_action("plugins_loaded", "load_language_sgr");
70
 
71
  function frontend_sgr_script() {
72
+ $sgr_site_key = filter_var(get_option("sgr_site_key"), FILTER_SANITIZE_FULL_SPECIAL_CHARS);
73
+ $sgr_display_list = array("comment_form_after_fields", "login_form", "register_form", "lost_password", "lostpassword_form", "retrieve_password", "resetpass_form", "woocommerce_login_form", "woocommerce_register_form", "woocommerce_lostpassword_form", "woocommerce_after_order_notes", "bp_after_signup_profile_fields");
 
 
 
 
 
74
 
75
+ wp_register_script("sgr_recaptcha_main", plugin_dir_url(__FILE__)."main.js");
76
+ wp_enqueue_script("sgr_recaptcha_main");
77
+ wp_localize_script("sgr_recaptcha_main", "sgr_recaptcha", array("site_key" => $sgr_site_key));
78
+ wp_register_script("sgr_recaptcha", "https://www.google.com/recaptcha/api.js?hl=".get_locale()."&onload=sgr&render=explicit");
79
+ wp_enqueue_script("sgr_recaptcha");
80
+ wp_enqueue_style("style", plugin_dir_url(__FILE__)."style.css");
81
+
82
+ foreach($sgr_display_list as $sgr_display) {
83
+ add_action($sgr_display, "sgr_display");
 
 
84
  }
85
  }
86
 
90
 
91
  function sgr_verify($input) {
92
  if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["g-recaptcha-response"])) {
93
+ $sgr_secret_key = filter_var(get_option("sgr_secret_key"), FILTER_SANITIZE_FULL_SPECIAL_CHARS);
94
+ $recaptcha_response = filter_input(INPUT_POST, "g-recaptcha-response", FILTER_SANITIZE_FULL_SPECIAL_CHARS);
95
+ $response = wp_remote_get("https://www.google.com/recaptcha/api/siteverify?secret={$sgr_secret_key}&response={$recaptcha_response}");
96
+ $response = json_decode($response["body"], 1);
97
 
98
+ if ($response["success"]) {
99
  return $input;
100
  } else {
101
+ wp_die("<p><strong>".__("ERROR:", "simple-google-recaptcha")."</strong> ".__("Google reCAPTCHA verification failed.", "simple-google-recaptcha")."</p>\n\n<p><a href=".wp_get_referer().">&laquo; ".__("Back", "simple-google-recaptcha")."</a>", "", 403);
102
  return null;
103
  }
 
104
  } else {
105
+ wp_die("<p><strong>".__("ERROR:", "simple-google-recaptcha")."</strong> ".__("Google reCAPTCHA verification failed.", "simple-google-recaptcha")." ".__("Do you have JavaScript enabled?", "simple-google-recaptcha")."</p>\n\n<p><a href=".wp_get_referer().">&laquo; ".__("Back", "simple-google-recaptcha")."</a>", "", 403);
106
  return null;
107
  }
108
  }
109
 
110
  function sgr_check() {
111
+ if (get_option("sgr_site_key") && get_option("sgr_secret_key") && !is_user_logged_in()) {
 
112
  add_action("login_enqueue_scripts", "frontend_sgr_script");
113
  add_action("wp_enqueue_scripts", "frontend_sgr_script");
114
 
115
+ $sgr_verify_list = array("preprocess_comment", "wp_authenticate_user", "registration_errors", "lostpassword_post", "resetpass_post", "bp_signup_validate", "woocommerce_register_post");
 
 
116
 
117
+ foreach($sgr_verify_list as $sgr_verify) {
118
+ add_action($sgr_verify, "sgr_verify");
119
+ }
 
 
 
120
  }
121
  }
122