| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Controller; |
| 4 |
|
| 5 |
use Calotes\Component\Request; |
| 6 |
use Calotes\Component\Response; |
| 7 |
use WP_Defender\Controller; |
| 8 |
use WP_Defender\Behavior\WPMUDEV; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Blocklist_Monitor |
| 12 |
* @package WP_Defender\Controller |
| 13 |
*/ |
| 14 |
class Blocklist_Monitor extends Controller { |
| 15 |
|
| 16 |
public const CACHE_BLACKLIST_STATUS = 'wpdefender_blacklist_status', CACHE_TIME = 300; |
| 17 |
|
| 18 |
public function __construct() { |
| 19 |
$this->attach_behavior( WPMUDEV::class, WPMUDEV::class ); |
| 20 |
$this->register_routes(); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* @return mixed |
| 25 |
*/ |
| 26 |
public function to_array() { |
| 27 |
return array(); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Reset settings and clear transient. |
| 32 |
*/ |
| 33 |
public function remove_settings() { |
| 34 |
$this->reset_blocklist_monitor(); |
| 35 |
delete_site_transient( self::CACHE_BLACKLIST_STATUS ); |
| 36 |
} |
| 37 |
|
| 38 |
public function remove_data() {} |
| 39 |
|
| 40 |
/** |
| 41 |
* All the variables that we will show on frontend, both in the main page, or dashboard widget. |
| 42 |
* |
| 43 |
* @return array |
| 44 |
*/ |
| 45 |
public function data_frontend() { |
| 46 |
return $this->dump_routes_and_nonces(); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Import the data of other source into this, it can be when HUB trigger the import, or user apply a preset. |
| 51 |
* |
| 52 |
* @param array $data |
| 53 |
*/ |
| 54 |
public function import_data( $data ) {} |
| 55 |
|
| 56 |
/** |
| 57 |
* @return mixed |
| 58 |
*/ |
| 59 |
private function reset_blocklist_monitor() { |
| 60 |
|
| 61 |
return $this->make_wpmu_request( WPMUDEV::API_BLACKLIST, [], [ |
| 62 |
'method' => 'DELETE' |
| 63 |
] ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Getting domain status. |
| 68 |
* |
| 69 |
* @return int |
| 70 |
*/ |
| 71 |
protected function domain_status() { |
| 72 |
$response = $this->make_wpmu_request( WPMUDEV::API_BLACKLIST ); |
| 73 |
|
| 74 |
if ( is_wp_error( $response ) ) { |
| 75 |
if ( 412 === $response->get_error_code() ) { |
| 76 |
return - 1; |
| 77 |
} |
| 78 |
|
| 79 |
return $response; |
| 80 |
} |
| 81 |
$status = 1; |
| 82 |
foreach ( $response['services'] as $service ) { |
| 83 |
if ( true === $service['blacklisted'] ) { |
| 84 |
$status = 0; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return $status; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Endpoint for getting domain status. |
| 93 |
* @defender_route |
| 94 |
*/ |
| 95 |
public function blacklist_status() { |
| 96 |
$status = get_site_transient( self::CACHE_BLACKLIST_STATUS ); |
| 97 |
if ( false === $status ) { |
| 98 |
$status = $this->domain_status(); |
| 99 |
set_site_transient( self::CACHE_BLACKLIST_STATUS, $status, self::CACHE_TIME ); |
| 100 |
} |
| 101 |
|
| 102 |
if ( is_wp_error( $status ) ) { |
| 103 |
return new Response( false, [ |
| 104 |
'status_error' => $status->get_error_message() |
| 105 |
] ); |
| 106 |
} |
| 107 |
|
| 108 |
return new Response( true, [ |
| 109 |
'status' => $status |
| 110 |
] ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* @param Request $request |
| 115 |
* |
| 116 |
* @return bool|Response |
| 117 |
* @defender_route |
| 118 |
*/ |
| 119 |
public function toggle_blacklist_status( Request $request ) { |
| 120 |
$data = $request->get_data( [ |
| 121 |
'status' => [ |
| 122 |
'type' => 'string', |
| 123 |
'sanitize' => 'sanitize_text_field' |
| 124 |
] |
| 125 |
] ); |
| 126 |
$current_status = $data['status'] ?? null; |
| 127 |
if ( ! in_array( $current_status, [ 'good', 'new', 'blacklisted' ] ) ) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
if ( ! $this->is_pro() ) { |
| 132 |
return new Response( false, [ |
| 133 |
'message' => __( "A WPMU DEV subscription is required for blocklist monitoring.", 'wpdef' ) |
| 134 |
] ); |
| 135 |
} |
| 136 |
if ( 'new' === $current_status ) { |
| 137 |
$this->make_wpmu_request( WPMUDEV::API_BLACKLIST, [], [ |
| 138 |
'method' => 'POST' |
| 139 |
] ); |
| 140 |
$status = $this->domain_status(); |
| 141 |
} else { |
| 142 |
$this->reset_blocklist_monitor(); |
| 143 |
$status = - 1; |
| 144 |
} |
| 145 |
set_site_transient( self::CACHE_BLACKLIST_STATUS, $status, self::CACHE_TIME ); |
| 146 |
|
| 147 |
return new Response( true, [ |
| 148 |
'status' => $status |
| 149 |
] ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* @param string $current_status |
| 154 |
* |
| 155 |
* @return mixed |
| 156 |
*/ |
| 157 |
public function change_status( $current_status ) { |
| 158 |
$status = get_site_transient( self::CACHE_BLACKLIST_STATUS ); |
| 159 |
if ( false === $status ) { |
| 160 |
$status = $this->domain_status(); |
| 161 |
} |
| 162 |
// Without changes. |
| 163 |
if ( $status === $current_status ) { |
| 164 |
return; |
| 165 |
} |
| 166 |
if ( '1' === $current_status ) { |
| 167 |
$this->make_wpmu_request( WPMUDEV::API_BLACKLIST, [], [ |
| 168 |
'method' => 'POST' |
| 169 |
] ); |
| 170 |
$status = $this->domain_status(); |
| 171 |
} else { |
| 172 |
$this->reset_blocklist_monitor(); |
| 173 |
$status = - 1; |
| 174 |
} |
| 175 |
set_site_transient( self::CACHE_BLACKLIST_STATUS, $status, self::CACHE_TIME ); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Get domain status. Variants: |
| 180 |
* '1' -> 'good' |
| 181 |
*/ |
| 182 |
public function get_status() { |
| 183 |
$status = get_site_transient( self::CACHE_BLACKLIST_STATUS ); |
| 184 |
if ( false === $status ) { |
| 185 |
$status = $this->domain_status(); |
| 186 |
} |
| 187 |
|
| 188 |
if ( is_wp_error( $status ) ) { |
| 189 |
return $status->get_error_message(); |
| 190 |
} |
| 191 |
|
| 192 |
return $status; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* @return array |
| 197 |
*/ |
| 198 |
public function export_strings() { |
| 199 |
if ( ! $this->is_pro() ) { |
| 200 |
return array( |
| 201 |
sprintf( __( 'Inactive %s', 'wpdef' ), '<span class="sui-tag sui-tag-pro">Pro</span>' ), |
| 202 |
); |
| 203 |
} |
| 204 |
|
| 205 |
if ( '1' === (string)$this->get_status() ) { |
| 206 |
$strings = array( __( 'Active', 'wpdef' ) ); |
| 207 |
} else { |
| 208 |
$strings = array( __( 'Inactive', 'wpdef' ) ); |
| 209 |
} |
| 210 |
|
| 211 |
return $strings; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* @param array $config |
| 216 |
* @param bool $is_pro |
| 217 |
* |
| 218 |
* @return array |
| 219 |
*/ |
| 220 |
public function config_strings( $config, $is_pro ) { |
| 221 |
if ( $is_pro ) { |
| 222 |
$strings = $config['enabled'] ? array( __( 'Active', 'wpdef' ) ) : array( __( 'Inactive', 'wpdef' ) ); |
| 223 |
} else { |
| 224 |
$strings = array( |
| 225 |
sprintf( |
| 226 |
/* translators: ... */ |
| 227 |
__( 'Inactive %s', 'wpdef' ), |
| 228 |
'<span class="sui-tag sui-tag-pro">Pro</span>' |
| 229 |
) |
| 230 |
); |
| 231 |
} |
| 232 |
|
| 233 |
return $strings; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Define labels for settings key. |
| 238 |
* |
| 239 |
* @return array |
| 240 |
*/ |
| 241 |
public function labels() { |
| 242 |
|
| 243 |
return array( |
| 244 |
'enabled' => __( 'Blocklist Monitor', 'wpdef' ), |
| 245 |
'status' => __( 'Status', 'wpdef' ), |
| 246 |
); |
| 247 |
} |
| 248 |
} |
| 249 |
|