Simple Google reCAPTCHA - Version 3.8

Version Description

  • Bugfix: Fix against rare error 'The response is no longer valid: either is too old or has been used previously.' for reCAPTCHA v3
  • New: Class converted to singleton - for possibility to use Simple Google reCAPTCHA in custom hooks
Download this release

Release Info

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

Code changes from version 3.7 to 3.8

Files changed (4) hide show
  1. readme.txt +5 -1
  2. sgr.js +2 -0
  3. simple-google-recaptcha.php +39 -19
  4. uninstall.php +2 -5
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: Minor
3
  Tags: recaptcha, spam, protect, google, invisible
4
  Requires at least: 4.6
5
  Tested up to: 5.7
6
- Stable tag: 3.7
7
  Requires PHP: 7.1
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
@@ -61,6 +61,10 @@ Use standard WordPress Plugins page. In emergency case, rename plugin folder und
61
  7. Emergency reCAPTCHA deactivate link
62
 
63
  == Changelog ==
 
 
 
 
64
  = 3.7 =
65
  * Bugfix: Important! Everybody who has version 3.6 should update as soon as possible! Fixed a bug with disappearing site&secret key.
66
 
3
  Tags: recaptcha, spam, protect, google, invisible
4
  Requires at least: 4.6
5
  Tested up to: 5.7
6
+ Stable tag: 3.8
7
  Requires PHP: 7.1
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
61
  7. Emergency reCAPTCHA deactivate link
62
 
63
  == Changelog ==
64
+ = 3.8 =
65
+ * Bugfix: Fix against rare error 'The response is no longer valid: either is too old or has been used previously.' for reCAPTCHA v3
66
+ * New: Class converted to singleton - for possibility to use Simple Google reCAPTCHA in custom hooks
67
+
68
  = 3.7 =
69
  * Bugfix: Important! Everybody who has version 3.6 should update as soon as possible! Fixed a bug with disappearing site&secret key.
70
 
sgr.js CHANGED
@@ -16,6 +16,8 @@ function sgr_3() {
16
  recaptcha.item(i).value = token;
17
  }
18
  });
 
 
19
  }
20
 
21
  document.addEventListener('DOMContentLoaded', function (event) {
16
  recaptcha.item(i).value = token;
17
  }
18
  });
19
+
20
+ setTimeout(sgr_3, 1000 * 100);
21
  }
22
 
23
  document.addEventListener('DOMContentLoaded', function (event) {
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: 3.7
6
  * Author: Michal Novák
7
  * Author URI: https://www.novami.cz
8
  * License: GPLv3
@@ -36,6 +36,8 @@ class SimpleGoogleRecaptcha
36
  const SGR_OPTION_SECRET_KEY = 'sgr_secret_key';
37
  const SGR_OPTION_HASH = 'sgr_hash';
38
 
 
 
39
  private $pluginName;
40
  private $version;
41
  private $loginDisable;
@@ -48,12 +50,27 @@ class SimpleGoogleRecaptcha
48
  /**
49
  * SimpleGoogleRecaptcha constructor.
50
  */
51
- public function __construct()
52
  {
53
  add_action('init', [$this, 'run']);
54
  add_action('activated_plugin', [$this, 'activation']);
55
  }
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  public function updateSettings()
58
  {
59
  if (current_user_can('manage_options')) {
@@ -73,13 +90,15 @@ class SimpleGoogleRecaptcha
73
  }
74
  setcookie(self::SGR_OPTION_HASH, md5($hash), time() + 60 * 60 * 24 * 10, '/');
75
  }
 
 
76
  }
77
 
78
  /**
79
  * @param $links
80
  * @return array
81
  */
82
- public function action_links($links)
83
  {
84
  return array_merge(['settings' => sprintf('<a href="options-general.php%s">%s</a>', self::SGR_PAGE_OPTIONS_QUERY, __('Settings', 'simple-google-recaptcha'))], $links);
85
  }
@@ -91,7 +110,7 @@ class SimpleGoogleRecaptcha
91
  }
92
  }
93
 
94
- public function options_page()
95
  {
96
  echo sprintf('<div class="wrap"><h1>%s - %s</h1><form method="post" action="%s">', $this->pluginName, __('Settings', 'simple-google-recaptcha'), self::SGR_PAGE_OPTIONS_QUERY);
97
 
@@ -103,11 +122,12 @@ class SimpleGoogleRecaptcha
103
  echo sprintf('<input type="hidden" name="%s" value="%s">%s</form>%s</div>', self::SGR_ACTION, self::UPDATE, PHP_EOL, $this->messageProtectionStatus());
104
  }
105
 
