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