$job Hydrated job row (status, type, error_message, …). * @return array{ok:bool,sent:bool,error?:string} */ public static function maybe_send( int $job_id, array $job ): array { if ( $job_id <= 0 ) { return array( 'ok' => false, 'sent' => false, 'error' => 'invalid job', ); } $status = (string) ( $job['status'] ?? '' ); if ( 'failed' !== $status ) { return array( 'ok' => true, 'sent' => false, ); } $guard = self::SENT_META_PREFIX . $job_id; if ( get_transient( $guard ) ) { return array( 'ok' => true, 'sent' => false, ); } $to = Settings::alert_email(); if ( ! is_email( $to ) ) { return array( 'ok' => false, 'sent' => false, 'error' => 'invalid alert email', ); } $site_name = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); $site_url = home_url( '/' ); $type = (string) ( $job['type'] ?? 'job' ); $phase = (string) ( $job['phase'] ?? '' ); $error = (string) ( $job['error_message'] ?? __( 'Unknown error', '007aj-backup-restore-verifier' ) ); $error = wp_strip_all_tags( $error ); if ( strlen( $error ) > 2000 ) { $error = substr( $error, 0, 2000 ) . '…'; } $subject = sprintf( /* translators: 1: site name, 2: job id */ __( '[%1$s] 007AJ Backup Restore Verifier job #%2$d failed', '007aj-backup-restore-verifier' ), $site_name, $job_id ); $body_lines = array( /* translators: %s: site name */ sprintf( __( 'Site: %s', '007aj-backup-restore-verifier' ), $site_name ), /* translators: %s: site URL */ sprintf( __( 'URL: %s', '007aj-backup-restore-verifier' ), $site_url ), /* translators: %d: job ID */ sprintf( __( 'Job ID: %d', '007aj-backup-restore-verifier' ), $job_id ), /* translators: %s: job type */ sprintf( __( 'Type: %s', '007aj-backup-restore-verifier' ), $type ), /* translators: %s: job phase */ sprintf( __( 'Phase: %s', '007aj-backup-restore-verifier' ), $phase ), '', __( 'Error summary:', '007aj-backup-restore-verifier' ), $error, '', __( 'Recent log lines:', '007aj-backup-restore-verifier' ), self::recent_log_text( $job_id ), '', __( 'This message was sent by 007AJ Backup Restore Verifier via wp_mail. No external email service is used.', '007aj-backup-restore-verifier' ), ); $body = implode( "\n", $body_lines ); /** * Filter failure mail before send. * * @param array{to:string,subject:string,body:string,job_id:int} $mail Mail payload. * @param array $job Job row. */ $mail = apply_filters( 'brverify_failure_mail', array( 'to' => $to, 'subject' => $subject, 'body' => $body, 'job_id' => $job_id, ), $job ); $sent = wp_mail( (string) $mail['to'], (string) $mail['subject'], (string) $mail['body'], array( 'Content-Type: text/plain; charset=UTF-8' ) ); // Guard for 24h even if send failed, to avoid mail storms on cron retries. set_transient( $guard, 1, DAY_IN_SECONDS ); if ( $sent ) { Logger::info( $job_id, 'Failure email sent to ' . (string) $mail['to'] ); return array( 'ok' => true, 'sent' => true, ); } Logger::warning( $job_id, 'Failure email wp_mail returned false.' ); return array( 'ok' => false, 'sent' => false, 'error' => 'wp_mail returned false', ); } /** * Last warning/error log lines for context. * * @param int $job_id Job id. */ private static function recent_log_text( int $job_id ): string { $out = array(); $rows = Logger::recent( $job_id, 15 ); foreach ( is_array( $rows ) ? $rows : array() as $row ) { if ( ! is_array( $row ) ) { continue; } $out[] = '- [' . (string) ( $row['level'] ?? '' ) . '] ' . wp_strip_all_tags( (string) ( $row['message'] ?? '' ) ); } return implode( "\n", $out ); } }