schedule !== $freq ) ) { wp_clear_scheduled_hook( 'icsec_scheduled_scan' ); $next = false; } if ( ! empty( $s['cron'] ) && ! $next ) { wp_schedule_event( time() + HOUR_IN_SECONDS, $freq, 'icsec_scheduled_scan' ); } } /** Removes the scheduled scan (on deactivation). */ function icsec_unschedule() { wp_clear_scheduled_hook( 'icsec_scheduled_scan' ); } /** The scheduled scan: emails only the problems that were not in the previous report. */ function icsec_run_scheduled_scan() { $old = get_option( ICSEC_Scanner::OPT_REPORT ); $known = array(); foreach ( (array) ( is_array( $old ) ? $old['findings'] : array() ) as $f ) { $known[ $f['key'] ] = true; } $report = ICSEC_Scanner::run_all(); $levels = 'critical' === ICSEC_Scanner::settings()['level'] ? array( 'critical' ) : array( 'critical', 'warning' ); $new = array(); foreach ( $report['findings'] as $f ) { if ( empty( $f['ignored'] ) && in_array( $f['sev'], $levels, true ) && ! isset( $known[ $f['key'] ] ) ) { $new[] = $f; } } if ( ! $new ) { return; } $site = wp_parse_url( home_url(), PHP_URL_HOST ); /* translators: %s: site domain */ $body = sprintf( __( 'The security scanner found new problems on %s:', 'icecodero-security-scanner' ), $site ) . "\n\n"; foreach ( array_slice( $new, 0, 50 ) as $f ) { $body .= '[' . icsec_severity_label( $f['sev'] ) . '] ' . ICSEC_Scanner::message( $f ) . ( $f['file'] ? ' – ' . $f['file'] : '' ) . "\n"; } /* translators: %s: link to the report in the dashboard */ $body .= "\n" . sprintf( __( 'Full report: %s', 'icecodero-security-scanner' ), admin_url( 'admin.php?page=icecodero-security-scanner' ) ) . "\n"; /* translators: 1: site domain, 2: number of new problems */ $subject = sprintf( _n( 'Security %1$s: %2$d new problem', 'Security %1$s: %2$d new problems', count( $new ), 'icecodero-security-scanner' ), $site, count( $new ) ); wp_mail( ICSEC_Scanner::settings()['email'], $subject, $body ); } add_action( 'icsec_scheduled_scan', 'icsec_run_scheduled_scan' );