| 1 |
<?php |
| 2 |
/** |
| 3 |
* Manages the block list monitoring functionality for domains. |
| 4 |
* |
| 5 |
* @package WP_Defender\Controller |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Controller; |
| 9 |
|
| 10 |
use WP_Error; |
| 11 |
use WP_Defender\Controller; |
| 12 |
use Calotes\Component\Request; |
| 13 |
use Calotes\Component\Response; |
| 14 |
use WP_Defender\Behavior\WPMUDEV; |
| 15 |
|
| 16 |
/** |
| 17 |
* Manages the block list monitoring functionality for domains. |
| 18 |
*/ |
| 19 |
class Blocklist_Monitor extends Controller { |
| 20 |
|
| 21 |
public const CACHE_BLACKLIST_STATUS = 'wpdefender_blacklist_status', CACHE_TIME = 300; |
| 22 |
|
| 23 |
/** |
| 24 |
* Initializes the model and service, registers routes, and sets up scheduled events if the model is active. |
| 25 |
*/ |
| 26 |
public function __construct() { |
| 27 |
$this->attach_behavior( WPMUDEV::class, WPMUDEV::class ); |
| 28 |
$this->register_routes(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Converts the current object state to an array. |
| 33 |
* |
| 34 |
* @return array The array representation of the object. |
| 35 |
*/ |
| 36 |
public function to_array(): array { |
| 37 |
return array(); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Removes settings for all submodules. |
| 42 |
*/ |
| 43 |
public function remove_settings() { |
| 44 |
$this->reset_blocklist_monitor(); |
| 45 |
delete_site_transient( self::CACHE_BLACKLIST_STATUS ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Delete all the data & the cache. |
| 50 |
*/ |
| 51 |
public function remove_data() { |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* All the variables that we will show on frontend, both in the main page, or dashboard widget. |
| 56 |
* |
| 57 |
* @return array |
| 58 |
*/ |
| 59 |
public function data_frontend() { |
| 60 |
return $this->dump_routes_and_nonces(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Imports data into the model. |
| 65 |
* |
| 66 |
* @param array $data Data to be imported into the model. |
| 67 |
*/ |
| 68 |
public function import_data( array $data ) { |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Resets the block list monitor by making a DELETE request to the WPMUDEV API. |
| 73 |
* |
| 74 |
* @return mixed The response from the WPMUDEV API. |
| 75 |
*/ |
| 76 |
private function reset_blocklist_monitor() { |
| 77 |
return $this->make_wpmu_request( WPMUDEV::API_BLACKLIST, array(), array( 'method' => 'DELETE' ) ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Getting domain status. |
| 82 |
* |
| 83 |
* @return int|WP_Error |
| 84 |
*/ |
| 85 |
protected function domain_status() { |
| 86 |
$response = $this->make_wpmu_request( WPMUDEV::API_BLACKLIST ); |
| 87 |
|
| 88 |
if ( is_wp_error( $response ) ) { |
| 89 |
if ( 412 === $response->get_error_code() ) { |
| 90 |
return - 1; |
| 91 |
} |
| 92 |
|
| 93 |
return $response; |
| 94 |
} |
| 95 |
$status = 1; |
| 96 |
foreach ( $response['services'] as $service ) { |
| 97 |
if ( true === $service['blacklisted'] ) { |
| 98 |
$status = 0; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return $status; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Endpoint for getting domain status. |
| 107 |
* |
| 108 |
* @return Response |
| 109 |
* @defender_route |
| 110 |
*/ |
| 111 |
public function blacklist_status(): Response { |
| 112 |
$status = get_site_transient( self::CACHE_BLACKLIST_STATUS ); |
| 113 |
if ( false === $status ) { |
| 114 |
$status = $this->domain_status(); |
| 115 |
set_site_transient( self::CACHE_BLACKLIST_STATUS, $status, self::CACHE_TIME ); |
| 116 |
} |
| 117 |
|
| 118 |
if ( is_wp_error( $status ) ) { |
| 119 |
return new Response( |
| 120 |
false, |
| 121 |
array( |
| 122 |
'status_error' => $status->get_error_message(), |
| 123 |
) |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
return new Response( true, array( 'status' => $status ) ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Toggles the domain's block list status based on the provided status. |
| 132 |
* |
| 133 |
* @param Request $request The request object containing the new status. |
| 134 |
* |
| 135 |
* @return bool|Response True on success, or Response object on failure. |
| 136 |
* @defender_route |
| 137 |
*/ |
| 138 |
public function toggle_blacklist_status( Request $request ) { |
| 139 |
$data = $request->get_data( |
| 140 |
array( |
| 141 |
'status' => array( |
| 142 |
'type' => 'string', |
| 143 |
'sanitize' => 'sanitize_text_field', |
| 144 |
), |
| 145 |
) |
| 146 |
); |
| 147 |
$current_status = $data['status'] ?? null; |
| 148 |
if ( ! in_array( $current_status, array( 'good', 'new', 'blacklisted' ), true ) ) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
if ( ! $this->is_pro() ) { |
| 153 |
return new Response( |
| 154 |
false, |
| 155 |
array( |
| 156 |
'message' => esc_html__( 'A WPMU DEV subscription is required for blocklist monitoring.', 'defender-security' ), |
| 157 |
) |
| 158 |
); |
| 159 |
} |
| 160 |
if ( 'new' === $current_status ) { |
| 161 |
$this->make_wpmu_request( WPMUDEV::API_BLACKLIST, array(), array( 'method' => 'POST' ) ); |
| 162 |
$status = $this->domain_status(); |
| 163 |
} else { |
| 164 |
$this->reset_blocklist_monitor(); |
| 165 |
$status = - 1; |
| 166 |
} |
| 167 |
set_site_transient( self::CACHE_BLACKLIST_STATUS, $status, self::CACHE_TIME ); |
| 168 |
|
| 169 |
return new Response( true, array( 'status' => $status ) ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Changes the domain's block list status if it differs from the current status. |
| 174 |
* |
| 175 |
* @param string $current_status The current status to compare against. |
| 176 |
* |
| 177 |
* @return void |
| 178 |
*/ |
| 179 |
public function change_status( $current_status ): void { |
| 180 |
$status = get_site_transient( self::CACHE_BLACKLIST_STATUS ); |
| 181 |
if ( false === $status ) { |
| 182 |
$status = $this->domain_status(); |
| 183 |
} |
| 184 |
// Check changes. |
| 185 |
if ( $status !== $current_status ) { |
| 186 |
if ( '1' === $current_status ) { |
| 187 |
$this->make_wpmu_request( WPMUDEV::API_BLACKLIST, array(), array( 'method' => 'POST' ) ); |
| 188 |
$status = $this->domain_status(); |
| 189 |
} else { |
| 190 |
$this->reset_blocklist_monitor(); |
| 191 |
$status = - 1; |
| 192 |
} |
| 193 |
set_site_transient( self::CACHE_BLACKLIST_STATUS, $status, self::CACHE_TIME ); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Get domain status. Variants: |
| 199 |
* '1' -> 'good' |
| 200 |
*/ |
| 201 |
public function get_status() { |
| 202 |
$status = get_site_transient( self::CACHE_BLACKLIST_STATUS ); |
| 203 |
if ( false === $status ) { |
| 204 |
$status = $this->domain_status(); |
| 205 |
} |
| 206 |
|
| 207 |
if ( is_wp_error( $status ) ) { |
| 208 |
return $status->get_error_message(); |
| 209 |
} |
| 210 |
|
| 211 |
return $status; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Exports strings. |
| 216 |
* |
| 217 |
* @return array An array of strings. |
| 218 |
*/ |
| 219 |
public function export_strings(): array { |
| 220 |
if ( ! $this->is_pro() ) { |
| 221 |
return array( |
| 222 |
sprintf( |
| 223 |
/* translators: %s: Html for Pro-tag. */ |
| 224 |
esc_html__( 'Inactive %s', 'defender-security' ), |
| 225 |
'<span class="sui-tag sui-tag-pro">Pro</span>' |
| 226 |
), |
| 227 |
); |
| 228 |
} |
| 229 |
|
| 230 |
if ( '1' === (string) $this->get_status() ) { |
| 231 |
$strings = array( esc_html__( 'Active', 'defender-security' ) ); |
| 232 |
} else { |
| 233 |
$strings = array( esc_html__( 'Inactive', 'defender-security' ) ); |
| 234 |
} |
| 235 |
|
| 236 |
return $strings; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Configures strings based on the provided configuration and subscription status. |
| 241 |
* |
| 242 |
* @param array $config Configuration array. |
| 243 |
* @param bool $is_pro Indicates whether the subscription is Pro. |
| 244 |
* |
| 245 |
* @return array An array of configuration strings. |
| 246 |
*/ |
| 247 |
public function config_strings( $config, $is_pro ): array { |
| 248 |
if ( $is_pro ) { |
| 249 |
$strings = $config['enabled'] ? array( esc_html__( 'Active', 'defender-security' ) ) : array( |
| 250 |
esc_html__( 'Inactive', 'defender-security' ), |
| 251 |
); |
| 252 |
} else { |
| 253 |
$strings = array( |
| 254 |
sprintf( |
| 255 |
/* translators: %s: Html for Pro-tag. */ |
| 256 |
esc_html__( 'Inactive %s', 'defender-security' ), |
| 257 |
'<span class="sui-tag sui-tag-pro">Pro</span>' |
| 258 |
), |
| 259 |
); |
| 260 |
} |
| 261 |
|
| 262 |
return $strings; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Define settings labels. |
| 267 |
* |
| 268 |
* @return array |
| 269 |
*/ |
| 270 |
public function labels(): array { |
| 271 |
return array( |
| 272 |
'enabled' => esc_html__( 'Blocklist Monitor', 'defender-security' ), |
| 273 |
'status' => esc_html__( 'Status', 'defender-security' ), |
| 274 |
); |
| 275 |
} |
| 276 |
} |
| 277 |
|