30 * DAY_IN_SECONDS, 'display' => __( 'Once a month (SafetyBis Security)', 'safetybis-security' ), ); return $schedules; } /** * Plan events at a random time, so sites do not all call servers at the same moment. */ public static function schedule() { add_filter( 'cron_schedules', array( __CLASS__, 'schedules' ) ); // phpcs:ignore WordPress.WP.CronInterval.ChangeDetected if ( ! wp_next_scheduled( self::WEEKLY ) ) { wp_schedule_event( time() + wp_rand( HOUR_IN_SECONDS, DAY_IN_SECONDS ), 'weekly', self::WEEKLY ); } if ( ! wp_next_scheduled( self::MONTHLY ) ) { wp_schedule_event( time() + wp_rand( HOUR_IN_SECONDS, 3 * DAY_IN_SECONDS ), 'safetybis_monthly', self::MONTHLY ); } } /** * Remove all events. */ public static function unschedule() { wp_clear_scheduled_hook( self::WEEKLY ); wp_clear_scheduled_hook( self::MONTHLY ); wp_clear_scheduled_hook( Runner::CRON_TICK ); } /** * Weekly full scan, when enabled. */ public static function weekly() { Refs::purge(); // Nothing runs on its own before the user has seen and saved the first-run screen. if ( Settings::get( 'onboarded' ) && Settings::get( 'weekly_scan' ) ) { Runner::start( array_keys( Runner::MODULES ), 'cron' ); } } /** * Monthly list update (does nothing without consent). */ public static function monthly() { ApiClient::update_list(); } /** * After a scheduled full scan: optional email when important issues are open. * * @param array $job Finished job. */ public static function scan_finished( $job ) { if ( ! is_array( $job ) || 'cron' !== ( isset( $job['trigger'] ) ? $job['trigger'] : '' ) || ! Settings::get( 'email_report' ) ) { return; } $counts = Findings::counts(); $high = 0; $medium = 0; foreach ( $counts as $by_severity ) { $high += isset( $by_severity['high'] ) ? $by_severity['high'] : 0; $medium += isset( $by_severity['medium'] ) ? $by_severity['medium'] : 0; } if ( 0 === $high + $medium ) { return; } $site = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); /* translators: 1: site name, 2: number of issues. */ $subject = sprintf( __( '[%1$s] SafetyBis Security: %2$d issues need attention', 'safetybis-security' ), $site, $high + $medium ); $lines = array( /* translators: %s: site address. */ sprintf( __( 'The weekly scan of %s has finished.', 'safetybis-security' ), home_url() ), '', /* translators: %d: number. */ sprintf( __( 'High priority: %d', 'safetybis-security' ), $high ), /* translators: %d: number. */ sprintf( __( 'Medium priority: %d', 'safetybis-security' ), $medium ), '', __( 'Open the report:', 'safetybis-security' ), admin_url( 'admin.php?page=safetybis-security' ), '', __( 'You receive this email because the weekly report is enabled in SafetyBis Security settings.', 'safetybis-security' ), ); wp_mail( get_option( 'admin_email' ), $subject, implode( "\n", $lines ) ); } }