*/ public static function defaults(): array { return array( 'enabled' => false, 'frequency' => 'weekly', // daily | weekly 'severity' => 'critical', // critical | warning — the floor that triggers a mail 'recipients' => '', ); } /** @return array */ private function settings(): array { return $this->options->get( 'audit_alerts', self::defaults() ); } public function ensure_schedule(): void { $settings = $this->settings(); $scheduled = wp_next_scheduled( self::CRON_HOOK ); if ( empty( $settings['enabled'] ) ) { if ( $scheduled ) { wp_unschedule_event( $scheduled, self::CRON_HOOK ); } return; } if ( ! $scheduled ) { // The audit itself is the expensive part, so the schedule is the // audit's schedule — there is no separate "check" pass. \AiRankSuite\Support\Cron::ensure( self::CRON_HOOK, 'daily' === $settings['frequency'] ? 'daily' : 'weekly', time() + HOUR_IN_SECONDS ); } } /** * Cron entry point: run the audit to completion, then mail what is new. */ public function run(): void { $settings = $this->settings(); if ( empty( $settings['enabled'] ) ) { return; } $status = $this->auditor->start(); $guard = 0; // A bounded loop rather than while(true): a bug in tick() must not turn // a cron event into an infinite one. while ( 'running' === ( $status['state'] ?? '' ) && $guard < 5000 ) { $status = $this->auditor->tick(); ++$guard; } $this->notify(); } /** * Mail the findings that were not present last time. * * @return array{new: int, sent: bool} */ public function notify(): array { global $wpdb; $settings = $this->settings(); $floor = 'warning' === $settings['severity'] ? array( 'critical', 'warning' ) : array( 'critical' ); $run = (string) get_option( 'airanksuite_audit_last', '' ); if ( '' === $run ) { return array( 'new' => 0, 'sent' => false, ); } $table = Database::audit(); $placeholders = implode( ',', array_fill( 0, count( $floor ), '%s' ) ); // phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared $rows = $wpdb->get_results( $wpdb->prepare( "SELECT fingerprint, check_id, severity, label, detail, post_id FROM {$table} WHERE run_id = %s AND severity IN ({$placeholders}) ORDER BY FIELD(severity,'critical','warning'), check_id LIMIT 200", ...array_merge( array( $run ), $floor ) ) ); // phpcs:enable $seen = get_option( self::SEEN, array() ); $seen = is_array( $seen ) ? array_map( 'strval', $seen ) : array(); $fresh = array(); $all = array(); foreach ( (array) $rows as $row ) { $all[] = (string) $row->fingerprint; if ( ! in_array( (string) $row->fingerprint, $seen, true ) ) { $fresh[] = $row; } } // Remember the whole current set, not the union with history: a problem // that was fixed and comes back is genuinely news, and should alert // again rather than be suppressed forever by a stale fingerprint. update_option( self::SEEN, $all, false ); if ( empty( $fresh ) ) { return array( 'new' => 0, 'sent' => false, ); } $sent = $this->mail( $fresh ); return array( 'new' => count( $fresh ), 'sent' => $sent, ); } /** * @param array $findings Fresh findings. */ private function mail( array $findings ): bool { $settings = $this->settings(); $recipients = array_values( array_filter( array_map( 'sanitize_email', preg_split( '/[\s,;]+/', (string) $settings['recipients'] ) ?: array() ) ) ); if ( empty( $recipients ) ) { $recipients = array( (string) get_option( 'admin_email' ) ); } $checks = Auditor::checks(); $site = (string) get_bloginfo( 'name' ); $critical = 0; $items = ''; foreach ( $findings as $row ) { $def = $checks[ $row->check_id ] ?? array(); $label = (string) ( $def['label'] ?? $row->check_id ); $fix = (string) ( $def['fix'] ?? '' ); if ( 'critical' === $row->severity ) { ++$critical; } $link = (int) $row->post_id > 0 ? (string) get_edit_post_link( (int) $row->post_id, '' ) : (string) admin_url( 'admin.php?page=ai-rank-suite#/audit' ); $items .= sprintf( '
%s
%s
%s%s
%s → ', 'critical' === $row->severity ? '#c5221f' : '#b06000', esc_html( (string) $row->severity ), esc_html( $label ), esc_html( (string) $row->label ), '' !== (string) $row->detail ? ' — ' . esc_html( (string) $row->detail ) : '', esc_url( $link ), esc_html( $fix !== '' ? __( 'How to fix', 'ai-rank-suite' ) : __( 'Open', 'ai-rank-suite' ) ) ); } $subject = sprintf( /* translators: 1: number of issues, 2: site name. */ _n( '%1$d new SEO issue on %2$s', '%1$d new SEO issues on %2$s', count( $findings ), 'ai-rank-suite' ), count( $findings ), $site ); $body = sprintf( '
%s
%s
%s
%s
', esc_html( $subject ), esc_html( sprintf( /* translators: %d: number of critical issues. */ _n( '%d of these is critical. Only issues that were not present at the last check are listed.', '%d of these are critical. Only issues that were not present at the last check are listed.', $critical, 'ai-rank-suite' ), $critical ) ), $items, esc_url( admin_url( 'admin.php?page=ai-rank-suite#/audit' ) ), esc_html__( 'Open the full audit', 'ai-rank-suite' ) ); return (bool) wp_mail( $recipients, $subject, $body, array( 'Content-Type: text/html; charset=UTF-8' ) ); } }