PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / trunk
Defender Security – Malware Scanner, Login Security & Firewall vtrunk
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 3.1.1 All 140 releases
defender-security / src / controller / class-waf.php

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

154 lines 3.8 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 * Option key storing the last known WAF status for change detection.
20 */
21 private const LAST_STATUS_OPTION = 'defender_waf_last_known_status';
22
23 /**
24 * Transient key set for 5 minutes when WAF status changes, used to drive the sync notice.
25 */
26 private const STATUS_CHANGED_TRANSIENT = 'defender_waf_status_changed';
27
28 /**
29 * The slug identifier for this controller.
30 *
31 * @var string
32 */
33 public $slug = 'wdf-waf';
34 /**
35 * The WPMUDEV instance used for interacting with WPMUDEV services.
36 *
37 * @var WPMUDEV
38 */
39 private $wpmudev;
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->wpmudev = wd_di()->get( WPMUDEV::class );
46 $this->register_routes();
47 }
48
49 /**
50 * Remove the cache and return latest data.
51 *
52 * @return Response
53 * @defender_route
54 */
55 public function recheck(): Response {
56 return new Response( true, array( 'waf' => $this->data_frontend() ) );
57 }
58
59 /**
60 * Converts the current object state to an array.
61 *
62 * @return array The array representation of the object.
63 */
64 public function to_array(): array {
65 return array();
66 }
67
68 /**
69 * Removes settings for all submodules.
70 */
71 public function remove_settings() {
72 }
73
74 /**
75 * Delete all the data & the cache.
76 */
77 public function remove_data() {
78 delete_option( self::LAST_STATUS_OPTION );
79 delete_transient( self::STATUS_CHANGED_TRANSIENT );
80 }
81
82 /**
83 * Provides data for the frontend.
84 *
85 * @return array An array of data for the frontend.
86 */
87 public function data_frontend(): array {
88 $current_status = $this->get_waf_status();
89 return array(
90 'site_id' => $this->wpmudev->get_site_id(),
91 'status' => $current_status,
92 'show_sync_notice' => $this->get_show_sync_notice( $current_status ),
93 );
94 }
95
96 /**
97 * Retrieves the WAF status for a given site.
98 *
99 * @return bool Returns false on failure, true if WAF is enabled.
100 */
101 public function get_waf_status(): bool {
102 $status = defender_get_hosting_feature_state( 'waf' );
103 return '' !== $status && true === (bool) $status;
104 }
105
106 /**
107 * Detects whether WAF status has recently changed and returns whether the sync notice should be shown.
108 *
109 * Sets a 5-minute transient on first detection of a status change so the notice persists across page loads.
110 *
111 * @param bool $current_status Current WAF enabled/disabled state.
112 * @return bool True if the sync notice should be displayed.
113 */
114 private function get_show_sync_notice( bool $current_status ): bool {
115 $stored = get_option( self::LAST_STATUS_OPTION, null );
116
117 if ( null === $stored ) {
118 update_option( self::LAST_STATUS_OPTION, $current_status );
119 return false;
120 }
121
122 if ( (bool) $stored !== $current_status ) {
123 update_option( self::LAST_STATUS_OPTION, $current_status );
124 set_transient( self::STATUS_CHANGED_TRANSIENT, true, 5 * MINUTE_IN_SECONDS );
125 return true;
126 }
127
128 return (bool) get_transient( self::STATUS_CHANGED_TRANSIENT );
129 }
130
131 /**
132 * Imports data into the model.
133 *
134 * @param array $data Data to be imported into the model.
135 */
136 public function import_data( array $data ) {
137 }
138
139 /**
140 * Exports strings.
141 *
142 * @return array An array of strings.
143 */
144 public function export_strings(): array {
145 return array(
146 sprintf(
147 /* translators: %s: Html for Pro-tag. */
148 esc_html__( 'Inactive %s', 'defender-security' ),
149 '<span class="sui-tag sui-tag-pro">Pro</span>'
150 ),
151 );
152 }
153 }
154