| 1 |
<?php // phpcs:disable WordPress.Files.FileName |
| 2 |
/** |
| 3 |
* Manages the Restricted Site Access plugin settings. |
| 4 |
* |
| 5 |
* ## EXAMPLES |
| 6 |
* |
| 7 |
* # Restricts site access. |
| 8 |
* $ wp rsa set-mode login |
| 9 |
* Success: Redirecting visitors to login screen. |
| 10 |
* |
| 11 |
* # Whitelists IP addresses. |
| 12 |
* $ wp rsa ip-add 192.0.0.1 |
| 13 |
* Success: Added 192.0.0.1 to the whitelist. |
| 14 |
*/ |
| 15 |
class Restricted_Site_Access_CLI extends WP_CLI_Command { |
| 16 |
|
| 17 |
/** |
| 18 |
* Stored command positional arguments. |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
private $args; |
| 23 |
|
| 24 |
/** |
| 25 |
* Stored command associative arguments. |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
private $assoc_args; |
| 30 |
|
| 31 |
/** |
| 32 |
* Whether the command is operating on the network or a single site. |
| 33 |
* |
| 34 |
* @var bool |
| 35 |
*/ |
| 36 |
private $is_network = false; |
| 37 |
|
| 38 |
/** |
| 39 |
* Sets how the site is restricted. |
| 40 |
* |
| 41 |
* ## OPTIONS |
| 42 |
* |
| 43 |
* <mode> |
| 44 |
* : What mode to set the plugin to. |
| 45 |
* --- |
| 46 |
* options: |
| 47 |
* - disable |
| 48 |
* - login |
| 49 |
* - redirect |
| 50 |
* - message |
| 51 |
* - page |
| 52 |
* --- |
| 53 |
* |
| 54 |
* [--redirect=<url>] |
| 55 |
* : What URL to redirect visitors to in case of the redirect mode. |
| 56 |
* |
| 57 |
* [--same-path] |
| 58 |
* : Preserve the path in case of the redirect mode. |
| 59 |
* |
| 60 |
* [--status-code=<code>] |
| 61 |
* : What status code to use, in case of the redirect mode. |
| 62 |
* --- |
| 63 |
* options: |
| 64 |
* - 301 |
| 65 |
* - 302 |
| 66 |
* - 307 |
| 67 |
* default: 302 |
| 68 |
* --- |
| 69 |
* |
| 70 |
* [--text=<text>] |
| 71 |
* : What message to display in case of message mode. |
| 72 |
* |
| 73 |
* [--page=<page-id>] |
| 74 |
* : What page ID to display in case of page mode. |
| 75 |
* |
| 76 |
* [--network] |
| 77 |
* : Multisite only. Sets this configuration for the network. |
| 78 |
* |
| 79 |
* ## EXAMPLES |
| 80 |
* |
| 81 |
* # Disables site restriction. |
| 82 |
* $ wp rsa set-mode disable |
| 83 |
* Success: Site restrictions disabled. |
| 84 |
* |
| 85 |
* # Redirects site visitors to a URL. |
| 86 |
* $ wp rsa set-mode redirect --url=http://example.com |
| 87 |
* Success: Site redirecting visitors to "http://example.com". |
| 88 |
* |
| 89 |
* # Shows site visitors a message. |
| 90 |
* $ wp rsa set-mode message --text="None shall pass!" |
| 91 |
* Success: Site message set. |
| 92 |
* |
| 93 |
* # Shows site visitors a page. |
| 94 |
* wp rsa set-mode page --page=123 |
| 95 |
* Success: Site showing visitors page "Welcome". |
| 96 |
* |
| 97 |
* @subcommand set-mode |
| 98 |
* @alias mode |
| 99 |
* |
| 100 |
* @param array $args Array with single value of what mode to set. |
| 101 |
* @param array $assoc_args Array with optional flags described above. |
| 102 |
* @return void |
| 103 |
*/ |
| 104 |
public function set_mode( $args, $assoc_args ) { |
| 105 |
// We don't need to validate the mode, as WP-CLI ensures that's correct. |
| 106 |
$mode = $args[0]; |
| 107 |
|
| 108 |
// Sets up and gets options. |
| 109 |
$this->setup( $args, $assoc_args ); |
| 110 |
$options = $this->get_options(); |
| 111 |
|
| 112 |
// Gets the current setting. |
| 113 |
$blog_public = (int) get_option( 'blog_public', 2 ); |
| 114 |
if ( $this->is_network ) { |
| 115 |
$blog_public = (int) get_site_option( 'blog_public', 2 ); |
| 116 |
} |
| 117 |
|
| 118 |
// Handles disabling the plugin. |
| 119 |
if ( 'disable' === $mode ) { |
| 120 |
if ( 2 !== $blog_public ) { |
| 121 |
WP_CLI::success( |
| 122 |
sprintf( |
| 123 |
/* translators: %s: What the user is updating: "Site" or "Network". */ |
| 124 |
__( '%s already not under restricted access.', 'restricted-site-access' ), |
| 125 |
$this->update_text() |
| 126 |
) |
| 127 |
); |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
if ( $this->is_network ) { |
| 132 |
update_site_option( 'blog_public', 1 ); |
| 133 |
} else { |
| 134 |
update_option( 'blog_public', 1 ); |
| 135 |
} |
| 136 |
|
| 137 |
WP_CLI::success( |
| 138 |
sprintf( |
| 139 |
/* translators: %s: What the user is updating: "Site" or "Network". */ |
| 140 |
__( '%s restrictions disabled.', 'restricted-site-access' ), |
| 141 |
$this->update_text() |
| 142 |
) |
| 143 |
); |
| 144 |
return; // Exit. |
| 145 |
} |
| 146 |
|
| 147 |
// Enables RSA if it's not already enabled. |
| 148 |
if ( 2 !== $blog_public ) { |
| 149 |
if ( $this->is_network ) { |
| 150 |
update_site_option( 'blog_public', 2 ); |
| 151 |
WP_CLI::debug( 'Enabled RSA on network.' ); |
| 152 |
} else { |
| 153 |
update_option( 'blog_public', 2 ); |
| 154 |
WP_CLI::debug( 'Enabled RSA.' ); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
// Login mode. Simple! |
| 159 |
if ( 'login' === $mode ) { |
| 160 |
$options['approach'] = 1; |
| 161 |
|
| 162 |
} elseif ( 'redirect' === $mode ) { |
| 163 |
$url = WP_CLI\Utils\get_flag_value( $assoc_args, 'redirect' ); |
| 164 |
if ( ! $url ) { |
| 165 |
WP_CLI::error( __( 'Redirect URL required.', 'restricted-site-access' ) ); |
| 166 |
} |
| 167 |
|
| 168 |
// Let WP-CLI validate the status code. |
| 169 |
$options = array_merge( |
| 170 |
$options, |
| 171 |
array( |
| 172 |
'approach' => 2, |
| 173 |
'redirect_url' => $url, |
| 174 |
'head_code' => WP_CLI\Utils\get_flag_value( $assoc_args, 'status-code' ), |
| 175 |
'redirect_path' => (int) WP_CLI\Utils\get_flag_value( $assoc_args, 'same-path', 0 ), |
| 176 |
) |
| 177 |
); |
| 178 |
|
| 179 |
// End redirect mode. |
| 180 |
} elseif ( 'message' === $mode ) { |
| 181 |
// Set default for message text. |
| 182 |
$message = WP_CLI\Utils\get_flag_value( $assoc_args, 'text' ); |
| 183 |
if ( ! $message ) { |
| 184 |
$message = __( 'Access to this site is restricted.', 'restricted-site-access' ); |
| 185 |
} |
| 186 |
$options['approach'] = 3; |
| 187 |
$options['message'] = $message; |
| 188 |
|
| 189 |
// End message mode. |
| 190 |
} elseif ( 'page' === $mode ) { |
| 191 |
// Validate page ID passed. |
| 192 |
$page_id = (int) WP_CLI\Utils\get_flag_value( $assoc_args, 'page' ); |
| 193 |
if ( ! $page_id ) { |
| 194 |
WP_CLI::error( __( 'Page required.', 'restricted-site-access' ) ); |
| 195 |
} |
| 196 |
$page = get_post( $page_id ); |
| 197 |
if ( ! $page || 'page' !== $page->post_type ) { |
| 198 |
WP_CLI::error( __( 'Page is invalid.', 'restricted-site-access' ) ); |
| 199 |
} |
| 200 |
|
| 201 |
$options['approach'] = 4; |
| 202 |
$options['page'] = $page_id; |
| 203 |
// End page mode. |
| 204 |
} |
| 205 |
|
| 206 |
$updated_options = $this->update_options( $options ); |
| 207 |
|
| 208 |
// Send update messages. |
| 209 |
$success_msg = ''; |
| 210 |
switch ( $mode ) { |
| 211 |
case 'login': |
| 212 |
/* translators: %s: Context: "Site" or "Network". */ |
| 213 |
$success_msg = __( '%s redirecting visitors to login.', 'restricted-site-access' ); |
| 214 |
break; |
| 215 |
case 'redirect': |
| 216 |
$success_msg = sprintf( |
| 217 |
/* translators: %s: Context: "Site" or "Network". %s: Redirect URL. */ |
| 218 |
__( '%%s redirecting visitors to "%s"', 'restricted-site-access' ), |
| 219 |
$updated_options['redirect_url'] |
| 220 |
); |
| 221 |
break; |
| 222 |
case 'message': |
| 223 |
/* translators: %s: Context: "Site" or "Network". */ |
| 224 |
$success_msg = __( '%s showing message to visitors.', 'restricted-site-access' ); |
| 225 |
break; |
| 226 |
case 'page': |
| 227 |
$success_msg = sprintf( |
| 228 |
/* translators: %s: "Site" or "Network". %s: Page title. */ |
| 229 |
__( '%%s showing visitors page "%s"', 'restricted-site-access' ), |
| 230 |
get_the_title( $page ) |
| 231 |
); |
| 232 |
break; |
| 233 |
default: |
| 234 |
/* translators: %s: What the user is updating: "Site" or "Network". */ |
| 235 |
$success_msg = __( '%s settings updated.', 'restricted-site-access' ); |
| 236 |
} |
| 237 |
|
| 238 |
WP_CLI::success( |
| 239 |
sprintf( |
| 240 |
$success_msg, |
| 241 |
$this->update_text() |
| 242 |
) |
| 243 |
); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Sets the network mode. |
| 248 |
* |
| 249 |
* ## OPTIONS |
| 250 |
* |
| 251 |
* <mode> |
| 252 |
* : Mode to set network. |
| 253 |
* --- |
| 254 |
* default: default |
| 255 |
* options: |
| 256 |
* - default |
| 257 |
* - enforce |
| 258 |
* --- |
| 259 |
* |
| 260 |
* ## EXAMPLES |
| 261 |
* |
| 262 |
* # Sets the multisite to enforce mode. |
| 263 |
* $ wp rsa set-network-mode enforce |
| 264 |
* Success: Set network mode to enforced. |
| 265 |
* |
| 266 |
* @subcommand set-network-mode |
| 267 |
* |
| 268 |
* @param array $args Array with single value of what mode to set. |
| 269 |
* @param array $assoc_args Associative arguments. Not used. |
| 270 |
*/ |
| 271 |
public function set_network_mode( $args, $assoc_args ) { |
| 272 |
if ( ! RSA_IS_NETWORK ) { |
| 273 |
WP_CLI::error( __( 'Cannot set network mode when plugin not activated on network.', 'restricted-site-access' ) ); |
| 274 |
} |
| 275 |
|
| 276 |
// We don't need to validate the mode, as WP-CLI ensures that's correct. |
| 277 |
$new_mode = $args[0]; |
| 278 |
$current_mode = get_site_option( 'rsa_mode', 'default' ); |
| 279 |
|
| 280 |
// Sets mode and shows message. |
| 281 |
if ( $new_mode === $current_mode ) { |
| 282 |
WP_CLI::warning( |
| 283 |
sprintf( |
| 284 |
/* translators: %s: Network mode. */ |
| 285 |
__( 'Mode is already set to %s.', 'restricted-site-access' ), |
| 286 |
$current_mode |
| 287 |
) |
| 288 |
); |
| 289 |
} else { |
| 290 |
update_site_option( 'rsa_mode', sanitize_key( $new_mode ) ); |
| 291 |
WP_CLI::success( |
| 292 |
sprintf( |
| 293 |
/* translators: %s: Network mode. */ |
| 294 |
__( 'Set network mode to %s.', 'restricted-site-access' ), |
| 295 |
$new_mode |
| 296 |
) |
| 297 |
); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Lists current IP whitelist. |
| 303 |
* |
| 304 |
* ## OPTIONS |
| 305 |
* |
| 306 |
* [--exclude-config] |
| 307 |
* : Don't include IPs from the configuration file. |
| 308 |
* |
| 309 |
* [--include-labels] |
| 310 |
* : Include labels. |
| 311 |
* |
| 312 |
* [--format=<format>] |
| 313 |
* : Render output in a particular format. |
| 314 |
* --- |
| 315 |
* default: table |
| 316 |
* options: |
| 317 |
* - table |
| 318 |
* - csv |
| 319 |
* - json |
| 320 |
* - yaml |
| 321 |
* --- |
| 322 |
* |
| 323 |
* [--network] |
| 324 |
* : Multisite only. Sets configuration for the network as a whole. |
| 325 |
* |
| 326 |
* ## EXAMPLES |
| 327 |
* |
| 328 |
* # Outputs currently whitelisted IPs in CSV format. |
| 329 |
* $ wp rsa ip-list --format=csv |
| 330 |
* 192.0.0.1,10.10.0.0 |
| 331 |
* |
| 332 |
* @subcommand ip-list |
| 333 |
* |
| 334 |
* @param array $args Positional arguments. Not used. |
| 335 |
* @param array $assoc_args Array with format value. |
| 336 |
*/ |
| 337 |
public function ip_list( $args, $assoc_args ) { |
| 338 |
$this->setup( $args, $assoc_args ); |
| 339 |
|
| 340 |
$show_labels = WP_CLI\Utils\get_flag_value( $assoc_args, 'include-labels', false ); |
| 341 |
$no_config = WP_CLI\Utils\get_flag_value( $assoc_args, 'exclude-config', false ); |
| 342 |
$ips = $this->get_current_ips( ! $no_config, $show_labels ); |
| 343 |
$items = array(); |
| 344 |
$fields = $show_labels ? array( 'ip', 'label' ) : array( 'ip' ); |
| 345 |
|
| 346 |
if ( 0 === count( $ips ) ) { |
| 347 |
WP_CLI::line( __( 'No IP addresses configured.', 'restricted-site-access' ) ); |
| 348 |
return; |
| 349 |
} |
| 350 |
|
| 351 |
if ( $show_labels ) { |
| 352 |
foreach ( $ips as $label => $ip ) { |
| 353 |
$items[] = compact( 'ip', 'label' ); |
| 354 |
} |
| 355 |
} else { |
| 356 |
foreach ( $ips as $ip ) { |
| 357 |
$items[] = compact( 'ip' ); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
$format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'table' ); |
| 362 |
WP_CLI\Utils\format_items( $format, $items, $fields ); |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Adds one or more IP addresses to the whitelist. |
| 367 |
* |
| 368 |
* ## OPTIONS |
| 369 |
* |
| 370 |
* <ip>... |
| 371 |
* : List of IP addresses to add to the whitelist. |
| 372 |
* |
| 373 |
* [--network] |
| 374 |
* : Multisite only. Sets configuration for the network as a whole. |
| 375 |
* |
| 376 |
* ## EXAMPLES |
| 377 |
* |
| 378 |
* # Adds 192.0.0.1 to IP whitelist. |
| 379 |
* $ wp rsa ip-add 192.0.0.1 |
| 380 |
* Success: Added 192.0.0.1 to site whitelist. |
| 381 |
* |
| 382 |
* # Adds 8.8.8.8 1.1.1.1 with labels Google and Cloudflare. |
| 383 |
* $ wp rsa ip-add 8.8.8.8=Google 1.1.1.1=Cloudflare |
| 384 |
* Success: Added 8.8.8.8, 1.1.1.1 to site whitelist. |
| 385 |
* |
| 386 |
* @subcommand ip-add |
| 387 |
* |
| 388 |
* @param array $args List of IPs to whitelist. |
| 389 |
* @param array $assoc_args Optional flags. |
| 390 |
*/ |
| 391 |
public function ip_add( $args, $assoc_args ) { |
| 392 |
$this->setup( $args, $assoc_args ); |
| 393 |
|
| 394 |
/** |
| 395 |
* The input arguments can be of the form: |
| 396 |
* wp rsa ip-add 8.8.8.8=Google 9.9.9.9 1.1.1.1=Cloudflare. |
| 397 |
* |
| 398 |
* Some input IP addresses may be provided with a label while |
| 399 |
* other might not. |
| 400 |
* |
| 401 |
* We will normalise the input as: |
| 402 |
* array( |
| 403 |
* array( |
| 404 |
* 'key' => '8.8.8.8', |
| 405 |
* 'label' => 'Google' |
| 406 |
* ), |
| 407 |
* array( |
| 408 |
* 'key' => '9.9.9.9', |
| 409 |
* 'label' => '' |
| 410 |
* ), |
| 411 |
* array( |
| 412 |
* 'key' => '1.1.1.1', |
| 413 |
* 'label' => 'Cloudflare' |
| 414 |
* ), |
| 415 |
* ) |
| 416 |
*/ |
| 417 |
$ips_and_labels_array = array(); |
| 418 |
foreach ( $args as $index => $item ) { |
| 419 |
$fragments = explode( '=', $item ); |
| 420 |
/** |
| 421 |
* If the IP doesn't have a corressponding label, |
| 422 |
* then set label to '[null]:x', where 'x' is an |
| 423 |
* integer. |
| 424 |
*/ |
| 425 |
if ( ! isset( $fragments[1] ) ) { |
| 426 |
$fragments[1] = ''; |
| 427 |
} |
| 428 |
|
| 429 |
$structure_ip_label_array = array( |
| 430 |
'ip' => $fragments[0], |
| 431 |
'label' => $fragments[1], |
| 432 |
); |
| 433 |
|
| 434 |
$ips_and_labels_array[] = $structure_ip_label_array; |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* Get all whitelisted IPs saved in DB. |
| 439 |
*/ |
| 440 |
$current_ips = $this->get_current_ips(); |
| 441 |
|
| 442 |
/** |
| 443 |
* This will only hold those input IP addresses |
| 444 |
* which are not already whitelisted. |
| 445 |
*/ |
| 446 |
$filtered_ips_and_labels = array(); |
| 447 |
|
| 448 |
/** |
| 449 |
* A simple for loop to filter the input IP addresses. |
| 450 |
*/ |
| 451 |
foreach ( $ips_and_labels_array as $ip_label_pair ) { |
| 452 |
if ( ! in_array( $ip_label_pair['ip'], $current_ips, true ) ) { |
| 453 |
$filtered_ips_and_labels[] = array( |
| 454 |
'ip' => $ip_label_pair['ip'], |
| 455 |
'label' => $ip_label_pair['label'], |
| 456 |
); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* Extract all IP address from the filtered array |
| 462 |
* as an indexed array. |
| 463 |
*/ |
| 464 |
$new_ips = array_map( |
| 465 |
function( $ip_label_pair ) { |
| 466 |
return $ip_label_pair['ip']; |
| 467 |
}, |
| 468 |
$filtered_ips_and_labels |
| 469 |
); |
| 470 |
|
| 471 |
// Validate the IP addresses. |
| 472 |
$valid_ips = array_filter( $new_ips, array( 'Restricted_Site_Access', 'is_ip' ) ); |
| 473 |
if ( 0 === count( $valid_ips ) ) { |
| 474 |
WP_CLI::error( __( 'No valid IP addresses provided.', 'restricted-site-access' ) ); |
| 475 |
} |
| 476 |
|
| 477 |
if ( 0 === count( $new_ips ) ) { |
| 478 |
// Only show a warning as this may be an automated process. |
| 479 |
WP_CLI::warning( |
| 480 |
sprintf( |
| 481 |
/* translators: %s: Context: "Site" or "Network". */ |
| 482 |
__( 'Provided IPs are already on %s whitelist.', 'restricted-site-access' ), |
| 483 |
$this->update_text( false ) |
| 484 |
) |
| 485 |
); |
| 486 |
return; |
| 487 |
} |
| 488 |
|
| 489 |
$ips_with_label = array(); |
| 490 |
$ips_without_label = array(); |
| 491 |
|
| 492 |
foreach ( $filtered_ips_and_labels as $ip_label_pair ) { |
| 493 |
if ( empty( $ip_label_pair['label'] ) ) { |
| 494 |
$ips_without_label[] = $ip_label_pair['ip']; |
| 495 |
} else { |
| 496 |
$ips_with_label[ $ip_label_pair['label'] ] = $ip_label_pair['ip']; |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
// Updates the option. |
| 501 |
Restricted_Site_Access::add_ips( array_merge( $ips_without_label, $ips_with_label ) ); |
| 502 |
|
| 503 |
WP_CLI::success( |
| 504 |
sprintf( |
| 505 |
/* translators: %1$s: IP addresses. %2$s: Context: "Site" or "Network". */ |
| 506 |
__( 'Added %1$s to %2$s whitelist.', 'restricted-site-access' ), |
| 507 |
implode( ', ', $new_ips ), |
| 508 |
$this->update_text( false ) |
| 509 |
) |
| 510 |
); |
| 511 |
|
| 512 |
WP_CLI::debug( |
| 513 |
sprintf( |
| 514 |
/* translators: %2$s: IP addresses. %1$s: Context: "Site" or "Network". */ |
| 515 |
__( 'Current %2$s whitelisted IPs are: %1$s', 'restricted-site-access' ), |
| 516 |
implode( ', ', Restricted_Site_Access::get_ips() ), |
| 517 |
$this->update_text( false ) |
| 518 |
) |
| 519 |
); |
| 520 |
} |
| 521 |
|
| 522 |
/** |
| 523 |
* Removes one or more IP addresses from the whitelist. |
| 524 |
* |
| 525 |
* ## OPTIONS |
| 526 |
* |
| 527 |
* <ip>... |
| 528 |
* : List of IP addresses to remove from the whitelist. |
| 529 |
* |
| 530 |
* [--network] |
| 531 |
* : Multisite only. Sets configuration for the network as a whole. |
| 532 |
* |
| 533 |
* ## EXAMPLES |
| 534 |
* |
| 535 |
* # Removes IP address from whitelist. |
| 536 |
* $ wp rsa ip-remove 192.0.0.1 |
| 537 |
* Success: Removed 192.0.0.1 from whitelist. |
| 538 |
* |
| 539 |
* @subcommand ip-remove |
| 540 |
* |
| 541 |
* @param array $args List of IPs to blacklist. |
| 542 |
* @param array $assoc_args Optional flags. |
| 543 |
*/ |
| 544 |
public function ip_remove( $args, $assoc_args ) { |
| 545 |
$this->setup( $args, $assoc_args ); |
| 546 |
|
| 547 |
// Validate the IP addresses. |
| 548 |
$valid_ips = array_filter( $args, array( 'Restricted_Site_Access', 'is_ip' ) ); |
| 549 |
if ( 0 === count( $valid_ips ) ) { |
| 550 |
WP_CLI::error( __( 'No valid IP addresses provided.', 'restricted-site-access' ) ); |
| 551 |
} |
| 552 |
|
| 553 |
// Get the IPs to remove. |
| 554 |
$current_ips = $this->get_current_ips( false ); |
| 555 |
$removed_ips = array_intersect( $valid_ips, $current_ips ); |
| 556 |
|
| 557 |
if ( 0 === count( $removed_ips ) ) { |
| 558 |
// Only show warning as this may be an automated process. |
| 559 |
WP_CLI::warning( |
| 560 |
sprintf( |
| 561 |
/* translators: %s: Context: "Site" or "Network". */ |
| 562 |
__( 'Provided IPs are not on %s whitelist.', 'restricted-site-access' ), |
| 563 |
$this->update_text( false ) |
| 564 |
) |
| 565 |
); |
| 566 |
return; |
| 567 |
} |
| 568 |
|
| 569 |
// Updates the option. |
| 570 |
Restricted_Site_Access::remove_ips( $removed_ips ); |
| 571 |
|
| 572 |
WP_CLI::success( |
| 573 |
sprintf( |
| 574 |
/* translators: %1$s: IP addresses. %2$s: Context: "Site" or "Network". */ |
| 575 |
__( 'Removed IPs %1$s from %2$s whitelist.', 'restricted-site-access' ), |
| 576 |
implode( ', ', $removed_ips ), |
| 577 |
$this->update_text( false ) |
| 578 |
) |
| 579 |
); |
| 580 |
|
| 581 |
WP_CLI::debug( |
| 582 |
sprintf( |
| 583 |
/* translators: %2$s: IP addresses. %1$s: Context: "Site" or "Network". */ |
| 584 |
__( 'Current %2$s whitelisted IPs are: %1$s', 'restricted-site-access' ), |
| 585 |
implode( ', ', Restricted_Site_Access::get_ips() ), |
| 586 |
$this->update_text( false ) |
| 587 |
) |
| 588 |
); |
| 589 |
} |
| 590 |
|
| 591 |
/** |
| 592 |
* Used to update an existing IP address or to |
| 593 |
* update the label of an existing IP address. |
| 594 |
* |
| 595 |
* ## OPTIONS |
| 596 |
* |
| 597 |
* <ip> |
| 598 |
* : IP address to update. |
| 599 |
* |
| 600 |
* [--new-ip] |
| 601 |
* : The IP address to replace with. |
| 602 |
* |
| 603 |
* [--new-label] |
| 604 |
* : The new label for the IP address. |
| 605 |
* |
| 606 |
* [--network] |
| 607 |
* : Multisite only. Sets configuration for the network as a whole. |
| 608 |
* |
| 609 |
* ## EXAMPLES |
| 610 |
* |
| 611 |
* # Update the label of IP 192.0.0.1 to "New label" |
| 612 |
* $ wp rsa ip-update 192.0.0.1 --new-label="New label" |
| 613 |
* Success: Fields correctly updated. |
| 614 |
* |
| 615 |
* # Replace the IP IP 192.0.0.1 to 200.1.2.3 |
| 616 |
* $ wp rsa ip-update 192.0.0.1 --new-ip=200.1.2.3 |
| 617 |
* Success: Fields correctly updated. |
| 618 |
* |
| 619 |
* @subcommand ip-update |
| 620 |
* |
| 621 |
* @param array $args IP to update. |
| 622 |
* @param array $assoc_args Optional flags. |
| 623 |
*/ |
| 624 |
public function ip_update( $args, $assoc_args ) { |
| 625 |
$this->setup( $args, $assoc_args ); |
| 626 |
|
| 627 |
if ( 0 === count( $assoc_args ) ) { |
| 628 |
\WP_CLI::error( __( 'Provide the arguments to update.', 'restricted-site-access' ) ); |
| 629 |
} |
| 630 |
|
| 631 |
$valid_ips = array_filter( $args, array( 'Restricted_Site_Access', 'is_ip' ) ); |
| 632 |
|
| 633 |
if ( 0 === count( $valid_ips ) ) { |
| 634 |
WP_CLI::error( __( 'No valid IP addresses provided.', 'restricted-site-access' ) ); |
| 635 |
} |
| 636 |
|
| 637 |
$new_ip = \WP_CLI\Utils\get_flag_value( $assoc_args, 'new-ip', false ); |
| 638 |
$new_label = \WP_CLI\Utils\get_flag_value( $assoc_args, 'new-label', false ); |
| 639 |
|
| 640 |
$update_status = Restricted_Site_Access::update_ip_or_label( $valid_ips[0], $new_ip, $new_label ); |
| 641 |
|
| 642 |
if ( is_wp_error( $update_status ) ) { |
| 643 |
WP_CLI::error( |
| 644 |
sprintf( |
| 645 |
'%s (%s)', |
| 646 |
$update_status->get_error_message(), |
| 647 |
$update_status->get_error_code() |
| 648 |
) |
| 649 |
); |
| 650 |
} |
| 651 |
|
| 652 |
WP_CLI::success( __( 'IP updated.', 'restricted-site-access' ) ); |
| 653 |
} |
| 654 |
|
| 655 |
/** |
| 656 |
* Sets list of IPs to whitelist. Overwrites current settings. |
| 657 |
* |
| 658 |
* ## OPTIONS |
| 659 |
* |
| 660 |
* <ip>... |
| 661 |
* : List of IP addresses to whitelist. |
| 662 |
* |
| 663 |
* [--network] |
| 664 |
* : Multisite only. Sets configuration for the network as a whole. |
| 665 |
* |
| 666 |
* ## EXAMPLES |
| 667 |
* |
| 668 |
* # Sets IP whitelist to 192.0.0.1. |
| 669 |
* $ wp rsa ip-set 192.0.0.1 |
| 670 |
* Success: Updated site IP whitelist to 192.0.0.1. |
| 671 |
* |
| 672 |
* @subcommand ip-set |
| 673 |
* |
| 674 |
* @param array $args List of IPs to set. |
| 675 |
* @param array $assoc_args Optional flags. |
| 676 |
*/ |
| 677 |
public function ip_set( $args, $assoc_args ) { |
| 678 |
$this->setup( $args, $assoc_args ); |
| 679 |
|
| 680 |
// Validate the IP addresses. |
| 681 |
$valid_ips = array_filter( $args, array( 'Restricted_Site_Access', 'is_ip' ) ); |
| 682 |
if ( 0 === count( $valid_ips ) ) { |
| 683 |
WP_CLI::error( __( 'No valid IP addresses provided.', 'restricted-site-access' ) ); |
| 684 |
} |
| 685 |
|
| 686 |
// Updates the option. |
| 687 |
Restricted_Site_Access::set_ips( $valid_ips ); |
| 688 |
|
| 689 |
WP_CLI::success( |
| 690 |
sprintf( |
| 691 |
/* translators: %2$s: IPs to whitelist, %1$s: Context: "Site" or "Network". */ |
| 692 |
__( 'Set %2$s IP whitelist to %1$s.', 'restricted-site-access' ), |
| 693 |
implode( ', ', Restricted_Site_Access::get_ips() ), |
| 694 |
$this->update_text( false ) |
| 695 |
) |
| 696 |
); |
| 697 |
} |
| 698 |
|
| 699 |
/** |
| 700 |
* Sets up the instance correctly. |
| 701 |
* |
| 702 |
* @param array $args Array with positional arguments. |
| 703 |
* @param array $assoc_args Array with associative arguments. |
| 704 |
* @return void |
| 705 |
*/ |
| 706 |
private function setup( $args = array(), $assoc_args = array() ) { |
| 707 |
$this->args = $args; |
| 708 |
$this->assoc_args = $assoc_args; |
| 709 |
|
| 710 |
$is_network = WP_CLI\Utils\get_flag_value( $assoc_args, 'network', false ); |
| 711 |
if ( $is_network && ! RSA_IS_NETWORK ) { |
| 712 |
WP_CLI::error( __( 'Cannot get network settings when plugin not activated on network.', 'restricted-site-access' ) ); |
| 713 |
} |
| 714 |
|
| 715 |
$this->is_network = (bool) $is_network; |
| 716 |
} |
| 717 |
|
| 718 |
/** |
| 719 |
* Gets plugin options, either from the network or specified site. |
| 720 |
* |
| 721 |
* @return array Array of options from database. |
| 722 |
*/ |
| 723 |
private function get_options() { |
| 724 |
return Restricted_Site_Access::get_options( $this->is_network ); |
| 725 |
} |
| 726 |
|
| 727 |
/** |
| 728 |
* Gets all current IPs, optionally including config IPs. |
| 729 |
* |
| 730 |
* @param bool $include_config Whether to include the config file IPs. Default true. |
| 731 |
* @param bool $include_labels Whether to include the comments. Default false. |
| 732 |
* @return array |
| 733 |
*/ |
| 734 |
private function get_current_ips( $include_config = true, $include_labels = false ) { |
| 735 |
return Restricted_Site_Access::get_ips( $include_config, $include_labels ); |
| 736 |
} |
| 737 |
|
| 738 |
/** |
| 739 |
* Updates options, potentially on the site. |
| 740 |
* |
| 741 |
* @param array $new_options Array of unsanitized options to save. |
| 742 |
* @return array The newly set options. |
| 743 |
*/ |
| 744 |
private function update_options( $new_options ) { |
| 745 |
$options = wp_parse_args( $new_options, $this->get_options() ); |
| 746 |
$sanitized_options = Restricted_Site_Access::sanitize_options( $options ); |
| 747 |
if ( $this->is_network ) { |
| 748 |
update_site_option( 'rsa_options', $sanitized_options ); |
| 749 |
} else { |
| 750 |
update_option( 'rsa_options', $sanitized_options ); |
| 751 |
} |
| 752 |
|
| 753 |
return $this->get_options(); |
| 754 |
} |
| 755 |
|
| 756 |
/** |
| 757 |
* Text used to indicate whether the user is updating a site or the network. |
| 758 |
* |
| 759 |
* @param bool $capitalize Whether to capitalize the text or not. |
| 760 |
* @return string |
| 761 |
*/ |
| 762 |
private function update_text( $capitalize = true ) { |
| 763 |
$text = _x( 'Site', 'update type', 'restricted-site-access' ); |
| 764 |
if ( $this->is_network ) { |
| 765 |
$text = _x( 'Network', 'update type', 'restricted-site-access' ); |
| 766 |
} |
| 767 |
|
| 768 |
if ( $capitalize ) { |
| 769 |
return $text; |
| 770 |
} |
| 771 |
return strtolower( $text ); |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
WP_CLI::add_command( 'rsa', 'Restricted_Site_Access_CLI' ); |
| 776 |
|