PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.6.1
Defender Security – Malware Scanner, Login Security & Firewall v5.6.1
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-strong-password.php

class-strong-password.php in Defender Security – Malware Scanner, Login Security & Firewall 5.6.1, at src/model/setting/class-strong-password.php

183 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handle strong password module.
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 strong password module.
14 */
15 class Strong_Password extends Setting {
16
17 /**
18 * Option name.
19 *
20 * @var string
21 */
22 protected $table = 'wd_strong_password_settings';
23
24 /**
25 * Feature status
26 *
27 * @defender_property
28 * @var bool
29 */
30 public $enabled = false;
31
32 /**
33 * List of user roles.
34 *
35 * @defender_property
36 * @var array
37 */
38 public $user_roles = array();
39
40 /**
41 * Default message.
42 *
43 * @defender_property
44 * @var string
45 */
46 public $message = '';
47
48 /**
49 * Plugin integrations settings.
50 *
51 * @defender_property
52 * @var array
53 */
54 public $plugins = array();
55
56 /**
57 * Form integrations settings.
58 *
59 * @defender_property
60 * @var array
61 */
62 public $forms = array();
63
64 /**
65 * Check if the model is activated.
66 *
67 * @return bool
68 */
69 public function is_active(): bool {
70 return (bool) apply_filters(
71 'wd_strong_password_enable',
72 $this->enabled && count( $this->user_roles ) > 0
73 );
74 }
75
76 /**
77 * Initializes the object before loading.
78 *
79 * @return void
80 */
81 protected function before_load(): void {
82 $this->user_roles = $this->get_default_roles();
83 $this->message = $this->get_message();
84 // Only set defaults if not already configured.
85 if ( 0 === count( $this->plugins ) ) {
86 $this->plugins = $this->get_default_plugins();
87 }
88 if ( 0 === count( $this->forms ) ) {
89 $this->forms = $this->get_default_forms();
90 }
91 }
92
93 /**
94 * Get applicable user roles.
95 */
96 protected function get_default_roles(): array {
97 if ( ! function_exists( 'get_editable_roles' ) ) {
98 return array( 'administrator' );
99 }
100
101 return array_keys( get_editable_roles() );
102 }
103
104 /**
105 * Get error message to show in ui.
106 *
107 * @return string
108 */
109 public function get_message(): string {
110 if ( '' === $this->message ) {
111 return $this->get_default_message();
112 }
113 return $this->message;
114 }
115
116 /**
117 * Get localized default message.
118 */
119 protected function get_default_message(): string {
120 return __(
121 "You are required to change your password because your password doesn't meet the strong password guidelines set by the administrator.",
122 'defender-security'
123 );
124 }
125
126 /**
127 * Get default plugin integrations.
128 */
129 protected function get_default_plugins(): array {
130 return array(
131 'woocommerce' => false,
132 );
133 }
134
135 /**
136 * Get default form integrations.
137 */
138 protected function get_default_forms(): array {
139 return array(
140 'woocommerce' => array(),
141 );
142 }
143
144 /**
145 * Check by deactivated locations. Only if the plugin is enabled.
146 *
147 * @return bool
148 */
149 public function is_unchecked_woo_locations(): bool {
150 return isset( $this->plugins['woocommerce'] ) && $this->plugins['woocommerce'] && array() === $this->forms['woocommerce'];
151 }
152
153 /**
154 * Validates the form after submission.
155 *
156 * @return void
157 */
158 protected function after_validate(): void {
159 if ( $this->is_unchecked_woo_locations() ) {
160 $this->errors['enable_woo'] = esc_html__(
161 "You have enabled Strong password rule for WooCommerce but you've not selected any form yet. Please select at least one form to proceed.",
162 'defender-security'
163 );
164 }
165 }
166
167 /**
168 * Export model data with proper type casting.
169 *
170 * @return array
171 */
172 public function export(): array {
173 $data = parent::export();
174
175 // Cast plugin booleans.
176 if ( isset( $data['plugins']['woocommerce'] ) ) {
177 $data['plugins']['woocommerce'] = (bool) $data['plugins']['woocommerce'];
178 }
179
180 return $data;
181 }
182 }
183