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 +355 -216 5.6.0 → 6.3.0 View file →
@@ -7,9 +7,8 @@
7 7
8 8 namespace WP_Defender\Controller;
9 9
10 10 use ActionScheduler;
11 -use WP_Defender\Component\Breadcrumbs;
12 11 use WP_Defender\Event;
13 12 use Valitron\Validator;
14 13 use Calotes\Component\Request;
15 14 use Calotes\Component\Response;
@@ -19,9 +18,11 @@
19 18 use WP_Defender\Model\Scan_Item;
20 19 use WP_Defender\Behavior\WPMUDEV;
21 20 use WP_Defender\Model\Scan as Model_Scan;
22 21 use WP_Defender\Behavior\Scan\Core_Integrity;
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,18 +116,24 @@
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
114 - // Schedule a time to clear completed action scheduler logs.
115 - if ( ! wp_next_scheduled( 'wpdef_clear_scan_logs' ) ) {
116 - wp_schedule_event( time(), 'weekly', 'wpdef_clear_scan_logs' );
117 - }
118 - add_action( 'wpdef_clear_scan_logs', array( $this, 'clear_scan_logs' ) );
125 + /**
126 + * Schedule a time to clear completed action scheduler logs.
127 + *
128 + * @var Network_Cron_Manager $network_cron_manager
129 + */
130 + $network_cron_manager = wd_di()->get( Network_Cron_Manager::class );
131 + $network_cron_manager->register_callback(
132 + 'wpdef_clear_scan_logs',
133 + array( $this, 'clear_scan_logs' ),
134 + WEEK_IN_SECONDS
135 + );
119 136
120 137 add_filter( 'heartbeat_nopriv_send', array( $this, 'nopriv_heartbeat' ), 10, 2 );
121 138
122 139 add_action(
@@ -130,9 +147,9 @@
130 147 *
131 148 * @return string The title of the page.
132 149 */
133 150 public function get_title(): string {
134 - return esc_html__( 'Malware Scanning', 'defender-security' );
151 + return esc_html__( 'Issues', 'defender-security' );
135 152 }
136 153
137 154 /**
138 155 * Clean up data after core updating.
@@ -145,20 +162,35 @@
145 162
146 163 /**
147 164 * Start a scan.
148 165 *
166 + * @param Request $request Request object.
167 + *
149 168 * @return Response
150 169 * @defender_route
151 170 * @defender_redirect
152 171 */
153 - 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 +
154 185 $model = Model_Scan::create();
155 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 );
156 190 $this->log( 'Initial ping self', self::SCAN_LOG );
157 - $this->service->gather_actioned_plugin_details();
191 + $this->run_scan_mechanisms_from( 'scan' );
158 192
159 - $this->do_async_scan( 'scan' );
160 -
161 193 return new Response(
162 194 true,
163 195 array(
164 196 'status' => $model->status,
@@ -163,8 +195,9 @@
163 195 array(
164 196 'status' => $model->status,
165 197 'status_text' => $model->get_status_text(),
166 198 'percent' => 0,
199 + 'scan_type' => $scan_type,
167 200 )
168 201 );
169 202 }
170 203
@@ -183,16 +216,15 @@
183 216 * @defender_route
184 217 * @is_public
185 218 */
186 219 public function process() {
187 - if ( $this->service->has_lock() ) {
220 + $lock_filename = $this->service->get_lock_filename();
221 + if ( ! $this->service->try_create_lock( $lock_filename ) ) {
188 222 $this->log( 'Fallback as already a process is running', self::SCAN_LOG );
189 223
190 224 return;
191 225 }
192 226
193 - // This creates file lock, for make sure only 1 process run as a time.
194 - $this->service->create_lock();
195 227 // Check if the ping is from self or not.
196 228 $ret = $this->service->process();
197 229 $this->log( 'process done, queue for next', self::SCAN_LOG );
198 230 if ( false === $ret ) {
@@ -197,13 +229,13 @@
197 229 $this->log( 'process done, queue for next', self::SCAN_LOG );
198 230 if ( false === $ret ) {
199 231 // Ping self.
200 232 $this->log( 'Scan not done, pinging', self::SCAN_LOG );
201 - $this->service->remove_lock();
233 + $this->service->remove_lock( $lock_filename );
202 234 $this->process();
203 235 } else {
204 236 $this->queue_to_sync_with_hub();
205 - $this->service->remove_lock();
237 + $this->service->remove_lock( $lock_filename );
206 238 }
207 239 }
208 240
209 241 /**
@@ -213,14 +245,16 @@
213 245 * @defender_route
214 246 * @defender_redirect
215 247 */
216 248 public function status(): Response {
249 + $scan_type = Model_Scan::get_scan_type();
217 250 $idle_scan = wd_di()->get( Model_Scan::class )->get_idle();
218 251
219 252 if ( is_object( $idle_scan ) ) {
220 253 $this->service->update_idle_scan_status();
254 + $response = $this->get_status_response_data( $idle_scan, $scan_type );
221 255
222 - return new Response( false, $idle_scan->to_array() );
256 + return new Response( true, $response );
223 257 }
224 258
225 259 $checksum_issue = get_site_option( Core_Integrity::ISSUE_CHECKSUMS, 'false' );
226 260 $checksum_scan = Model_Scan::get_core_check();
@@ -225,21 +259,34 @@
225 259 $checksum_issue = get_site_option( Core_Integrity::ISSUE_CHECKSUMS, 'false' );
226 260 $checksum_scan = Model_Scan::get_core_check();
227 261 if ( 'false' !== $checksum_issue && is_object( $checksum_scan ) ) {
228 262 $this->service->update_idle_scan_status_by_checksum_issue( $checksum_scan );
263 + $response = $this->get_status_response_data( $checksum_scan, $scan_type );
229 264
230 - return new Response( false, $checksum_scan->to_array() );
265 + return new Response( true, $response );
231 266 }
232 267
233 268 $scan = Model_Scan::get_active();
234 269 if ( is_object( $scan ) ) {
270 + $response = $this->get_status_response_data( $scan, $scan_type );
235 271
236 - return new Response( false, $scan->to_array() );
272 + return new Response( true, $response );
237 273 }
274 +
238 275 $scan = Model_Scan::get_last();
239 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 + }
240 287
241 - return new Response( true, $scan->to_array() );
288 + return new Response( true, $response );
242 289 }
243 290
244 291 return new Response(
245 292 false,
@@ -249,8 +296,24 @@
249 296 );
250 297 }
251 298
252 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 + /**
253 316 * Cancel current scan.
254 317 *
255 318 * @return Response
256 319 * @defender_route
@@ -258,8 +321,9 @@
258 321 */
259 322 public function cancel(): Response {
260 323 $component = wd_di()->get( Scan_Component::class );
261 324 $component->cancel_a_scan();
325 + Model_Scan::clear_scan_type();
262 326 $last = Model_Scan::get_last();
263 327 if ( is_object( $last ) && ! is_wp_error( $last ) ) {
264 328 $last = $last->to_array();
265 329 }
@@ -370,9 +434,9 @@
370 434 $intention,
371 435 $allowed_intentions,
372 436 true
373 437 ) ) {
374 - wp_die();
438 + return new Response( false, array() );
375 439 }
376 440
377 441 $scan = Model_Scan::get_last();
378 442 if ( $scan instanceof Model_Scan ) {
@@ -377,14 +441,22 @@
377 441 $scan = Model_Scan::get_last();
378 442 if ( $scan instanceof Model_Scan ) {
379 443 $item = $scan->get_issue( $id );
380 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 + }
381 458
382 - if ( 'quarantine' === $intention ) {
383 - $result = $item->$intention( $data['parent_action'] );
384 - } else {
385 - $result = $item->$intention();
386 - }
387 459 // Maybe track.
388 460 if ( $this->is_tracking_active() ) {
389 461 $this->item_action_analytics( $item, $intention );
390 462 }
@@ -416,8 +488,12 @@
416 488
417 489 if ( $scan instanceof Model_Scan ) {
418 490 $result['scan'] = $scan->to_array();
419 491
492 + if ( 'quarantine' === $intention && isset( $this->quarantine_controller ) ) {
493 + $result['quarantine'] = $this->quarantine_controller->data_frontend()['list'] ?? array();
494 + }
495 +
420 496 $success = true;
421 497 if ( isset( $result['success'] ) && false === $result['success'] ) {
422 498 $success = false;
423 499 }
@@ -455,10 +531,10 @@
455 531 $items = $data['items'] ?? array();
456 532 $intention = $data['bulk'] ?? false;
457 533
458 534 if (
459 - empty( $items )
460 - || ! is_array( $items )
535 + ! is_array( $items )
536 + || array() === $items
461 537 || ! in_array( $intention, array( 'ignore', 'unignore', 'delete' ), true )
462 538 ) {
463 539 return new Response( false, array() );
464 540 }
@@ -473,13 +549,11 @@
473 549 $none_delete_items = array();
474 550 $sync_hub = false;
475 551 foreach ( $items as $id ) {
476 552 if ( 'ignore' === $intention ) {
477 - $scan->ignore_issue( (int) $id );
478 - $sync_hub = true;
553 + $sync_hub = $scan->ignore_issue( (int) $id );
479 554 } elseif ( 'unignore' === $intention ) {
480 - $scan->unignore_issue( (int) $id );
481 - $sync_hub = true;
555 + $sync_hub = $scan->unignore_issue( (int) $id );
482 556 } elseif ( 'delete' === $intention ) {
483 557 $item = $scan->get_issue( (int) $id );
484 558 // Work with every item.
485 559 if ( is_object( $item ) && $item->has_method( $intention ) ) {
@@ -502,9 +576,9 @@
502 576 $this->queue_to_sync_with_hub();
503 577 }
504 578
505 579 $result = array();
506 - if ( ! empty( $none_delete_items ) ) {
580 + if ( array() !== $none_delete_items ) {
507 581 $result['message'] = sprintf(
508 582 /* translators: %s: Vulnerability item(es) */
509 583 _n(
510 584 'Defender doesn\'t have enough permission to remove this file: %s',
@@ -524,9 +598,9 @@
524 598 // Refresh scan instance.
525 599 $scan = Model_Scan::get_last();
526 600 $result['scan'] = $scan->to_array();
527 601
528 - return new Response( empty( $none_delete_items ), $result );
602 + return new Response( array() === $none_delete_items, $result );
529 603 }
530 604
531 605 /**
532 606 * Save settings.
@@ -539,18 +613,15 @@
539 613 * @defender_route
540 614 */
541 615 public function save_settings( Request $request ): Response {
542 616 $data = $request->get_data_by_model( $this->model );
543 - // Case#1: enable all child options, if parent and all child options are disabled, so that there is no notice when saving.
544 - if (
545 - ! $data['integrity_check']
546 - && ! $data['check_core']
547 - && ! $data['check_plugins']
548 - ) {
549 - $data['check_core'] = true;
550 - $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'];
551 623 }
552 -
553 624 // Case#2: Suspicious code is activated BUT File change detection is deactivated then show the notice.
554 625 if ( $data['scan_malware'] && ! $data['integrity_check'] ) {
555 626 $response = array(
556 627 'type_notice' => 'info',
@@ -566,36 +637,17 @@
566 637 '</strong>'
567 638 ),
568 639 );
569 640 } else {
570 - // Prepare response message for usual successful case.
571 641 $response = array(
572 642 'message' => esc_html__( 'Your settings have been updated.', 'defender-security' ),
573 643 'auto_close' => true,
574 644 );
575 645 }
576 - // Additional cases are in the Scan model.
577 - $report_change = false;
578 - // If 'Scheduled Scanning' is checked then need to change Malware_Report.
579 - if ( true === $data['scheduled_scanning'] ) {
580 - $report = new Malware_Report();
581 - $report_change = true;
582 - $report->frequency = $data['frequency'];
583 - $report->day = $data['day'];
584 - $report->day_n = (int) $data['day_n'];
585 - $report->time = $data['time'];
586 - // Disable 'Scheduled Scanning'.
587 - } elseif ( true === $this->model->scheduled_scanning && false === $data['scheduled_scanning'] ) {
588 - $report = new Malware_Report();
589 - $report_change = true;
590 - $report->status = \WP_Defender\Model\Notification::STATUS_DISABLED;
591 - }
592 -
593 646 $before_import_schedule = $this->model->quarantine_expire_schedule;
594 647
595 648 $this->model->import( $data );
596 649 if ( $this->model->validate() ) {
597 -
598 650 if ( class_exists( 'WP_Defender\Component\Quarantine' ) ) {
599 651 $quarantine_component = wd_di()->get( Quarantine_Component::class );
600 652 $quarantine_component->reschedule_file_expiry_cron(
601 653 $before_import_schedule,
@@ -602,14 +654,9 @@
602 654 $data['quarantine_expire_schedule']
603 655 );
604 656 }
605 657
606 - // Todo: need to disable Malware_Notification & Malware_Report if all scan settings are deactivated?
607 658 $this->model->save();
608 - // Save Report's changes.
609 - if ( $report_change ) {
610 - $report->save();
611 - }
612 659 Config_Hub_Helper::set_clear_active_flag();
613 660
614 661 return new Response(
615 662 true,
@@ -643,9 +690,9 @@
643 690 'type' => 'string',
644 691 'sanitize' => 'sanitize_text_field',
645 692 ),
646 693 'type' => array(
647 - 'type' => 'string',
694 + 'type' => 'array',
648 695 'sanitize' => 'sanitize_text_field',
649 696 ),
650 697 'per_page' => array(
651 698 'type' => 'string',
@@ -664,70 +711,167 @@
664 711 if ( ! $v->validate() ) {
665 712 return new Response(
666 713 false,
667 714 array(
668 - 'message' => '',
715 + 'message' => esc_html__( 'Wrong scan issue data.', 'defender-security' ),
669 716 )
670 717 );
671 718 }
672 719
673 720 $scan = Model_Scan::get_last();
674 - $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'] );
675 722
676 - return new Response(
677 - true,
678 - array(
679 - 'issue' => $issues['issues_items'],
680 - 'ignored' => $issues['ignored_items'],
681 - 'paging' => $issues['paging'],
682 - 'count' => $issues['count'],
683 - )
723 + $response = array(
724 + 'issue' => $issues['issues_items'],
725 + 'ignored' => $issues['ignored_items'],
726 + 'paging' => $issues['paging'],
727 + 'count' => $issues['count'],
684 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 );
685 735 }
686 736
687 737 /**
688 - * Update the run background state for active scan.
738 + * Normalize issue type filters from the legacy and redesign payloads.
689 739 *
690 - * @param Request $request Request object.
740 + * @param array|string $types Requested issue type(s).
691 741 *
692 - * @return Response Response object.
693 - * @defender_route
742 + * @return array|string
694 743 */
695 - public function update_background( Request $request ): Response {
696 - $data = $request->get_data(
697 - array(
698 - 'run_background' => array(
699 - 'type' => 'boolean',
700 - 'sanitize' => 'rest_sanitize_boolean',
701 - ),
702 - )
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,
703 752 );
704 753
705 - $run_background = $data['run_background'] ?? false;
706 - $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 ) );
707 765
708 - if ( is_object( $scan ) && $run_background ) {
709 - set_site_transient( 'defender_run_background', $scan->id, HOUR_IN_SECONDS * 2 );
766 + if ( 1 === count( $types ) ) {
767 + return $types[0];
768 + }
710 769
711 - return new Response(
712 - true,
713 - array(
714 - 'scan' => $scan->to_array(),
715 - )
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' => '',
716 785 );
717 786 }
718 787
719 - return new Response(
720 - false,
721 - array(
722 - 'message' => esc_html__( 'No active scan found', 'defender-security' ),
723 - )
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 + ),
724 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;
725 807 }
726 808
727 809 /**
728 - * Render main page.
810 + * Returns scan result data for the frontend on page load.
729 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 + *
730 874 * @return void
731 875 */
732 876 public function main_view(): void {
733 877 $this->render( 'main' );
@@ -740,11 +884,58 @@
740 884 public function enqueue_assets() {
741 885 if ( ! $this->is_page_active() ) {
742 886 return;
743 887 }
744 - wp_localize_script( 'def-scan', 'scan', $this->data_frontend() );
745 - wp_enqueue_script( 'def-scan' );
746 - 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 +
747 938 $this->enqueue_main_assets();
748 939 }
749 940
750 941 /**
@@ -783,9 +974,12 @@
783 974 /**
784 975 * Delete all the data & the cache.
785 976 */
786 977 public function remove_data(): void {
978 + delete_site_option( self::FIRST_SCAN_STARTED );
787 979 delete_site_option( Model_Scan::IGNORE_INDEXER );
980 + delete_site_option( Model_Scan::OPTION_SCAN_TYPE );
981 + Model_Scan::clear_all_status_messages();
788 982 delete_site_option( Core_Integrity::ISSUE_CHECKSUMS );
789 983 delete_site_transient( Plugin_Integrity::$org_slugs );
790 984 delete_site_transient( Plugin_Integrity::$org_responses );
791 985 }
@@ -804,58 +998,23 @@
804 998 $scan = null;
805 999 } else {
806 1000 $scan = is_object( $scan ) ? $scan->to_array( $per_page, $paged ) : $last->to_array( $per_page, $paged );
807 1001 }
808 - $settings = new Scan_Settings();
809 - $report = wd_di()->get( Malware_Report::class );
810 - $report_text = esc_html__( 'Automatic scans are disabled', 'defender-security' );
811 - if ( $settings->scheduled_scanning && isset( $settings->frequency ) ) {
812 - $report_text = sprintf(
813 - /* translators: 1. Line break tag. 2. Frequency value. */
814 - esc_html__( 'Automatic scans are %1$srunning %2$s', 'defender-security' ),
815 - '<br/>',
816 - $settings->frequency
817 - );
818 - }
1002 + $settings = new Scan_Settings();
1003 + $report = wd_di()->get( Malware_Report::class );
819 1004
820 - $misc['outdated_period'] = \WP_Defender\Behavior\Scan\Abandoned_Plugin::get_outdated_period();
821 - $misc['labels'] = $settings->labels();
822 - $misc['days_of_week'] = $this->get_days_of_week();
823 - $misc['times_of_day'] = $this->get_times();
824 - $misc['timezone_text'] = sprintf(
825 - /* translators: 1. Timezone. 2. Time. */
826 - esc_html__(
827 - 'Your timezone is set to %1$s, so your current time is %2$s.',
828 - 'defender-security'
829 - ),
830 - '<strong>' . wp_timezone_string() . '</strong>',
831 - '<strong>' . wp_date( 'H:i', time() ) . '</strong>'
832 - );
1005 + $scan['isEnabledScanType'] = $this->service->is_any_scan_type_active();
833 1006
834 - // Todo: add logic for deactivated scan settings. Maybe display some notice.
835 - $data = array(
1007 + $data = array(
836 1008 'scan' => $scan,
837 1009 'settings' => $settings->export(),
838 - 'report' => $report_text,
839 - 'active_tools' => array(
840 - 'integrity_check' => $settings->integrity_check,
841 - 'check_known_vuln' => $settings->check_known_vuln,
842 - 'scan_malware' => $settings->scan_malware,
843 - 'scheduled_scanning' => $settings->scheduled_scanning,
844 - 'check_abandoned_plugin' => $settings->check_abandoned_plugin,
845 - ),
846 1010 'notification' => $report->to_string(),
847 - 'next_run' => $report->get_next_run_as_string(),
848 - 'misc' => $misc,
849 - 'upsell' => array(
850 - 'scan' => $this->get_scan_upsell( 'scan' ),
1011 + 'misc' => array(
1012 + 'labels' => $settings->labels(),
851 1013 ),
852 1014 );
1015 + $data['quarantine'] = $this->quarantine_controller->data_frontend();
853 1016
854 - if ( class_exists( 'WP_Defender\Controller\Quarantine' ) ) {
855 - $data['quarantine'] = $this->quarantine_controller->data_frontend();
856 - }
857 -
858 1017 return array_merge( $data, $this->dump_routes_and_nonces() );
859 1018 }
860 1019
861 1020 /**
@@ -864,9 +1023,9 @@
864 1023 * @param array $data Data to be imported into the model.
865 1024 */
866 1025 public function import_data( array $data ) {
867 1026 $model = $this->model;
868 - if ( empty( $data ) ) {
1027 + if ( array() === $data ) {
869 1028 $model->scheduled_scanning = false;
870 1029 $model->frequency = 'weekly';
871 1030 $model->day_n = 1;
872 1031 $model->day = 'sunday';
@@ -880,30 +1039,8 @@
880 1039 }
881 1040 }
882 1041
883 1042 /**
884 - * Checks if any scan is active.
885 - *
886 - * @param bool $is_pro Indicates if the product is a pro version.
887 - *
888 - * @return bool True if any scan is active, false otherwise.
889 - */
890 - private function is_any_active( bool $is_pro ): bool {
891 - $settings = new Scan_Settings();
892 - $file_change_check = $settings->is_checked_any_file_change_types();
893 -
894 - if ( $is_pro ) {
895 - // Pro version. Check all parent types.
896 - return $file_change_check || $settings->check_known_vuln || $settings->scan_malware;
897 - } else {
898 - // Free version:
899 - // Check the 'File change detection' type because only it's available with nested types.
900 - // Check the Abandoned plugin type.
901 - return $file_change_check || $settings->check_abandoned_plugin;
902 - }
903 - }
904 -
905 - /**
906 1043 * Exports strings.
907 1044 *
908 1045 * @return array An array of strings.
909 1046 */
@@ -908,48 +1045,37 @@
908 1045 * @return array An array of strings.
909 1046 */
910 1047 public function export_strings(): array {
911 1048 $strings = array();
912 - if ( $this->is_any_active( $this->is_pro ) ) {
1049 + if ( $this->service->is_any_scan_type_active() ) {
913 1050 $strings[] = esc_html__( 'Active', 'defender-security' );
914 1051 } else {
915 1052 $strings[] = esc_html__( 'Inactive', 'defender-security' );
916 1053 }
917 1054
918 - $scan_report = new Malware_Report();
919 1055 $scan_notification = new Malware_Notification();
920 1056 if ( 'enabled' === $scan_notification->status ) {
921 1057 $strings[] = esc_html__( 'Email notifications active', 'defender-security' );
922 1058 }
923 - if ( $this->is_pro && 'enabled' === $scan_report->status ) {
924 1059 $strings[] = sprintf(
925 - /* translators: %s: Frequency value. */
926 - esc_html__( 'Email reports sending %s', 'defender-security' ),
927 - $scan_report->frequency
928 - );
929 - } elseif ( ! $this->is_pro ) {
930 - $strings[] = sprintf(
931 1060 /* translators: %s: Html for Pro-tag. */
932 - esc_html__( 'Email report inactive %s', 'defender-security' ),
1061 + esc_html__( 'Scheduled scan inactive %s', 'defender-security' ),
933 1062 '<span class="sui-tag sui-tag-pro">Pro</span>'
934 1063 );
935 - }
936 1064
937 1065 return $strings;
938 1066 }
939 1067
940 1068 /**
941 - * Generates configuration strings based on the provided configuration and
942 - * whether the product is a pro version.
1069 + * Generates configuration strings based on the provided configuration.
943 1070 *
944 1071 * @param array $config Configuration data.
945 - * @param bool $is_pro Indicates if the product is a pro version.
946 1072 *
947 1073 * @return array Returns an array of configuration strings.
948 1074 */
949 - public function config_strings( array $config, bool $is_pro ): array {
1075 + public function config_strings( array $config ): array {
950 1076 $strings = array();
951 - $strings[] = $this->service->is_any_scan_active( $config )
1077 + $strings[] = $this->service->check_scan_active_by( $config )
952 1078 ? esc_html__( 'Active', 'defender-security' )
953 1079 : esc_html__( 'Inactive', 'defender-security' );
954 1080
955 1081 if ( 'enabled' === $config['notification'] ) {
@@ -954,18 +1080,13 @@
954 1080
955 1081 if ( 'enabled' === $config['notification'] ) {
956 1082 $strings[] = esc_html__( 'Email notifications active', 'defender-security' );
957 1083 }
958 - if ( $is_pro && 'enabled' === $config['report'] ) {
1084 + // No Pro membership and No hosted site.
1085 + if ( ! $this->is_unlocked ) {
959 1086 $strings[] = sprintf(
960 - /* translators: %s: Frequency value. */
961 - esc_html__( 'Email reports sending %s', 'defender-security' ),
962 - $config['frequency']
963 - );
964 - } elseif ( ! $is_pro ) {
965 - $strings[] = sprintf(
966 1087 /* translators: %s: Html for Pro-tag. */
967 - esc_html__( 'Email report inactive %s', 'defender-security' ),
1088 + esc_html__( 'Scheduled scan inactive %s', 'defender-security' ),
968 1089 '<span class="sui-tag sui-tag-pro">Pro</span>'
969 1090 );
970 1091 }
971 1092
@@ -972,8 +1093,20 @@
972 1093 return $strings;
973 1094 }
974 1095
975 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 + /**
976 1109 * Triggers the asynchronous scan.
977 1110 *
978 1111 * @param string $type Denotes type of the scan from the following 4 possible values: scan, install, hub or report.
979 1112 *
@@ -983,15 +1116,17 @@
983 1116 wd_di()->get( Model_Scan::class )->delete_idle();
984 1117 // Delete the slug from the previous scan.
985 1118 delete_site_option( Core_Integrity::ISSUE_CHECKSUMS );
986 1119
987 - as_enqueue_async_action(
988 - 'defender/async_scan',
989 - array(
990 - 'type' => $type,
991 - ),
992 - 'defender'
993 - );
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 + }
994 1129 }
995 1130
996 1131 /**
997 1132 * Clear completed action scheduler logs.
@@ -1036,14 +1171,18 @@
1036 1171 *
1037 1172 * @return void
1038 1173 */
1039 1174 public function scan_completed_analytics( $action_id ) {
1040 - 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 + ) {
1041 1180 $scan_analytics = wd_di()->get( Scan_Analytics::class );
1042 1181
1043 1182 $scan_model = wd_di()->get( Model_Scan::class );
1044 1183 $analytics_data = $scan_analytics->scan_completed( $scan_model );
1045 - if ( empty( $analytics_data ) ) {
1184 + if ( array() === $analytics_data ) {
1046 1185 return;
1047 1186 }
1048 1187
1049 1188 $this->track_feature(