| @@ -14,10 +14,19 @@ | ||
| 14 | 14 | /** |
| 15 | 15 | * Handles the settings, data, and other functionality related to the Web Application Firewall (WAF). |
| 16 | 16 | */ |
| 17 | 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'; | |
| 18 | 22 | |
| 19 | 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 | + /** | |
| 20 | 29 | * The slug identifier for this controller. |
| 21 | 30 | * |
| 22 | 31 | * @var string |
| 23 | 32 | */ |
| @@ -33,57 +42,12 @@ | ||
| 33 | 42 | * Initializes the model and service, registers routes, and sets up scheduled events if the model is active. |
| 34 | 43 | */ |
| 35 | 44 | public function __construct() { |
| 36 | 45 | $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 | 46 | $this->register_routes(); |
| 54 | 47 | } |
| 55 | 48 | |
| 56 | 49 | /** |
| 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 | 50 | * Remove the cache and return latest data. |
| 87 | 51 | * |
| 88 | 52 | * @return Response |
| 89 | 53 | * @defender_route |
| @@ -88,45 +52,18 @@ | ||
| 88 | 52 | * @return Response |
| 89 | 53 | * @defender_route |
| 90 | 54 | */ |
| 91 | 55 | public function recheck(): Response { |
| 92 | - return new Response( true, array( 'waf' => $this->data_frontend()['waf'] ) ); | |
| 56 | + return new Response( true, array( 'waf' => $this->data_frontend() ) ); | |
| 93 | 57 | } |
| 94 | 58 | |
| 95 | 59 | /** |
| 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 | 60 | * Converts the current object state to an array. |
| 118 | 61 | * |
| 119 | 62 | * @return array The array representation of the object. |
| 120 | 63 | */ |
| 121 | 64 | 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 | - ); | |
| 65 | + return array(); | |
| 129 | 66 | } |
| 130 | 67 | |
| 131 | 68 | /** |
| 132 | 69 | * Removes settings for all submodules. |
| @@ -137,8 +74,10 @@ | ||
| 137 | 74 | /** |
| 138 | 75 | * Delete all the data & the cache. |
| 139 | 76 | */ |
| 140 | 77 | public function remove_data() { |
| 78 | + delete_option( self::LAST_STATUS_OPTION ); | |
| 79 | + delete_transient( self::STATUS_CHANGED_TRANSIENT ); | |
| 141 | 80 | } |
| 142 | 81 | |
| 143 | 82 | /** |
| 144 | 83 | * Provides data for the frontend. |
| @@ -145,23 +84,52 @@ | ||
| 145 | 84 | * |
| 146 | 85 | * @return array An array of data for the frontend. |
| 147 | 86 | */ |
| 148 | 87 | public function data_frontend(): array { |
| 149 | - $site_id = $this->wpmudev->get_site_id(); | |
| 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 | + } | |
| 150 | 95 | |
| 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 | - ); | |
| 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; | |
| 161 | 104 | } |
| 162 | 105 | |
| 163 | 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 | + /** | |
| 164 | 132 | * Imports data into the model. |
| 165 | 133 | * |
| 166 | 134 | * @param array $data Data to be imported into the model. |
| 167 | 135 | */ |
| @@ -173,7 +141,13 @@ | ||
| 173 | 141 | * |
| 174 | 142 | * @return array An array of strings. |
| 175 | 143 | */ |
| 176 | 144 | public function export_strings(): array { |
| 177 | - return 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 | + ); | |
| 178 | 152 | } |
| 179 | 153 | } |