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 +350 -283 5.5.1 → 6.3.0 View file →
@@ -7,15 +7,13 @@
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;
16 15 use WP_Defender\Controller\Quarantine;
17 -use WP_Defender\Component\Rate;
18 16 use WP_Defender\Traits\Formats;
19 17 use WP_Defender\Traits\Scan_Upsell;
20 18 use WP_Defender\Model\Scan_Item;
21 19 use WP_Defender\Behavior\WPMUDEV;
@@ -20,9 +18,11 @@
20 18 use WP_Defender\Model\Scan_Item;
21 19 use WP_Defender\Behavior\WPMUDEV;
22 20 use WP_Defender\Model\Scan as Model_Scan;
23 21 use WP_Defender\Behavior\Scan\Core_Integrity;
22 +use WP_Defender\Component\Network_Cron_Manager;
24 23 use WP_Defender\Component\Scan as Scan_Component;
24 +use WP_Defender\Component\Rate as Rate_Component;
25 25 use WP_Defender\Model\Setting\Scan as Scan_Settings;
26 26 use WP_Defender\Model\Notification\Malware_Report;
27 27 use WP_Defender\Component\Config\Config_Hub_Helper;
28 28 use WP_Defender\Helper\Analytics\Scan as Scan_Analytics;
@@ -27,8 +27,9 @@
27 27 use WP_Defender\Component\Config\Config_Hub_Helper;
28 28 use WP_Defender\Helper\Analytics\Scan as Scan_Analytics;
29 29 use WP_Defender\Model\Notification\Malware_Notification;
30 30 use WP_Defender\Component\Quarantine as Quarantine_Component;
31 +use WP_Defender\Behavior\Scan\Plugin_Integrity;
31 32
32 33 /**
33 34 * Contains methods for handling scans.
34 35 */
@@ -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 */
@@ -79,22 +92,17 @@
79 92 public function __construct() {
80 93 $this->register_page(
81 94 $this->get_title(),
82 95 $this->slug,
83 - array(
84 - $this,
85 - 'main_view',
86 - ),
96 + array( $this, 'main_view' ),
87 97 $this->parent_slug
88 98 );
89 99
90 - $this->model = new Scan_Settings();
91 - $this->service = wd_di()->get( Scan_Component::class );
92 - $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 );
93 103
94 - if ( class_exists( 'WP_Defender\Controller\Quarantine' ) ) {
95 - $this->quarantine_controller = wd_di()->get( Quarantine::class );
96 - }
104 + $this->is_unlocked = wd_di()->get( WPMUDEV::class )->is_pro();
97 105
98 106 $this->register_routes();
99 107 add_action( 'defender_enqueue_assets', array( $this, 'enqueue_assets' ) );
100 108 add_action( 'wp_ajax_defender_process_scan', array( $this, 'process' ) );
@@ -108,18 +116,24 @@
108 116 if (
109 117 is_admin() &&
110 118 'plugins.php' === $pagenow &&
111 119 apply_filters( 'wd_display_vulnerability_warnings', true ) &&
112 - $this->is_pro
120 + wd_di()->get( WPMUDEV::class )->is_apikey_available()
113 121 ) {
114 122 $this->service->display_vulnerability_warnings();
115 123 }
116 124
117 - // Schedule a time to clear completed action scheduler logs.
118 - if ( ! wp_next_scheduled( 'wpdef_clear_scan_logs' ) ) {
119 - wp_schedule_event( time(), 'weekly', 'wpdef_clear_scan_logs' );
120 - }
121 - 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 + );
122 136
123 137 add_filter( 'heartbeat_nopriv_send', array( $this, 'nopriv_heartbeat' ), 10, 2 );
124 138
125 139 add_action(
@@ -125,9 +139,8 @@
125 139 add_action(
126 140 'action_scheduler_completed_action',
127 141 array( $this, 'scan_completed_analytics' )
128 142 );
129 - add_action( 'admin_init', array( $this, 'mark_page_visited' ) );
130 143 }
131 144
132 145 /**
133 146 * Return the title of the page.
@@ -134,15 +147,9 @@
134 147 *
135 148 * @return string The title of the page.
136 149 */
137 150 public function get_title(): string {
138 - $default = esc_html__( 'Malware Scanning', 'defender-security' );
139 - // Check if the user has already visited the feature page.
140 - if ( wd_di()->get( Breadcrumbs::class )->get_meta_key() ) {
141 - return $default;
142 - }
143 -
144 - return $default . '<span class=wd-new-feature-dot></span>';
151 + return esc_html__( 'Issues', 'defender-security' );
145 152 }
146 153
147 154 /**
148 155 * Clean up data after core updating.
@@ -155,19 +162,35 @@
155 162
156 163 /**
157 164 * Start a scan.
158 165 *
166 + * @param Request $request Request object.
167 + *
159 168 * @return Response
160 169 * @defender_route
161 170 * @defender_redirect
162 171 */
163 - 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 +
164 185 $model = Model_Scan::create();
165 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 );
166 190 $this->log( 'Initial ping self', self::SCAN_LOG );
191 + $this->run_scan_mechanisms_from( 'scan' );
167 192
168 - $this->do_async_scan( 'scan' );
169 -
170 193 return new Response(
171 194 true,
172 195 array(
173 196 'status' => $model->status,
@@ -172,8 +195,9 @@
172 195 array(
173 196 'status' => $model->status,
174 197 'status_text' => $model->get_status_text(),
175 198 'percent' => 0,
199 + 'scan_type' => $scan_type,
176 200 )
177 201 );
178 202 }
179 203
@@ -192,16 +216,15 @@
192 216 * @defender_route
193 217 * @is_public
194 218 */
195 219 public function process() {
196 - if ( $this->service->has_lock() ) {
220 + $lock_filename = $this->service->get_lock_filename();
221 + if ( ! $this->service->try_create_lock( $lock_filename ) ) {
197 222 $this->log( 'Fallback as already a process is running', self::SCAN_LOG );
198 223
199 224 return;
200 225 }
201 226
202 - // This creates file lock, for make sure only 1 process run as a time.
203 - $this->service->create_lock();
204 227 // Check if the ping is from self or not.
205 228 $ret = $this->service->process();
206 229 $this->log( 'process done, queue for next', self::SCAN_LOG );
207 230 if ( false === $ret ) {
@@ -206,13 +229,13 @@
206 229 $this->log( 'process done, queue for next', self::SCAN_LOG );
207 230 if ( false === $ret ) {
208 231 // Ping self.
209 232 $this->log( 'Scan not done, pinging', self::SCAN_LOG );
210 - $this->service->remove_lock();
233 + $this->service->remove_lock( $lock_filename );
211 234 $this->process();
212 235 } else {
213 236 $this->queue_to_sync_with_hub();
214 - $this->service->remove_lock();
237 + $this->service->remove_lock( $lock_filename );
215 238 }
216 239 }
217 240
218 241 /**
@@ -222,14 +245,16 @@
222 245 * @defender_route
223 246 * @defender_redirect
224 247 */
225 248 public function status(): Response {
249 + $scan_type = Model_Scan::get_scan_type();
226 250 $idle_scan = wd_di()->get( Model_Scan::class )->get_idle();
227 251
228 252 if ( is_object( $idle_scan ) ) {
229 253 $this->service->update_idle_scan_status();
254 + $response = $this->get_status_response_data( $idle_scan, $scan_type );
230 255
231 - return new Response( false, $idle_scan->to_array() );
256 + return new Response( true, $response );
232 257 }
233 258
234 259 $checksum_issue = get_site_option( Core_Integrity::ISSUE_CHECKSUMS, 'false' );
235 260 $checksum_scan = Model_Scan::get_core_check();
@@ -234,21 +259,34 @@
234 259 $checksum_issue = get_site_option( Core_Integrity::ISSUE_CHECKSUMS, 'false' );
235 260 $checksum_scan = Model_Scan::get_core_check();
236 261 if ( 'false' !== $checksum_issue && is_object( $checksum_scan ) ) {
237 262 $this->service->update_idle_scan_status_by_checksum_issue( $checksum_scan );
263 + $response = $this->get_status_response_data( $checksum_scan, $scan_type );
238 264
239 - return new Response( false, $checksum_scan->to_array() );
265 + return new Response( true, $response );
240 266 }
241 267
242 268 $scan = Model_Scan::get_active();
243 269 if ( is_object( $scan ) ) {
270 + $response = $this->get_status_response_data( $scan, $scan_type );
244 271
245 - return new Response( false, $scan->to_array() );
272 + return new Response( true, $response );
246 273 }
274 +
247 275 $scan = Model_Scan::get_last();
248 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 + }
249 287
250 - return new Response( true, $scan->to_array() );
288 + return new Response( true, $response );
251 289 }
252 290
253 291 return new Response(
254 292 false,
@@ -258,8 +296,24 @@
258 296 );
259 297 }
260 298
261 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 + /**
262 316 * Cancel current scan.
263 317 *
264 318 * @return Response
265 319 * @defender_route
@@ -267,8 +321,9 @@
267 321 */
268 322 public function cancel(): Response {
269 323 $component = wd_di()->get( Scan_Component::class );
270 324 $component->cancel_a_scan();
325 + Model_Scan::clear_scan_type();
271 326 $last = Model_Scan::get_last();
272 327 if ( is_object( $last ) && ! is_wp_error( $last ) ) {
273 328 $last = $last->to_array();
274 329 }
@@ -379,9 +434,9 @@
379 434 $intention,
380 435 $allowed_intentions,
381 436 true
382 437 ) ) {
383 - wp_die();
438 + return new Response( false, array() );
384 439 }
385 440
386 441 $scan = Model_Scan::get_last();
387 442 if ( $scan instanceof Model_Scan ) {
@@ -386,14 +441,22 @@
386 441 $scan = Model_Scan::get_last();
387 442 if ( $scan instanceof Model_Scan ) {
388 443 $item = $scan->get_issue( $id );
389 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 + }
390 458
391 - if ( 'quarantine' === $intention ) {
392 - $result = $item->$intention( $data['parent_action'] );
393 - } else {
394 - $result = $item->$intention();
395 - }
396 459 // Maybe track.
397 460 if ( $this->is_tracking_active() ) {
398 461 $this->item_action_analytics( $item, $intention );
399 462 }
@@ -425,8 +488,12 @@
425 488
426 489 if ( $scan instanceof Model_Scan ) {
427 490 $result['scan'] = $scan->to_array();
428 491
492 + if ( 'quarantine' === $intention && isset( $this->quarantine_controller ) ) {
493 + $result['quarantine'] = $this->quarantine_controller->data_frontend()['list'] ?? array();
494 + }
495 +
429 496 $success = true;
430 497 if ( isset( $result['success'] ) && false === $result['success'] ) {
431 498 $success = false;
432 499 }
@@ -464,10 +531,10 @@
464 531 $items = $data['items'] ?? array();
465 532 $intention = $data['bulk'] ?? false;
466 533
467 534 if (
468 - empty( $items )
469 - || ! is_array( $items )
535 + ! is_array( $items )
536 + || array() === $items
470 537 || ! in_array( $intention, array( 'ignore', 'unignore', 'delete' ), true )
471 538 ) {
472 539 return new Response( false, array() );
473 540 }
@@ -482,13 +549,11 @@
482 549 $none_delete_items = array();
483 550 $sync_hub = false;
484 551 foreach ( $items as $id ) {
485 552 if ( 'ignore' === $intention ) {
486 - $scan->ignore_issue( (int) $id );
487 - $sync_hub = true;
553 + $sync_hub = $scan->ignore_issue( (int) $id );
488 554 } elseif ( 'unignore' === $intention ) {
489 - $scan->unignore_issue( (int) $id );
490 - $sync_hub = true;
555 + $sync_hub = $scan->unignore_issue( (int) $id );
491 556 } elseif ( 'delete' === $intention ) {
492 557 $item = $scan->get_issue( (int) $id );
493 558 // Work with every item.
494 559 if ( is_object( $item ) && $item->has_method( $intention ) ) {
@@ -511,9 +576,9 @@
511 576 $this->queue_to_sync_with_hub();
512 577 }
513 578
514 579 $result = array();
515 - if ( ! empty( $none_delete_items ) ) {
580 + if ( array() !== $none_delete_items ) {
516 581 $result['message'] = sprintf(
517 582 /* translators: %s: Vulnerability item(es) */
518 583 _n(
519 584 'Defender doesn\'t have enough permission to remove this file: %s',
@@ -533,9 +598,9 @@
533 598 // Refresh scan instance.
534 599 $scan = Model_Scan::get_last();
535 600 $result['scan'] = $scan->to_array();
536 601
537 - return new Response( empty( $none_delete_items ), $result );
602 + return new Response( array() === $none_delete_items, $result );
538 603 }
539 604
540 605 /**
541 606 * Save settings.
@@ -548,18 +613,15 @@
548 613 * @defender_route
549 614 */
550 615 public function save_settings( Request $request ): Response {
551 616 $data = $request->get_data_by_model( $this->model );
552 - // Case#1: enable all child options, if parent and all child options are disabled, so that there is no notice when saving.
553 - if (
554 - ! $data['integrity_check']
555 - && ! $data['check_core']
556 - && ! $data['check_plugins']
557 - ) {
558 - $data['check_core'] = true;
559 - $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'];
560 623 }
561 -
562 624 // Case#2: Suspicious code is activated BUT File change detection is deactivated then show the notice.
563 625 if ( $data['scan_malware'] && ! $data['integrity_check'] ) {
564 626 $response = array(
565 627 'type_notice' => 'info',
@@ -575,36 +637,17 @@
575 637 '</strong>'
576 638 ),
577 639 );
578 640 } else {
579 - // Prepare response message for usual successful case.
580 641 $response = array(
581 642 'message' => esc_html__( 'Your settings have been updated.', 'defender-security' ),
582 643 'auto_close' => true,
583 644 );
584 645 }
585 - // Additional cases are in the Scan model.
586 - $report_change = false;
587 - // If 'Scheduled Scanning' is checked then need to change Malware_Report.
588 - if ( true === $data['scheduled_scanning'] ) {
589 - $report = new Malware_Report();
590 - $report_change = true;
591 - $report->frequency = $data['frequency'];
592 - $report->day = $data['day'];
593 - $report->day_n = $data['day_n'];
594 - $report->time = $data['time'];
595 - // Disable 'Scheduled Scanning'.
596 - } elseif ( true === $this->model->scheduled_scanning && false === $data['scheduled_scanning'] ) {
597 - $report = new Malware_Report();
598 - $report_change = true;
599 - $report->status = \WP_Defender\Model\Notification::STATUS_DISABLED;
600 - }
601 -
602 646 $before_import_schedule = $this->model->quarantine_expire_schedule;
603 647
604 648 $this->model->import( $data );
605 649 if ( $this->model->validate() ) {
606 -
607 650 if ( class_exists( 'WP_Defender\Component\Quarantine' ) ) {
608 651 $quarantine_component = wd_di()->get( Quarantine_Component::class );
609 652 $quarantine_component->reschedule_file_expiry_cron(
610 653 $before_import_schedule,
@@ -611,14 +654,9 @@
611 654 $data['quarantine_expire_schedule']
612 655 );
613 656 }
614 657
615 - // Todo: need to disable Malware_Notification & Malware_Report if all scan settings are deactivated?
616 658 $this->model->save();
617 - // Save Report's changes.
618 - if ( $report_change ) {
619 - $report->save();
620 - }
621 659 Config_Hub_Helper::set_clear_active_flag();
622 660
623 661 return new Response(
624 662 true,
@@ -652,9 +690,9 @@
652 690 'type' => 'string',
653 691 'sanitize' => 'sanitize_text_field',
654 692 ),
655 693 'type' => array(
656 - 'type' => 'string',
694 + 'type' => 'array',
657 695 'sanitize' => 'sanitize_text_field',
658 696 ),
659 697 'per_page' => array(
660 698 'type' => 'string',
@@ -673,108 +711,166 @@
673 711 if ( ! $v->validate() ) {
674 712 return new Response(
675 713 false,
676 714 array(
677 - 'message' => '',
715 + 'message' => esc_html__( 'Wrong scan issue data.', 'defender-security' ),
678 716 )
679 717 );
680 718 }
681 719
682 720 $scan = Model_Scan::get_last();
683 - $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'] );
684 722
685 - return new Response(
686 - true,
687 - array(
688 - 'issue' => $issues['issues_items'],
689 - 'ignored' => $issues['ignored_items'],
690 - 'paging' => $issues['paging'],
691 - 'count' => $issues['count'],
692 - )
723 + $response = array(
724 + 'issue' => $issues['issues_items'],
725 + 'ignored' => $issues['ignored_items'],
726 + 'paging' => $issues['paging'],
727 + 'count' => $issues['count'],
693 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 );
694 735 }
695 736
696 737 /**
697 - * Handle notice.
698 - * Send the notice to the admin dashboard of the site.
738 + * Normalize issue type filters from the legacy and redesign payloads.
699 739 *
700 - * @return Response Response object.
701 - * @defender_route
740 + * @param array|string $types Requested issue type(s).
741 + *
742 + * @return array|string
702 743 */
703 - public function handle_notice(): Response {
704 - update_site_option( Rate::SLUG_FOR_BUTTON_RATE, true );
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 + );
705 753
706 - return new Response( true, array() );
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;
707 771 }
708 772
709 773 /**
710 - * Handle postponed notice.
711 - * Reset counters for postponed notice.
774 + * Get relative and exact display times for the last completed scan.
712 775 *
713 - * @return Response Response object.
714 - * @defender_route
776 + * @param Model_Scan|null $last Last completed scan.
777 + *
778 + * @return array
715 779 */
716 - public function postpone_notice(): Response {
717 - Rate::reset_counters();
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 + }
718 787
719 - return new Response( true, array() );
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;
720 807 }
721 808
722 809 /**
723 - * Handle refuse notice.
724 - * Send the refuse notice to the admin dashboard of the site.
810 + * Returns scan result data for the frontend on page load.
725 811 *
726 - * @return Response Response object.
727 - * @defender_route
812 + * @return array
728 813 */
729 - public function refuse_notice(): Response {
730 - update_site_option( Rate::SLUG_FOR_BUTTON_THANKS, true );
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;
731 819
732 - return new Response( true, array() );
733 - }
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 + }
734 839
735 - /**
736 - * Update the run background state for active scan.
737 - *
738 - * @param Request $request Request object.
739 - *
740 - * @return Response Response object.
741 - * @defender_route
742 - */
743 - public function update_background( Request $request ): Response {
744 - $data = $request->get_data(
745 - array(
746 - 'run_background' => array(
747 - 'type' => 'boolean',
748 - 'sanitize' => 'rest_sanitize_boolean',
749 - ),
750 - )
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(),
751 852 );
752 853
753 - $run_background = $data['run_background'] ?? false;
754 - $scan = Model_Scan::get_active();
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 ) );
755 857
756 - if ( is_object( $scan ) && $run_background ) {
757 - set_site_transient( 'defender_run_background', $scan->id, HOUR_IN_SECONDS * 2 );
758 -
759 - return new Response(
760 - true,
761 - array(
762 - 'scan' => $scan->to_array(),
763 - )
858 + if ( isset( $this->quarantine_controller ) ) {
859 + $data['quarantine'] = array(
860 + 'list' => $this->quarantine_controller->data_frontend()['list'] ?? array(),
764 861 );
765 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'] ) );
766 867
767 - return new Response(
768 - false,
769 - array(
770 - 'message' => esc_html__( 'No active scan found', 'defender-security' ),
771 - )
772 - );
868 + return array( 'scan' => $data );
773 869 }
774 870
775 871 /**
776 - * Render main page.
872 + * Render the main page.
777 873 *
778 874 * @return void
779 875 */
780 876 public function main_view(): void {
@@ -788,11 +884,58 @@
788 884 public function enqueue_assets() {
789 885 if ( ! $this->is_page_active() ) {
790 886 return;
791 887 }
792 - wp_localize_script( 'def-scan', 'scan', $this->data_frontend() );
793 - wp_enqueue_script( 'def-scan' );
794 - 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 +
795 938 $this->enqueue_main_assets();
796 939 }
797 940
798 941 /**
@@ -831,11 +974,15 @@
831 974 /**
832 975 * Delete all the data & the cache.
833 976 */
834 977 public function remove_data(): void {
978 + delete_site_option( self::FIRST_SCAN_STARTED );
835 979 delete_site_option( Model_Scan::IGNORE_INDEXER );
980 + delete_site_option( Model_Scan::OPTION_SCAN_TYPE );
981 + Model_Scan::clear_all_status_messages();
836 982 delete_site_option( Core_Integrity::ISSUE_CHECKSUMS );
837 - wd_di()->get( Breadcrumbs::class )->delete_meta_key();
983 + delete_site_transient( Plugin_Integrity::$org_slugs );
984 + delete_site_transient( Plugin_Integrity::$org_responses );
838 985 }
839 986
840 987 /**
841 988 * Provides data for the frontend.
@@ -851,71 +998,23 @@
851 998 $scan = null;
852 999 } else {
853 1000 $scan = is_object( $scan ) ? $scan->to_array( $per_page, $paged ) : $last->to_array( $per_page, $paged );
854 1001 }
855 - $settings = new Scan_Settings();
856 - $report = wd_di()->get( Malware_Report::class );
857 - $report_text = esc_html__( 'Automatic scans are disabled', 'defender-security' );
858 - if ( $settings->scheduled_scanning && isset( $settings->frequency ) ) {
859 - $report_text = sprintf(
860 - /* translators: 1. Line break tag. 2. Frequency value. */
861 - esc_html__( 'Automatic scans are %1$srunning %2$s', 'defender-security' ),
862 - '<br/>',
863 - $settings->frequency
864 - );
865 - }
866 - // Prepare additional data.
867 - if ( defender_is_wp_org_version() ) {
868 - $scan_array = Rate::what_scan_notice_display();
869 - $misc = array(
870 - 'rating_is_displayed' => ! Rate::was_rate_request() && ! empty( $scan_array['text'] ),
871 - 'rating_text' => $scan_array['text'],
872 - 'rating_type' => $scan_array['slug'],
873 - );
874 - } else {
875 - $misc = array(
876 - 'days_of_week' => $this->get_days_of_week(),
877 - 'times_of_day' => $this->get_times(),
878 - 'timezone_text' => sprintf(
879 - /* translators: %s - timezone, %s - time */
880 - esc_html__( 'Your timezone is set to %1$s, so your current time is %2$s.', 'defender-security' ),
881 - '<strong>' . wp_timezone_string() . '</strong>',
882 - '<strong>' . wp_date( 'H:i' ) . '</strong>'
883 - ),
884 - 'show_notice' => ! $settings->scheduled_scanning
885 - && 'scheduled_scanning' === defender_get_data_from_request( 'enable', 'g' ),
886 - 'rating_is_displayed' => false,
887 - 'rating_text' => '',
888 - 'rating_type' => '',
889 - );
890 - }
891 - $misc['outdated_period'] = \WP_Defender\Behavior\Scan\Abandoned_Plugin::get_outdated_period();
892 - $misc['labels'] = $settings->labels();
1002 + $settings = new Scan_Settings();
1003 + $report = wd_di()->get( Malware_Report::class );
893 1004
894 - // Todo: add logic for deactivated scan settings. Maybe display some notice.
895 - $data = array(
1005 + $scan['isEnabledScanType'] = $this->service->is_any_scan_type_active();
1006 +
1007 + $data = array(
896 1008 'scan' => $scan,
897 1009 'settings' => $settings->export(),
898 - 'report' => $report_text,
899 - 'active_tools' => array(
900 - 'integrity_check' => $settings->integrity_check,
901 - 'check_known_vuln' => $settings->check_known_vuln,
902 - 'scan_malware' => $settings->scan_malware,
903 - 'scheduled_scanning' => $settings->scheduled_scanning,
904 - 'check_abandoned_plugin' => $settings->check_abandoned_plugin,
905 - ),
906 1010 'notification' => $report->to_string(),
907 - 'next_run' => $report->get_next_run_as_string(),
908 - 'misc' => $misc,
909 - 'upsell' => array(
910 - 'scan' => $this->get_scan_upsell( 'scan' ),
1011 + 'misc' => array(
1012 + 'labels' => $settings->labels(),
911 1013 ),
912 1014 );
1015 + $data['quarantine'] = $this->quarantine_controller->data_frontend();
913 1016
914 - if ( class_exists( 'WP_Defender\Controller\Quarantine' ) ) {
915 - $data['quarantine'] = $this->quarantine_controller->data_frontend();
916 - }
917 -
918 1017 return array_merge( $data, $this->dump_routes_and_nonces() );
919 1018 }
920 1019
921 1020 /**
@@ -924,12 +1023,12 @@
924 1023 * @param array $data Data to be imported into the model.
925 1024 */
926 1025 public function import_data( array $data ) {
927 1026 $model = $this->model;
928 - if ( empty( $data ) ) {
1027 + if ( array() === $data ) {
929 1028 $model->scheduled_scanning = false;
930 1029 $model->frequency = 'weekly';
931 - $model->day_n = '1';
1030 + $model->day_n = 1;
932 1031 $model->day = 'sunday';
933 1032 $model->time = '4:00';
934 1033 $model->save();
935 1034 } else {
@@ -940,30 +1039,8 @@
940 1039 }
941 1040 }
942 1041
943 1042 /**
944 - * Checks if any scan is active.
945 - *
946 - * @param bool $is_pro Indicates if the product is a pro version.
947 - *
948 - * @return bool True if any scan is active, false otherwise.
949 - */
950 - private function is_any_active( bool $is_pro ): bool {
951 - $settings = new Scan_Settings();
952 - $file_change_check = $settings->is_checked_any_file_change_types();
953 -
954 - if ( $is_pro ) {
955 - // Pro version. Check all parent types.
956 - return $file_change_check || $settings->check_known_vuln || $settings->scan_malware;
957 - } else {
958 - // Free version:
959 - // Check the 'File change detection' type because only it's available with nested types.
960 - // Check the Abandoned plugin type.
961 - return $file_change_check || $settings->check_abandoned_plugin;
962 - }
963 - }
964 -
965 - /**
966 1043 * Exports strings.
967 1044 *
968 1045 * @return array An array of strings.
969 1046 */
@@ -968,48 +1045,37 @@
968 1045 * @return array An array of strings.
969 1046 */
970 1047 public function export_strings(): array {
971 1048 $strings = array();
972 - if ( $this->is_any_active( $this->is_pro ) ) {
1049 + if ( $this->service->is_any_scan_type_active() ) {
973 1050 $strings[] = esc_html__( 'Active', 'defender-security' );
974 1051 } else {
975 1052 $strings[] = esc_html__( 'Inactive', 'defender-security' );
976 1053 }
977 1054
978 - $scan_report = new Malware_Report();
979 1055 $scan_notification = new Malware_Notification();
980 1056 if ( 'enabled' === $scan_notification->status ) {
981 1057 $strings[] = esc_html__( 'Email notifications active', 'defender-security' );
982 1058 }
983 - if ( $this->is_pro && 'enabled' === $scan_report->status ) {
984 1059 $strings[] = sprintf(
985 - /* translators: %s: Frequency value. */
986 - esc_html__( 'Email reports sending %s', 'defender-security' ),
987 - $scan_report->frequency
988 - );
989 - } elseif ( ! $this->is_pro ) {
990 - $strings[] = sprintf(
991 1060 /* translators: %s: Html for Pro-tag. */
992 - esc_html__( 'Email report inactive %s', 'defender-security' ),
1061 + esc_html__( 'Scheduled scan inactive %s', 'defender-security' ),
993 1062 '<span class="sui-tag sui-tag-pro">Pro</span>'
994 1063 );
995 - }
996 1064
997 1065 return $strings;
998 1066 }
999 1067
1000 1068 /**
1001 - * Generates configuration strings based on the provided configuration and
1002 - * whether the product is a pro version.
1069 + * Generates configuration strings based on the provided configuration.
1003 1070 *
1004 1071 * @param array $config Configuration data.
1005 - * @param bool $is_pro Indicates if the product is a pro version.
1006 1072 *
1007 1073 * @return array Returns an array of configuration strings.
1008 1074 */
1009 - public function config_strings( array $config, bool $is_pro ): array {
1075 + public function config_strings( array $config ): array {
1010 1076 $strings = array();
1011 - $strings[] = $this->service->is_any_scan_active( $config, $is_pro )
1077 + $strings[] = $this->service->check_scan_active_by( $config )
1012 1078 ? esc_html__( 'Active', 'defender-security' )
1013 1079 : esc_html__( 'Inactive', 'defender-security' );
1014 1080
1015 1081 if ( 'enabled' === $config['notification'] ) {
@@ -1014,18 +1080,13 @@
1014 1080
1015 1081 if ( 'enabled' === $config['notification'] ) {
1016 1082 $strings[] = esc_html__( 'Email notifications active', 'defender-security' );
1017 1083 }
1018 - if ( $is_pro && 'enabled' === $config['report'] ) {
1084 + // No Pro membership and No hosted site.
1085 + if ( ! $this->is_unlocked ) {
1019 1086 $strings[] = sprintf(
1020 - /* translators: %s: Frequency value. */
1021 - esc_html__( 'Email reports sending %s', 'defender-security' ),
1022 - $config['frequency']
1023 - );
1024 - } elseif ( ! $is_pro ) {
1025 - $strings[] = sprintf(
1026 1087 /* translators: %s: Html for Pro-tag. */
1027 - esc_html__( 'Email report inactive %s', 'defender-security' ),
1088 + esc_html__( 'Scheduled scan inactive %s', 'defender-security' ),
1028 1089 '<span class="sui-tag sui-tag-pro">Pro</span>'
1029 1090 );
1030 1091 }
1031 1092
@@ -1032,8 +1093,20 @@
1032 1093 return $strings;
1033 1094 }
1034 1095
1035 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 + /**
1036 1109 * Triggers the asynchronous scan.
1037 1110 *
1038 1111 * @param string $type Denotes type of the scan from the following 4 possible values: scan, install, hub or report.
1039 1112 *
@@ -1043,15 +1116,17 @@
1043 1116 wd_di()->get( Model_Scan::class )->delete_idle();
1044 1117 // Delete the slug from the previous scan.
1045 1118 delete_site_option( Core_Integrity::ISSUE_CHECKSUMS );
1046 1119
1047 - as_enqueue_async_action(
1048 - 'defender/async_scan',
1049 - array(
1050 - 'type' => $type,
1051 - ),
1052 - 'defender'
1053 - );
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 + }
1054 1129 }
1055 1130
1056 1131 /**
1057 1132 * Clear completed action scheduler logs.
@@ -1096,14 +1171,18 @@
1096 1171 *
1097 1172 * @return void
1098 1173 */
1099 1174 public function scan_completed_analytics( $action_id ) {
1100 - 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 + ) {
1101 1180 $scan_analytics = wd_di()->get( Scan_Analytics::class );
1102 1181
1103 1182 $scan_model = wd_di()->get( Model_Scan::class );
1104 1183 $analytics_data = $scan_analytics->scan_completed( $scan_model );
1105 - if ( empty( $analytics_data ) ) {
1184 + if ( array() === $analytics_data ) {
1106 1185 return;
1107 1186 }
1108 1187
1109 1188 $this->track_feature(
@@ -1110,18 +1189,6 @@
1110 1189 $analytics_data['event'],
1111 1190 $analytics_data['data']
1112 1191 );
1113 1192 }
1114 - }
1115 -
1116 - /**
1117 - * Marks the feature page as visited.
1118 - *
1119 - * @return void
1120 - */
1121 - public function mark_page_visited(): void {
1122 - if ( 'wdf-scan' !== defender_get_current_page() ) {
1123 - return;
1124 - }
1125 - wd_di()->get( Breadcrumbs::class )->update_meta_key();
1126 1193 }
1127 1194 }