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

class-strong-password.php in Defender Security – Malware Scanner, Login Security & Firewall 5.5.1, at src/controller/class-strong-password.php

227 lines 6.3 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\Controller
6 */
7
8 namespace WP_Defender\Controller;
9
10 use WP_Defender\Event;
11 use Calotes\Component\Request;
12 use Calotes\Component\Response;
13 use WP_Defender\Integrations\Woocommerce;
14 use WP_Defender\Component\Config\Config_Hub_Helper;
15 use WP_Defender\Component\Strong_Password as Service;
16 use WP_Defender\Model\Setting\Strong_Password as Settings;
17
18 /**
19 * Handle strong password module.
20 */
21 class Strong_Password extends Event {
22
23 /**
24 * The model for the strong password module.
25 *
26 * @var Settings|null
27 */
28 protected ?Settings $model = null;
29
30 /**
31 * The service for the strong password module.
32 *
33 * @var Service|null
34 */
35 protected ?Service $service = null;
36
37 /**
38 * Helper instance for reuse methods from pwned password protection.
39 *
40 * @var Password_Protection|null
41 */
42 private ?Password_Protection $helper;
43
44 /**
45 * Initializes the model and service, registers routes, and sets up scheduled events if the model is active.
46 */
47 public function __construct() {
48 $this->model = wd_di()->get( Settings::class );
49 $this->service = wd_di()->get( Service::class );
50 $this->helper = wd_di()->get( Password_Protection::class );
51 add_filter( 'wp_defender_advanced_tools_data', array( $this, 'script_data' ) );
52 $this->register_routes();
53 if ( $this->model->is_active() ) {
54 // Update site url on sub-site when MaskLogin is disabled.
55 if (
56 is_multisite() && ! is_main_site()
57 && ! wd_di()->get( \WP_Defender\Model\Setting\Mask_Login::class )->is_active()
58 ) {
59 add_filter( 'network_site_url', array( $this->helper, 'filter_site_url' ), 100, 2 );
60 }
61 remove_action( 'user_profile_update_errors', 'slt_fsp_validate_profile_update', 0 );
62 remove_action( 'resetpass_form', 'slt_fsp_validate_resetpass_form', 10 );
63 if ( is_admin() ) {
64 add_action( 'admin_enqueue_scripts', array( $this->service, 'scripts' ) );
65 add_action( 'user_profile_update_errors', array( $this->service, 'on_profile_update' ), 100, 3 );
66 }
67 add_action( 'login_enqueue_scripts', array( $this->service, 'scripts' ) );
68 add_action( 'wp_enqueue_scripts', array( $this->service, 'scripts' ) );
69 add_action( 'validate_password_reset', array( $this->service, 'on_password_reset' ), 100, 2 );
70 add_action( 'wp_authenticate_user', array( $this->service, 'during_authentication' ), 100, 2 );
71 add_filter( 'random_password', array( $this->service, 'generate_password' ), 10, 2 );
72 if ( wd_di()->get( Woocommerce::class )->is_wc_login_context() ) {
73 add_filter( 'woocommerce_reset_password_message', array( $this->service, 'add_woocommerce_error_message' ) );
74 }
75 }
76 }
77
78 /**
79 * Provide data to the frontend via localized script.
80 *
81 * @param array $data Data collection is ready to passed.
82 *
83 * @return array Modified data array with added this controller data.
84 */
85 public function script_data( array $data ): array {
86 $data['strong_password'] = $this->data_frontend();
87
88 return $data;
89 }
90
91 /**
92 * All the variables that we will show on frontend, both in the main page, or dashboard widget.
93 *
94 * @return array
95 */
96 public function data_frontend(): array {
97 return array_merge(
98 array(
99 'is_active' => $this->model->is_active(),
100 'model' => $this->model->export(),
101 'all_roles' => wp_list_pluck( get_editable_roles(), 'name' ),
102 ),
103 $this->dump_routes_and_nonces()
104 );
105 }
106
107 /**
108 * Save settings.
109 *
110 * @param Request $request The request object containing new settings data.
111 *
112 * @return Response
113 * @defender_route
114 */
115 public function save_settings( Request $request ): Response {
116 $model_data = $request->get_data_by_model( $this->model );
117 $this->model->import( $model_data );
118 if ( $this->model->validate() ) {
119 $this->model->save();
120 Config_Hub_Helper::set_clear_active_flag();
121
122 $response = array(
123 'auto_close' => true,
124 );
125 if ( $this->model->enabled && empty( $this->model->user_roles ) ) {
126 // we need to control this message in front.
127 $response['warning'] = sprintf(
128 /* translators: 1. Open tag. 2. Close tag. */
129 esc_html__( 'You need to check at least one of the %1$sStrong Passwords checks preferences below%2$s and save your settings to enable Strong Password Protection.', 'defender-security' ),
130 '<b>',
131 '</b>'
132 );
133
134 return new Response( true, array_merge( $response, $this->data_frontend() ) );
135 }
136
137 if ( $this->maybe_track() ) {
138 $prev_data = $this->model->get_old_settings();
139 if ( ! empty( $prev_data ) ) {
140 if ( $this->model->enabled && ! $prev_data['enabled'] ) {
141 $need_track = true;
142 $event = 'def_feature_activated';
143 } elseif ( ! $this->model->enabled && $prev_data['enabled'] ) {
144 $need_track = true;
145 $event = 'def_feature_deactivated';
146 } else {
147 $need_track = false;
148 }
149
150 if ( $need_track ) {
151 $data = array(
152 'Feature' => 'Strong Password',
153 'Triggered From' => 'Feature page',
154 );
155 $this->track_feature( $event, $data );
156 }
157 }
158 }
159
160 return new Response( true, array_merge( $response, $this->data_frontend() ) );
161 }
162
163 return new Response(
164 false,
165 array(
166 'message' => $this->model->get_formatted_errors(),
167 )
168 );
169 }
170
171 /**
172 * Export the data of this module, we will use this for export to HUB, create a preset etc.
173 *
174 * @return array
175 */
176 public function to_array() {
177 return array();
178 }
179
180 /**
181 * Import the data of other source into this, it can be when HUB trigger the import, or user apply a preset.
182 *
183 * @param array $data Data from other source.
184 *
185 * @return null|void
186 */
187 public function import_data( array $data ) {
188 $this->model->import( $data );
189 if ( $this->model->validate() ) {
190 $this->model->save();
191 }
192 }
193
194 /**
195 * Remove all settings, configs generated in this container runtime.
196 *
197 * @return mixed
198 */
199 public function remove_settings() {
200 }
201
202 /**
203 * Remove all data.
204 */
205 public function remove_data() {}
206
207 /**
208 * Export strings.
209 *
210 * @return array
211 */
212 public function export_strings(): array {
213 return array(
214 $this->model->is_active() ? esc_html__( 'Active', 'defender-security' ) : esc_html__( 'Inactive', 'defender-security' ),
215 );
216 }
217
218 /**
219 * Provides data for the dashboard widget.
220 *
221 * @return array An array of dashboard widget data.
222 */
223 public function dashboard_widget(): array {
224 return array( 'model' => $this->model->export() );
225 }
226 }
227