PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.6.0
Defender Security – Malware Scanner, Login Security & Firewall v5.6.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / model / setting / class-recaptcha.php

class-recaptcha.php in Defender Security – Malware Scanner, Login Security & Firewall 5.6.0, at src/model/setting/class-recaptcha.php

373 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handle reCaptcha settings.
4 *
5 * @package WP_Defender\Model\Setting
6 */
7
8 namespace WP_Defender\Model\Setting;
9
10 use Calotes\Model\Setting;
11
12 /**
13 * Model for reCaptcha settings.
14 */
15 class Recaptcha extends Setting {
16
17 /**
18 * Option name.
19 *
20 * @var string
21 */
22 protected $table = 'wd_recaptcha_settings';
23
24 /**
25 * Feature status.
26 *
27 * @var bool
28 * @defender_property
29 */
30 public $enabled = false;
31
32 /**
33 * Active reCaptcha type.
34 *
35 * @var string
36 * @defender_property
37 * @rule required
38 * @rule in[v2_checkbox,v2_invisible,v3_recaptcha]
39 */
40 public $active_type = 'v2_checkbox';
41
42 /**
43 * Data for v2 checkbox reCaptcha.
44 *
45 * @var array
46 * @defender_property
47 */
48 public $data_v2_checkbox;
49
50 /**
51 * Data for v2 invisible reCaptcha.
52 *
53 * @var array
54 * @defender_property
55 */
56 public $data_v2_invisible;
57
58 /**
59 * Data for v3 reCaptcha.
60 *
61 * @var array
62 * @defender_property
63 */
64 public $data_v3_recaptcha;
65
66 /**
67 * Language for reCaptcha.
68 *
69 * @var string
70 * @defender_property
71 * @rule required
72 */
73 public $language = '';
74
75 /**
76 * Message for reCaptcha.
77 *
78 * @var string
79 * @defender_property
80 * @sanitize sanitize_textarea_field
81 */
82 public $message = '';
83
84 /**
85 * Locations for reCaptcha.
86 *
87 * @var array
88 * @defender_property
89 * @rule required
90 */
91 public $locations = array();
92
93 /**
94 * Flag to detect WooCommerce.
95 *
96 * @var bool
97 * @defender_property
98 */
99 public $detect_woo = false;
100
101 /**
102 * Checked locations for WooCommerce.
103 *
104 * @var array
105 * @defender_property
106 * @rule required
107 */
108 public $woo_checked_locations = array();
109
110 /**
111 * Flag to detect BuddyPress.
112 *
113 * @var bool
114 * @defender_property
115 */
116 public $detect_buddypress = false;
117
118 /**
119 * Checked locations for BuddyPress.
120 *
121 * @var array
122 * @defender_property
123 * @rule required
124 */
125 public $buddypress_checked_locations = array();
126
127 /**
128 * Flag to disable reCaptcha for known users.
129 *
130 * @var bool
131 * @defender_property
132 */
133 public $disable_for_known_users = true;
134
135 /**
136 * Rules for validating the reCaptcha settings.
137 *
138 * @var array
139 */
140 protected $rules = array(
141 array( array( 'enabled', 'detect_woo', 'detect_buddypress' ), 'boolean' ),
142 array( array( 'active_type' ), 'in', array( 'v2_checkbox', 'v2_invisible', 'v3_recaptcha' ) ),
143 );
144
145 /**
146 * Retrieves the default values for the reCAPTCHA settings.
147 *
148 * @return array An associative array containing the default values.
149 */
150 public function get_default_values(): array {
151 return array(
152 'message' => esc_html__( 'reCAPTCHA verification failed. Please try again.', 'defender-security' ),
153 );
154 }
155
156 /**
157 * Load default values.
158 *
159 * @return void
160 */
161 protected function before_load(): void {
162 $default_values = $this->get_default_values();
163 $this->message = $default_values['message'];
164 $this->language = 'automatic';
165 $this->data_v2_checkbox = array(
166 'key' => '',
167 'secret' => '',
168 'size' => 'normal',
169 'style' => 'light',
170 );
171 $this->data_v2_invisible = array(
172 'key' => '',
173 'secret' => '',
174 );
175 $this->data_v3_recaptcha = array(
176 'key' => '',
177 'secret' => '',
178 'threshold' => '0.5',
179 );
180 }
181
182 /**
183 * Checks if the given reCAPTCHA type is valid and has all the necessary data.
184 *
185 * @param string $active_type The reCAPTCHA type to check.
186 *
187 * @return bool Returns true if the reCAPTCHA type is valid and has all the necessary data, false otherwise.
188 */
189 private function check_recaptcha_type( string $active_type ): bool {
190 if (
191 'v2_checkbox' === $active_type
192 && '' !== $this->data_v2_checkbox['key']
193 && '' !== $this->data_v2_checkbox['secret']
194 ) {
195 return true;
196 } elseif (
197 'v2_invisible' === $active_type
198 && '' !== $this->data_v2_invisible['key']
199 && '' !== $this->data_v2_invisible['secret']
200 ) {
201 return true;
202 } elseif (
203 'v3_recaptcha' === $active_type
204 && '' !== $this->data_v3_recaptcha['key'] && '' !== $this->data_v3_recaptcha['secret']
205 ) {
206 return true;
207 } else {
208 return false;
209 }
210 }
211
212 /**
213 * Checks if the reCAPTCHA is active.
214 *
215 * @return bool Returns true if the reCAPTCHA is active, false otherwise.
216 */
217 public function is_active(): bool {
218 return (bool) apply_filters(
219 'wd_recaptcha_enable',
220 $this->enabled
221 && '' !== $this->active_type
222 && '' !== $this->language
223 // For each Recaptcha type.
224 && $this->check_recaptcha_type( $this->active_type )
225 );
226 }
227
228 /**
229 * Is activated any default location?
230 *
231 * @return bool
232 */
233 public function enable_default_location(): bool {
234 return array() !== $this->locations;
235 }
236
237 /**
238 * Level#2 check by any activated location. Only if the plugin is enabled.
239 *
240 * @return bool
241 */
242 public function enable_woo_location(): bool {
243 return $this->detect_woo && array() !== $this->woo_checked_locations;
244 }
245
246 /**
247 * Level#2 check by deactivated locations. Only if the plugin is enabled.
248 *
249 * @return bool
250 */
251 public function is_unchecked_woo_locations(): bool {
252 return $this->detect_woo && array() === $this->woo_checked_locations;
253 }
254
255 /**
256 * Level#1 check. If the plugin is disabled, there is no point further.
257 *
258 * @param bool $is_woo_activated Whether WooCommerce is activated or not.
259 *
260 * @return bool
261 */
262 public function check_woo_locations( $is_woo_activated ): bool {
263 if ( ! $is_woo_activated ) {
264 return false;
265 }
266
267 return $this->enable_woo_location();
268 }
269
270 /**
271 * Level#2 check by any activated location. Only if the plugin is enabled.
272 *
273 * @return bool
274 */
275 public function enable_buddypress_location(): bool {
276 return $this->detect_buddypress && array() !== $this->buddypress_checked_locations;
277 }
278
279 /**
280 * Level#2 check by deactivated locations. Only if the plugin is enabled.
281 *
282 * @return bool
283 */
284 public function is_unchecked_buddypress_locations(): bool {
285 return $this->detect_buddypress && array() === $this->buddypress_checked_locations;
286 }
287
288 /**
289 * Level#1 Checks if the BuddyPress locations are valid and have all the necessary data.
290 *
291 * @param bool $is_buddypress_activated Whether BuddyPress is activated or not.
292 *
293 * @return bool Returns true if the BuddyPress locations are valid and have all the necessary data, false otherwise.
294 */
295 public function check_buddypress_locations( $is_buddypress_activated ): bool {
296 if ( ! $is_buddypress_activated ) {
297 return false;
298 }
299
300 return $this->enable_buddypress_location();
301 }
302
303 /**
304 * Define settings labels.
305 *
306 * @return array
307 */
308 public function labels(): array {
309 return array(
310 'enabled' => self::get_module_name(),
311 'active_type' => esc_html__( 'Configure reCaptcha', 'defender-security' ),
312 'v2_checkbox' => esc_html__( 'V2 Checkbox', 'defender-security' ),
313 'v2_invisible' => esc_html__( 'V2 Invisible', 'defender-security' ),
314 'v3_recaptcha' => esc_html__( 'reCAPTCHA V3', 'defender-security' ),
315 'language' => esc_html__( 'Language', 'defender-security' ),
316 'message' => esc_html__( 'Error Message', 'defender-security' ),
317 'locations' => esc_html__( 'CAPTCHA Locations', 'defender-security' ),
318 'detect_woo' => esc_html__( 'WooCommerce', 'defender-security' ),
319 'detect_buddypress' => esc_html__( 'BuddyPress', 'defender-security' ),
320 'disable_for_known_users' => esc_html__( 'Disable for logged in users', 'defender-security' ),
321 );
322 }
323
324 /**
325 * Validates the form after submission.
326 *
327 * @return void
328 */
329 protected function after_validate(): void {
330 // Case with multi errors.
331 if ( $this->is_unchecked_woo_locations() && $this->is_unchecked_buddypress_locations() ) {
332 // The text of the notation is only in the first key, but we add the number of keys depending on the disabled locations of the plugins.
333 $this->errors['enable_woo'] = esc_html__(
334 'You have enabled reCaptcha for more than one plugin. Please select at least one form location for each plugin and click Save Changes again.',
335 'defender-security'
336 );
337 $this->errors['enable_buddypress'] = '';
338 } else {
339 // Individual cases with plugins.
340 if ( $this->is_unchecked_woo_locations() ) {
341 $this->errors['enable_woo'] = esc_html__(
342 'reCAPTCHA for WooCommerce is enabled, but no WooCommerce forms are selected. Please select at least one WooCommerce form location and then click Save Changes again.',
343 'defender-security'
344 );
345 }
346 if ( $this->is_unchecked_buddypress_locations() ) {
347 $this->errors['enable_buddypress'] = esc_html__(
348 'reCAPTCHA for BuddyPress is enabled, but no BuddyPress forms are selected. Please select at least one BuddyPress form location and then click Save Changes again.',
349 'defender-security'
350 );
351 }
352 }
353 }
354
355 /**
356 * Disable for logged in users or enable.
357 *
358 * @return bool
359 */
360 public function display_for_known_users() {
361 return ! ( $this->disable_for_known_users && is_user_logged_in() );
362 }
363
364 /**
365 * Get module name.
366 *
367 * @return string
368 */
369 public static function get_module_name(): string {
370 return esc_html__( 'Google reCAPTCHA', 'defender-security' );
371 }
372 }
373