| @@ -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,13 +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; |
| 47 | +use WP_Defender\Upgrader; | |
| 49 | 48 | |
| 50 | 49 | trait Defender_Bootstrap { |
| 50 | + | |
| 51 | 51 | /** |
| 52 | 52 | * Table name for quarantine. |
| 53 | 53 | * |
| 54 | 54 | * @var string |
| @@ -62,99 +62,15 @@ | ||
| 62 | 62 | */ |
| 63 | 63 | private $scan_item_table = 'defender_scan_item'; |
| 64 | 64 | |
| 65 | 65 | /** |
| 66 | - * Check is all quarantine dependent table is having storage engine InnoDB. | |
| 67 | - * | |
| 68 | - * @return bool True if all dependent table is InnoDB else false. | |
| 69 | - */ | |
| 70 | - private function is_quarantine_dependent_tables_innodb(): bool { | |
| 71 | - global $wpdb; | |
| 72 | - | |
| 73 | - $tables = array( $wpdb->users, $wpdb->base_prefix . $this->scan_item_table ); | |
| 74 | - $total_table = count( $tables ); | |
| 75 | - | |
| 76 | - return $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 77 | - $wpdb->prepare( | |
| 78 | - "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' );", | |
| 79 | - $total_table, | |
| 80 | - $wpdb->dbname, | |
| 81 | - 'innodb', | |
| 82 | - ) | |
| 83 | - ) === '1'; | |
| 84 | - } | |
| 85 | - | |
| 86 | - /** | |
| 87 | - * Creates the quarantine table if it doesn't exist. | |
| 88 | - * | |
| 89 | - * @return void | |
| 90 | - */ | |
| 91 | - public function create_table_quarantine() { | |
| 92 | - global $wpdb; | |
| 93 | - | |
| 94 | - // Define table names and charset. | |
| 95 | - $quarantine_table = $wpdb->base_prefix . $this->quarantine_table; | |
| 96 | - $scan_item_table = $wpdb->base_prefix . $this->scan_item_table; | |
| 97 | - $charset_collate = $wpdb->get_charset_collate(); | |
| 98 | - $unique_id = uniqid( $wpdb->prefix ); | |
| 99 | - | |
| 100 | - $common_columns = <<<'SQL' | |
| 101 | - `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT, | |
| 102 | - `defender_scan_item_id` int UNSIGNED DEFAULT NULL, | |
| 103 | - `file_hash` char(53) NOT NULL, | |
| 104 | - `file_full_path` text NOT NULL, | |
| 105 | - `file_original_name` tinytext NOT NULL, | |
| 106 | - `file_extension` varchar(16) DEFAULT NULL, | |
| 107 | - `file_mime_type` varchar(64) DEFAULT NULL, | |
| 108 | - `file_rw_permission` smallint UNSIGNED DEFAULT NULL, | |
| 109 | - `file_owner` varchar(255) DEFAULT NULL, | |
| 110 | - `file_group` varchar(255) DEFAULT NULL, | |
| 111 | - `file_version` varchar(32) DEFAULT NULL, | |
| 112 | - `file_category` tinyint UNSIGNED DEFAULT 0, | |
| 113 | - `file_modified_time` datetime NOT NULL, | |
| 114 | - `source_slug` varchar(255) NOT NULL, | |
| 115 | - `created_time` datetime NOT NULL, | |
| 116 | - `created_by` bigint UNSIGNED DEFAULT NULL, | |
| 117 | - PRIMARY KEY (`id`) | |
| 118 | - SQL; | |
| 119 | - | |
| 120 | - // Define key names. | |
| 121 | - $scan_item_key = "{$unique_id}_defender_scan_item_id"; | |
| 122 | - $created_by_key = "{$unique_id}_created_by"; | |
| 123 | - | |
| 124 | - // Build the SQL statement based on the storage engine. | |
| 125 | - if ( $this->is_quarantine_dependent_tables_innodb() ) { | |
| 126 | - $sql = <<<SQL | |
| 127 | - CREATE TABLE IF NOT EXISTS `{$quarantine_table}` ( | |
| 128 | - $common_columns, | |
| 129 | - CONSTRAINT `{$scan_item_key}` | |
| 130 | - FOREIGN KEY (`defender_scan_item_id`) REFERENCES {$scan_item_table}(`id`) | |
| 131 | - ON UPDATE CASCADE ON DELETE SET NULL, | |
| 132 | - CONSTRAINT `{$created_by_key}` | |
| 133 | - FOREIGN KEY (`created_by`) REFERENCES {$wpdb->users}(`ID`) | |
| 134 | - ON UPDATE CASCADE ON DELETE SET NULL | |
| 135 | - ) {$charset_collate}; | |
| 136 | - SQL; | |
| 137 | - } else { | |
| 138 | - $sql = <<<SQL | |
| 139 | - CREATE TABLE IF NOT EXISTS `{$quarantine_table}` ( | |
| 140 | - $common_columns, | |
| 141 | - KEY `{$scan_item_key}` (`defender_scan_item_id`), | |
| 142 | - KEY `{$created_by_key}` (`created_by`) | |
| 143 | - ) {$charset_collate}; | |
| 144 | - SQL; | |
| 145 | - } | |
| 146 | - | |
| 147 | - // Execute the SQL query. | |
| 148 | - $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 149 | - } | |
| 150 | - | |
| 151 | - /** | |
| 152 | 66 | * Activation. |
| 153 | 67 | */ |
| 154 | 68 | private function activation_hook_common(): void { |
| 155 | - $this->create_database_tables(); | |
| 69 | + Upgrader::date_activated(); | |
| 70 | + $this->create_database_tables_common(); | |
| 156 | 71 | $this->on_activation(); |
| 72 | + $this->set_activation_redirect_flag(); | |
| 157 | 73 | // Create a file with a random key if it doesn't exist. |
| 158 | 74 | ( new Crypt() )->create_key_file(); |
| 159 | 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. |
| 160 | 76 | $settings = wd_di()->get( Main_Setting::class ); |
| @@ -168,14 +84,12 @@ | ||
| 168 | 84 | wp_schedule_single_event( time() + 5, 'wpdef_smart_ip_detection_ping' ); |
| 169 | 85 | } |
| 170 | 86 | |
| 171 | 87 | /** |
| 172 | - * Deactivation. | |
| 88 | + * Deactivation - clears shared cron hooks present in both free and pro versions. | |
| 173 | 89 | */ |
| 174 | - public function deactivation_hook(): void { | |
| 90 | + protected function deactivation_hook_common(): void { | |
| 175 | 91 | wp_clear_scheduled_hook( 'firewall_clean_up_logs' ); |
| 176 | - wp_clear_scheduled_hook( 'audit_sync_events' ); | |
| 177 | - wp_clear_scheduled_hook( 'audit_clean_up_logs' ); | |
| 178 | 92 | wp_clear_scheduled_hook( 'wdf_maybe_send_report' ); |
| 179 | 93 | wp_clear_scheduled_hook( 'wp_defender_clear_logs' ); |
| 180 | 94 | wp_clear_scheduled_hook( 'wpdef_sec_key_gen' ); |
| 181 | 95 | wp_clear_scheduled_hook( 'wpdef_clear_scan_logs' ); |
| @@ -181,9 +95,8 @@ | ||
| 181 | 95 | wp_clear_scheduled_hook( 'wpdef_clear_scan_logs' ); |
| 182 | 96 | wp_clear_scheduled_hook( 'wpdef_log_rotational_delete' ); |
| 183 | 97 | wp_clear_scheduled_hook( 'wpdef_update_geoip' ); |
| 184 | 98 | wp_clear_scheduled_hook( 'wpdef_fetch_global_ip_list' ); |
| 185 | - wp_clear_scheduled_hook( 'wpdef_quarantine_delete_expired' ); | |
| 186 | 99 | wp_clear_scheduled_hook( 'wpdef_firewall_clean_up_lockout' ); |
| 187 | 100 | wp_clear_scheduled_hook( 'wpdef_firewall_send_compact_logs_to_api' ); |
| 188 | 101 | wp_clear_scheduled_hook( 'wpdef_firewall_fetch_trusted_proxy_preset_ips' ); |
| 189 | 102 | wp_clear_scheduled_hook( 'wpdef_firewall_clean_up_unlockout' ); |
| @@ -194,9 +107,8 @@ | ||
| 194 | 107 | wp_clear_scheduled_hook( 'wpdef_rotate_malicious_bot_secret_hash' ); |
| 195 | 108 | |
| 196 | 109 | // Remove old legacy cron jobs if they exist. |
| 197 | 110 | wp_clear_scheduled_hook( 'lockoutReportCron' ); |
| 198 | - wp_clear_scheduled_hook( 'auditReportCron' ); | |
| 199 | 111 | wp_clear_scheduled_hook( 'cleanUpOldLog' ); |
| 200 | 112 | wp_clear_scheduled_hook( 'scanReportCron' ); |
| 201 | 113 | wp_clear_scheduled_hook( 'tweaksSendNotification' ); |
| 202 | 114 | } |
| @@ -249,34 +161,18 @@ | ||
| 249 | 161 | PRIMARY KEY (`id`), |
| 250 | 162 | UNIQUE KEY ip (ip) |
| 251 | 163 | ) {$charset_collate}; |
| 252 | 164 | SQL; |
| 253 | - $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 | |
| 254 | 166 | } |
| 255 | 167 | |
| 256 | 168 | /** |
| 257 | - * Creates Defender's tables. | |
| 258 | - * | |
| 259 | - * @since 2.7.1 No use dbDelta because PHP v8.1 triggers an error when calling query "DESCRIBE {$table};" if the | |
| 260 | - * table doesn't exist. | |
| 169 | + * Creates the audit log table used for caching audit events. | |
| 261 | 170 | */ |
| 262 | - protected function create_database_tables(): void { | |
| 171 | + protected function create_table_audit_log(): void { | |
| 263 | 172 | global $wpdb; |
| 264 | 173 | |
| 265 | 174 | $charset_collate = $wpdb->get_charset_collate(); |
| 266 | - // Hide errors. | |
| 267 | - $wpdb->hide_errors(); | |
| 268 | - // Email log table. | |
| 269 | - $sql = "CREATE TABLE IF NOT EXISTS {$wpdb->base_prefix}defender_email_log ( | |
| 270 | - `id` int(11) unsigned NOT NULL AUTO_INCREMENT, | |
| 271 | - `timestamp` int NOT NULL, | |
| 272 | - `source` varchar(255) NOT NULL, | |
| 273 | - `to` varchar(255) NOT NULL, | |
| 274 | - PRIMARY KEY (`id`), | |
| 275 | - KEY `source` (`source`) | |
| 276 | -) $charset_collate;"; | |
| 277 | - $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 278 | - | |
| 279 | 175 | // Audit log table. Though our data mainly store on API side, we will need a table for caching. |
| 280 | 176 | $sql = "CREATE TABLE IF NOT EXISTS {$wpdb->base_prefix}defender_audit_log ( |
| 281 | 177 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT, |
| 282 | 178 | `timestamp` int NOT NULL, |
| @@ -297,9 +193,33 @@ | ||
| 297 | 193 | KEY `context` (`context`), |
| 298 | 194 | KEY `ip` (`ip`) |
| 299 | 195 | ) $charset_collate;"; |
| 300 | 196 | $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 197 | + } | |
| 301 | 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 | + | |
| 302 | 222 | // Scan item table. |
| 303 | 223 | $sql = "CREATE TABLE IF NOT EXISTS {$wpdb->base_prefix}defender_scan_item ( |
| 304 | 224 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT, |
| 305 | 225 | `parent_id` int NOT NULL, |
| @@ -364,36 +284,106 @@ | ||
| 364 | 284 | KEY `attempt_404` (`attempt_404`) |
| 365 | 285 | ) $charset_collate;"; |
| 366 | 286 | $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 367 | 287 | |
| 368 | - $this->create_table_quarantine(); | |
| 369 | - | |
| 370 | - // Create Unlock table. | |
| 288 | + // Create unlockout table. | |
| 371 | 289 | $this->create_table_unlockout(); |
| 372 | 290 | // Create Blocklist table. |
| 373 | 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(); | |
| 374 | 296 | } |
| 375 | 297 | |
| 376 | 298 | /** |
| 377 | - * Check if this is onboarding. | |
| 299 | + * Check if all quarantine-dependent tables use the InnoDB storage engine. | |
| 378 | 300 | * |
| 379 | - * @return bool | |
| 301 | + * @return bool True if all dependent tables are InnoDB, false otherwise. | |
| 380 | 302 | */ |
| 381 | - private function is_onboarding(): bool { | |
| 382 | - /** | |
| 383 | - * Display Onboarding if: | |
| 384 | - * it's a fresh install and there were no requests from the Hub before, | |
| 385 | - * after Reset Settings. | |
| 386 | - * | |
| 387 | - * @var HUB | |
| 388 | - */ | |
| 389 | - $hub_class = wd_di()->get( HUB::class ); | |
| 390 | - $hub_class->set_onboarding_status( Onboard_Model::maybe_show_onboarding() ); | |
| 303 | + private function is_quarantine_dependent_tables_innodb(): bool { | |
| 304 | + global $wpdb; | |
| 391 | 305 | |
| 392 | - 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'; | |
| 393 | 317 | } |
| 394 | 318 | |
| 395 | 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 | + /** | |
| 396 | 386 | * Initialize the common modules of the application. |
| 397 | 387 | * |
| 398 | 388 | * @return void |
| 399 | 389 | */ |
| @@ -400,39 +390,34 @@ | ||
| 400 | 390 | private function init_modules_common(): void { |
| 401 | 391 | // Init main ORM. |
| 402 | 392 | Array_Cache::set( 'orm', new Mapper() ); |
| 403 | 393 | |
| 404 | - if ( $this->is_onboarding() ) { | |
| 405 | - // If it's cli we should start this normally. | |
| 406 | - Array_Cache::set( 'onboard', wd_di()->get( Onboard::class ) ); | |
| 407 | - } else { | |
| 408 | - // Initialize the main controllers of every module. | |
| 409 | - wd_di()->get( Dashboard::class ); | |
| 410 | - } | |
| 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 ); | |
| 411 | 399 | wd_di()->get( Security_Tweaks::class ); |
| 412 | - wd_di()->get( Scan::class ); | |
| 413 | 400 | wd_di()->get( Audit_Logging::class ); |
| 414 | 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 ); | |
| 415 | 405 | wd_di()->get( WAF::class ); |
| 416 | 406 | wd_di()->get( Two_Factor::class ); |
| 417 | - wd_di()->get( Advanced_Tools::class ); | |
| 418 | 407 | wd_di()->get( Mask_Login::class ); |
| 419 | - wd_di()->get( Security_Headers::class ); | |
| 420 | 408 | wd_di()->get( Captcha::class ); |
| 421 | 409 | wd_di()->get( Notification::class ); |
| 410 | + wd_di()->get( Recipients::class ); | |
| 422 | 411 | wd_di()->get( Main_Setting::class ); |
| 423 | 412 | wd_di()->get( Blocklist_Monitor::class ); |
| 424 | 413 | wd_di()->get( Password_Protection::class ); |
| 425 | 414 | wd_di()->get( Password_Reset::class ); |
| 426 | 415 | wd_di()->get( Webauthn::class ); |
| 427 | - wd_di()->get( Expert_Services::class ); | |
| 428 | 416 | wd_di()->get( Hub_Connector_Controller::class ); |
| 429 | 417 | wd_di()->get( Strong_Password::class ); |
| 430 | 418 | wd_di()->get( Session_Protection::class ); |
| 431 | - | |
| 432 | - if ( class_exists( 'WP_Defender\Controller\Quarantine' ) ) { | |
| 433 | - wd_di()->get( Quarantine::class ); | |
| 434 | - } | |
| 419 | + wd_di()->get( Quarantine::class ); | |
| 435 | 420 | wd_di()->get( Data_Tracking::class ); |
| 436 | 421 | if ( defender_is_wp_org_version() ) { |
| 437 | 422 | wd_di()->get( Rate_Controller::class ); |
| 438 | 423 | } |
| @@ -464,11 +449,36 @@ | ||
| 464 | 449 | * @return void |
| 465 | 450 | */ |
| 466 | 451 | private function register_styles(): void { |
| 467 | 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 | + ); | |
| 468 | 475 | |
| 469 | 476 | $css_files = array( |
| 470 | - 'defender' => WP_DEFENDER_BASE_URL . 'assets/css/styles.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', | |
| 471 | 481 | ); |
| 472 | 482 | |
| 473 | 483 | foreach ( $css_files as $slug => $file ) { |
| 474 | 484 | wp_register_style( $slug, $file, array(), DEFENDER_VERSION ); |
| @@ -481,70 +491,39 @@ | ||
| 481 | 491 | * @return void |
| 482 | 492 | */ |
| 483 | 493 | private function register_scripts(): void { |
| 484 | 494 | $base_url = WP_DEFENDER_BASE_URL; |
| 485 | - $dependencies = array( 'def-vue', 'def-manifest', 'defender', 'wp-i18n' ); | |
| 495 | + $dependencies = array( 'def-vue', 'def-manifest', 'def-core-ui', 'defender', 'wp-i18n' ); | |
| 486 | 496 | $js_files = array( |
| 487 | - 'wpmudev-sui' => array( | |
| 497 | + 'wpmudev-sui' => array( | |
| 488 | 498 | $base_url . 'assets/js/shared-ui.js', |
| 489 | 499 | ), |
| 490 | - 'defender' => array( | |
| 500 | + 'defender' => array( | |
| 491 | 501 | $base_url . 'assets/js/scripts.js', |
| 492 | 502 | ), |
| 493 | - 'def-vue' => array( | |
| 503 | + 'def-deactivation-survey' => array( | |
| 504 | + $base_url . 'assets/js/deactivation-survey.js', | |
| 505 | + array( 'clipboard', 'wpmudev-sui' ), | |
| 506 | + ), | |
| 507 | + 'def-admin' => array( | |
| 508 | + $base_url . 'assets/js/admin.js', | |
| 509 | + array( 'def-deactivation-survey' ), | |
| 510 | + ), | |
| 511 | + 'def-vue' => array( | |
| 494 | 512 | $base_url . 'assets/js/vendor.js', |
| 495 | 513 | ), |
| 496 | - 'def-manifest' => array( | |
| 514 | + 'def-manifest' => array( | |
| 497 | 515 | $base_url . 'assets/js/manifest.js', |
| 498 | 516 | ), |
| 499 | - 'def-dashboard' => array( | |
| 500 | - $base_url . 'assets/app/dashboard.js', | |
| 501 | - $dependencies, | |
| 517 | + 'def-core-ui' => array( | |
| 518 | + $base_url . 'assets/js/core-ui.js', | |
| 519 | + array( 'def-vue', 'def-manifest' ), | |
| 502 | 520 | ), |
| 503 | - 'def-securitytweaks' => array( | |
| 504 | - $base_url . 'assets/app/security-tweak.js', | |
| 505 | - array_merge( $dependencies, array( 'clipboard', 'wpmudev-sui' ) ), | |
| 506 | - ), | |
| 507 | - 'def-scan' => array( | |
| 508 | - $base_url . 'assets/app/scan.js', | |
| 509 | - array_merge( $dependencies, array( 'clipboard', 'wpmudev-sui' ) ), | |
| 510 | - ), | |
| 511 | - 'def-audit' => array( | |
| 512 | - $base_url . 'assets/app/audit.js', | |
| 521 | + // React files. | |
| 522 | + 'def-setup-wizard' => array( | |
| 523 | + $base_url . 'assets/js/setup-wizard.js', | |
| 513 | 524 | $dependencies, |
| 514 | 525 | ), |
| 515 | - 'def-iplockout' => array( | |
| 516 | - $base_url . 'assets/app/ip-lockout.js', | |
| 517 | - array_merge( $dependencies, array( 'wpmudev-sui' ) ), | |
| 518 | - ), | |
| 519 | - 'def-advancedtools' => array( | |
| 520 | - $base_url . 'assets/app/advanced-tools.js', | |
| 521 | - $dependencies, | |
| 522 | - ), | |
| 523 | - 'def-settings' => array( | |
| 524 | - $base_url . 'assets/app/settings.js', | |
| 525 | - $dependencies, | |
| 526 | - ), | |
| 527 | - 'def-2fa' => array( | |
| 528 | - $base_url . 'assets/app/two-fa.js', | |
| 529 | - $dependencies, | |
| 530 | - ), | |
| 531 | - 'def-notification' => array( | |
| 532 | - $base_url . 'assets/app/notification.js', | |
| 533 | - $dependencies, | |
| 534 | - ), | |
| 535 | - 'def-waf' => array( | |
| 536 | - $base_url . 'assets/app/waf.js', | |
| 537 | - $dependencies, | |
| 538 | - ), | |
| 539 | - 'def-onboard' => array( | |
| 540 | - $base_url . 'assets/app/onboard.js', | |
| 541 | - $dependencies, | |
| 542 | - ), | |
| 543 | - 'def-expert-services' => array( | |
| 544 | - $base_url . '/assets/app/expert-services.js', | |
| 545 | - $dependencies, | |
| 546 | - ), | |
| 547 | 526 | ); |
| 548 | 527 | |
| 549 | 528 | foreach ( $js_files as $slug => $file ) { |
| 550 | 529 | if ( isset( $file[1] ) ) { |
| @@ -569,17 +548,12 @@ | ||
| 569 | 548 | private function localize_script(): void { |
| 570 | 549 | $wpmu_dev = new WPMUDEV(); |
| 571 | 550 | global $wp_defender_central; |
| 572 | 551 | |
| 573 | - $misc = array(); | |
| 574 | - $data_tracking = wd_di()->get( Data_Tracking::class ); | |
| 575 | - $is_tracking = $data_tracking->show_tracking_modal(); | |
| 576 | - if ( $is_tracking ) { | |
| 577 | - $misc = $data_tracking->get_tracking_modal(); | |
| 578 | - } | |
| 552 | + $misc = array(); | |
| 579 | 553 | $misc['high_contrast'] = defender_high_contrast(); |
| 580 | - | |
| 581 | - if ( defender_is_wp_org_version() ) { | |
| 554 | + $is_wp_org = defender_is_wp_org_version(); | |
| 555 | + if ( $is_wp_org ) { | |
| 582 | 556 | $misc['rating'] = array(); |
| 583 | 557 | $rate_service = Rate_Component::is_achievement_displayed(); |
| 584 | 558 | if ( $rate_service['is_displayed'] ) { |
| 585 | 559 | $misc['rating'] = wd_di()->get( Rate_Controller::class )->data_frontend(); |
| @@ -601,9 +575,12 @@ | ||
| 601 | 575 | 'home_url' => network_home_url(), |
| 602 | 576 | 'site_url' => network_site_url(), |
| 603 | 577 | 'admin_url' => network_admin_url(), |
| 604 | 578 | 'defender_url' => WP_DEFENDER_BASE_URL, |
| 579 | + // There might be Pro version but without pro features, for example when the membership expires. | |
| 605 | 580 | 'is_free' => $wpmu_dev->is_pro() ? 0 : 1, |
| 581 | + // Strictly Free version. | |
| 582 | + 'is_wp_org' => $is_wp_org ? 1 : 0, | |
| 606 | 583 | 'is_membership' => true, |
| 607 | 584 | 'is_whitelabel' => $wpmu_dev->is_whitelabel_enabled() ? 'enabled' : 'disabled', |
| 608 | 585 | 'wpmu_dev_url_action' => $wpmu_dev->hide_wpmu_dev_urls() ? 'hide' : 'show', |
| 609 | 586 | 'opcache_save_comments' => $wp_defender_central->is_opcache_save_comments_disabled() ? 'disabled' : 'enabled', |
| @@ -611,9 +588,8 @@ | ||
| 611 | 588 | 'wpmudev_url' => WP_DEFENDER_DOCS_LINK, |
| 612 | 589 | 'wpmudev_support_ticket_text' => defender_support_ticket_text(), |
| 613 | 590 | 'wpmudev_api_base_url' => $wpmu_dev->get_api_base_url(), |
| 614 | 591 | 'upgrade_title' => esc_html__( 'UPGRADE TO PRO', 'defender-security' ), |
| 615 | - 'tracking_modal' => $is_tracking ? 'show' : 'hide', | |
| 616 | 592 | 'hosted' => $wpmu_dev->is_wpmu_hosting(), |
| 617 | 593 | 'file_upload_nonce' => wp_create_nonce( 'defender_file_upload' ), |
| 618 | 594 | 'wpmudev_hub_link' => 'https://wpmudev.com/hub2/', |
| 619 | 595 | ) |
| @@ -619,8 +595,18 @@ | ||
| 619 | 595 | ) |
| 620 | 596 | ); |
| 621 | 597 | |
| 622 | 598 | wp_localize_script( 'defender', 'defenderGetText', defender_gettext_translations() ); |
| 599 | + | |
| 600 | + wp_localize_script( | |
| 601 | + 'def-deactivation-survey', | |
| 602 | + 'defender', | |
| 603 | + array( | |
| 604 | + 'usage_tracking' => wd_di()->get( \WP_Defender\Model\Setting\Main_Setting::class )->usage_tracking, | |
| 605 | + 'admin_url' => admin_url( 'admin-ajax.php' ), | |
| 606 | + 'nonce' => wp_create_nonce( 'defender_deactivation_survey_modal' ), | |
| 607 | + ) | |
| 608 | + ); | |
| 623 | 609 | } |
| 624 | 610 | |
| 625 | 611 | /** |
| 626 | 612 | * Register all core assets. |
| @@ -636,8 +622,10 @@ | ||
| 636 | 622 | /** |
| 637 | 623 | * Trigger mandatory actions on activation. |
| 638 | 624 | */ |
| 639 | 625 | private function on_activation(): void { |
| 626 | + update_site_option( Data_Tracking::TRACKING_SLUG, true ); | |
| 627 | + | |
| 640 | 628 | add_action( |
| 641 | 629 | 'admin_init', |
| 642 | 630 | function () { |
| 643 | 631 | $security_tweaks = wd_di()->get( Security_Tweaks::class ); |
| @@ -657,8 +645,87 @@ | ||
| 657 | 645 | return defender_cron_schedules( $schedules ); |
| 658 | 646 | } |
| 659 | 647 | |
| 660 | 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 | + /** | |
| 661 | 728 | * Initialize the modules and register the plugin routes. Also include the admin class, adds WP-CLI commands. |
| 662 | 729 | * |
| 663 | 730 | * @return void |
| 664 | 731 | */ |
| @@ -667,9 +734,11 @@ | ||
| 667 | 734 | add_action( |
| 668 | 735 | 'after_setup_theme', |
| 669 | 736 | function () { |
| 670 | 737 | add_filter( 'cron_schedules', array( $this, 'cron_schedules' ) ); |
| 671 | - $this->init_modules(); | |
| 738 | + if ( method_exists( $this, 'init_modules' ) ) { | |
| 739 | + $this->init_modules(); | |
| 740 | + } | |
| 672 | 741 | } |
| 673 | 742 | ); |
| 674 | 743 | // Register routes. |
| 675 | 744 | add_action( |
| @@ -678,14 +747,16 @@ | ||
| 678 | 747 | require_once WP_DEFENDER_DIR . 'src/routes.php'; |
| 679 | 748 | }, |
| 680 | 749 | 9 |
| 681 | 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 ); | |
| 682 | 753 | // Register the Hub Connector early to handle the auth callback during the admin init hook. |
| 683 | 754 | add_action( 'plugins_loaded', array( wd_di()->get( Hub_Connector::class ), 'init' ) ); |
| 684 | - // Register the Cross-Sell module. | |
| 685 | - add_action( 'init', array( wd_di()->get( \WP_Defender\Component\Cross_Sell::class ), 'init' ), 9 ); | |
| 686 | 755 | // Include admin class. Don't use is_admin(). |
| 687 | 756 | add_action( 'admin_init', array( ( new Admin() ), 'init' ) ); |
| 757 | + // Initialize deactivation survey. | |
| 758 | + add_action( 'admin_enqueue_scripts', array( ( new Admin() ), 'init_deactivation_survey' ) ); | |
| 688 | 759 | // Add WP-CLI commands. |
| 689 | 760 | if ( defender_is_wp_cli() ) { |
| 690 | 761 | WP_CLI::add_command( 'defender', Cli::class ); |
| 691 | 762 | } |
| @@ -692,6 +763,58 @@ | ||
| 692 | 763 | // Rotational logger initialization. |
| 693 | 764 | add_action( 'init', array( ( new Rotation_Logger() ), 'init' ), 99 ); |
| 694 | 765 | // Handle plugin deactivation. |
| 695 | 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; | |
| 696 | 819 | } |
| 697 | 820 | } |