106
- public function menu()
107
  {
108
- $this->enqueue_main();
109
- add_submenu_page('options-general.php', $this->pluginName, 'Google reCAPTCHA', 'manage_options', 'sgr_options', [$this, 'options_page']);
110
- add_action('admin_init', [$this, 'display_options']);
 
111
  }
112
 
113
  public function display_sgr_site_key()
@@ -135,7 +155,7 @@ class SimpleGoogleRecaptcha
135
  echo sprintf('<input type="checkbox" name="%1$s" id="%1$s" value="1" %2$s />', self::SGR_OPTION_BADGE_HIDE, checked(1, $this->badgeHide, false));
136
  }
137
 
138
- public function display_options()
139
  {
140
  $fields = [
141
  ['id' => self::SGR_OPTION_SITE_KEY, 'label' => __('Site Key', 'simple-google-recaptcha')],
@@ -153,7 +173,7 @@ class SimpleGoogleRecaptcha
153
  }
154
  }
155
 
156
- public function enqueue_main()
157
  {
158
  $jsName = 'sgr.js';
159
  $jsPath = sprintf('%s%s', plugin_dir_path(__FILE__), $jsName);
@@ -166,7 +186,7 @@ class SimpleGoogleRecaptcha
166
  wp_enqueue_style(self::SGR_MAIN, sprintf('%s%s', plugin_dir_url(__FILE__), $cssName), [], filemtime($cssPath));
167
  }
168
 
169
- public function enqueue_scripts()
170
  {
171
  $apiUrlBase = sprintf('https://www.recaptcha.net/recaptcha/api.js?hl=%s', get_locale());
172
  $jsUrl = sprintf('%s&onload=sgr_2&render=explicit', $apiUrlBase);
@@ -180,7 +200,7 @@ class SimpleGoogleRecaptcha
180
 
181
  public function frontend()
182
  {
183
- $this->enqueue_main();
184
 
185
  $sgr_display_list = [
186
  'comment_form_after_fields',
@@ -204,10 +224,10 @@ class SimpleGoogleRecaptcha
204
  array_push($sgr_verify_list, 'wp_authenticate_user');
205
  }
206
 
207
- $sgrDisplay = $this->version === 3 ? 'v3_display' : 'v2_display';
208
 
209
  foreach ($sgr_display_list as $sgr_display) {
210
- add_action($sgr_display, [$this, 'enqueue_scripts']);
211
  add_action($sgr_display, [$this, $sgrDisplay]);
212
  }
213
 
@@ -216,14 +236,14 @@ class SimpleGoogleRecaptcha
216
  }
217
  }
218
 
219
- public function v2_display()
220
  {
221
  $this->displayDisableProtection();
222
 
223
  echo '<div class="sgr-main"></div>';
224
  }
225
 
226
- public function v3_display()
227
  {
228
  $badgeText = null;
229
 
@@ -372,8 +392,8 @@ class SimpleGoogleRecaptcha
372
  $this->disableProtection();
373
  }
374
 
375
- add_filter(sprintf('plugin_action_links_%s', plugin_basename(__FILE__)), [$this, 'action_links']);
376
- add_action('admin_menu', [$this, 'menu']);
377
 
378
  if (!is_user_logged_in() && !wp_doing_ajax() && !function_exists('wpcf7_contact_form_shortcode') && $this->siteKey && $this->secretKey) {
379
  $this->frontend();
@@ -381,4 +401,4 @@ class SimpleGoogleRecaptcha
381
  }
382
  }
383
 
384
- new SimpleGoogleRecaptcha();
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: 3.8
6
  * Author: Michal Novák
7
  * Author URI: https://www.novami.cz
8
  * License: GPLv3
36
  const SGR_OPTION_SECRET_KEY = 'sgr_secret_key';
37
  const SGR_OPTION_HASH = 'sgr_hash';
38
 
39
+ public static $instance;
40
+
41
  private $pluginName;
42
  private $version;
43
  private $loginDisable;
50
  /**
51
  * SimpleGoogleRecaptcha constructor.
52
  */
53
+ private function __construct()
54
  {
55
  add_action('init', [$this, 'run']);
56
  add_action('activated_plugin', [$this, 'activation']);
57
  }
58
 
59
+ /**
60
+ * @return SimpleGoogleRecaptcha
61
+ */
62
+ public static function getInstance()
63
+ {
64
+ if (!self::$instance instanceof self) {
65
+ self::$instance = new self();
66
+ }
67
+
68
+ return self::$instance;
69
+ }
70
+
71
+ /**
72
+ * @return bool
73
+ */
74
  public function updateSettings()
75
  {
76
  if (current_user_can('manage_options')) {
90
  }
91
  setcookie(self::SGR_OPTION_HASH, md5($hash), time() + 60 * 60 * 24 * 10, '/');
92
  }
