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

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

180 lines 3.7 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(
47 &$this,
48 'main_view',
49 ),
50 $this->parent_slug
51 );
52 add_action( 'defender_enqueue_assets', array( $this, 'enqueue_assets' ) );
53 $this->register_routes();
54 }
55
56 /**
57 * Enqueues scripts and styles for this page.
58 * Only enqueues assets if the page is active.
59 */
60 public function enqueue_assets() {
61 if ( ! $this->is_page_active() ) {
62 return;
63 }
64 wp_localize_script( 'def-waf', 'waf', $this->data_frontend() );
65 wp_enqueue_script( 'def-waf' );
66 $this->enqueue_main_assets();
67 }
68
69 /**
70 * Renders the main view for this page.
71 */
72 public function main_view() {
73 $this->render( 'main' );
74 }
75
76 /**
77 * Retrieves the WAF status for a given site.
78 *
79 * @return bool Returns false on failure, true if WAF is enabled.
80 */
81 public function get_waf_status(): bool {
82 return ! empty( defender_get_hosting_feature_state( 'waf' ) );
83 }
84
85 /**
86 * Remove the cache and return latest data.
87 *
88 * @return Response
89 * @defender_route
90 */
91 public function recheck(): Response {
92 return new Response( true, array( 'waf' => $this->data_frontend()['waf'] ) );
93 }
94
95 /**
96 * Checks if the WAF dashboard widget should be shown.
97 *
98 * @return bool Whether to show the widget or not.
99 */
100 public function maybe_show_dashboard_widget(): bool {
101 if (
102 // Not hosted on us.
103 ! $this->wpmudev->is_wpmu_hosting()
104 // Pro.
105 && true === $this->wpmudev->is_pro()
106 // Enable whitelabel.
107 && $this->wpmudev->is_whitelabel_enabled()
108 ) {
109 // Hide it.
110 return false;
111 }
112
113 return true;
114 }
115
116 /**
117 * Converts the current object state to an array.
118 *
119 * @return array The array representation of the object.
120 */
121 public function to_array(): array {
122 return array(
123 'waf' => array(
124 'hosted' => $this->wpmudev->is_wpmu_hosting(),
125 'status' => $this->get_waf_status(),
126 'maybe_show' => $this->maybe_show_dashboard_widget(),
127 ),
128 );
129 }
130
131 /**
132 * Removes settings for all submodules.
133 */
134 public function remove_settings() {
135 }
136
137 /**
138 * Delete all the data & the cache.
139 */
140 public function remove_data() {
141 }
142
143 /**
144 * Provides data for the frontend.
145 *
146 * @return array An array of data for the frontend.
147 */
148 public function data_frontend(): array {
149 $site_id = $this->wpmudev->get_site_id();
150
151 return array_merge(
152 array(
153 'site_id' => $site_id,
154 'waf' => array(
155 'hosted' => $this->wpmudev->is_wpmu_hosting(),
156 'status' => $this->get_waf_status(),
157 ),
158 ),
159 $this->dump_routes_and_nonces()
160 );
161 }
162
163 /**
164 * Imports data into the model.
165 *
166 * @param array $data Data to be imported into the model.
167 */
168 public function import_data( array $data ) {
169 }
170
171 /**
172 * Exports strings.
173 *
174 * @return array An array of strings.
175 */
176 public function export_strings(): array {
177 return array();
178 }
179 }
180