PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.4.0
Defender Security – Malware Scanner, Login Security & Firewall v5.4.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 / controller / class-dashboard.php

class-dashboard.php in Defender Security – Malware Scanner, Login Security & Firewall 5.4.0, at src/controller/class-dashboard.php

308 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles the main admin page.
4 *
5 * @package WP_Defender\Controller
6 */
7
8 namespace WP_Defender\Controller;
9
10 use WP_Defender\Event;
11 use Calotes\Helper\HTTP;
12 use Calotes\Helper\Route;
13 use WP_Defender\Traits\IO;
14 use Calotes\Component\Request;
15 use Calotes\Component\Response;
16 use WP_Defender\Traits\Formats;
17 use WP_Defender\Behavior\WPMUDEV;
18 use WP_Defender\Component\Feature_Modal;
19 use WP_Defender\Controller\Hub_Connector;
20 use WP_Defender\Model\Setting\Global_Ip_Lockout;
21 use WP_Defender\Component\Config\Config_Hub_Helper;
22 use WP_Defender\Component\IP\Global_IP as Global_IP_Component;
23 use WP_Defender\Controller\Antibot_Global_Firewall;
24 use WP_Defender\Model\Setting\Session_Protection;
25
26 /**
27 * Handles the main admin page.
28 */
29 class Dashboard extends Event {
30
31 use IO;
32 use Formats;
33
34 /**
35 * The slug identifier for this controller.
36 *
37 * @var string
38 */
39 public $slug = 'wp-defender';
40
41 /**
42 * Initializes the model and service, registers routes, and sets up scheduled events if the model is active.
43 */
44 public function __construct() {
45 $this->attach_behavior( WPMUDEV::class, WPMUDEV::class );
46 $this->add_main_page();
47 $this->register_routes();
48 add_action( 'defender_enqueue_assets', array( $this, 'enqueue_assets' ) );
49 add_filter( 'custom_menu_order', '__return_true' );
50 add_filter( 'menu_order', array( $this, 'menu_order' ) );
51 add_action( 'admin_init', array( $this, 'maybe_redirect_notification_request' ), 99 );
52 }
53
54 /**
55 * Because we move the notifications on separate modules, so links from HUB should be redirected to correct URL.
56 *
57 * @return void|null
58 */
59 public function maybe_redirect_notification_request() {
60 $page = HTTP::get( 'page' );
61 if ( ! in_array( $page, array( 'wdf-scan', 'wdf-ip-lockout', 'wdf-hardener', 'wdf-logging' ), true ) ) {
62 return;
63 }
64 $view = HTTP::get( 'view' );
65 if ( in_array( $view, array( 'reporting', 'notification', 'report' ), true ) ) {
66 wp_safe_redirect( network_admin_url( 'admin.php?page=wdf-notification' ) );
67 exit;
68 }
69 }
70
71 /**
72 * Filter out the defender menu for changing text.
73 *
74 * @param array $menu_order The current menu order.
75 *
76 * @return array
77 */
78 public function menu_order( $menu_order ) {
79 global $submenu;
80 if ( isset( $submenu['wp-defender'] ) ) {
81 $defender_menu = $submenu['wp-defender'];
82 $defender_menu[0][0] = esc_html__( 'Dashboard', 'defender-security' );
83 $defender_menu = array_values( $defender_menu );
84 $submenu['wp-defender'] = $defender_menu; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
85 }
86
87 global $menu;
88 // Get the total scanning active issues.
89 $count = wd_di()->get( \WP_Defender\Component\Scan::class )->indicator_issue_count();
90
91 $indicator = $count > 0
92 ? ' <span class="update-plugins wd-issue-indicator-sidebar"><span class="plugin-count">' . $count . '</span></span>'
93 : null;
94 foreach ( $menu as $k => $item ) {
95 if ( 'wp-defender' === $item[2] ) {
96 $menu[ $k ][0] .= $indicator; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
97 }
98 }
99
100 return $menu_order;
101 }
102
103 /**
104 * Registers the main page in the WordPress admin menu.
105 */
106 protected function add_main_page() {
107 $this->register_page(
108 $this->get_menu_title(),
109 $this->parent_slug,
110 array(
111 &$this,
112 'main_view',
113 ),
114 null,
115 $this->get_menu_icon()
116 );
117 }
118
119 /**
120 * Renders the main view for this page.
121 */
122 public function main_view() {
123 $this->render( 'main' );
124 }
125
126 /**
127 * Enqueues scripts and styles for this page.
128 * Only enqueues assets if the page is active.
129 */
130 public function enqueue_assets() {
131 if ( ! $this->is_page_active() ) {
132 return;
133 }
134 wp_localize_script(
135 'def-dashboard',
136 'dashboard',
137 array_merge( $this->data_frontend(), $this->dump_routes_and_nonces() )
138 );
139 wp_enqueue_script( 'def-dashboard' );
140 $this->enqueue_main_assets();
141 }
142
143 /**
144 * Handles the request to hide new features modal.
145 *
146 * @param Request $request The request object containing data.
147 *
148 * @return Response The response object indicating success or failure.
149 * @defender_route
150 */
151 public function hide_new_features( Request $request ): Response {
152 $data = $request->get_data(
153 array(
154 'intention' => array(
155 'type' => 'string',
156 'sanitize' => 'sanitize_text_field',
157 ),
158 )
159 );
160 $intention = $data['intention'] ?? false;
161 if ( 'welcome_modal' === $intention ) {
162 Feature_Modal::delete_modal_key();
163 }
164
165 return new Response( true, array() );
166 }
167
168 /**
169 * Activate Global IP submodule with the enabled Auto sync option.
170 *
171 * @return Response
172 * @defender_route
173 */
174 public function activate_global_ip(): Response {
175 // Changes for Global IP.
176 $model = wd_di()->get( Global_Ip_Lockout::class );
177 $model->enabled = true;
178 $model->blocklist_autosync = true;
179 $model->save();
180 // Clear Global IP reminder.
181 wd_di()->get( Global_IP_Component::class )->delete_dashboard_notice_reminder();
182 // Changes for Hub.
183 Config_Hub_Helper::set_clear_active_flag();
184
185 return new Response(
186 true,
187 array(
188 'redirect' => network_admin_url( 'admin.php?page=wdf-ip-lockout&view=global-ip' ),
189 'interval' => 1,
190 )
191 );
192 }
193
194 /**
195 * Activate Session Protection submodule.
196 *
197 * @return Response
198 * @defender_route
199 */
200 public function activate_session_protection(): Response {
201 $model = wd_di()->get( Session_Protection::class );
202 $model->enabled = true;
203 $model->save();
204 // Changes for Hub.
205 Config_Hub_Helper::set_clear_active_flag();
206
207 return new Response(
208 true,
209 array(
210 'redirect' => network_admin_url( 'admin.php?page=wdf-advanced-tools&view=session-protection' ),
211 'interval' => 1,
212 )
213 );
214 }
215
216 /**
217 * Remove Global IP notice reminder.
218 *
219 * @return Response
220 * @defender_route
221 */
222 public function remove_global_ip_notice_reminder(): Response {
223 wd_di()->get( Global_IP_Component::class )->delete_dashboard_notice_reminder();
224
225 return new Response( true, array() );
226 }
227
228 /**
229 * Removes settings for all submodules.
230 */
231 public function remove_settings() {
232 wd_di()->get( Feature_Modal::class )->upgrade_site_options();
233 }
234
235 /**
236 * Delete all the data & the cache.
237 */
238 public function remove_data() {
239 }
240
241 /**
242 * Provides data for the frontend.
243 *
244 * @return array An array of data for the frontend.
245 */
246 public function data_frontend(): array {
247 [ $endpoints, $nonces ] = Route::export_routes( 'dashboard' );
248 $firewall = wd_di()->get( Firewall::class );
249
250 return array_merge(
251 wd_di()->get( Feature_Modal::class )->get_dashboard_modals(),
252 array(
253 'scan' => wd_di()->get( Scan::class )->data_frontend(),
254 'firewall' => $firewall->data_frontend(),
255 'waf' => wd_di()->get( WAF::class )->data_frontend(),
256 'audit' => wd_di()->get( Audit_Logging::class )->data_frontend(),
257 'blacklist' => array(
258 'nonces' => $nonces,
259 'endpoints' => $endpoints,
260 ),
261 'blocklist_monitor' => wd_di()->get( Blocklist_Monitor::class )->data_frontend(),
262 'two_fa' => wd_di()->get( Two_Factor::class )->data_frontend(),
263 'advanced_tools' => array(
264 'mask_login' => wd_di()->get( Mask_Login::class )->dashboard_widget(),
265 'security_headers' => wd_di()->get( Security_Headers::class )->dashboard_widget(),
266 'pwned_passwords' => wd_di()->get( Password_Protection::class )->dashboard_widget(),
267 'recaptcha' => wd_di()->get( Recaptcha::class )->dashboard_widget(),
268 'strong_passwords' => wd_di()->get( Strong_Password::class )->dashboard_widget(),
269 'session_protection' => wd_di()->get( Session_Protection::class )->export(),
270 ),
271 'security_tweaks' => wd_di()->get( Security_Tweaks::class )->dashboard_widget(),
272 'notifications' => wd_di()->get( Notification::class )->data_frontend(),
273 'settings' => wd_di()->get( Main_Setting::class )->data_frontend(),
274 'countries' => $firewall->dashboard_widget(),
275 'global_ip' => wd_di()->get( Global_Ip::class )->data_frontend(),
276 'hub_connector' => wd_di()->get( Hub_Connector::class )->data_frontend(),
277 'antibot' => wd_di()->get( Antibot_Global_Firewall::class )->data_frontend(),
278 )
279 );
280 }
281
282 /**
283 * Converts the current object state to an array.
284 *
285 * @return array The array representation of the object.
286 */
287 public function to_array(): array {
288 return array();
289 }
290
291 /**
292 * Imports data into the model.
293 *
294 * @param array $data Data to be imported into the model.
295 */
296 public function import_data( array $data ) {
297 }
298
299 /**
300 * Exports strings.
301 *
302 * @return array An array of strings.
303 */
304 public function export_strings(): array {
305 return array();
306 }
307 }
308