| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Controller; |
| 4 |
|
| 5 |
use Calotes\Component\Request; |
| 6 |
use Calotes\Component\Response; |
| 7 |
use Calotes\Helper\HTTP; |
| 8 |
use Calotes\Helper\Route; |
| 9 |
use WP_Defender\Behavior\WPMUDEV; |
| 10 |
use WP_Defender\Controller; |
| 11 |
use WP_Defender\Traits\Formats; |
| 12 |
use WP_Defender\Traits\IO; |
| 13 |
use WP_Defender\Component\Feature_Modal; |
| 14 |
|
| 15 |
/** |
| 16 |
* This class will use to create a main admin page. |
| 17 |
* |
| 18 |
* Class Dashboard |
| 19 |
* @package WP_Defender\Controller |
| 20 |
* @method bool is_pro |
| 21 |
*/ |
| 22 |
class Dashboard extends Controller { |
| 23 |
use IO, Formats; |
| 24 |
|
| 25 |
public $slug = 'wp-defender'; |
| 26 |
|
| 27 |
public function __construct() { |
| 28 |
$this->attach_behavior( WPMUDEV::class, WPMUDEV::class ); |
| 29 |
$this->add_main_page(); |
| 30 |
$this->register_routes(); |
| 31 |
add_action( 'defender_enqueue_assets', array( &$this, 'enqueue_assets' ) ); |
| 32 |
add_filter( 'custom_menu_order', '__return_true' ); |
| 33 |
add_filter( 'menu_order', array( &$this, 'menu_order' ) ); |
| 34 |
add_action( 'admin_init', array( &$this, 'maybe_redirect_notification_request' ), 99 ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Because we move the notifications on separate modules, so links from HUB should be redirected to correct URL. |
| 39 |
* |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
public function maybe_redirect_notification_request() { |
| 43 |
$page = HTTP::get( 'page' ); |
| 44 |
if ( ! in_array( $page, array( 'wdf-scan', 'wdf-ip-lockout', 'wdf-hardener', 'wdf-logging' ), true ) ) { |
| 45 |
return; |
| 46 |
} |
| 47 |
$view = HTTP::get( 'view' ); |
| 48 |
if ( in_array( $view, array( 'reporting', 'notification', 'report' ), true ) ) { |
| 49 |
wp_redirect( network_admin_url( 'admin.php?page=wdf-notification' ) ); |
| 50 |
exit; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Filter out the defender menu for changing text. |
| 56 |
* |
| 57 |
* @param $menu_order |
| 58 |
* |
| 59 |
* @return mixed |
| 60 |
*/ |
| 61 |
public function menu_order( $menu_order ) { |
| 62 |
global $submenu; |
| 63 |
if ( isset( $submenu['wp-defender'] ) ) { |
| 64 |
$defender_menu = $submenu['wp-defender']; |
| 65 |
$defender_menu[0][0] = esc_html__( 'Dashboard', 'wpdef' ); |
| 66 |
$defender_menu = array_values( $defender_menu ); |
| 67 |
$submenu['wp-defender'] = $defender_menu; |
| 68 |
} |
| 69 |
|
| 70 |
global $menu; |
| 71 |
// Get the total scanning active issues. |
| 72 |
$count = wd_di()->get( \WP_Defender\Component\Scan::class )->indicator_issue_count(); |
| 73 |
|
| 74 |
$indicator = $count > 0 |
| 75 |
? ' <span class="update-plugins wd-issue-indicator-sidebar"><span class="plugin-count">' . $count . '</span></span>' |
| 76 |
: null; |
| 77 |
foreach ( $menu as $k => $item ) { |
| 78 |
if ( 'wp-defender' === $item[2] ) { |
| 79 |
$menu[ $k ][0] .= $indicator; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return $menu_order; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Determine if we should show the quick setup. |
| 88 |
* This will show in such scenario: |
| 89 |
* 1. New setup. |
| 90 |
* 2. Just upgrade from free. |
| 91 |
* |
| 92 |
* @return int |
| 93 |
* @defender_property |
| 94 |
*/ |
| 95 |
public function maybe_show_quick_setup() { |
| 96 |
if ( get_site_transient( 'wp_defender_is_free_activated' ) === 1 ) { |
| 97 |
return 1; |
| 98 |
} |
| 99 |
|
| 100 |
// Site just created. |
| 101 |
if ( get_site_option( 'wp_defender_shown_activator' ) === false ) { |
| 102 |
return 1; |
| 103 |
} |
| 104 |
|
| 105 |
return 0; |
| 106 |
} |
| 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 |
public function main_view() { |
| 122 |
$this->render( 'main' ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Enqueue assets & output data. |
| 127 |
*/ |
| 128 |
public function enqueue_assets() { |
| 129 |
if ( ! $this->is_page_active() ) { |
| 130 |
return; |
| 131 |
} |
| 132 |
wp_localize_script( 'def-dashboard', 'dashboard', array_merge( $this->data_frontend(), $this->dump_routes_and_nonces() ) ); |
| 133 |
wp_enqueue_script( 'def-dashboard' ); |
| 134 |
$this->enqueue_main_assets(); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* @param Request $request |
| 139 |
* |
| 140 |
* @return Response |
| 141 |
* @defender_route |
| 142 |
*/ |
| 143 |
public function hide_new_features( Request $request ) { |
| 144 |
$data = $request->get_data( |
| 145 |
array( |
| 146 |
'intention' => array( |
| 147 |
'type' => 'string', |
| 148 |
'sanitize' => 'sanitize_text_field', |
| 149 |
), |
| 150 |
) |
| 151 |
); |
| 152 |
$intention = $data['intention'] ?? false; |
| 153 |
if ( 'welcome_modal' === $intention ) { |
| 154 |
delete_site_option( Feature_Modal::FEATURE_SLUG ); |
| 155 |
} |
| 156 |
|
| 157 |
return new Response( true, array() ); |
| 158 |
} |
| 159 |
|
| 160 |
public function remove_settings() { |
| 161 |
wd_di()->get( Feature_Modal::class )->upgrade_site_options(); |
| 162 |
} |
| 163 |
|
| 164 |
public function remove_data() {} |
| 165 |
|
| 166 |
/** |
| 167 |
* @return array |
| 168 |
*/ |
| 169 |
public function data_frontend() { |
| 170 |
[$endpoints, $nonces] = Route::export_routes( 'dashboard' ); |
| 171 |
|
| 172 |
return array_merge( |
| 173 |
wd_di()->get( Feature_Modal::class )->get_dashboard_modals(), |
| 174 |
array( |
| 175 |
'scan' => wd_di()->get( Scan::class )->data_frontend(), |
| 176 |
'firewall' => wd_di()->get( Firewall::class )->data_frontend(), |
| 177 |
'waf' => wd_di()->get( WAF::class )->data_frontend(), |
| 178 |
'audit' => wd_di()->get( Audit_Logging::class )->data_frontend(), |
| 179 |
'blacklist' => array( |
| 180 |
'nonces' => $nonces, |
| 181 |
'endpoints' => $endpoints, |
| 182 |
), |
| 183 |
'blocklist_monitor' => wd_di()->get( Blocklist_Monitor::class )->data_frontend(), |
| 184 |
'two_fa' => wd_di()->get( Two_Factor::class )->data_frontend(), |
| 185 |
'advanced_tools' => array( |
| 186 |
'mask_login' => wd_di()->get( Mask_Login::class )->dashboard_widget(), |
| 187 |
'security_headers' => wd_di()->get( Security_Headers::class )->dashboard_widget(), |
| 188 |
'pwned_passwords' => wd_di()->get( Password_Protection::class )->dashboard_widget(), |
| 189 |
'recaptcha' => wd_di()->get( Recaptcha::class )->dashboard_widget(), |
| 190 |
), |
| 191 |
'security_tweaks' => wd_di()->get( Security_Tweaks::class )->data_frontend(), |
| 192 |
'tutorials' => wd_di()->get( Tutorial::class )->data_frontend(), |
| 193 |
'notifications' => wd_di()->get( Notification::class )->data_frontend(), |
| 194 |
'settings' => wd_di()->get( Main_Setting::class )->data_frontend(), |
| 195 |
) |
| 196 |
); |
| 197 |
} |
| 198 |
|
| 199 |
public function to_array() {} |
| 200 |
|
| 201 |
public function import_data( $data ) {} |
| 202 |
|
| 203 |
/** |
| 204 |
* @return array |
| 205 |
*/ |
| 206 |
public function export_strings() { |
| 207 |
return array(); |
| 208 |
} |
| 209 |
} |
| 210 |
|