| @@ -20,8 +20,9 @@ | ||
| 20 | 20 | use WP_Defender\Model\Scan as Model_Scan; |
| 21 | 21 | use WP_Defender\Behavior\Scan\Core_Integrity; |
| 22 | 22 | use WP_Defender\Component\Network_Cron_Manager; |
| 23 | 23 | use WP_Defender\Component\Scan as Scan_Component; |
| 24 | +use WP_Defender\Component\Rate as Rate_Component; | |
| 24 | 25 | use WP_Defender\Model\Setting\Scan as Scan_Settings; |
| 25 | 26 | use WP_Defender\Model\Notification\Malware_Report; |
| 26 | 27 | use WP_Defender\Component\Config\Config_Hub_Helper; |
| 27 | 28 | use WP_Defender\Helper\Analytics\Scan as Scan_Analytics; |
| @@ -37,9 +38,22 @@ | ||
| 37 | 38 | use Formats; |
| 38 | 39 | use Scan_Upsell; |
| 39 | 40 | |
| 40 | 41 | public const SCAN_LOG = 'scan.log'; |
| 42 | + | |
| 41 | 43 | /** |
| 44 | + * Records whether a scan has been started on this installation. | |
| 45 | + * | |
| 46 | + * @var string | |
| 47 | + */ | |
| 48 | + public const FIRST_SCAN_STARTED = 'wp_defender_first_scan_started'; | |
| 49 | + | |
| 50 | + /** | |
| 51 | + * Default number of issue items per page on the Scan Issues UI. | |
| 52 | + */ | |
| 53 | + public const DEFAULT_PER_PAGE = 10; | |
| 54 | + | |
| 55 | + /** | |
| 42 | 56 | * The slug identifier for this controller. |
| 43 | 57 | * |
| 44 | 58 | * @var string |
| 45 | 59 | */ |
| @@ -57,9 +71,8 @@ | ||
| 57 | 71 | * |
| 58 | 72 | * @var Scan_Component |
| 59 | 73 | */ |
| 60 | 74 | protected $service; |
| 61 | - | |
| 62 | 75 | /** |
| 63 | 76 | * Quarantine controller. |
| 64 | 77 | * |
| 65 | 78 | * @var Quarantine |
| @@ -66,13 +79,13 @@ | ||
| 66 | 79 | */ |
| 67 | 80 | private $quarantine_controller; |
| 68 | 81 | |
| 69 | 82 | /** |
| 70 | - * Indicates whether the current installation is a pro version. | |
| 83 | + * Is the Malware Report option unlocked? | |
| 71 | 84 | * |
| 72 | 85 | * @var bool |
| 73 | 86 | */ |
| 74 | - private $is_pro; | |
| 87 | + private $is_unlocked; | |
| 75 | 88 | |
| 76 | 89 | /** |
| 77 | 90 | * Initializes the model and service, registers routes, and sets up scheduled events if the model is active. |
| 78 | 91 | */ |
| @@ -83,15 +96,13 @@ | ||
| 83 | 96 | array( $this, 'main_view' ), |
| 84 | 97 | $this->parent_slug |
| 85 | 98 | ); |
| 86 | 99 | |
| 87 | - $this->model = new Scan_Settings(); | |
| 88 | - $this->service = wd_di()->get( Scan_Component::class ); | |
| 89 | - $this->is_pro = wd_di()->get( WPMUDEV::class )->is_pro(); | |
| 100 | + $this->model = wd_di()->get( Scan_Settings::class ); | |
| 101 | + $this->service = wd_di()->get( Scan_Component::class ); | |
| 102 | + $this->quarantine_controller = wd_di()->get( Quarantine::class ); | |
| 90 | 103 | |
| 91 | - if ( class_exists( 'WP_Defender\Controller\Quarantine' ) ) { | |
| 92 | - $this->quarantine_controller = wd_di()->get( Quarantine::class ); | |
| 93 | - } | |
| 104 | + $this->is_unlocked = wd_di()->get( WPMUDEV::class )->is_pro(); | |
| 94 | 105 | |
| 95 | 106 | $this->register_routes(); |
| 96 | 107 | add_action( 'defender_enqueue_assets', array( $this, 'enqueue_assets' ) ); |
| 97 | 108 | add_action( 'wp_ajax_defender_process_scan', array( $this, 'process' ) ); |
| @@ -105,9 +116,9 @@ | ||
| 105 | 116 | if ( |
| 106 | 117 | is_admin() && |
| 107 | 118 | 'plugins.php' === $pagenow && |
| 108 | 119 | apply_filters( 'wd_display_vulnerability_warnings', true ) && |
| 109 | - $this->is_pro | |
| 120 | + wd_di()->get( WPMUDEV::class )->is_apikey_available() | |
| 110 | 121 | ) { |
| 111 | 122 | $this->service->display_vulnerability_warnings(); |
| 112 | 123 | } |
| 113 | 124 | |
| @@ -136,9 +147,9 @@ | ||
| 136 | 147 | * |
| 137 | 148 | * @return string The title of the page. |
| 138 | 149 | */ |
| 139 | 150 | public function get_title(): string { |
| 140 | - return esc_html__( 'Malware Scanning', 'defender-security' ); | |
| 151 | + return esc_html__( 'Issues', 'defender-security' ); | |
| 141 | 152 | } |
| 142 | 153 | |
| 143 | 154 | /** |
| 144 | 155 | * Clean up data after core updating. |
| @@ -151,15 +162,32 @@ | ||
| 151 | 162 | |
| 152 | 163 | /** |
| 153 | 164 | * Start a scan. |
| 154 | 165 | * |
| 166 | + * @param Request $request Request object. | |
| 167 | + * | |
| 155 | 168 | * @return Response |
| 156 | 169 | * @defender_route |
| 157 | 170 | * @defender_redirect |
| 158 | 171 | */ |
| 159 | - public function start(): Response { | |
| 172 | + public function start( Request $request ): Response { | |
| 173 | + $data = $request->get_data( | |
| 174 | + array( | |
| 175 | + 'scan_type' => array( | |
| 176 | + 'type' => 'string', | |
| 177 | + 'sanitize' => 'sanitize_key', | |
| 178 | + ), | |
| 179 | + ) | |
| 180 | + ); | |
| 181 | + $scan_type = in_array( $data['scan_type'] ?? '', array( 'deep', 'malware' ), true ) | |
| 182 | + ? $data['scan_type'] | |
| 183 | + : 'malware'; | |
| 184 | + | |
| 160 | 185 | $model = Model_Scan::create(); |
| 161 | 186 | if ( is_object( $model ) && ! is_wp_error( $model ) ) { |
| 187 | + update_site_option( self::FIRST_SCAN_STARTED, '1' ); | |
| 188 | + set_transient( 'defender_scan_triggered_by_' . $model->id, get_current_user_id(), HOUR_IN_SECONDS ); | |
| 189 | + Model_Scan::set_scan_type( $scan_type ); | |
| 162 | 190 | $this->log( 'Initial ping self', self::SCAN_LOG ); |
| 163 | 191 | $this->run_scan_mechanisms_from( 'scan' ); |
| 164 | 192 | |
| 165 | 193 | return new Response( |
| @@ -167,8 +195,9 @@ | ||
| 167 | 195 | array( |
| 168 | 196 | 'status' => $model->status, |
| 169 | 197 | 'status_text' => $model->get_status_text(), |
| 170 | 198 | 'percent' => 0, |
| 199 | + 'scan_type' => $scan_type, | |
| 171 | 200 | ) |
| 172 | 201 | ); |
| 173 | 202 | } |
| 174 | 203 | |
| @@ -188,16 +217,14 @@ | ||
| 188 | 217 | * @is_public |
| 189 | 218 | */ |
| 190 | 219 | public function process() { |
| 191 | 220 | $lock_filename = $this->service->get_lock_filename(); |
| 192 | - if ( $this->service->has_lock( $lock_filename ) ) { | |
| 221 | + if ( ! $this->service->try_create_lock( $lock_filename ) ) { | |
| 193 | 222 | $this->log( 'Fallback as already a process is running', self::SCAN_LOG ); |
| 194 | 223 | |
| 195 | 224 | return; |
| 196 | 225 | } |
| 197 | 226 | |
| 198 | - // This creates file lock, for make sure only 1 process run as a time. | |
| 199 | - $this->service->create_lock( $lock_filename ); | |
| 200 | 227 | // Check if the ping is from self or not. |
| 201 | 228 | $ret = $this->service->process(); |
| 202 | 229 | $this->log( 'process done, queue for next', self::SCAN_LOG ); |
| 203 | 230 | if ( false === $ret ) { |
| @@ -218,14 +245,16 @@ | ||
| 218 | 245 | * @defender_route |
| 219 | 246 | * @defender_redirect |
| 220 | 247 | */ |
| 221 | 248 | public function status(): Response { |
| 249 | + $scan_type = Model_Scan::get_scan_type(); | |
| 222 | 250 | $idle_scan = wd_di()->get( Model_Scan::class )->get_idle(); |
| 223 | 251 | |
| 224 | 252 | if ( is_object( $idle_scan ) ) { |
| 225 | 253 | $this->service->update_idle_scan_status(); |
| 254 | + $response = $this->get_status_response_data( $idle_scan, $scan_type ); | |
| 226 | 255 | |
| 227 | - return new Response( false, $idle_scan->to_array() ); | |
| 256 | + return new Response( true, $response ); | |
| 228 | 257 | } |
| 229 | 258 | |
| 230 | 259 | $checksum_issue = get_site_option( Core_Integrity::ISSUE_CHECKSUMS, 'false' ); |
| 231 | 260 | $checksum_scan = Model_Scan::get_core_check(); |
| @@ -230,21 +259,33 @@ | ||
| 230 | 259 | $checksum_issue = get_site_option( Core_Integrity::ISSUE_CHECKSUMS, 'false' ); |
| 231 | 260 | $checksum_scan = Model_Scan::get_core_check(); |
| 232 | 261 | if ( 'false' !== $checksum_issue && is_object( $checksum_scan ) ) { |
| 233 | 262 | $this->service->update_idle_scan_status_by_checksum_issue( $checksum_scan ); |
| 263 | + $response = $this->get_status_response_data( $checksum_scan, $scan_type ); | |
| 234 | 264 | |
| 235 | - return new Response( false, $checksum_scan->to_array() ); | |
| 265 | + return new Response( true, $response ); | |
| 236 | 266 | } |
| 237 | 267 | |
| 238 | 268 | $scan = Model_Scan::get_active(); |
| 239 | 269 | if ( is_object( $scan ) ) { |
| 270 | + $response = $this->get_status_response_data( $scan, $scan_type ); | |
| 240 | 271 | |
| 241 | - return new Response( false, $scan->to_array() ); | |
| 272 | + return new Response( true, $response ); | |
| 242 | 273 | } |
| 274 | + | |
| 243 | 275 | $scan = Model_Scan::get_last(); |
| 244 | 276 | if ( is_object( $scan ) && ! is_wp_error( $scan ) ) { |
| 245 | - $response = $scan->to_array(); | |
| 246 | - $response['message'] = __( 'Malware scan completed successfully!', 'defender-security' ); | |
| 277 | + $response = array_merge( $this->get_status_response_data( $scan, $scan_type ), $this->get_last_scan_time_data( $scan ) ); | |
| 278 | + $response['message'] = __( 'Malware scan completed successfully!', 'defender-security' ); | |
| 279 | + $response['scan_type'] = $scan_type; | |
| 280 | + if ( 'deep' === $scan_type ) { | |
| 281 | + $security_tweaks = wd_di()->get( \WP_Defender\Controller\Security_Tweaks::class )->dashboard_widget(); | |
| 282 | + $response['hardening_count'] = (int) ( $security_tweaks['summary']['issues_count'] ?? 0 ); | |
| 283 | + } | |
| 284 | + if ( isset( $this->quarantine_controller ) ) { | |
| 285 | + $response['quarantine'] = $this->quarantine_controller->data_frontend()['list'] ?? array(); | |
| 286 | + } | |
| 287 | + | |
| 247 | 288 | return new Response( true, $response ); |
| 248 | 289 | } |
| 249 | 290 | |
| 250 | 291 | return new Response( |
| @@ -255,8 +296,24 @@ | ||
| 255 | 296 | ); |
| 256 | 297 | } |
| 257 | 298 | |
| 258 | 299 | /** |
| 300 | + * Build the compatible status payload and consume its queued messages. | |
| 301 | + * | |
| 302 | + * @param Model_Scan $scan Scan model used to build the status payload. | |
| 303 | + * @param string $scan_type Current scan type. | |
| 304 | + */ | |
| 305 | + private function get_status_response_data( Model_Scan $scan, string $scan_type ): array { | |
| 306 | + $response = $scan->to_array(); | |
| 307 | + $response['status_text'] = $response['status_text'] ?? $scan->get_status_text(); | |
| 308 | + $response['percent'] = $response['percent'] ?? $scan->percent; | |
| 309 | + $response['scan_type'] = $scan_type; | |
| 310 | + $response['status_messages'] = $scan->drain_status_messages(); | |
| 311 | + | |
| 312 | + return $response; | |
| 313 | + } | |
| 314 | + | |
| 315 | + /** | |
| 259 | 316 | * Cancel current scan. |
| 260 | 317 | * |
| 261 | 318 | * @return Response |
| 262 | 319 | * @defender_route |
| @@ -264,8 +321,9 @@ | ||
| 264 | 321 | */ |
| 265 | 322 | public function cancel(): Response { |
| 266 | 323 | $component = wd_di()->get( Scan_Component::class ); |
| 267 | 324 | $component->cancel_a_scan(); |
| 325 | + Model_Scan::clear_scan_type(); | |
| 268 | 326 | $last = Model_Scan::get_last(); |
| 269 | 327 | if ( is_object( $last ) && ! is_wp_error( $last ) ) { |
| 270 | 328 | $last = $last->to_array(); |
| 271 | 329 | } |
| @@ -376,9 +434,9 @@ | ||
| 376 | 434 | $intention, |
| 377 | 435 | $allowed_intentions, |
| 378 | 436 | true |
| 379 | 437 | ) ) { |
| 380 | - wp_die(); | |
| 438 | + return new Response( false, array() ); | |
| 381 | 439 | } |
| 382 | 440 | |
| 383 | 441 | $scan = Model_Scan::get_last(); |
| 384 | 442 | if ( $scan instanceof Model_Scan ) { |
| @@ -383,14 +441,22 @@ | ||
| 383 | 441 | $scan = Model_Scan::get_last(); |
| 384 | 442 | if ( $scan instanceof Model_Scan ) { |
| 385 | 443 | $item = $scan->get_issue( $id ); |
| 386 | 444 | if ( is_object( $item ) && $item->has_method( $intention ) ) { |
| 445 | + if ( 'resolve' === $intention ) { | |
| 446 | + $result = $item->resolve(); | |
| 447 | + } elseif ( 'quarantine' === $intention ) { | |
| 448 | + $result = $item->quarantine( $data['parent_action'], $item->owner ); | |
| 449 | + } elseif ( 'delete' === $intention ) { | |
| 450 | + $result = $item->delete(); | |
| 451 | + } elseif ( 'ignore' === $intention ) { | |
| 452 | + $result = $item->ignore(); | |
| 453 | + } elseif ( 'unignore' === $intention ) { | |
| 454 | + $result = $item->unignore(); | |
| 455 | + } elseif ( 'pull_src' === $intention ) { | |
| 456 | + $result = $item->pull_src(); | |
| 457 | + } | |
| 387 | 458 | |
| 388 | - if ( 'quarantine' === $intention ) { | |
| 389 | - $result = $item->$intention( $data['parent_action'], $item->owner ); | |
| 390 | - } else { | |
| 391 | - $result = $item->$intention(); | |
| 392 | - } | |
| 393 | 459 | // Maybe track. |
| 394 | 460 | if ( $this->is_tracking_active() ) { |
| 395 | 461 | $this->item_action_analytics( $item, $intention ); |
| 396 | 462 | } |
| @@ -422,8 +488,12 @@ | ||
| 422 | 488 | |
| 423 | 489 | if ( $scan instanceof Model_Scan ) { |
| 424 | 490 | $result['scan'] = $scan->to_array(); |
| 425 | 491 | |
| 492 | + if ( 'quarantine' === $intention && isset( $this->quarantine_controller ) ) { | |
| 493 | + $result['quarantine'] = $this->quarantine_controller->data_frontend()['list'] ?? array(); | |
| 494 | + } | |
| 495 | + | |
| 426 | 496 | $success = true; |
| 427 | 497 | if ( isset( $result['success'] ) && false === $result['success'] ) { |
| 428 | 498 | $success = false; |
| 429 | 499 | } |
| @@ -543,18 +613,15 @@ | ||
| 543 | 613 | * @defender_route |
| 544 | 614 | */ |
| 545 | 615 | public function save_settings( Request $request ): Response { |
| 546 | 616 | $data = $request->get_data_by_model( $this->model ); |
| 547 | - // Case#1: enable all child options, if parent and all child options are disabled, so that there is no notice when saving. | |
| 548 | - if ( | |
| 549 | - ! $data['integrity_check'] | |
| 550 | - && ! $data['check_core'] | |
| 551 | - && ! $data['check_plugins'] | |
| 552 | - ) { | |
| 553 | - $data['check_core'] = true; | |
| 554 | - $data['check_plugins'] = true; | |
| 617 | + // Prepare for the state's change. | |
| 618 | + $old_integrity_check_state = $this->model->integrity_check; | |
| 619 | + // Case#1: inherit the parent's state to nested options. | |
| 620 | + if ( $old_integrity_check_state !== $data['integrity_check'] ) { | |
| 621 | + $data['check_core'] = $data['integrity_check']; | |
| 622 | + $data['check_plugins'] = $data['integrity_check']; | |
| 555 | 623 | } |
| 556 | - | |
| 557 | 624 | // Case#2: Suspicious code is activated BUT File change detection is deactivated then show the notice. |
| 558 | 625 | if ( $data['scan_malware'] && ! $data['integrity_check'] ) { |
| 559 | 626 | $response = array( |
| 560 | 627 | 'type_notice' => 'info', |
| @@ -570,36 +637,17 @@ | ||
| 570 | 637 | '</strong>' |
| 571 | 638 | ), |
| 572 | 639 | ); |
| 573 | 640 | } else { |
| 574 | - // Prepare response message for usual successful case. | |
| 575 | 641 | $response = array( |
| 576 | 642 | 'message' => esc_html__( 'Your settings have been updated.', 'defender-security' ), |
| 577 | 643 | 'auto_close' => true, |
| 578 | 644 | ); |
| 579 | 645 | } |
| 580 | - // Additional cases are in the Scan model. | |
| 581 | - $report_change = false; | |
| 582 | - // If 'Scheduled Scanning' is checked then need to change Malware_Report. | |
| 583 | - if ( true === $data['scheduled_scanning'] ) { | |
| 584 | - $report = new Malware_Report(); | |
| 585 | - $report_change = true; | |
| 586 | - $report->frequency = $data['frequency']; | |
| 587 | - $report->day = $data['day']; | |
| 588 | - $report->day_n = (int) $data['day_n']; | |
| 589 | - $report->time = $data['time']; | |
| 590 | - // Disable 'Scheduled Scanning'. | |
| 591 | - } elseif ( true === $this->model->scheduled_scanning && false === $data['scheduled_scanning'] ) { | |
| 592 | - $report = new Malware_Report(); | |
| 593 | - $report_change = true; | |
| 594 | - $report->status = \WP_Defender\Model\Notification::STATUS_DISABLED; | |
| 595 | - } | |
| 596 | - | |
| 597 | 646 | $before_import_schedule = $this->model->quarantine_expire_schedule; |
| 598 | 647 | |
| 599 | 648 | $this->model->import( $data ); |
| 600 | 649 | if ( $this->model->validate() ) { |
| 601 | - | |
| 602 | 650 | if ( class_exists( 'WP_Defender\Component\Quarantine' ) ) { |
| 603 | 651 | $quarantine_component = wd_di()->get( Quarantine_Component::class ); |
| 604 | 652 | $quarantine_component->reschedule_file_expiry_cron( |
| 605 | 653 | $before_import_schedule, |
| @@ -606,14 +654,9 @@ | ||
| 606 | 654 | $data['quarantine_expire_schedule'] |
| 607 | 655 | ); |
| 608 | 656 | } |
| 609 | 657 | |
| 610 | - // Todo: need to disable Malware_Notification & Malware_Report if all scan settings are deactivated? | |
| 611 | 658 | $this->model->save(); |
| 612 | - // Save Report's changes. | |
| 613 | - if ( $report_change ) { | |
| 614 | - $report->save(); | |
| 615 | - } | |
| 616 | 659 | Config_Hub_Helper::set_clear_active_flag(); |
| 617 | 660 | |
| 618 | 661 | return new Response( |
| 619 | 662 | true, |
| @@ -647,9 +690,9 @@ | ||
| 647 | 690 | 'type' => 'string', |
| 648 | 691 | 'sanitize' => 'sanitize_text_field', |
| 649 | 692 | ), |
| 650 | 693 | 'type' => array( |
| 651 | - 'type' => 'string', | |
| 694 | + 'type' => 'array', | |
| 652 | 695 | 'sanitize' => 'sanitize_text_field', |
| 653 | 696 | ), |
| 654 | 697 | 'per_page' => array( |
| 655 | 698 | 'type' => 'string', |
| @@ -668,30 +711,167 @@ | ||
| 668 | 711 | if ( ! $v->validate() ) { |
| 669 | 712 | return new Response( |
| 670 | 713 | false, |
| 671 | 714 | array( |
| 672 | - 'message' => '', | |
| 715 | + 'message' => esc_html__( 'Wrong scan issue data.', 'defender-security' ), | |
| 673 | 716 | ) |
| 674 | 717 | ); |
| 675 | 718 | } |
| 676 | 719 | |
| 677 | 720 | $scan = Model_Scan::get_last(); |
| 678 | - $issues = $scan->to_array( $data['per_page'], $data['paged'], $data['type'] ); | |
| 721 | + $issues = $scan->to_array( $data['per_page'], $data['paged'], $this->normalize_issue_types( $data['type'] ), $data['scenario'] ); | |
| 679 | 722 | |
| 680 | - return new Response( | |
| 681 | - true, | |
| 682 | - array( | |
| 683 | - 'issue' => $issues['issues_items'], | |
| 684 | - 'ignored' => $issues['ignored_items'], | |
| 685 | - 'paging' => $issues['paging'], | |
| 686 | - 'count' => $issues['count'], | |
| 687 | - ) | |
| 723 | + $response = array( | |
| 724 | + 'issue' => $issues['issues_items'], | |
| 725 | + 'ignored' => $issues['ignored_items'], | |
| 726 | + 'paging' => $issues['paging'], | |
| 727 | + 'count' => $issues['count'], | |
| 688 | 728 | ); |
| 729 | + | |
| 730 | + if ( class_exists( 'WP_Defender\Controller\Quarantine' ) ) { | |
| 731 | + $response['quarantine'] = $this->quarantine_controller->data_frontend()['list'] ?? array(); | |
| 732 | + } | |
| 733 | + | |
| 734 | + return new Response( true, $response ); | |
| 689 | 735 | } |
| 690 | 736 | |
| 691 | 737 | /** |
| 692 | - * Render main page. | |
| 738 | + * Normalize issue type filters from the legacy and redesign payloads. | |
| 693 | 739 | * |
| 740 | + * @param array|string $types Requested issue type(s). | |
| 741 | + * | |
| 742 | + * @return array|string | |
| 743 | + */ | |
| 744 | + private function normalize_issue_types( $types ) { | |
| 745 | + $type_aliases = array( | |
| 746 | + 'core' => Scan_Item::TYPE_INTEGRITY, | |
| 747 | + 'plugin' => Scan_Item::TYPE_PLUGIN_CHECK, | |
| 748 | + 'suspicious' => Scan_Item::TYPE_SUSPICIOUS, | |
| 749 | + 'known_vulnerability' => Scan_Item::TYPE_VULNERABILITY, | |
| 750 | + 'plugin_closed' => Scan_Item::TYPE_PLUGIN_CLOSED, | |
| 751 | + 'plugin_outdated' => Scan_Item::TYPE_PLUGIN_OUTDATED, | |
| 752 | + ); | |
| 753 | + | |
| 754 | + $types = is_array( $types ) ? $types : array( $types ); | |
| 755 | + $types = array_filter( | |
| 756 | + array_map( | |
| 757 | + static function ( $type ) use ( $type_aliases ) { | |
| 758 | + return $type_aliases[ $type ] ?? $type; | |
| 759 | + }, | |
| 760 | + $types | |
| 761 | + ), | |
| 762 | + 'boolval' | |
| 763 | + ); | |
| 764 | + $types = array_values( array_unique( $types ) ); | |
| 765 | + | |
| 766 | + if ( 1 === count( $types ) ) { | |
| 767 | + return $types[0]; | |
| 768 | + } | |
| 769 | + | |
| 770 | + return $types; | |
| 771 | + } | |
| 772 | + | |
| 773 | + /** | |
| 774 | + * Get relative and exact display times for the last completed scan. | |
| 775 | + * | |
| 776 | + * @param Model_Scan|null $last Last completed scan. | |
| 777 | + * | |
| 778 | + * @return array | |
| 779 | + */ | |
| 780 | + private function get_last_scan_time_data( $last ): array { | |
| 781 | + if ( ! is_object( $last ) || ! isset( $last->date_start ) || '' === $last->date_start ) { | |
| 782 | + return array( | |
| 783 | + 'last_scan' => '', | |
| 784 | + 'last_scan_time' => '', | |
| 785 | + ); | |
| 786 | + } | |
| 787 | + | |
| 788 | + $last_scan_timestamp = strtotime( $last->date_start . ' UTC' ); | |
| 789 | + $time_difference = time() - $last_scan_timestamp; | |
| 790 | + $data = array( | |
| 791 | + 'last_scan' => sprintf( | |
| 792 | + /* translators: %s: human-readable time difference, e.g. "5 minutes ago" */ | |
| 793 | + __( 'Scanned %s ago', 'defender-security' ), | |
| 794 | + human_time_diff( $last_scan_timestamp ) | |
| 795 | + ), | |
| 796 | + ); | |
| 797 | + | |
| 798 | + if ( $time_difference < DAY_IN_SECONDS ) { | |
| 799 | + $data['last_scan_time'] = wp_date( 'g:i A', $last_scan_timestamp ); | |
| 800 | + } elseif ( $time_difference < YEAR_IN_SECONDS ) { | |
| 801 | + $data['last_scan_time'] = wp_date( 'D, j M g:i A', $last_scan_timestamp ); | |
| 802 | + } else { | |
| 803 | + $data['last_scan_time'] = wp_date( 'j M Y, g:i A', $last_scan_timestamp ); | |
| 804 | + } | |
| 805 | + | |
| 806 | + return $data; | |
| 807 | + } | |
| 808 | + | |
| 809 | + /** | |
| 810 | + * Returns scan result data for the frontend on page load. | |
| 811 | + * | |
| 812 | + * @return array | |
| 813 | + */ | |
| 814 | + public function get_initial_scan_data(): array { | |
| 815 | + $scan = Model_Scan::get_active(); | |
| 816 | + $last = Model_Scan::get_last(); | |
| 817 | + $per_page = self::DEFAULT_PER_PAGE; | |
| 818 | + $paged = 1; | |
| 819 | + | |
| 820 | + if ( ! is_object( $scan ) && ! is_object( $last ) ) { | |
| 821 | + $scan_data = null; | |
| 822 | + } elseif ( is_object( $scan ) && is_object( $last ) ) { | |
| 823 | + // If an active scan exists AND there's a previous completed scan, | |
| 824 | + // merge the active scan's progress with the last scan's issue data. | |
| 825 | + // This ensures that during a page refresh while scanning, users still | |
| 826 | + // see the previous scan results while the new scan is in progress. | |
| 827 | + $scan_data = $scan->to_array( $per_page, $paged ); | |
| 828 | + $last_data = $last->to_array( $per_page, $paged ); | |
| 829 | + // Preserve previous scan's issue data. | |
| 830 | + $scan_data['issues_items'] = $last_data['issues_items'] ?? array(); | |
| 831 | + $scan_data['ignored_items'] = $last_data['ignored_items'] ?? array(); | |
| 832 | + $scan_data['count'] = $last_data['count'] ?? array(); | |
| 833 | + $scan_data['paging'] = $last_data['paging'] ?? array(); | |
| 834 | + } elseif ( is_object( $scan ) ) { | |
| 835 | + $scan_data = $scan->to_array( $per_page, $paged ); | |
| 836 | + } else { | |
| 837 | + $scan_data = $last->to_array( $per_page, $paged ); | |
| 838 | + } | |
| 839 | + | |
| 840 | + $first_scan_started = get_site_option( self::FIRST_SCAN_STARTED, null ); | |
| 841 | + if ( null === $first_scan_started ) { | |
| 842 | + // Existing installations backward compatibility. | |
| 843 | + $first_scan_started = is_object( $scan ) || is_object( $last ); | |
| 844 | + } else { | |
| 845 | + $first_scan_started = '1' === (string) $first_scan_started; | |
| 846 | + } | |
| 847 | + | |
| 848 | + $data = array( | |
| 849 | + 'scan' => $scan_data, | |
| 850 | + 'has_first_scan_started' => $first_scan_started, | |
| 851 | + 'isEnabledScanType' => $this->service->is_any_scan_type_active(), | |
| 852 | + ); | |
| 853 | + | |
| 854 | + // Always expose the last completed scan time at the outer level so the | |
| 855 | + // dashboard can show it even while a new scan is actively running. | |
| 856 | + $data = array_merge( $data, $this->get_last_scan_time_data( $last ) ); | |
| 857 | + | |
| 858 | + if ( isset( $this->quarantine_controller ) ) { | |
| 859 | + $data['quarantine'] = array( | |
| 860 | + 'list' => $this->quarantine_controller->data_frontend()['list'] ?? array(), | |
| 861 | + ); | |
| 862 | + } | |
| 863 | + // Display Rate notice. Without the rating's type. | |
| 864 | + $data['isRatingDisplayed'] = defender_is_wp_org_version() | |
| 865 | + && is_array( $scan_data ) && isset( $data['scan']['issues_items'] ) | |
| 866 | + && Rate_Component::is_displayed_in_redesigned_version( count( $data['scan']['issues_items'] ) ); | |
| 867 | + | |
| 868 | + return array( 'scan' => $data ); | |
| 869 | + } | |
| 870 | + | |
| 871 | + /** | |
| 872 | + * Render the main page. | |
| 873 | + * | |
| 694 | 874 | * @return void |
| 695 | 875 | */ |
| 696 | 876 | public function main_view(): void { |
| 697 | 877 | $this->render( 'main' ); |
| @@ -704,11 +884,58 @@ | ||
| 704 | 884 | public function enqueue_assets() { |
| 705 | 885 | if ( ! $this->is_page_active() ) { |
| 706 | 886 | return; |
| 707 | 887 | } |
| 708 | - wp_localize_script( 'def-scan', 'scan', $this->data_frontend() ); | |
| 709 | - wp_enqueue_script( 'def-scan' ); | |
| 710 | - wp_enqueue_script( 'clipboard' ); | |
| 888 | + | |
| 889 | + $handle = 'defender-ui-scan'; | |
| 890 | + wp_enqueue_script( | |
| 891 | + $handle, | |
| 892 | + WP_DEFENDER_BASE_URL . 'assets/js/scan-ui.js', | |
| 893 | + array( 'def-vue', 'def-manifest', 'def-core-ui', 'defender', 'wp-i18n' ), | |
| 894 | + DEFENDER_VERSION, | |
| 895 | + true | |
| 896 | + ); | |
| 897 | + wp_set_script_translations( $handle, 'wpdef' ); | |
| 898 | + | |
| 899 | + $scan_routes_data = $this->dump_routes_and_nonces(); | |
| 900 | + $quarantine_routes_data = $this->quarantine_controller->dump_routes_and_nonces(); | |
| 901 | + if ( defender_is_wp_org_version() ) { | |
| 902 | + $rate_routes_nonces = wd_di()->get( \WP_Defender\Controller\Rate::class )->dump_routes_and_nonces(); | |
| 903 | + $rate_routes = $rate_routes_nonces['routes']; | |
| 904 | + $rate_nonces = $rate_routes_nonces['nonces']; | |
| 905 | + } else { | |
| 906 | + $rate_routes = array(); | |
| 907 | + $rate_nonces = array(); | |
| 908 | + } | |
| 909 | + | |
| 910 | + wp_localize_script( | |
| 911 | + $handle, | |
| 912 | + 'defenderUIData', | |
| 913 | + array_merge( | |
| 914 | + $this->get_shared_data(), | |
| 915 | + array( | |
| 916 | + 'routes' => array_merge( | |
| 917 | + $scan_routes_data['routes'], | |
| 918 | + $quarantine_routes_data['routes'], | |
| 919 | + $rate_routes | |
| 920 | + ), | |
| 921 | + 'nonces' => array_merge( | |
| 922 | + $scan_routes_data['nonces'], | |
| 923 | + $quarantine_routes_data['nonces'], | |
| 924 | + $rate_nonces | |
| 925 | + ), | |
| 926 | + ), | |
| 927 | + $this->get_initial_scan_data() | |
| 928 | + ) | |
| 929 | + ); | |
| 930 | + | |
| 931 | + wp_enqueue_style( | |
| 932 | + $handle, | |
| 933 | + WP_DEFENDER_BASE_URL . 'assets/css/core-ui.css', | |
| 934 | + array(), | |
| 935 | + DEFENDER_VERSION | |
| 936 | + ); | |
| 937 | + | |
| 711 | 938 | $this->enqueue_main_assets(); |
| 712 | 939 | } |
| 713 | 940 | |
| 714 | 941 | /** |
| @@ -747,9 +974,12 @@ | ||
| 747 | 974 | /** |
| 748 | 975 | * Delete all the data & the cache. |
| 749 | 976 | */ |
| 750 | 977 | public function remove_data(): void { |
| 978 | + delete_site_option( self::FIRST_SCAN_STARTED ); | |
| 751 | 979 | delete_site_option( Model_Scan::IGNORE_INDEXER ); |
| 980 | + delete_site_option( Model_Scan::OPTION_SCAN_TYPE ); | |
| 981 | + Model_Scan::clear_all_status_messages(); | |
| 752 | 982 | delete_site_option( Core_Integrity::ISSUE_CHECKSUMS ); |
| 753 | 983 | delete_site_transient( Plugin_Integrity::$org_slugs ); |
| 754 | 984 | delete_site_transient( Plugin_Integrity::$org_responses ); |
| 755 | 985 | } |
| @@ -768,61 +998,23 @@ | ||
| 768 | 998 | $scan = null; |
| 769 | 999 | } else { |
| 770 | 1000 | $scan = is_object( $scan ) ? $scan->to_array( $per_page, $paged ) : $last->to_array( $per_page, $paged ); |
| 771 | 1001 | } |
| 772 | - $settings = new Scan_Settings(); | |
| 773 | - $report = wd_di()->get( Malware_Report::class ); | |
| 774 | - $report_text = esc_html__( 'Automatic scans are disabled', 'defender-security' ); | |
| 775 | - if ( $settings->scheduled_scanning && isset( $settings->frequency ) ) { | |
| 776 | - $report_text = sprintf( | |
| 777 | - /* translators: 1. Line break tag. 2. Frequency value. */ | |
| 778 | - esc_html__( 'Automatic scans are %1$srunning %2$s', 'defender-security' ), | |
| 779 | - '<br/>', | |
| 780 | - $settings->frequency | |
| 781 | - ); | |
| 782 | - } | |
| 783 | - $misc = array( | |
| 784 | - 'outdated_period' => \WP_Defender\Behavior\Scan\Abandoned_Plugin::get_outdated_period(), | |
| 785 | - 'labels' => $settings->labels(), | |
| 786 | - 'days_of_week' => $this->get_days_of_week(), | |
| 787 | - 'times_of_day' => $this->get_times(), | |
| 788 | - ); | |
| 789 | - $misc['timezone_text'] = sprintf( | |
| 790 | - /* translators: 1. Timezone. 2. Time. */ | |
| 791 | - esc_html__( | |
| 792 | - 'Your timezone is set to %1$s, so your current time is %2$s.', | |
| 793 | - 'defender-security' | |
| 794 | - ), | |
| 795 | - '<strong>' . wp_timezone_string() . '</strong>', | |
| 796 | - '<strong>' . wp_date( 'H:i', time() ) . '</strong>' | |
| 797 | - ); | |
| 1002 | + $settings = new Scan_Settings(); | |
| 1003 | + $report = wd_di()->get( Malware_Report::class ); | |
| 798 | 1004 | |
| 799 | - // Todo: add logic for deactivated scan settings. Maybe display some notice. | |
| 800 | - $data = array( | |
| 801 | - 'scan' => $scan, | |
| 802 | - 'settings' => $settings->export(), | |
| 803 | - 'report' => $report_text, | |
| 804 | - 'active_tools' => array( | |
| 805 | - 'integrity_check' => $settings->integrity_check, | |
| 806 | - 'check_known_vuln' => $settings->check_known_vuln, | |
| 807 | - 'scan_malware' => $settings->scan_malware, | |
| 808 | - 'scheduled_scanning' => $settings->scheduled_scanning, | |
| 809 | - 'check_abandoned_plugin' => $settings->check_abandoned_plugin, | |
| 1005 | + $scan['isEnabledScanType'] = $this->service->is_any_scan_type_active(); | |
| 1006 | + | |
| 1007 | + $data = array( | |
| 1008 | + 'scan' => $scan, | |
| 1009 | + 'settings' => $settings->export(), | |
| 1010 | + 'notification' => $report->to_string(), | |
| 1011 | + 'misc' => array( | |
| 1012 | + 'labels' => $settings->labels(), | |
| 810 | 1013 | ), |
| 811 | - 'notification' => $report->to_string(), | |
| 812 | - 'next_run' => $report->get_next_run_as_string(), | |
| 813 | - 'misc' => $misc, | |
| 814 | - 'upsell' => array( | |
| 815 | - 'scan' => $this->get_scan_upsell( 'scan' ), | |
| 816 | - ), | |
| 817 | - 'hub_connector' => wd_di()->get( Hub_Connector::class )->data_frontend(), | |
| 818 | - 'antibot' => wd_di()->get( Antibot_Global_Firewall::class )->data_frontend(), | |
| 819 | 1014 | ); |
| 1015 | + $data['quarantine'] = $this->quarantine_controller->data_frontend(); | |
| 820 | 1016 | |
| 821 | - if ( class_exists( 'WP_Defender\Controller\Quarantine' ) ) { | |
| 822 | - $data['quarantine'] = $this->quarantine_controller->data_frontend(); | |
| 823 | - } | |
| 824 | - | |
| 825 | 1017 | return array_merge( $data, $this->dump_routes_and_nonces() ); |
| 826 | 1018 | } |
| 827 | 1019 | |
| 828 | 1020 | /** |
| @@ -847,30 +1039,8 @@ | ||
| 847 | 1039 | } |
| 848 | 1040 | } |
| 849 | 1041 | |
| 850 | 1042 | /** |
| 851 | - * Checks if any scan is active. | |
| 852 | - * | |
| 853 | - * @param bool $is_pro Indicates if the product is a pro version. | |
| 854 | - * | |
| 855 | - * @return bool True if any scan is active, false otherwise. | |
| 856 | - */ | |
| 857 | - private function is_any_active( bool $is_pro ): bool { | |
| 858 | - $settings = new Scan_Settings(); | |
| 859 | - $file_change_check = $settings->is_checked_any_file_change_types(); | |
| 860 | - | |
| 861 | - if ( $is_pro ) { | |
| 862 | - // Pro version. Check all parent types. | |
| 863 | - return $file_change_check || $settings->check_known_vuln || $settings->scan_malware; | |
| 864 | - } else { | |
| 865 | - // Free version: | |
| 866 | - // Check the 'File change detection' type because only it's available with nested types. | |
| 867 | - // Check the Abandoned plugin type. | |
| 868 | - return $file_change_check || $settings->check_abandoned_plugin; | |
| 869 | - } | |
| 870 | - } | |
| 871 | - | |
| 872 | - /** | |
| 873 | 1043 | * Exports strings. |
| 874 | 1044 | * |
| 875 | 1045 | * @return array An array of strings. |
| 876 | 1046 | */ |
| @@ -875,48 +1045,37 @@ | ||
| 875 | 1045 | * @return array An array of strings. |
| 876 | 1046 | */ |
| 877 | 1047 | public function export_strings(): array { |
| 878 | 1048 | $strings = array(); |
| 879 | - if ( $this->is_any_active( $this->is_pro ) ) { | |
| 1049 | + if ( $this->service->is_any_scan_type_active() ) { | |
| 880 | 1050 | $strings[] = esc_html__( 'Active', 'defender-security' ); |
| 881 | 1051 | } else { |
| 882 | 1052 | $strings[] = esc_html__( 'Inactive', 'defender-security' ); |
| 883 | 1053 | } |
| 884 | 1054 | |
| 885 | - $scan_report = new Malware_Report(); | |
| 886 | 1055 | $scan_notification = new Malware_Notification(); |
| 887 | 1056 | if ( 'enabled' === $scan_notification->status ) { |
| 888 | 1057 | $strings[] = esc_html__( 'Email notifications active', 'defender-security' ); |
| 889 | 1058 | } |
| 890 | - if ( $this->is_pro && 'enabled' === $scan_report->status ) { | |
| 891 | 1059 | $strings[] = sprintf( |
| 892 | - /* translators: %s: Frequency value. */ | |
| 893 | - esc_html__( 'Email reports sending %s', 'defender-security' ), | |
| 894 | - $scan_report->frequency | |
| 895 | - ); | |
| 896 | - } elseif ( ! $this->is_pro ) { | |
| 897 | - $strings[] = sprintf( | |
| 898 | 1060 | /* translators: %s: Html for Pro-tag. */ |
| 899 | - esc_html__( 'Email report inactive %s', 'defender-security' ), | |
| 1061 | + esc_html__( 'Scheduled scan inactive %s', 'defender-security' ), | |
| 900 | 1062 | '<span class="sui-tag sui-tag-pro">Pro</span>' |
| 901 | 1063 | ); |
| 902 | - } | |
| 903 | 1064 | |
| 904 | 1065 | return $strings; |
| 905 | 1066 | } |
| 906 | 1067 | |
| 907 | 1068 | /** |
| 908 | - * Generates configuration strings based on the provided configuration and | |
| 909 | - * whether the product is a pro version. | |
| 1069 | + * Generates configuration strings based on the provided configuration. | |
| 910 | 1070 | * |
| 911 | 1071 | * @param array $config Configuration data. |
| 912 | - * @param bool $is_pro Indicates if the product is a pro version. | |
| 913 | 1072 | * |
| 914 | 1073 | * @return array Returns an array of configuration strings. |
| 915 | 1074 | */ |
| 916 | - public function config_strings( array $config, bool $is_pro ): array { | |
| 1075 | + public function config_strings( array $config ): array { | |
| 917 | 1076 | $strings = array(); |
| 918 | - $strings[] = $this->service->is_any_scan_active( $config ) | |
| 1077 | + $strings[] = $this->service->check_scan_active_by( $config ) | |
| 919 | 1078 | ? esc_html__( 'Active', 'defender-security' ) |
| 920 | 1079 | : esc_html__( 'Inactive', 'defender-security' ); |
| 921 | 1080 | |
| 922 | 1081 | if ( 'enabled' === $config['notification'] ) { |
| @@ -921,18 +1080,13 @@ | ||
| 921 | 1080 | |
| 922 | 1081 | if ( 'enabled' === $config['notification'] ) { |
| 923 | 1082 | $strings[] = esc_html__( 'Email notifications active', 'defender-security' ); |
| 924 | 1083 | } |
| 925 | - if ( $is_pro && 'enabled' === $config['report'] ) { | |
| 1084 | + // No Pro membership and No hosted site. | |
| 1085 | + if ( ! $this->is_unlocked ) { | |
| 926 | 1086 | $strings[] = sprintf( |
| 927 | - /* translators: %s: Frequency value. */ | |
| 928 | - esc_html__( 'Email reports sending %s', 'defender-security' ), | |
| 929 | - $config['frequency'] | |
| 930 | - ); | |
| 931 | - } elseif ( ! $is_pro ) { | |
| 932 | - $strings[] = sprintf( | |
| 933 | 1087 | /* translators: %s: Html for Pro-tag. */ |
| 934 | - esc_html__( 'Email report inactive %s', 'defender-security' ), | |
| 1088 | + esc_html__( 'Scheduled scan inactive %s', 'defender-security' ), | |
| 935 | 1089 | '<span class="sui-tag sui-tag-pro">Pro</span>' |
| 936 | 1090 | ); |
| 937 | 1091 | } |
| 938 | 1092 | |
| @@ -962,15 +1116,17 @@ | ||
| 962 | 1116 | wd_di()->get( Model_Scan::class )->delete_idle(); |
| 963 | 1117 | // Delete the slug from the previous scan. |
| 964 | 1118 | delete_site_option( Core_Integrity::ISSUE_CHECKSUMS ); |
| 965 | 1119 | |
| 966 | - as_enqueue_async_action( | |
| 967 | - 'defender/async_scan', | |
| 968 | - array( | |
| 969 | - 'type' => $type, | |
| 970 | - ), | |
| 971 | - 'defender' | |
| 972 | - ); | |
| 1120 | + if ( function_exists( 'as_enqueue_async_action' ) ) { | |
| 1121 | + as_enqueue_async_action( | |
| 1122 | + 'defender/async_scan', | |
| 1123 | + array( | |
| 1124 | + 'type' => $type, | |
| 1125 | + ), | |
| 1126 | + 'defender' | |
| 1127 | + ); | |
| 1128 | + } | |
| 973 | 1129 | } |
| 974 | 1130 | |
| 975 | 1131 | /** |
| 976 | 1132 | * Clear completed action scheduler logs. |
| @@ -1015,9 +1171,13 @@ | ||
| 1015 | 1171 | * |
| 1016 | 1172 | * @return void |
| 1017 | 1173 | */ |
| 1018 | 1174 | public function scan_completed_analytics( $action_id ) { |
| 1019 | - if ( 'defender' === ActionScheduler::store()->fetch_action( $action_id )->get_group() ) { | |
| 1175 | + if ( | |
| 1176 | + class_exists( ActionScheduler::class ) | |
| 1177 | + && method_exists( ActionScheduler::class, 'store' ) | |
| 1178 | + && 'defender' === ActionScheduler::store()->fetch_action( $action_id )->get_group() | |
| 1179 | + ) { | |
| 1020 | 1180 | $scan_analytics = wd_di()->get( Scan_Analytics::class ); |
| 1021 | 1181 | |
| 1022 | 1182 | $scan_model = wd_di()->get( Model_Scan::class ); |
| 1023 | 1183 | $analytics_data = $scan_analytics->scan_completed( $scan_model ); |