PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.7.2
Defender Security – Malware Scanner, Login Security & Firewall v5.7.2
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-waf.php

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

183 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles Web Application Firewall related functionality.
4 *
5 * @package WP_Defender\Controller
6 */
7
8 namespace WP_Defender\Controller;
9
10 use WP_Defender\Controller;
11 use Calotes\Component\Response;
12 use WP_Defender\Behavior\WPMUDEV;
13
14 /**
15 * Handles the settings, data, and other functionality related to the Web Application Firewall (WAF).
16 */
17 class WAF extends Controller {
18
19 /**
20 * The slug identifier for this controller.
21 *
22 * @var string
23 */
24 public $slug = 'wdf-waf';
25 /**
26 * The WPMUDEV instance used for interacting with WPMUDEV services.
27 *
28 * @var WPMUDEV
29 */
30 private $wpmudev;
31
32 /**
33 * Initializes the model and service, registers routes, and sets up scheduled events if the model is active.
34 */
35 public function __construct() {
36 $this->wpmudev = wd_di()->get( WPMUDEV::class );
37
38 // Return the constructor and do not register WAF page if Whitelable is enabled.
39 if ( $this->wpmudev->is_whitelabel_enabled() ) {
40 return;
41 }
42
43 $this->register_page(
44 esc_html__( 'WAF', 'defender-security' ),
45 $this->slug,
46 array( $this, 'main_view' ),
47 $this->parent_slug
48 );
49 add_action( 'defender_enqueue_assets', array( $this, 'enqueue_assets' ) );
50 $this->register_routes();
51 }
52
53 /**
54 * Enqueues scripts and styles for this page.
55 * Only enqueues assets if the page is active.
56 */
57 public function enqueue_assets() {
58 if ( ! $this->is_page_active() ) {
59 return;
60 }
61 wp_localize_script( 'def-waf', 'waf', $this->data_frontend() );
62 wp_enqueue_script( 'def-waf' );
63 $this->enqueue_main_assets();
64 }
65
66 /**
67 * Renders the main view for this page.
68 */
69 public function main_view() {
70 $this->render( 'main' );
71 }
72
73 /**
74 * Retrieves the WAF status for a given site.
75 *
76 * @return bool Returns false on failure, true if WAF is enabled.
77 */
78 public function get_waf_status(): bool {
79 return ! empty( defender_get_hosting_feature_state( 'waf' ) );
80 }
81
82 /**
83 * Remove the cache and return latest data.
84 *
85 * @return Response
86 * @defender_route
87 */
88 public function recheck(): Response {
89 return new Response( true, array( 'waf' => $this->data_frontend()['waf'] ) );
90 }
91
92 /**
93 * Checks if the WAF dashboard widget should be shown.
94 *
95 * @return bool Whether to show the widget or not.
96 */
97 public function maybe_show_dashboard_widget(): bool {
98 if (
99 // Not hosted on us.
100 ! $this->wpmudev->is_wpmu_hosting()
101 // Pro.
102 && true === $this->wpmudev->is_pro()
103 // Enable whitelabel.
104 && $this->wpmudev->is_whitelabel_enabled()
105 ) {
106 // Hide it.
107 return false;
108 }
109
110 return true;
111 }
112
113 /**
114 * Converts the current object state to an array.
115 *
116 * @return array The array representation of the object.
117 */
118 public function to_array(): array {
119 return array(
120 'waf' => array(
121 'hosted' => $this->wpmudev->is_wpmu_hosting(),
122 'status' => $this->get_waf_status(),
123 'maybe_show' => $this->maybe_show_dashboard_widget(),
124 ),
125 );
126 }
127
128 /**
129 * Removes settings for all submodules.
130 *
131 * @return mixed
132 */
133 public function remove_settings() {
134 }
135
136 /**
137 * Delete all the data & the cache.
138 *
139 * @return mixed
140 */
141 public function remove_data() {
142 }
143
144 /**
145 * Provides data for the frontend.
146 *
147 * @return array An array of data for the frontend.
148 */
149 public function data_frontend(): array {
150 $site_id = $this->wpmudev->get_site_id();
151
152 return array_merge(
153 array(
154 'site_id' => $site_id,
155 'waf' => array(
156 'hosted' => $this->wpmudev->is_wpmu_hosting(),
157 'status' => $this->get_waf_status(),
158 ),
159 'hub_connector' => wd_di()->get( Hub_Connector::class )->data_frontend(),
160 'antibot' => wd_di()->get( Antibot_Global_Firewall::class )->data_frontend(),
161 ),
162 $this->dump_routes_and_nonces()
163 );
164 }
165
166 /**
167 * Imports data into the model.
168 *
169 * @param array $data Data to be imported into the model.
170 */
171 public function import_data( array $data ) {
172 }
173
174 /**
175 * Exports strings.
176 *
177 * @return array An array of strings.
178 */
179 public function export_strings(): array {
180 return array();
181 }
182 }
183