| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Controller; |
| 4 |
|
| 5 |
use Calotes\Component\Response; |
| 6 |
use WP_Defender\Behavior\WPMUDEV; |
| 7 |
use WP_Defender\Controller; |
| 8 |
|
| 9 |
class WAF extends Controller { |
| 10 |
public $slug = 'wdf-waf'; |
| 11 |
|
| 12 |
private $wpmudev; |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
$this->wpmudev = wd_di()->get( \WP_Defender\Behavior\WPMUDEV::class ); |
| 16 |
|
| 17 |
// Return the constructor and do not register WAF page if whitelable is enabled |
| 18 |
if ( $this->wpmudev->is_whitelabel_enabled() ) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
$this->register_page( esc_html__( 'WAF', 'wpdef' ), $this->slug, [ |
| 23 |
&$this, |
| 24 |
'main_view' |
| 25 |
], $this->parent_slug ); |
| 26 |
add_action( 'defender_enqueue_assets', [ &$this, 'enqueue_assets' ] ); |
| 27 |
$this->register_routes(); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Checks whether we're on WPMU DEV Hosting |
| 32 |
* |
| 33 |
* @return bool |
| 34 |
*/ |
| 35 |
public function is_wpmu_hosting() { |
| 36 |
return isset( $_SERVER['WPMUDEV_HOSTED'] ) && ! empty( $_SERVER['WPMUDEV_HOSTED'] ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Enqueue assets & output data |
| 41 |
*/ |
| 42 |
public function enqueue_assets() { |
| 43 |
if ( ! $this->is_page_active() ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
wp_localize_script( 'def-waf', 'waf', $this->data_frontend() ); |
| 47 |
wp_enqueue_script( 'def-waf' ); |
| 48 |
$this->enqueue_main_assets(); |
| 49 |
} |
| 50 |
|
| 51 |
public function main_view() { |
| 52 |
$this->render( 'main' ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @param $site_id |
| 57 |
* |
| 58 |
* @return bool|mixed |
| 59 |
*/ |
| 60 |
public function get_waf_status( $site_id ) { |
| 61 |
if ( false === $site_id ) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
$cached = get_site_transient( 'def_waf_status' ); |
| 66 |
if ( in_array( $cached, [ 'enabled', 'disabled' ] ) ) { |
| 67 |
return $cached === 'enabled'; |
| 68 |
} |
| 69 |
|
| 70 |
$this->attach_behavior( WPMUDEV::class, WPMUDEV::class ); |
| 71 |
$ret = $this->make_wpmu_request( WPMUDEV::API_WAF ); |
| 72 |
if ( is_wp_error( $ret ) ) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
$status = $ret['waf']['is_active']; |
| 76 |
set_site_transient( 'def_waf_status', $status === true ? 'enabled' : 'disabled', 300 ); |
| 77 |
|
| 78 |
return $status; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* And endpoint for removing the cache and return latest data. |
| 83 |
* |
| 84 |
* @return Response |
| 85 |
* @defender_route |
| 86 |
*/ |
| 87 |
public function recheck() { |
| 88 |
delete_site_transient( 'def_waf_status' ); |
| 89 |
|
| 90 |
return new Response( true, [ |
| 91 |
'waf' => $this->data_frontend()['waf'] |
| 92 |
] ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @return bool |
| 97 |
*/ |
| 98 |
public function maybe_show_dashboard_widget() { |
| 99 |
if ( |
| 100 |
//not hosted on us |
| 101 |
! $this->is_wpmu_hosting() |
| 102 |
//is pro |
| 103 |
&& $this->is_pro() === true |
| 104 |
//and enable whitelabel |
| 105 |
&& $this->is_whitelabel_enabled() |
| 106 |
) { |
| 107 |
//hide it |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* This is for dashboard widget. |
| 116 |
* |
| 117 |
* @return array |
| 118 |
*/ |
| 119 |
public function to_array() { |
| 120 |
$this->attach_behavior( WPMUDEV::class, WPMUDEV::class ); |
| 121 |
$site_id = $this->get_site_id(); |
| 122 |
|
| 123 |
return [ |
| 124 |
'waf' => [ |
| 125 |
'hosted' => $this->is_wpmu_hosting(), |
| 126 |
'status' => $this->get_waf_status( $site_id ), |
| 127 |
'maybe_show' => $this->maybe_show_dashboard_widget(), |
| 128 |
], |
| 129 |
]; |
| 130 |
} |
| 131 |
|
| 132 |
public function remove_settings() {} |
| 133 |
|
| 134 |
public function remove_data() {} |
| 135 |
|
| 136 |
/** |
| 137 |
* All the variables that we will show on frontend, both in the main page, or dashboard widget. |
| 138 |
* |
| 139 |
* @return array |
| 140 |
*/ |
| 141 |
public function data_frontend() { |
| 142 |
$this->attach_behavior( WPMUDEV::class, WPMUDEV::class ); |
| 143 |
$site_id = $this->get_site_id(); |
| 144 |
|
| 145 |
return array_merge( [ |
| 146 |
'site_id' => $site_id, |
| 147 |
'waf' => [ |
| 148 |
'hosted' => $this->is_wpmu_hosting(), |
| 149 |
'status' => $this->get_waf_status( $site_id ), |
| 150 |
], |
| 151 |
], $this->dump_routes_and_nonces() ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Import the data of other source into this, it can be when HUB trigger the import, or user apply a preset. |
| 156 |
* |
| 157 |
* @param array $data |
| 158 |
*/ |
| 159 |
public function import_data( $data ) {} |
| 160 |
|
| 161 |
/** |
| 162 |
* @return array |
| 163 |
*/ |
| 164 |
public function export_strings() { |
| 165 |
return []; |
| 166 |
} |
| 167 |
} |
| 168 |
|