| @@ -203,9 +203,9 @@ | ||
| 203 | 203 | if ( is_array( $data['allow_list'] ) ) { |
| 204 | 204 | foreach ( $data['allow_list'] as $key => $ip ) { |
| 205 | 205 | $error = $this->display_validation_message( $ip ); |
| 206 | 206 | |
| 207 | - if ( ! empty( $error ) ) { | |
| 207 | + if ( array() !== $error ) { | |
| 208 | 208 | $errors['allow_list'] = array_merge( $error, $errors['allow_list'] ); |
| 209 | 209 | unset( $data['allow_list'][ $key ] ); |
| 210 | 210 | } |
| 211 | 211 | } |
| @@ -215,9 +215,9 @@ | ||
| 215 | 215 | if ( is_array( $data['block_list'] ) ) { |
| 216 | 216 | foreach ( $data['block_list'] as $key => $ip ) { |
| 217 | 217 | $error = $this->display_validation_message( $ip ); |
| 218 | 218 | |
| 219 | - if ( ! empty( $error ) ) { | |
| 219 | + if ( array() !== $error ) { | |
| 220 | 220 | $errors['block_list'] = array_merge( $error, $errors['block_list'] ); |
| 221 | 221 | unset( $data['block_list'][ $key ] ); |
| 222 | 222 | } |
| 223 | 223 | } |
| @@ -236,9 +236,12 @@ | ||
| 236 | 236 | $this->block_list = $value['block_list']; |
| 237 | 237 | |
| 238 | 238 | self::set_last_synced(); |
| 239 | 239 | |
| 240 | - if ( ! empty( $errors['allow_list'] ) || ! empty( $errors['block_list'] ) ) { | |
| 240 | + if ( | |
| 241 | + ( isset( $errors['allow_list'] ) && array() !== $errors['allow_list'] ) | |
| 242 | + || ( isset( $errors['block_list'] ) && array() !== $errors['block_list'] ) | |
| 243 | + ) { | |
| 241 | 244 | return new WP_Error( 'defender_hub_api_invalid_ips', esc_html__( 'Invalid IP(s)', 'defender-security' ), $errors ); |
| 242 | 245 | } else { |
| 243 | 246 | return $ret; |
| 244 | 247 | } |
| @@ -253,25 +256,25 @@ | ||
| 253 | 256 | $data = $this->global_list; |
| 254 | 257 | $last_synced = self::get_last_synced(); |
| 255 | 258 | |
| 256 | 259 | return array( |
| 257 | - 'allow_list' => ! empty( $data['allow_list'] ) && is_array( $data['allow_list'] ) ? | |
| 260 | + 'allow_list' => isset( $data['allow_list'] ) && is_array( $data['allow_list'] ) && array() !== $data['allow_list'] ? | |
| 258 | 261 | implode( '<br>', $data['allow_list'] ) : |
| 259 | 262 | '', |
| 260 | - 'block_list' => ! empty( $data['block_list'] ) && is_array( $data['block_list'] ) ? | |
| 263 | + 'block_list' => isset( $data['block_list'] ) && is_array( $data['block_list'] ) && array() !== $data['block_list'] ? | |
| 261 | 264 | implode( '<br>', $data['block_list'] ) : |
| 262 | 265 | '', |
| 263 | 266 | 'last_synced' => 0 !== $last_synced ? |
| 264 | 267 | $this->format_date_time( $last_synced ) : |
| 265 | 268 | esc_html__( 'Never', 'defender-security' ), |
| 266 | - 'block_list_count' => ! empty( $data['block_list'] ) && is_array( $data['block_list'] ) ? $this->format_number( count( $data['block_list'] ) ) : 0, | |
| 267 | - 'last_update_time_utc' => ! empty( $data['last_update_time_utc'] ) ? | |
| 269 | + 'block_list_count' => isset( $data['block_list'] ) && is_array( $data['block_list'] ) && array() !== $data['block_list'] ? $this->format_number( count( $data['block_list'] ) ) : 0, | |
| 270 | + 'last_update_time_utc' => isset( $data['last_update_time_utc'] ) && false !== $data['last_update_time_utc'] && '' !== $data['last_update_time_utc'] ? | |
| 268 | 271 | $this->format_date_time( $data['last_update_time_utc'] ) : |
| 269 | 272 | esc_html__( 'Never', 'defender-security' ), |
| 270 | - 'last_update_time' => ! empty( $data['last_update_time'] ) ? | |
| 273 | + 'last_update_time' => isset( $data['last_update_time'] ) && false !== $data['last_update_time'] && '' !== $data['last_update_time'] ? | |
| 271 | 274 | $this->format_date_time( $data['last_update_time'] ) : |
| 272 | 275 | esc_html__( 'Never', 'defender-security' ), |
| 273 | - 'is_synced_before' => ! empty( $data ), | |
| 276 | + 'is_synced_before' => array() !== $data, | |
| 274 | 277 | ); |
| 275 | 278 | } |
| 276 | 279 | |
| 277 | 280 | /** |
| @@ -287,8 +290,10 @@ | ||
| 287 | 290 | |
| 288 | 291 | try { |
| 289 | 292 | $data = $this->make_wpmu_free_request( WPMUDEV::API_GLOBAL_IP_LIST ); |
| 290 | 293 | } catch ( Exception $e ) { |
| 294 | + $this->log( 'Global IP API Error: ' . $e->getMessage(), Firewall::FIREWALL_LOG ); | |
| 295 | + | |
| 291 | 296 | return new WP_Error( 'defender_hub_api_invalid_returned', $e->getMessage() ); |
| 292 | 297 | } |
| 293 | 298 | |
| 294 | 299 | if ( is_array( $data ) ) { |
| @@ -298,10 +303,14 @@ | ||
| 298 | 303 | $updated_time = $this->fetch_global_ip_list_updated_time(); |
| 299 | 304 | |
| 300 | 305 | if ( |
| 301 | 306 | is_wp_error( $updated_time ) || |
| 302 | - empty( $updated_time['last_update_time_utc'] ) || | |
| 303 | - empty( $data['last_update_time_utc'] ) || | |
| 307 | + ! isset( $updated_time['last_update_time_utc'] ) || | |
| 308 | + ! is_string( $updated_time['last_update_time_utc'] ) || | |
| 309 | + '' === $updated_time['last_update_time_utc'] || | |
| 310 | + ! isset( $data['last_update_time_utc'] ) || | |
| 311 | + ! is_string( $data['last_update_time_utc'] ) || | |
| 312 | + '' === $data['last_update_time_utc'] || | |
| 304 | 313 | strtotime( $updated_time['last_update_time_utc'] ) > strtotime( $data['last_update_time_utc'] ) |
| 305 | 314 | ) { |
| 306 | 315 | $this->attach_behavior( WPMUDEV::class, WPMUDEV::class ); |
| 307 | 316 | |
| @@ -307,8 +316,10 @@ | ||
| 307 | 316 | |
| 308 | 317 | try { |
| 309 | 318 | $data = $this->make_wpmu_free_request( WPMUDEV::API_GLOBAL_IP_LIST ); |
| 310 | 319 | } catch ( Exception $e ) { |
| 320 | + $this->log( 'Global IP API Error: ' . $e->getMessage(), Firewall::FIREWALL_LOG ); | |
| 321 | + | |
| 311 | 322 | return new WP_Error( 'defender_hub_api_invalid_returned', $e->getMessage() ); |
| 312 | 323 | } |
| 313 | 324 | |
| 314 | 325 | if ( is_array( $data ) ) { |
| @@ -356,10 +367,12 @@ | ||
| 356 | 367 | * @since 4.12.0 Send a request using API key from the Dashboard plugin or HCM. |
| 357 | 368 | * @return array|WP_Error |
| 358 | 369 | */ |
| 359 | 370 | public function add_to_global_ip_list( array $params ) { |
| 360 | - $is_allow_list = ! empty( $params['allow_list'] ); | |
| 361 | - $is_block_list = ! empty( $params['block_list'] ); | |
| 371 | + $allow_list = isset( $params['allow_list'] ) ? (array) $params['allow_list'] : array(); | |
| 372 | + $block_list = isset( $params['block_list'] ) ? (array) $params['block_list'] : array(); | |
| 373 | + $is_allow_list = array() !== $allow_list; | |
| 374 | + $is_block_list = array() !== $block_list; | |
| 362 | 375 | if ( ! $is_allow_list && ! $is_block_list ) { |
| 363 | 376 | return new WP_Error( |
| 364 | 377 | 'global_ip_invalid_params', |
| 365 | 378 | esc_html__( 'Invalid Global IP parameter(s) provided.', 'defender-security' ) |
| @@ -367,12 +380,12 @@ | ||
| 367 | 380 | } |
| 368 | 381 | |
| 369 | 382 | $data = array(); |
| 370 | 383 | if ( $is_allow_list ) { |
| 371 | - $data['allow_list'] = (array) $params['allow_list']; | |
| 384 | + $data['allow_list'] = $allow_list; | |
| 372 | 385 | } |
| 373 | 386 | if ( $is_block_list ) { |
| 374 | - $data['block_list'] = (array) $params['block_list']; | |
| 387 | + $data['block_list'] = $block_list; | |
| 375 | 388 | } |
| 376 | 389 | |
| 377 | 390 | $this->attach_behavior( WPMUDEV::class, WPMUDEV::class ); |
| 378 | 391 | $ret = $this->make_wpmu_request( |
| @@ -417,9 +430,9 @@ | ||
| 417 | 430 | * @return bool |
| 418 | 431 | */ |
| 419 | 432 | public function is_show_dashboard_notice(): bool { |
| 420 | 433 | $reminder = $this->get_dashboard_notice_reminder(); |
| 421 | - if ( $this->is_global_ip_enabled() || empty( $reminder ) || time() < $reminder ) { | |
| 434 | + if ( $this->is_global_ip_enabled() || ! is_int( $reminder ) || time() < $reminder ) { | |
| 422 | 435 | return false; |
| 423 | 436 | } |
| 424 | 437 | |
| 425 | 438 | $is_pro = $this->wpmudev->is_pro(); |
| @@ -425,15 +438,13 @@ | ||
| 425 | 438 | $is_pro = $this->wpmudev->is_pro(); |
| 426 | 439 | $user_roles = is_user_logged_in() ? $this->get_roles( wp_get_current_user() ) : array(); |
| 427 | 440 | |
| 428 | 441 | $is_show_to_user = false; |
| 429 | - if ( ! $is_pro && ! empty( | |
| 430 | - array_intersect( | |
| 431 | - $user_roles, | |
| 432 | - array( | |
| 433 | - $this->super_admin_slug, | |
| 434 | - 'administrator', | |
| 435 | - ) | |
| 442 | + if ( ! $is_pro && array() !== array_intersect( | |
| 443 | + $user_roles, | |
| 444 | + array( | |
| 445 | + $this->super_admin_slug, | |
| 446 | + 'administrator', | |
| 436 | 447 | ) |
| 437 | 448 | ) ) { |
| 438 | 449 | $is_show_to_user = true; |
| 439 | 450 | } elseif ( $is_pro && $this->is_wpmu_dev_admin() ) { |
| @@ -473,9 +484,9 @@ | ||
| 473 | 484 | * |
| 474 | 485 | * @return array|WP_Error |
| 475 | 486 | */ |
| 476 | 487 | public function remove_from_blocklist( array $ips ) { |
| 477 | - if ( empty( $ips ) ) { | |
| 488 | + if ( array() === $ips ) { | |
| 478 | 489 | return new WP_Error( 'defender_hub_api_invalid_ips', esc_html__( 'No IP(s) provided.', 'defender-security' ) ); |
| 479 | 490 | } |
| 480 | 491 | |
| 481 | 492 | $this->attach_behavior( WPMUDEV::class, WPMUDEV::class ); |
| @@ -588,11 +599,39 @@ | ||
| 588 | 599 | * |
| 589 | 600 | * @return void |
| 590 | 601 | */ |
| 591 | 602 | public function handle_expired_membership(): void { |
| 592 | - if ( $this->is_expired_membership_type() && $this->model->enabled ) { | |
| 603 | + if ( $this->model->enabled && $this->is_expired_membership_type() ) { | |
| 593 | 604 | $this->model->enabled = false; |
| 594 | 605 | $this->model->save(); |
| 595 | 606 | $this->log( 'Central IP automatically disabled due to expired membership.', 'central_ip.log' ); |
| 596 | 607 | } |
| 608 | + } | |
| 609 | + | |
| 610 | + /** | |
| 611 | + * Toggle the Custom IP List feature on hosting. | |
| 612 | + * | |
| 613 | + * @param bool $enable True to enable, false to disable. | |
| 614 | + * | |
| 615 | + * @return bool|WP_Error True on success, false if not on WPMU hosting, WP_Error on failure. | |
| 616 | + */ | |
| 617 | + public function toggle_on_hosting( bool $enable ) { | |
| 618 | + if ( ! $this->wpmudev->is_wpmu_hosting() ) { | |
| 619 | + return false; | |
| 620 | + } | |
| 621 | + | |
| 622 | + $this->attach_behavior( WPMUDEV::class, WPMUDEV::class ); | |
| 623 | + | |
| 624 | + $data = $this->make_wpmu_request( | |
| 625 | + WPMUDEV::API_GLOBAL_IP_LIST_HOSTING, | |
| 626 | + array( 'is_active' => $enable ), | |
| 627 | + array( 'method' => 'PUT' ) | |
| 628 | + ); | |
| 629 | + | |
| 630 | + if ( is_wp_error( $data ) ) { | |
| 631 | + $this->log( 'Global IP API Error: ' . $data->get_error_message(), Firewall::FIREWALL_LOG ); | |
| 632 | + return $data; | |
| 633 | + } | |
| 634 | + | |
| 635 | + return true; | |
| 597 | 636 | } |
| 598 | 637 | } |