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