93
+
94
+ return true;
95
  }
96
 
97
  /**
98
  * @param $links
99
  * @return array
100
  */
101
+ public function actionLinks($links)
102
  {
103
  return array_merge(['settings' => sprintf('<a href="options-general.php%s">%s</a>', self::SGR_PAGE_OPTIONS_QUERY, __('Settings', 'simple-google-recaptcha'))], $links);
104
  }
110
  }
111
  }
112
 
113
+ public function optionsPage()
114
  {
115
  echo sprintf('<div class="wrap"><h1>%s - %s</h1><form method="post" action="%s">', $this->pluginName, __('Settings', 'simple-google-recaptcha'), self::SGR_PAGE_OPTIONS_QUERY);
116
 
122
  echo sprintf('<input type="hidden" name="%s" value="%s">%s</form>%s</div>', self::SGR_ACTION, self::UPDATE, PHP_EOL, $this->messageProtectionStatus());
123
  }
124
 
125
+ public function adminMenu()
126
  {
127
+ $this->enqueueMain();
128
+
129
+ add_submenu_page('options-general.php', $this->pluginName, 'Google reCAPTCHA', 'manage_options', 'sgr_options', [$this, 'optionsPage']);
130
+ add_action('admin_init', [$this, 'displayOptions']);
131
  }
132
 
133
  public function display_sgr_site_key()
155
  echo sprintf('<input type="checkbox" name="%1$s" id="%1$s" value="1" %2$s />', self::SGR_OPTION_BADGE_HIDE, checked(1, $this->badgeHide, false));
156
  }
157
 
158
+ public function displayOptions()
159
  {
160
  $fields = [
161
  ['id' => self::SGR_OPTION_SITE_KEY, 'label' => __('Site Key', 'simple-google-recaptcha')],
173
  }
174
  }
175
 
176
+ public function enqueueMain()
177
  {
178
  $jsName = 'sgr.js';
179
  $jsPath = sprintf('%s%s', plugin_dir_path(__FILE__), $jsName);
186
  wp_enqueue_style(self::SGR_MAIN, sprintf('%s%s', plugin_dir_url(__FILE__), $cssName), [], filemtime($cssPath));
187
  }
188
 
189
+ public function enqueueScripts()
190
  {
191
  $apiUrlBase = sprintf('https://www.recaptcha.net/recaptcha/api.js?hl=%s', get_locale());
192
  $jsUrl = sprintf('%s&onload=sgr_2&render=explicit', $apiUrlBase);
200
 
201
  public function frontend()
202
  {
203
+ $this->enqueueMain();
204
 
205
  $sgr_display_list = [
206
  'comment_form_after_fields',
224
  array_push($sgr_verify_list, 'wp_authenticate_user');
225
  }
226
 
227
+ $sgrDisplay = $this->version === 3 ? 'v3Display' : 'v2Display';
228
 
229
  foreach ($sgr_display_list as $sgr_display) {
230
+ add_action($sgr_display, [$this, 'enqueueScripts']);
231
  add_action($sgr_display, [$this, $sgrDisplay]);
232
  }
233
 
236
  }
237
  }
238
 
239
+ public function v2Display()
240
  {
241
  $this->displayDisableProtection();
242
 
243
  echo '<div class="sgr-main"></div>';
244
  }
245
 
246
+ public function v3Display()
247
  {
248
  $badgeText = null;
249
 
392
  $this->disableProtection();
393
  }
394
 
395
+ add_filter(sprintf('plugin_action_links_%s', plugin_basename(__FILE__)), [$this, 'actionLinks']);
396
+ add_action('admin_menu', [$this, 'adminMenu']);
397
 
398
  if (!is_user_logged_in() && !wp_doing_ajax() && !function_exists('wpcf7_contact_form_shortcode') && $this->siteKey && $this->secretKey) {
399
  $this->frontend();
401
  }
402
  }
403
 
404
+ SimpleGoogleRecaptcha::getInstance();
uninstall.php CHANGED
@@ -15,12 +15,9 @@ class SimpleGoogleRecaptchaUninstall
15
  */
16
  public function __construct()
17
  {
18
- $this->sgr_delete(['site_key', 'secret_key', 'login_disable', 'version', 'badge_hide']);
19
- }
20
 
21
- private function sgr_delete($array)
22
- {
23
- foreach ($array as $item) {
24
  delete_option(sprintf('sgr_%s', $item));
25
  }
26
  }
15
  */
16
  public function __construct()
17
  {
18
+ $options = ['site_key', 'secret_key', 'login_disable', 'version', 'badge_hide'];
 
19
 
20
+ foreach ($options as $item) {
 
 
21
  delete_option(sprintf('sgr_%s', $item));
22
  }
23
  }