WEEK_IN_SECONDS, 'display' => __( 'Once Weekly', 'ai-guardwall' ), ); } $schedules['svabg_15min'] = array( 'interval' => 15 * MINUTE_IN_SECONDS, 'display' => __( 'Every 15 minutes (AI GuardWall)', 'ai-guardwall' ), ); $schedules['svabg_2days'] = array( 'interval' => 2 * DAY_IN_SECONDS, 'display' => __( 'Every other day (AI GuardWall)', 'ai-guardwall' ), ); $schedules['svabg_monthly'] = array( 'interval' => 30 * DAY_IN_SECONDS, 'display' => __( 'Every 30 days (AI GuardWall)', 'ai-guardwall' ), ); return $schedules; } public static function schedule_all() { if ( ! wp_next_scheduled( self::HOOK_MAINTENANCE ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::HOOK_MAINTENANCE ); } self::reschedule_scan(); } public static function reschedule_scan() { $ts = wp_next_scheduled( self::HOOK_SCAN ); if ( $ts ) { wp_unschedule_event( $ts, self::HOOK_SCAN ); } $schedule = Softvolt_Ai_Bodyguard_Settings::get( 'scan_schedule', 'daily' ); $recurrence = array( 'twicedaily' => 'twicedaily', 'daily' => 'daily', 'every_other_day' => 'svabg_2days', 'weekdays' => 'daily', 'weekends' => 'daily', 'weekly' => 'weekly', ); if ( ! isset( $recurrence[ $schedule ] ) ) { return; } $hour = (int) Softvolt_Ai_Bodyguard_Settings::get( 'scan_hour', 3 ); $local = strtotime( sprintf( 'today %02d:00', $hour ), current_time( 'timestamp' ) ); $local = $local <= current_time( 'timestamp' ) ? $local + DAY_IN_SECONDS : $local; $first = $local - ( current_time( 'timestamp' ) - time() ); wp_schedule_event( $first, $recurrence[ $schedule ], self::HOOK_SCAN ); } public static function unschedule_all() { foreach ( array( self::HOOK_MAINTENANCE, self::HOOK_SCAN, self::HOOK_TICK, self::HOOK_WATCHDOG ) as $hook ) { wp_clear_scheduled_hook( $hook ); } } public static function maintenance() { Softvolt_Ai_Bodyguard_Logger::purge( (int) Softvolt_Ai_Bodyguard_Settings::get( 'log_retention_days', 30 ) ); Softvolt_Ai_Bodyguard_Lockouts::purge_expired(); Softvolt_Ai_Bodyguard_Scanner::purge_old_scans( 20 ); Softvolt_Ai_Bodyguard_Integrity::refresh_core_checksums(); if ( class_exists( 'Softvolt_Ai_Bodyguard_Traffic' ) ) { Softvolt_Ai_Bodyguard_Traffic::trim(); } if ( class_exists( 'Softvolt_Ai_Bodyguard_Comments_Guard' ) ) { Softvolt_Ai_Bodyguard_Comments_Guard::empty_old_spam(); } // Rotate the security keys on a schedule (everyone logs in again). $days = (int) Softvolt_Ai_Bodyguard_Settings::get( 'salt_rotate_days', 0 ); $last = (int) get_option( 'svabg_salts_rotated', 0 ); if ( $days > 0 && time() - $last > $days * DAY_IN_SECONDS && Softvolt_Ai_Bodyguard_Wpconfig::is_writable() ) { $done = Softvolt_Ai_Bodyguard_Tools::rotate_salts(); if ( ! is_wp_error( $done ) ) { update_option( 'svabg_salts_rotated', time(), false ); } } } public static function scheduled_scan() { if ( Softvolt_Ai_Bodyguard_Scanner::is_running() ) { return; } // Weekday or weekend schedules run daily and skip the other days. $schedule = Softvolt_Ai_Bodyguard_Settings::get( 'scan_schedule', 'daily' ); $day = (int) wp_date( 'N' ); if ( ( 'weekdays' === $schedule && $day > 5 ) || ( 'weekends' === $schedule && $day <= 5 ) ) { return; } $id = Softvolt_Ai_Bodyguard_Scanner::start( 'scheduled' ); if ( $id ) { self::queue_tick( 1 ); } } public static function queue_tick( $delay = 5 ) { if ( ! wp_next_scheduled( self::HOOK_TICK ) ) { wp_schedule_single_event( time() + max( 1, (int) $delay ), self::HOOK_TICK ); } // A step killed by a host time limit or a fatal error never queues the next one: the watchdog does. if ( ! wp_next_scheduled( self::HOOK_WATCHDOG ) ) { wp_schedule_single_event( time() + 10 * MINUTE_IN_SECONDS, self::HOOK_WATCHDOG ); } if ( ! defined( 'DOING_CRON' ) ) { spawn_cron(); } } /** * Resume a background scan whose steps stopped (no progress for 5 minutes and no step queued). */ public static function watchdog() { if ( ! Softvolt_Ai_Bodyguard_Scanner::is_running() ) { return; } $state = Softvolt_Ai_Bodyguard_Scanner::state(); if ( ! wp_next_scheduled( self::HOOK_TICK ) && time() - (int) ( $state['updated'] ?? 0 ) > 5 * MINUTE_IN_SECONDS ) { Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'scan', __( 'The background scan had stopped; it was resumed.', 'ai-guardwall' ) ); wp_schedule_single_event( time() + 1, self::HOOK_TICK ); } wp_schedule_single_event( time() + 10 * MINUTE_IN_SECONDS, self::HOOK_WATCHDOG ); } public static function tick() { if ( ! Softvolt_Ai_Bodyguard_Scanner::is_running() ) { return; } $budget = max( 10, (int) Softvolt_Ai_Bodyguard_Settings::get( 'scan_time_budget', 12 ) * 2 ); $result = Softvolt_Ai_Bodyguard_Scanner::step( $budget ); if ( ! empty( $result['done'] ) ) { return; } self::queue_tick( 3 ); } }