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/traits/defender-bootstrap.php +304 -208 5.8.0 → 6.3.0 View file →
@@ -16,14 +16,14 @@
16 16 use WP_Defender\Controller\WAF;
17 17 use WP_Defender\Controller\Scan;
18 18 use WP_Defender\Component\Crypt;
19 19 use WP_Defender\Behavior\WPMUDEV;
20 -use WP_Defender\Controller\Onboard;
21 20 use WP_Defender\Controller\Webauthn;
22 21 use WP_Defender\Controller\Dashboard;
23 22 use WP_Defender\Controller\Captcha;
23 +use WP_Defender\Controller\Recipients;
24 +use WP_Defender\Controller\Quarantine;
24 25 use WP_Defender\Controller\Mask_Login;
25 -use WP_Defender\Controller\Quarantine;
26 26 use WP_Defender\Controller\Two_Factor;
27 27 use WP_Defender\Component\Hub_Connector;
28 28 use WP_Defender\Controller\Main_Setting;
29 29 use WP_Defender\Controller\Notification;
@@ -28,14 +28,13 @@
28 28 use WP_Defender\Controller\Main_Setting;
29 29 use WP_Defender\Controller\Notification;
30 30 use WP_Defender\Controller\Audit_Logging;
31 31 use WP_Defender\Controller\Data_Tracking;
32 -use WP_Defender\Controller\Advanced_Tools;
32 +use WP_Defender\Controller\Login_Access;
33 33 use WP_Defender\Controller\Password_Reset;
34 +use WP_Defender\Controller\Setup_Wizard;
34 35 use WP_Defender\Controller\Strong_Password;
35 -use WP_Defender\Controller\Expert_Services;
36 36 use WP_Defender\Controller\Security_Tweaks;
37 -use WP_Defender\Controller\Security_Headers;
38 37 use WP_Defender\Controller\Blocklist_Monitor;
39 38 use WP_Defender\Controller\Session_Protection;
40 39 use WP_Defender\Controller\Password_Protection;
41 40 use WP_Defender\Component\Network_Cron_Manager;
@@ -42,14 +41,14 @@
42 41 use WP_Defender\Component\Logger\Rotation_Logger;
43 42 use WP_Defender\Component\Firewall as Firewall_Component;
44 43 use WP_Defender\Controller\Firewall as Firewall_Controller;
45 44 use WP_Defender\Controller\Hub_Connector as Hub_Connector_Controller;
46 -use WP_Defender\Model\Onboard as Onboard_Model;
47 45 use WP_Defender\Controller\Rate as Rate_Controller;
48 46 use WP_Defender\Component\Rate as Rate_Component;
49 47 use WP_Defender\Upgrader;
50 48
51 49 trait Defender_Bootstrap {
50 +
52 51 /**
53 52 * Table name for quarantine.
54 53 *
55 54 * @var string
@@ -63,100 +62,15 @@
63 62 */
64 63 private $scan_item_table = 'defender_scan_item';
65 64
66 65 /**
67 - * Check is all quarantine dependent table is having storage engine InnoDB.
68 - *
69 - * @return bool True if all dependent table is InnoDB else false.
70 - */
71 - private function is_quarantine_dependent_tables_innodb(): bool {
72 - global $wpdb;
73 -
74 - $tables = array( $wpdb->users, $wpdb->base_prefix . $this->scan_item_table );
75 - $total_table = count( $tables );
76 -
77 - return $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
78 - $wpdb->prepare(
79 - "SELECT COUNT(`ENGINE`) = %d FROM information_schema.TABLES WHERE TABLE_SCHEMA = %s AND `ENGINE` = %s AND TABLE_NAME IN ( '{$wpdb->users}', '{$wpdb->base_prefix}defender_scan_item' );",
80 - $total_table,
81 - $wpdb->dbname,
82 - 'innodb',
83 - )
84 - ) === '1';
85 - }
86 -
87 - /**
88 - * Creates the quarantine table if it doesn't exist.
89 - *
90 - * @return void
91 - */
92 - public function create_table_quarantine() {
93 - global $wpdb;
94 -
95 - // Define table names and charset.
96 - $quarantine_table = $wpdb->base_prefix . $this->quarantine_table;
97 - $scan_item_table = $wpdb->base_prefix . $this->scan_item_table;
98 - $charset_collate = $wpdb->get_charset_collate();
99 - $unique_id = uniqid( $wpdb->prefix );
100 -
101 - $common_columns = <<<'SQL'
102 - `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT,
103 - `defender_scan_item_id` int UNSIGNED DEFAULT NULL,
104 - `file_hash` char(53) NOT NULL,
105 - `file_full_path` text NOT NULL,
106 - `file_original_name` tinytext NOT NULL,
107 - `file_extension` varchar(16) DEFAULT NULL,
108 - `file_mime_type` varchar(64) DEFAULT NULL,
109 - `file_rw_permission` smallint UNSIGNED DEFAULT NULL,
110 - `file_owner` varchar(255) DEFAULT NULL,
111 - `file_group` varchar(255) DEFAULT NULL,
112 - `file_version` varchar(32) DEFAULT NULL,
113 - `file_category` tinyint UNSIGNED DEFAULT 0,
114 - `file_modified_time` datetime NOT NULL,
115 - `source_slug` varchar(255) NOT NULL,
116 - `created_time` datetime NOT NULL,
117 - `created_by` bigint UNSIGNED DEFAULT NULL,
118 - PRIMARY KEY (`id`)
119 - SQL;
120 -
121 - // Define key names.
122 - $scan_item_key = "{$unique_id}_defender_scan_item_id";
123 - $created_by_key = "{$unique_id}_created_by";
124 -
125 - // Build the SQL statement based on the storage engine.
126 - if ( $this->is_quarantine_dependent_tables_innodb() ) {
127 - $sql = <<<SQL
128 - CREATE TABLE IF NOT EXISTS `{$quarantine_table}` (
129 - $common_columns,
130 - CONSTRAINT `{$scan_item_key}`
131 - FOREIGN KEY (`defender_scan_item_id`) REFERENCES {$scan_item_table}(`id`)
132 - ON UPDATE CASCADE ON DELETE SET NULL,
133 - CONSTRAINT `{$created_by_key}`
134 - FOREIGN KEY (`created_by`) REFERENCES {$wpdb->users}(`ID`)
135 - ON UPDATE CASCADE ON DELETE SET NULL
136 - ) {$charset_collate};
137 - SQL;
138 - } else {
139 - $sql = <<<SQL
140 - CREATE TABLE IF NOT EXISTS `{$quarantine_table}` (
141 - $common_columns,
142 - KEY `{$scan_item_key}` (`defender_scan_item_id`),
143 - KEY `{$created_by_key}` (`created_by`)
144 - ) {$charset_collate};
145 - SQL;
146 - }
147 -
148 - // Execute the SQL query.
149 - $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
150 - }
151 -
152 - /**
153 66 * Activation.
154 67 */
155 68 private function activation_hook_common(): void {
156 69 Upgrader::date_activated();
157 - $this->create_database_tables();
70 + $this->create_database_tables_common();
158 71 $this->on_activation();
72 + $this->set_activation_redirect_flag();
159 73 // Create a file with a random key if it doesn't exist.
160 74 ( new Crypt() )->create_key_file();
161 75 // If this is a plugin reactivating, then track it. No need the check by 'wd_nofresh_install' key because the option is disabled by default.
162 76 $settings = wd_di()->get( Main_Setting::class );
@@ -170,14 +84,12 @@
170 84 wp_schedule_single_event( time() + 5, 'wpdef_smart_ip_detection_ping' );
171 85 }
172 86
173 87 /**
174 - * Deactivation.
88 + * Deactivation - clears shared cron hooks present in both free and pro versions.
175 89 */
176 - public function deactivation_hook(): void {
90 + protected function deactivation_hook_common(): void {
177 91 wp_clear_scheduled_hook( 'firewall_clean_up_logs' );
178 - wp_clear_scheduled_hook( 'audit_sync_events' );
179 - wp_clear_scheduled_hook( 'audit_clean_up_logs' );
180 92 wp_clear_scheduled_hook( 'wdf_maybe_send_report' );
181 93 wp_clear_scheduled_hook( 'wp_defender_clear_logs' );
182 94 wp_clear_scheduled_hook( 'wpdef_sec_key_gen' );
183 95 wp_clear_scheduled_hook( 'wpdef_clear_scan_logs' );
@@ -183,9 +95,8 @@
183 95 wp_clear_scheduled_hook( 'wpdef_clear_scan_logs' );
184 96 wp_clear_scheduled_hook( 'wpdef_log_rotational_delete' );
185 97 wp_clear_scheduled_hook( 'wpdef_update_geoip' );
186 98 wp_clear_scheduled_hook( 'wpdef_fetch_global_ip_list' );
187 - wp_clear_scheduled_hook( 'wpdef_quarantine_delete_expired' );
188 99 wp_clear_scheduled_hook( 'wpdef_firewall_clean_up_lockout' );
189 100 wp_clear_scheduled_hook( 'wpdef_firewall_send_compact_logs_to_api' );
190 101 wp_clear_scheduled_hook( 'wpdef_firewall_fetch_trusted_proxy_preset_ips' );
191 102 wp_clear_scheduled_hook( 'wpdef_firewall_clean_up_unlockout' );
@@ -196,9 +107,8 @@
196 107 wp_clear_scheduled_hook( 'wpdef_rotate_malicious_bot_secret_hash' );
197 108
198 109 // Remove old legacy cron jobs if they exist.
199 110 wp_clear_scheduled_hook( 'lockoutReportCron' );
200 - wp_clear_scheduled_hook( 'auditReportCron' );
201 111 wp_clear_scheduled_hook( 'cleanUpOldLog' );
202 112 wp_clear_scheduled_hook( 'scanReportCron' );
203 113 wp_clear_scheduled_hook( 'tweaksSendNotification' );
204 114 }
@@ -251,34 +161,18 @@
251 161 PRIMARY KEY (`id`),
252 162 UNIQUE KEY ip (ip)
253 163 ) {$charset_collate};
254 164 SQL;
255 - $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
165 + $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
256 166 }
257 167
258 168 /**
259 - * Creates Defender's tables.
260 - *
261 - * @since 2.7.1 No use dbDelta because PHP v8.1 triggers an error when calling query "DESCRIBE {$table};" if the
262 - * table doesn't exist.
169 + * Creates the audit log table used for caching audit events.
263 170 */
264 - protected function create_database_tables(): void {
171 + protected function create_table_audit_log(): void {
265 172 global $wpdb;
266 173
267 174 $charset_collate = $wpdb->get_charset_collate();
268 - // Hide errors.
269 - $wpdb->hide_errors();
270 - // Email log table.
271 - $sql = "CREATE TABLE IF NOT EXISTS {$wpdb->base_prefix}defender_email_log (
272 - `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
273 - `timestamp` int NOT NULL,
274 - `source` varchar(255) NOT NULL,
275 - `to` varchar(255) NOT NULL,
276 - PRIMARY KEY (`id`),
277 - KEY `source` (`source`)
278 -) $charset_collate;";
279 - $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
280 -
281 175 // Audit log table. Though our data mainly store on API side, we will need a table for caching.
282 176 $sql = "CREATE TABLE IF NOT EXISTS {$wpdb->base_prefix}defender_audit_log (
283 177 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
284 178 `timestamp` int NOT NULL,
@@ -299,9 +193,33 @@
299 193 KEY `context` (`context`),
300 194 KEY `ip` (`ip`)
301 195 ) $charset_collate;";
302 196 $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
197 + }
303 198
199 + /**
200 + * Creates Defender's tables.
201 + *
202 + * @since 2.7.1 No use dbDelta because PHP v8.1 triggers an error when calling query "DESCRIBE {$table};" if the
203 + * table doesn't exist.
204 + */
205 + protected function create_database_tables_common(): void {
206 + global $wpdb;
207 +
208 + $charset_collate = $wpdb->get_charset_collate();
209 + // Hide errors.
210 + $wpdb->hide_errors();
211 + // Email log table.
212 + $sql = "CREATE TABLE IF NOT EXISTS {$wpdb->base_prefix}defender_email_log (
213 + `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
214 + `timestamp` int NOT NULL,
215 + `source` varchar(255) NOT NULL,
216 + `to` varchar(255) NOT NULL,
217 + PRIMARY KEY (`id`),
218 + KEY `source` (`source`)
219 +) $charset_collate;";
220 + $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
221 +
304 222 // Scan item table.
305 223 $sql = "CREATE TABLE IF NOT EXISTS {$wpdb->base_prefix}defender_scan_item (
306 224 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
307 225 `parent_id` int NOT NULL,
@@ -366,36 +284,106 @@
366 284 KEY `attempt_404` (`attempt_404`)
367 285 ) $charset_collate;";
368 286 $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
369 287
370 - $this->create_table_quarantine();
371 -
372 - // Create Unlock table.
288 + // Create unlockout table.
373 289 $this->create_table_unlockout();
374 290 // Create Blocklist table.
375 291 $this->create_table_blocklist();
292 + // Create Quarantine table.
293 + $this->create_table_quarantine();
294 + // Create Audit Log table.
295 + $this->create_table_audit_log();
376 296 }
377 297
378 298 /**
379 - * Check if this is onboarding.
299 + * Check if all quarantine-dependent tables use the InnoDB storage engine.
380 300 *
381 - * @return bool
301 + * @return bool True if all dependent tables are InnoDB, false otherwise.
382 302 */
383 - private function is_onboarding(): bool {
384 - /**
385 - * Display Onboarding if:
386 - * it's a fresh install and there were no requests from the Hub before,
387 - * after Reset Settings.
388 - *
389 - * @var HUB
390 - */
391 - $hub_class = wd_di()->get( HUB::class );
392 - $hub_class->set_onboarding_status( Onboard_Model::maybe_show_onboarding() );
303 + private function is_quarantine_dependent_tables_innodb(): bool {
304 + global $wpdb;
393 305
394 - return $hub_class->get_onboarding_status() && ! defender_is_wp_cli();
306 + $tables = array( $wpdb->users, $wpdb->base_prefix . $this->scan_item_table );
307 + $total_table = count( $tables );
308 +
309 + return $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
310 + $wpdb->prepare(
311 + "SELECT COUNT(`ENGINE`) = %d FROM information_schema.TABLES WHERE TABLE_SCHEMA = %s AND `ENGINE` = %s AND TABLE_NAME IN ( '{$wpdb->users}', '{$wpdb->base_prefix}defender_scan_item' );",
312 + $total_table,
313 + $wpdb->dbname,
314 + 'innodb',
315 + )
316 + ) === '1';
395 317 }
396 318
397 319 /**
320 + * Creates the quarantine table if it doesn't exist.
321 + * Uses InnoDB foreign key constraints when the dependent tables support it,
322 + * otherwise falls back to plain KEY indexes.
323 + *
324 + * @return void
325 + */
326 + public function create_table_quarantine(): void {
327 + global $wpdb;
328 +
329 + $quarantine_table = $wpdb->base_prefix . $this->quarantine_table;
330 + $scan_item_table = $wpdb->base_prefix . $this->scan_item_table;
331 + $charset_collate = $wpdb->get_charset_collate();
332 + $unique_id = uniqid( $wpdb->prefix );
333 +
334 + $common_columns = <<<'SQL'
335 + `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT,
336 + `defender_scan_item_id` int UNSIGNED DEFAULT NULL,
337 + `file_hash` char(53) NOT NULL,
338 + `file_full_path` text NOT NULL,
339 + `file_original_name` tinytext NOT NULL,
340 + `file_extension` varchar(16) DEFAULT NULL,
341 + `file_mime_type` varchar(64) DEFAULT NULL,
342 + `file_rw_permission` smallint UNSIGNED DEFAULT NULL,
343 + `file_owner` varchar(255) DEFAULT NULL,
344 + `file_group` varchar(255) DEFAULT NULL,
345 + `file_version` varchar(32) DEFAULT NULL,
346 + `file_category` tinyint UNSIGNED DEFAULT 0,
347 + `file_modified_time` datetime NOT NULL,
348 + `source_slug` varchar(255) NOT NULL,
349 + `created_time` datetime NOT NULL,
350 + `created_by` bigint UNSIGNED DEFAULT NULL,
351 + PRIMARY KEY (`id`)
352 + SQL;
353 +
354 + // Define key names.
355 + $scan_item_key = "{$unique_id}_defender_scan_item_id";
356 + $created_by_key = "{$unique_id}_created_by";
357 +
358 + // Build the SQL statement based on the storage engine.
359 + if ( $this->is_quarantine_dependent_tables_innodb() ) {
360 + $sql = <<<SQL
361 + CREATE TABLE IF NOT EXISTS `{$quarantine_table}` (
362 + $common_columns,
363 + CONSTRAINT `{$scan_item_key}`
364 + FOREIGN KEY (`defender_scan_item_id`) REFERENCES {$scan_item_table}(`id`)
365 + ON UPDATE CASCADE ON DELETE SET NULL,
366 + CONSTRAINT `{$created_by_key}`
367 + FOREIGN KEY (`created_by`) REFERENCES {$wpdb->users}(`ID`)
368 + ON UPDATE CASCADE ON DELETE SET NULL
369 + ) {$charset_collate};
370 + SQL;
371 + } else {
372 + $sql = <<<SQL
373 + CREATE TABLE IF NOT EXISTS `{$quarantine_table}` (
374 + $common_columns,
375 + KEY `{$scan_item_key}` (`defender_scan_item_id`),
376 + KEY `{$created_by_key}` (`created_by`)
377 + ) {$charset_collate};
378 + SQL;
379 + }
380 +
381 + // Execute the SQL query.
382 + $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
383 + }
384 +
385 + /**
398 386 * Initialize the common modules of the application.
399 387 *
400 388 * @return void
401 389 */
@@ -402,39 +390,34 @@
402 390 private function init_modules_common(): void {
403 391 // Init main ORM.
404 392 Array_Cache::set( 'orm', new Mapper() );
405 393
406 - if ( $this->is_onboarding() ) {
407 - // If it's cli we should start this normally.
408 - Array_Cache::set( 'onboard', wd_di()->get( Onboard::class ) );
409 - } else {
410 - // Initialize the main controllers of every module.
411 - wd_di()->get( Dashboard::class );
412 - }
394 + // New setup wizard is rendered directly inside Dashboard React UI.
395 + wd_di()->get( Setup_Wizard::class );
396 + wd_di()->get( \WP_Defender\Controller\Activity_Log::class );
397 + wd_di()->get( Dashboard::class );
398 + wd_di()->get( Scan::class );
413 399 wd_di()->get( Security_Tweaks::class );
414 - wd_di()->get( Scan::class );
415 400 wd_di()->get( Audit_Logging::class );
416 401 wd_di()->get( Firewall_Controller::class );
402 + wd_di()->get( Login_Access::class );
403 + // Initialize Mask Login globally so runtime protections hook into wp-login/wp-admin requests.
404 + wd_di()->get( Mask_Login::class );
417 405 wd_di()->get( WAF::class );
418 406 wd_di()->get( Two_Factor::class );
419 - wd_di()->get( Advanced_Tools::class );
420 407 wd_di()->get( Mask_Login::class );
421 - wd_di()->get( Security_Headers::class );
422 408 wd_di()->get( Captcha::class );
423 409 wd_di()->get( Notification::class );
410 + wd_di()->get( Recipients::class );
424 411 wd_di()->get( Main_Setting::class );
425 412 wd_di()->get( Blocklist_Monitor::class );
426 413 wd_di()->get( Password_Protection::class );
427 414 wd_di()->get( Password_Reset::class );
428 415 wd_di()->get( Webauthn::class );
429 - wd_di()->get( Expert_Services::class );
430 416 wd_di()->get( Hub_Connector_Controller::class );
431 417 wd_di()->get( Strong_Password::class );
432 418 wd_di()->get( Session_Protection::class );
433 -
434 - if ( class_exists( 'WP_Defender\Controller\Quarantine' ) ) {
435 - wd_di()->get( Quarantine::class );
436 - }
419 + wd_di()->get( Quarantine::class );
437 420 wd_di()->get( Data_Tracking::class );
438 421 if ( defender_is_wp_org_version() ) {
439 422 wd_di()->get( Rate_Controller::class );
440 423 }
@@ -466,13 +449,36 @@
466 449 * @return void
467 450 */
468 451 private function register_styles(): void {
469 452 wp_enqueue_style( 'defender-menu', WP_DEFENDER_BASE_URL . 'assets/css/defender-icon.css', array(), DEFENDER_VERSION );
453 + wp_add_inline_style(
454 + 'defender-menu',
455 + '#toplevel_page_wp-defender .wd-issue-indicator-sidebar {
456 + position: relative;
457 + background-color: transparent !important;
458 + width: 8px;
459 + min-width: unset;
460 + padding: 0;
461 + margin-left: 4px;
462 + }
463 + #toplevel_page_wp-defender .wd-issue-indicator-sidebar::after {
464 + content: "";
465 + position: absolute;
466 + width: 8px;
467 + height: 8px;
468 + top: 50%;
469 + left: 50%;
470 + transform: translate(-50%, -50%);
471 + border-radius: 50%;
472 + background: #d63638;
473 + }'
474 + );
470 475
471 476 $css_files = array(
472 - 'defender' => WP_DEFENDER_BASE_URL . 'assets/css/styles.css',
473 - 'def-sui' => WP_DEFENDER_BASE_URL . 'assets/css/shared-ui.css',
474 - 'def-admin' => WP_DEFENDER_BASE_URL . 'assets/css/admin.css',
477 + 'defender' => WP_DEFENDER_BASE_URL . 'assets/css/styles.css',
478 + 'def-sui' => WP_DEFENDER_BASE_URL . 'assets/css/shared-ui.css',
479 + 'def-admin' => WP_DEFENDER_BASE_URL . 'assets/css/admin.css',
480 + 'def-core-ui-style' => WP_DEFENDER_BASE_URL . 'assets/css/core-ui.css',
475 481 );
476 482
477 483 foreach ( $css_files as $slug => $file ) {
478 484 wp_register_style( $slug, $file, array(), DEFENDER_VERSION );
@@ -485,9 +491,9 @@
485 491 * @return void
486 492 */
487 493 private function register_scripts(): void {
488 494 $base_url = WP_DEFENDER_BASE_URL;
489 - $dependencies = array( 'def-vue', 'def-manifest', 'defender', 'wp-i18n' );
495 + $dependencies = array( 'def-vue', 'def-manifest', 'def-core-ui', 'defender', 'wp-i18n' );
490 496 $js_files = array(
491 497 'wpmudev-sui' => array(
492 498 $base_url . 'assets/js/shared-ui.js',
493 499 ),
@@ -507,56 +513,17 @@
507 513 ),
508 514 'def-manifest' => array(
509 515 $base_url . 'assets/js/manifest.js',
510 516 ),
511 - 'def-dashboard' => array(
512 - $base_url . 'assets/app/dashboard.js',
513 - $dependencies,
517 + 'def-core-ui' => array(
518 + $base_url . 'assets/js/core-ui.js',
519 + array( 'def-vue', 'def-manifest' ),
514 520 ),
515 - 'def-securitytweaks' => array(
516 - $base_url . 'assets/app/security-tweak.js',
517 - array_merge( $dependencies, array( 'clipboard', 'wpmudev-sui' ) ),
518 - ),
519 - 'def-scan' => array(
520 - $base_url . 'assets/app/scan.js',
521 - array_merge( $dependencies, array( 'clipboard', 'wpmudev-sui' ) ),
522 - ),
523 - 'def-audit' => array(
524 - $base_url . 'assets/app/audit.js',
521 + // React files.
522 + 'def-setup-wizard' => array(
523 + $base_url . 'assets/js/setup-wizard.js',
525 524 $dependencies,
526 525 ),
527 - 'def-iplockout' => array(
528 - $base_url . 'assets/app/ip-lockout.js',
529 - array_merge( $dependencies, array( 'wpmudev-sui' ) ),
530 - ),
531 - 'def-advancedtools' => array(
532 - $base_url . 'assets/app/advanced-tools.js',
533 - $dependencies,
534 - ),
535 - 'def-settings' => array(
536 - $base_url . 'assets/app/settings.js',
537 - $dependencies,
538 - ),
539 - 'def-2fa' => array(
540 - $base_url . 'assets/app/two-fa.js',
541 - $dependencies,
542 - ),
543 - 'def-notification' => array(
544 - $base_url . 'assets/app/notification.js',
545 - $dependencies,
546 - ),
547 - 'def-waf' => array(
548 - $base_url . 'assets/app/waf.js',
549 - $dependencies,
550 - ),
551 - 'def-onboard' => array(
552 - $base_url . 'assets/app/onboard.js',
553 - $dependencies,
554 - ),
555 - 'def-expert-services' => array(
556 - $base_url . '/assets/app/expert-services.js',
557 - $dependencies,
558 - ),
559 526 );
560 527
561 528 foreach ( $js_files as $slug => $file ) {
562 529 if ( isset( $file[1] ) ) {
@@ -581,14 +548,9 @@
581 548 private function localize_script(): void {
582 549 $wpmu_dev = new WPMUDEV();
583 550 global $wp_defender_central;
584 551
585 - $misc = array();
586 - $data_tracking = wd_di()->get( Data_Tracking::class );
587 - $is_tracking = $data_tracking->show_tracking_modal();
588 - if ( $is_tracking ) {
589 - $misc = $data_tracking->get_tracking_modal();
590 - }
552 + $misc = array();
591 553 $misc['high_contrast'] = defender_high_contrast();
592 554 $is_wp_org = defender_is_wp_org_version();
593 555 if ( $is_wp_org ) {
594 556 $misc['rating'] = array();
@@ -626,9 +588,8 @@
626 588 'wpmudev_url' => WP_DEFENDER_DOCS_LINK,
627 589 'wpmudev_support_ticket_text' => defender_support_ticket_text(),
628 590 'wpmudev_api_base_url' => $wpmu_dev->get_api_base_url(),
629 591 'upgrade_title' => esc_html__( 'UPGRADE TO PRO', 'defender-security' ),
630 - 'tracking_modal' => $is_tracking ? 'show' : 'hide',
631 592 'hosted' => $wpmu_dev->is_wpmu_hosting(),
632 593 'file_upload_nonce' => wp_create_nonce( 'defender_file_upload' ),
633 594 'wpmudev_hub_link' => 'https://wpmudev.com/hub2/',
634 595 )
@@ -661,8 +622,10 @@
661 622 /**
662 623 * Trigger mandatory actions on activation.
663 624 */
664 625 private function on_activation(): void {
626 + update_site_option( Data_Tracking::TRACKING_SLUG, true );
627 +
665 628 add_action(
666 629 'admin_init',
667 630 function () {
668 631 $security_tweaks = wd_di()->get( Security_Tweaks::class );
@@ -682,8 +645,87 @@
682 645 return defender_cron_schedules( $schedules );
683 646 }
684 647
685 648 /**
649 + * Serves script translations from the single MO file our in-site translation
650 + * tools (e.g. WPMU DEV Hub's translation editor) produce, since those only
651 + * generate one MO/PO file per plugin rather than the per-script JSON files
652 + * `wp_set_script_translations()` normally expects.
653 + *
654 + * @param string|false $translations Translations as JSON, false if there are none.
655 + * @param string|false $file Path to the translation file to load, false if there isn't one.
656 + * @param string $handle Name of the script to register a translation domain to.
657 + * @param string $domain The text domain.
658 + *
659 + * @return string|false
660 + */
661 + public function provide_script_translations( $translations, $file, $handle, $domain ) {
662 + if ( 'defender-security' !== $domain ) {
663 + return $translations;
664 + }
665 +
666 + static $served = false;
667 + static $cached_json = null;
668 +
669 + if ( $served ) {
670 + return wp_json_encode(
671 + array(
672 + 'locale_data' => array(
673 + 'defender-security' => array(
674 + '' => array(
675 + 'domain' => 'defender-security',
676 + ),
677 + ),
678 + ),
679 + )
680 + );
681 + }
682 +
683 + if ( null !== $cached_json ) {
684 + return $cached_json;
685 + }
686 +
687 + $locale = is_admin() ? get_user_locale() : get_locale();
688 + $mo_file = WP_LANG_DIR . "/plugins/{$domain}-{$locale}.mo";
689 +
690 + if ( ! file_exists( $mo_file ) ) {
691 + return $translations;
692 + }
693 +
694 + $mo = new \MO();
695 + if ( ! $mo->import_from_file( $mo_file ) ) {
696 + return $translations;
697 + }
698 +
699 + $locale_data = array(
700 + '' => array(
701 + 'domain' => $domain,
702 + 'lang' => $locale,
703 + ),
704 + );
705 +
706 + if ( isset( $mo->headers['Plural-Forms'] ) && '' !== $mo->headers['Plural-Forms'] ) {
707 + $locale_data['']['plural-forms'] = $mo->headers['Plural-Forms'];
708 + }
709 +
710 + foreach ( $mo->entries as $msgid => $entry ) {
711 + $locale_data[ $msgid ] = $entry->translations;
712 + }
713 +
714 + $cached_json = wp_json_encode(
715 + array(
716 + 'locale_data' => array(
717 + $domain => $locale_data,
718 + ),
719 + )
720 + );
721 +
722 + $served = true;
723 +
724 + return $cached_json;
725 + }
726 +
727 + /**
686 728 * Initialize the modules and register the plugin routes. Also include the admin class, adds WP-CLI commands.
687 729 *
688 730 * @return void
689 731 */
@@ -692,9 +734,11 @@
692 734 add_action(
693 735 'after_setup_theme',
694 736 function () {
695 737 add_filter( 'cron_schedules', array( $this, 'cron_schedules' ) );
696 - $this->init_modules();
738 + if ( method_exists( $this, 'init_modules' ) ) {
739 + $this->init_modules();
740 + }
697 741 }
698 742 );
699 743 // Register routes.
700 744 add_action(
@@ -703,12 +747,12 @@
703 747 require_once WP_DEFENDER_DIR . 'src/routes.php';
704 748 },
705 749 9
706 750 );
751 + // Serve script translations from the single MO file our in-site translation tools produce.
752 + add_filter( 'pre_load_script_translations', array( $this, 'provide_script_translations' ), 10, 4 );
707 753 // Register the Hub Connector early to handle the auth callback during the admin init hook.
708 754 add_action( 'plugins_loaded', array( wd_di()->get( Hub_Connector::class ), 'init' ) );
709 - // Register the Cross-Sell module.
710 - add_action( 'init', array( wd_di()->get( \WP_Defender\Component\Cross_Sell::class ), 'init' ), 9 );
711 755 // Include admin class. Don't use is_admin().
712 756 add_action( 'admin_init', array( ( new Admin() ), 'init' ) );
713 757 // Initialize deactivation survey.
714 758 add_action( 'admin_enqueue_scripts', array( ( new Admin() ), 'init_deactivation_survey' ) );
@@ -719,6 +763,58 @@
719 763 // Rotational logger initialization.
720 764 add_action( 'init', array( ( new Rotation_Logger() ), 'init' ), 99 );
721 765 // Handle plugin deactivation.
722 766 add_action( 'deactivated_plugin', array( ( new HUB() ), 'intercept_deactivate' ) );
767 + }
768 +
769 + /**
770 + * Sets a one-time redirect flag after plugin activation.
771 + *
772 + * The actual redirect is executed later during `admin_init` to avoid exiting activation flow prematurely.
773 + *
774 + * @return void
775 + */
776 + public function set_activation_redirect_flag(): void {
777 + if ( defender_is_wp_cli() ) {
778 + return;
779 + }
780 +
781 + update_site_option( 'wp_defender_activation_redirect', 1 );
782 + }
783 +
784 + /**
785 + * Redirects once to the Defender dashboard after activation.
786 + *
787 + * Safety checks:
788 + * - Redirect only when the activation flag exists.
789 + * - Skip AJAX, cron and WP-CLI contexts.
790 + * - Redirect only for users with proper admin capability.
791 + *
792 + * @return void
793 + */
794 + public function maybe_redirect_after_activation(): void {
795 + if ( ! (bool) get_site_option( 'wp_defender_activation_redirect' ) ) {
796 + return;
797 + }
798 +
799 + delete_site_option( 'wp_defender_activation_redirect' );
800 +
801 + if ( wp_doing_ajax() || wp_doing_cron() || defender_is_wp_cli() ) {
802 + return;
803 + }
804 +
805 + $cap = is_multisite() ? 'manage_network_options' : 'manage_options';
806 + if ( ! current_user_can( $cap ) ) {
807 + return;
808 + }
809 +
810 + $url = add_query_arg(
811 + array(
812 + 'page' => 'wp-defender',
813 + ),
814 + is_multisite() ? network_admin_url( 'admin.php' ) : admin_url( 'admin.php' )
815 + );
816 +
817 + wp_safe_redirect( $url );
818 + exit;
723 819 }
724 820 }