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-dashboard.php

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

310 lines 8.8 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 // Change the global $submenu variable, because otherwise the menu name/order will not change.
85 $submenu['wp-defender'] = $defender_menu; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
86 }
87
88 global $menu;
89 // Get the total scanning active issues.
90 $count = wd_di()->get( \WP_Defender\Component\Scan::class )->indicator_issue_count();
91
92 $indicator = $count > 0
93 ? ' <span class="update-plugins wd-issue-indicator-sidebar"><span class="plugin-count">' . $count . '</span></span>'
94 : null;
95 foreach ( $menu as $k => $item ) {
96 if ( 'wp-defender' === $item[2] ) {
97 // Add a badge next to the "Defender" menu item in the global $menu variable.
98 $menu[ $k ][0] .= $indicator; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
99 }
100 }
101
102 return $menu_order;
103 }
104
105 /**
106 * Registers the main page in the WordPress admin menu.
107 */
108 protected function add_main_page() {
109 $this->register_page(
110 $this->get_menu_title(),
111 $this->parent_slug,
112 array(
113 &$this,
114 'main_view',
115 ),
116 null,
117 $this->get_menu_icon()
118 );
119 }
120
121 /**
122 * Renders the main view for this page.
123 */
124 public function main_view() {
125 $this->render( 'main' );
126 }
127
128 /**
129 * Enqueues scripts and styles for this page.
130 * Only enqueues assets if the page is active.
131 */
132 public function enqueue_assets() {
133 if ( ! $this->is_page_active() ) {
134 return;
135 }
136 wp_localize_script(
137 'def-dashboard',
138 'dashboard',
139 array_merge( $this->data_frontend(), $this->dump_routes_and_nonces() )
140 );
141 wp_enqueue_script( 'def-dashboard' );
142 $this->enqueue_main_assets();
143 }
144
145 /**
146 * Handles the request to hide new features modal.
147 *
148 * @param Request $request The request object containing data.
149 *
150 * @return Response The response object indicating success or failure.
151 * @defender_route
152 */
153 public function hide_new_features( Request $request ): Response {
154 $data = $request->get_data(
155 array(
156 'intention' => array(
157 'type' => 'string',
158 'sanitize' => 'sanitize_text_field',
159 ),
160 )
161 );
162 $intention = $data['intention'] ?? false;
163 if ( 'welcome_modal' === $intention ) {
164 Feature_Modal::delete_modal_key();
165 }
166
167 return new Response( true, array() );
168 }
169
170 /**
171 * Activate Global IP submodule with the enabled Auto sync option.
172 *
173 * @return Response
174 * @defender_route
175 */
176 public function activate_global_ip(): Response {
177 // Changes for Global IP.
178 $model = wd_di()->get( Global_Ip_Lockout::class );
179 $model->enabled = true;
180 $model->blocklist_autosync = true;
181 $model->save();
182 // Clear Global IP reminder.
183 wd_di()->get( Global_IP_Component::class )->delete_dashboard_notice_reminder();
184 // Changes for Hub.
185 Config_Hub_Helper::set_clear_active_flag();
186
187 return new Response(
188 true,
189 array(
190 'redirect' => network_admin_url( 'admin.php?page=wdf-ip-lockout&view=global-ip' ),
191 'interval' => 1,
192 )
193 );
194 }
195
196 /**
197 * Activate Session Protection submodule.
198 *
199 * @return Response
200 * @defender_route
201 */
202 public function activate_session_protection(): Response {
203 $model = wd_di()->get( Session_Protection::class );
204 $model->enabled = true;
205 $model->save();
206 // Changes for Hub.
207 Config_Hub_Helper::set_clear_active_flag();
208
209 return new Response(
210 true,
211 array(
212 'redirect' => network_admin_url( 'admin.php?page=wdf-advanced-tools&view=session-protection' ),
213 'interval' => 1,
214 )
215 );
216 }
217
218 /**
219 * Remove Global IP notice reminder.
220 *
221 * @return Response
222 * @defender_route
223 */
224 public function remove_global_ip_notice_reminder(): Response {
225 wd_di()->get( Global_IP_Component::class )->delete_dashboard_notice_reminder();
226
227 return new Response( true, array() );
228 }
229
230 /**
231 * Removes settings for all submodules.
232 */
233 public function remove_settings() {
234 wd_di()->get( Feature_Modal::class )->upgrade_site_options();
235 }
236
237 /**
238 * Delete all the data & the cache.
239 */
240 public function remove_data() {
241 }
242
243 /**
244 * Provides data for the frontend.
245 *
246 * @return array An array of data for the frontend.
247 */
248 public function data_frontend(): array {
249 [ $endpoints, $nonces ] = Route::export_routes( 'dashboard' );
250 $firewall = wd_di()->get( Firewall::class );
251
252 return array_merge(
253 wd_di()->get( Feature_Modal::class )->get_dashboard_modals(),
254 array(
255 'scan' => wd_di()->get( Scan::class )->data_frontend(),
256 'firewall' => $firewall->data_frontend(),
257 'waf' => wd_di()->get( WAF::class )->data_frontend(),
258 'audit' => wd_di()->get( Audit_Logging::class )->data_frontend(),
259 'blacklist' => array(
260 'nonces' => $nonces,
261 'endpoints' => $endpoints,
262 ),
263 'blocklist_monitor' => wd_di()->get( Blocklist_Monitor::class )->data_frontend(),
264 'two_fa' => wd_di()->get( Two_Factor::class )->data_frontend(),
265 'advanced_tools' => array(
266 'mask_login' => wd_di()->get( Mask_Login::class )->dashboard_widget(),
267 'security_headers' => wd_di()->get( Security_Headers::class )->dashboard_widget(),
268 'pwned_passwords' => wd_di()->get( Password_Protection::class )->dashboard_widget(),
269 'recaptcha' => wd_di()->get( Recaptcha::class )->dashboard_widget(),
270 'strong_passwords' => wd_di()->get( Strong_Password::class )->dashboard_widget(),
271 'session_protection' => wd_di()->get( Session_Protection::class )->export(),
272 ),
273 'security_tweaks' => wd_di()->get( Security_Tweaks::class )->dashboard_widget(),
274 'notifications' => wd_di()->get( Notification::class )->data_frontend(),
275 'settings' => wd_di()->get( Main_Setting::class )->data_frontend(),
276 'countries' => $firewall->dashboard_widget(),
277 'global_ip' => wd_di()->get( Global_Ip::class )->data_frontend(),
278 'hub_connector' => wd_di()->get( Hub_Connector::class )->data_frontend(),
279 'antibot' => wd_di()->get( Antibot_Global_Firewall::class )->data_frontend(),
280 )
281 );
282 }
283
284 /**
285 * Converts the current object state to an array.
286 *
287 * @return array The array representation of the object.
288 */
289 public function to_array(): array {
290 return array();
291 }
292
293 /**
294 * Imports data into the model.
295 *
296 * @param array $data Data to be imported into the model.
297 */
298 public function import_data( array $data ) {
299 }
300
301 /**
302 * Exports strings.
303 *
304 * @return array An array of strings.
305 */
306 public function export_strings(): array {
307 return array();
308 }
309 }
310