PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
← All changes | src/controller/class-scan.php +346 -215 5.7.0 → 6.3.0 View file →
@@ -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,20 +162,35 @@
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 - $this->service->gather_actioned_plugin_details();
191 + $this->run_scan_mechanisms_from( 'scan' );
164 192
165 - $this->do_async_scan( 'scan' );
166 -
167 193 return new Response(
168 194 true,
169 195 array(
170 196 'status' => $model->status,
@@ -169,8 +195,9 @@
169 195 array(
170 196 'status' => $model->status,
171 197 'status_text' => $model->get_status_text(),
172 198 'percent' => 0,
199 + 'scan_type' => $scan_type,
173 200 )
174 201 );
175 202 }
176 203
@@ -189,16 +216,15 @@
189 216 * @defender_route
190 217 * @is_public
191 218 */
192 219 public function process() {
193 - if ( $this->service->has_lock() ) {
220 + $lock_filename = $this->service->get_lock_filename();
221 + if ( ! $this->service->try_create_lock( $lock_filename ) ) {
194 222 $this->log( 'Fallback as already a process is running', self::SCAN_LOG );
195 223
196 224 return;
197 225 }
198 226
199 - // This creates file lock, for make sure only 1 process run as a time.
200 - $this->service->create_lock();
201 227 // Check if the ping is from self or not.
202 228 $ret = $this->service->process();
203 229 $this->log( 'process done, queue for next', self::SCAN_LOG );
204 230 if ( false === $ret ) {
@@ -203,13 +229,13 @@
203 229 $this->log( 'process done, queue for next', self::SCAN_LOG );
204 230 if ( false === $ret ) {
205 231 // Ping self.
206 232 $this->log( 'Scan not done, pinging', self::SCAN_LOG );
207 - $this->service->remove_lock();
233 + $this->service->remove_lock( $lock_filename );
208 234 $this->process();
209 235 } else {
210 236 $this->queue_to_sync_with_hub();
211 - $this->service->remove_lock();
237 + $this->service->remove_lock( $lock_filename );
212 238 }
213 239 }
214 240
215 241 /**
@@ -219,14 +245,16 @@
219 245 * @defender_route
220 246 * @defender_redirect
221 247 */
222 248 public function status(): Response {
249 + $scan_type = Model_Scan::get_scan_type();
223 250 $idle_scan = wd_di()->get( Model_Scan::class )->get_idle();
224 251
225 252 if ( is_object( $idle_scan ) ) {
226 253 $this->service->update_idle_scan_status();
254 + $response = $this->get_status_response_data( $idle_scan, $scan_type );
227 255
228 - return new Response( false, $idle_scan->to_array() );
256 + return new Response( true, $response );
229 257 }
230 258
231 259 $checksum_issue = get_site_option( Core_Integrity::ISSUE_CHECKSUMS, 'false' );
232 260 $checksum_scan = Model_Scan::get_core_check();
@@ -231,21 +259,34 @@
231 259 $checksum_issue = get_site_option( Core_Integrity::ISSUE_CHECKSUMS, 'false' );
232 260 $checksum_scan = Model_Scan::get_core_check();
233 261 if ( 'false' !== $checksum_issue && is_object( $checksum_scan ) ) {
234 262 $this->service->update_idle_scan_status_by_checksum_issue( $checksum_scan );
263 + $response = $this->get_status_response_data( $checksum_scan, $scan_type );
235 264
236 - return new Response( false, $checksum_scan->to_array() );
265 + return new Response( true, $response );
237 266 }
238 267
239 268 $scan = Model_Scan::get_active();
240 269 if ( is_object( $scan ) ) {
270 + $response = $this->get_status_response_data( $scan, $scan_type );
241 271
242 - return new Response( false, $scan->to_array() );
272 + return new Response( true, $response );
243 273 }
274 +
244 275 $scan = Model_Scan::get_last();
245 276 if ( is_object( $scan ) && ! is_wp_error( $scan ) ) {
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 + }
246 287
247 - return new Response( true, $scan->to_array() );
288 + return new Response( true, $response );
248 289 }
249 290
250 291 return new Response(
251 292 false,
@@ -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'] );
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 }
@@ -461,10 +531,10 @@
461 531 $items = $data['items'] ?? array();
462 532 $intention = $data['bulk'] ?? false;
463 533
464 534 if (
465 - empty( $items )
466 - || ! is_array( $items )
535 + ! is_array( $items )
536 + || array() === $items
467 537 || ! in_array( $intention, array( 'ignore', 'unignore', 'delete' ), true )
468 538 ) {
469 539 return new Response( false, array() );
470 540 }
@@ -479,13 +549,11 @@
479 549 $none_delete_items = array();
480 550 $sync_hub = false;
481 551 foreach ( $items as $id ) {
482 552 if ( 'ignore' === $intention ) {
483 - $scan->ignore_issue( (int) $id );
484 - $sync_hub = true;
553 + $sync_hub = $scan->ignore_issue( (int) $id );
485 554 } elseif ( 'unignore' === $intention ) {
486 - $scan->unignore_issue( (int) $id );
487 - $sync_hub = true;
555 + $sync_hub = $scan->unignore_issue( (int) $id );
488 556 } elseif ( 'delete' === $intention ) {
489 557 $item = $scan->get_issue( (int) $id );
490 558 // Work with every item.
491 559 if ( is_object( $item ) && $item->has_method( $intention ) ) {
@@ -508,9 +576,9 @@
508 576 $this->queue_to_sync_with_hub();
509 577 }
510 578
511 579 $result = array();
512 - if ( ! empty( $none_delete_items ) ) {
580 + if ( array() !== $none_delete_items ) {
513 581 $result['message'] = sprintf(
514 582 /* translators: %s: Vulnerability item(es) */
515 583 _n(
516 584 'Defender doesn\'t have enough permission to remove this file: %s',
@@ -530,9 +598,9 @@
530 598 // Refresh scan instance.
531 599 $scan = Model_Scan::get_last();
532 600 $result['scan'] = $scan->to_array();
533 601
534 - return new Response( empty( $none_delete_items ), $result );
602 + return new Response( array() === $none_delete_items, $result );
535 603 }
536 604
537 605 /**
538 606 * Save settings.
@@ -545,18 +613,15 @@
545 613 * @defender_route
546 614 */
547 615 public function save_settings( Request $request ): Response {
548 616 $data = $request->get_data_by_model( $this->model );
549 - // Case#1: enable all child options, if parent and all child options are disabled, so that there is no notice when saving.
550 - if (
551 - ! $data['integrity_check']
552 - && ! $data['check_core']
553 - && ! $data['check_plugins']
554 - ) {
555 - $data['check_core'] = true;
556 - $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'];
557 623 }
558 -
559 624 // Case#2: Suspicious code is activated BUT File change detection is deactivated then show the notice.
560 625 if ( $data['scan_malware'] && ! $data['integrity_check'] ) {
561 626 $response = array(
562 627 'type_notice' => 'info',
@@ -572,36 +637,17 @@
572 637 '</strong>'
573 638 ),
574 639 );
575 640 } else {
576 - // Prepare response message for usual successful case.
577 641 $response = array(
578 642 'message' => esc_html__( 'Your settings have been updated.', 'defender-security' ),
579 643 'auto_close' => true,
580 644 );
581 645 }
582 - // Additional cases are in the Scan model.
583 - $report_change = false;
584 - // If 'Scheduled Scanning' is checked then need to change Malware_Report.
585 - if ( true === $data['scheduled_scanning'] ) {
586 - $report = new Malware_Report();
587 - $report_change = true;
588 - $report->frequency = $data['frequency'];
589 - $report->day = $data['day'];
590 - $report->day_n = (int) $data['day_n'];
591 - $report->time = $data['time'];
592 - // Disable 'Scheduled Scanning'.
593 - } elseif ( true === $this->model->scheduled_scanning && false === $data['scheduled_scanning'] ) {
594 - $report = new Malware_Report();
595 - $report_change = true;
596 - $report->status = \WP_Defender\Model\Notification::STATUS_DISABLED;
597 - }
598 -
599 646 $before_import_schedule = $this->model->quarantine_expire_schedule;
600 647
601 648 $this->model->import( $data );
602 649 if ( $this->model->validate() ) {
603 -
604 650 if ( class_exists( 'WP_Defender\Component\Quarantine' ) ) {
605 651 $quarantine_component = wd_di()->get( Quarantine_Component::class );
606 652 $quarantine_component->reschedule_file_expiry_cron(
607 653 $before_import_schedule,
@@ -608,14 +654,9 @@
608 654 $data['quarantine_expire_schedule']
609 655 );
610 656 }
611 657
612 - // Todo: need to disable Malware_Notification & Malware_Report if all scan settings are deactivated?
613 658 $this->model->save();
614 - // Save Report's changes.
615 - if ( $report_change ) {
616 - $report->save();
617 - }
618 659 Config_Hub_Helper::set_clear_active_flag();
619 660
620 661 return new Response(
621 662 true,
@@ -649,9 +690,9 @@
649 690 'type' => 'string',
650 691 'sanitize' => 'sanitize_text_field',
651 692 ),
652 693 'type' => array(
653 - 'type' => 'string',
694 + 'type' => 'array',
654 695 'sanitize' => 'sanitize_text_field',
655 696 ),
656 697 'per_page' => array(
657 698 'type' => 'string',
@@ -670,70 +711,167 @@
670 711 if ( ! $v->validate() ) {
671 712 return new Response(
672 713 false,
673 714 array(
674 - 'message' => '',
715 + 'message' => esc_html__( 'Wrong scan issue data.', 'defender-security' ),
675 716 )
676 717 );
677 718 }
678 719
679 720 $scan = Model_Scan::get_last();
680 - $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'] );
681 722
682 - return new Response(
683 - true,
684 - array(
685 - 'issue' => $issues['issues_items'],
686 - 'ignored' => $issues['ignored_items'],
687 - 'paging' => $issues['paging'],
688 - 'count' => $issues['count'],
689 - )
723 + $response = array(
724 + 'issue' => $issues['issues_items'],
725 + 'ignored' => $issues['ignored_items'],
726 + 'paging' => $issues['paging'],
727 + 'count' => $issues['count'],
690 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 );
691 735 }
692 736
693 737 /**
694 - * Update the run background state for active scan.
738 + * Normalize issue type filters from the legacy and redesign payloads.
695 739 *
696 - * @param Request $request Request object.
740 + * @param array|string $types Requested issue type(s).
697 741 *
698 - * @return Response Response object.
699 - * @defender_route
742 + * @return array|string
700 743 */
701 - public function update_background( Request $request ): Response {
702 - $data = $request->get_data(
703 - array(
704 - 'run_background' => array(
705 - 'type' => 'boolean',
706 - 'sanitize' => 'rest_sanitize_boolean',
707 - ),
708 - )
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,
709 752 );
710 753
711 - $run_background = $data['run_background'] ?? false;
712 - $scan = Model_Scan::get_active();
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 ) );
713 765
714 - if ( is_object( $scan ) && $run_background ) {
715 - set_site_transient( 'defender_run_background', $scan->id, HOUR_IN_SECONDS * 2 );
766 + if ( 1 === count( $types ) ) {
767 + return $types[0];
768 + }
716 769
717 - return new Response(
718 - true,
719 - array(
720 - 'scan' => $scan->to_array(),
721 - )
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' => '',
722 785 );
723 786 }
724 787
725 - return new Response(
726 - false,
727 - array(
728 - 'message' => esc_html__( 'No active scan found', 'defender-security' ),
729 - )
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 + ),
730 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;
731 807 }
732 808
733 809 /**
734 - * Render main page.
810 + * Returns scan result data for the frontend on page load.
735 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 + *
736 874 * @return void
737 875 */
738 876 public function main_view(): void {
739 877 $this->render( 'main' );
@@ -746,11 +884,58 @@
746 884 public function enqueue_assets() {
747 885 if ( ! $this->is_page_active() ) {
748 886 return;
749 887 }
750 - wp_localize_script( 'def-scan', 'scan', $this->data_frontend() );
751 - wp_enqueue_script( 'def-scan' );
752 - 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 +
753 938 $this->enqueue_main_assets();
754 939 }
755 940
756 941 /**
@@ -789,9 +974,12 @@
789 974 /**
790 975 * Delete all the data & the cache.
791 976 */
792 977 public function remove_data(): void {
978 + delete_site_option( self::FIRST_SCAN_STARTED );
793 979 delete_site_option( Model_Scan::IGNORE_INDEXER );
980 + delete_site_option( Model_Scan::OPTION_SCAN_TYPE );
981 + Model_Scan::clear_all_status_messages();
794 982 delete_site_option( Core_Integrity::ISSUE_CHECKSUMS );
795 983 delete_site_transient( Plugin_Integrity::$org_slugs );
796 984 delete_site_transient( Plugin_Integrity::$org_responses );
797 985 }
@@ -810,60 +998,23 @@
810 998 $scan = null;
811 999 } else {
812 1000 $scan = is_object( $scan ) ? $scan->to_array( $per_page, $paged ) : $last->to_array( $per_page, $paged );
813 1001 }
814 - $settings = new Scan_Settings();
815 - $report = wd_di()->get( Malware_Report::class );
816 - $report_text = esc_html__( 'Automatic scans are disabled', 'defender-security' );
817 - if ( $settings->scheduled_scanning && isset( $settings->frequency ) ) {
818 - $report_text = sprintf(
819 - /* translators: 1. Line break tag. 2. Frequency value. */
820 - esc_html__( 'Automatic scans are %1$srunning %2$s', 'defender-security' ),
821 - '<br/>',
822 - $settings->frequency
823 - );
824 - }
1002 + $settings = new Scan_Settings();
1003 + $report = wd_di()->get( Malware_Report::class );
825 1004
826 - $misc['outdated_period'] = \WP_Defender\Behavior\Scan\Abandoned_Plugin::get_outdated_period();
827 - $misc['labels'] = $settings->labels();
828 - $misc['days_of_week'] = $this->get_days_of_week();
829 - $misc['times_of_day'] = $this->get_times();
830 - $misc['timezone_text'] = sprintf(
831 - /* translators: 1. Timezone. 2. Time. */
832 - esc_html__(
833 - 'Your timezone is set to %1$s, so your current time is %2$s.',
834 - 'defender-security'
835 - ),
836 - '<strong>' . wp_timezone_string() . '</strong>',
837 - '<strong>' . wp_date( 'H:i', time() ) . '</strong>'
838 - );
1005 + $scan['isEnabledScanType'] = $this->service->is_any_scan_type_active();
839 1006
840 - // Todo: add logic for deactivated scan settings. Maybe display some notice.
841 - $data = array(
842 - 'scan' => $scan,
843 - 'settings' => $settings->export(),
844 - 'report' => $report_text,
845 - 'active_tools' => array(
846 - 'integrity_check' => $settings->integrity_check,
847 - 'check_known_vuln' => $settings->check_known_vuln,
848 - 'scan_malware' => $settings->scan_malware,
849 - 'scheduled_scanning' => $settings->scheduled_scanning,
850 - 'check_abandoned_plugin' => $settings->check_abandoned_plugin,
1007 + $data = array(
1008 + 'scan' => $scan,
1009 + 'settings' => $settings->export(),
1010 + 'notification' => $report->to_string(),
1011 + 'misc' => array(
1012 + 'labels' => $settings->labels(),
851 1013 ),
852 - 'notification' => $report->to_string(),
853 - 'next_run' => $report->get_next_run_as_string(),
854 - 'misc' => $misc,
855 - 'upsell' => array(
856 - 'scan' => $this->get_scan_upsell( 'scan' ),
857 - ),
858 - 'hub_connector' => wd_di()->get( Hub_Connector::class )->data_frontend(),
859 - 'antibot' => wd_di()->get( Antibot_Global_Firewall::class )->data_frontend(),
860 1014 );
1015 + $data['quarantine'] = $this->quarantine_controller->data_frontend();
861 1016
862 - if ( class_exists( 'WP_Defender\Controller\Quarantine' ) ) {
863 - $data['quarantine'] = $this->quarantine_controller->data_frontend();
864 - }
865 -
866 1017 return array_merge( $data, $this->dump_routes_and_nonces() );
867 1018 }
868 1019
869 1020 /**
@@ -872,9 +1023,9 @@
872 1023 * @param array $data Data to be imported into the model.
873 1024 */
874 1025 public function import_data( array $data ) {
875 1026 $model = $this->model;
876 - if ( empty( $data ) ) {
1027 + if ( array() === $data ) {
877 1028 $model->scheduled_scanning = false;
878 1029 $model->frequency = 'weekly';
879 1030 $model->day_n = 1;
880 1031 $model->day = 'sunday';
@@ -888,30 +1039,8 @@
888 1039 }
889 1040 }
890 1041
891 1042 /**
892 - * Checks if any scan is active.
893 - *
894 - * @param bool $is_pro Indicates if the product is a pro version.
895 - *
896 - * @return bool True if any scan is active, false otherwise.
897 - */
898 - private function is_any_active( bool $is_pro ): bool {
899 - $settings = new Scan_Settings();
900 - $file_change_check = $settings->is_checked_any_file_change_types();
901 -
902 - if ( $is_pro ) {
903 - // Pro version. Check all parent types.
904 - return $file_change_check || $settings->check_known_vuln || $settings->scan_malware;
905 - } else {
906 - // Free version:
907 - // Check the 'File change detection' type because only it's available with nested types.
908 - // Check the Abandoned plugin type.
909 - return $file_change_check || $settings->check_abandoned_plugin;
910 - }
911 - }
912 -
913 - /**
914 1043 * Exports strings.
915 1044 *
916 1045 * @return array An array of strings.
917 1046 */
@@ -916,48 +1045,37 @@
916 1045 * @return array An array of strings.
917 1046 */
918 1047 public function export_strings(): array {
919 1048 $strings = array();
920 - if ( $this->is_any_active( $this->is_pro ) ) {
1049 + if ( $this->service->is_any_scan_type_active() ) {
921 1050 $strings[] = esc_html__( 'Active', 'defender-security' );
922 1051 } else {
923 1052 $strings[] = esc_html__( 'Inactive', 'defender-security' );
924 1053 }
925 1054
926 - $scan_report = new Malware_Report();
927 1055 $scan_notification = new Malware_Notification();
928 1056 if ( 'enabled' === $scan_notification->status ) {
929 1057 $strings[] = esc_html__( 'Email notifications active', 'defender-security' );
930 1058 }
931 - if ( $this->is_pro && 'enabled' === $scan_report->status ) {
932 1059 $strings[] = sprintf(
933 - /* translators: %s: Frequency value. */
934 - esc_html__( 'Email reports sending %s', 'defender-security' ),
935 - $scan_report->frequency
936 - );
937 - } elseif ( ! $this->is_pro ) {
938 - $strings[] = sprintf(
939 1060 /* translators: %s: Html for Pro-tag. */
940 - esc_html__( 'Email report inactive %s', 'defender-security' ),
1061 + esc_html__( 'Scheduled scan inactive %s', 'defender-security' ),
941 1062 '<span class="sui-tag sui-tag-pro">Pro</span>'
942 1063 );
943 - }
944 1064
945 1065 return $strings;
946 1066 }
947 1067
948 1068 /**
949 - * Generates configuration strings based on the provided configuration and
950 - * whether the product is a pro version.
1069 + * Generates configuration strings based on the provided configuration.
951 1070 *
952 1071 * @param array $config Configuration data.
953 - * @param bool $is_pro Indicates if the product is a pro version.
954 1072 *
955 1073 * @return array Returns an array of configuration strings.
956 1074 */
957 - public function config_strings( array $config, bool $is_pro ): array {
1075 + public function config_strings( array $config ): array {
958 1076 $strings = array();
959 - $strings[] = $this->service->is_any_scan_active( $config )
1077 + $strings[] = $this->service->check_scan_active_by( $config )
960 1078 ? esc_html__( 'Active', 'defender-security' )
961 1079 : esc_html__( 'Inactive', 'defender-security' );
962 1080
963 1081 if ( 'enabled' === $config['notification'] ) {
@@ -962,18 +1080,13 @@
962 1080
963 1081 if ( 'enabled' === $config['notification'] ) {
964 1082 $strings[] = esc_html__( 'Email notifications active', 'defender-security' );
965 1083 }
966 - if ( $is_pro && 'enabled' === $config['report'] ) {
1084 + // No Pro membership and No hosted site.
1085 + if ( ! $this->is_unlocked ) {
967 1086 $strings[] = sprintf(
968 - /* translators: %s: Frequency value. */
969 - esc_html__( 'Email reports sending %s', 'defender-security' ),
970 - $config['frequency']
971 - );
972 - } elseif ( ! $is_pro ) {
973 - $strings[] = sprintf(
974 1087 /* translators: %s: Html for Pro-tag. */
975 - esc_html__( 'Email report inactive %s', 'defender-security' ),
1088 + esc_html__( 'Scheduled scan inactive %s', 'defender-security' ),
976 1089 '<span class="sui-tag sui-tag-pro">Pro</span>'
977 1090 );
978 1091 }
979 1092
@@ -980,8 +1093,20 @@
980 1093 return $strings;
981 1094 }
982 1095
983 1096 /**
1097 + * Run different scan actions based on the scan location.
1098 + *
1099 + * @param string $type Denotes type of the scan from the following 4 possible values: scan, install, hub or report.
1100 + *
1101 + * @return void
1102 + */
1103 + public function run_scan_mechanisms_from( $type ) {
1104 + $this->service->gather_actioned_plugin_details();
1105 + $this->do_async_scan( $type );
1106 + }
1107 +
1108 + /**
984 1109 * Triggers the asynchronous scan.
985 1110 *
986 1111 * @param string $type Denotes type of the scan from the following 4 possible values: scan, install, hub or report.
987 1112 *
@@ -991,15 +1116,17 @@
991 1116 wd_di()->get( Model_Scan::class )->delete_idle();
992 1117 // Delete the slug from the previous scan.
993 1118 delete_site_option( Core_Integrity::ISSUE_CHECKSUMS );
994 1119
995 - as_enqueue_async_action(
996 - 'defender/async_scan',
997 - array(
998 - 'type' => $type,
999 - ),
1000 - 'defender'
1001 - );
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 + }
1002 1129 }
1003 1130
1004 1131 /**
1005 1132 * Clear completed action scheduler logs.
@@ -1044,14 +1171,18 @@
1044 1171 *
1045 1172 * @return void
1046 1173 */
1047 1174 public function scan_completed_analytics( $action_id ) {
1048 - 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 + ) {
1049 1180 $scan_analytics = wd_di()->get( Scan_Analytics::class );
1050 1181
1051 1182 $scan_model = wd_di()->get( Model_Scan::class );
1052 1183 $analytics_data = $scan_analytics->scan_completed( $scan_model );
1053 - if ( empty( $analytics_data ) ) {
1184 + if ( array() === $analytics_data ) {
1054 1185 return;
1055 1186 }
1056 1187
1057 1188 $this->track_feature(