sync_cleanup_cron( self::is_enabled() ); } /** * Record an email as it's about to be sent. * * @param array $args The compact() of to/subject/message/headers/attachments wp_mail() is about to send. * @return array Unchanged -- this is an observer, not a transform. */ public function log_mail( array $args ): array { if ( ! self::is_enabled() ) { return $args; } $headers = $this->headers_to_string( $args['headers'] ?? '' ); $log_body = $this->log_body_enabled(); $this->last_insert_id = LogRepository::insert( array( 'to_address' => $this->to_address_to_string( $args['to'] ?? '' ), 'subject' => (string) ( $args['subject'] ?? '' ), 'body' => $log_body ? (string) ( $args['message'] ?? '' ) : null, 'headers' => $log_body ? $headers : null, 'is_html' => false !== stripos( $headers, 'text/html' ), ) ); return $args; } /** * Mark the most recently logged email as failed. * * @param \WP_Error $error The error wp_mail_failed is fired with. * @return void */ public function log_failure( \WP_Error $error ): void { if ( ! self::is_enabled() || null === $this->last_insert_id ) { return; } LogRepository::mark_failed( $this->last_insert_id, $error->get_error_message() ); $this->last_insert_id = null; } /** * Delete logged emails past the configured retention window. * * @return void */ public function run_cleanup(): void { LogRepository::delete_older_than( $this->retention_days() ); } /** * Schedule or unschedule the daily cleanup cron to match the toggle. * * @param bool $enabled Whether logging is on. * @return void */ private function sync_cleanup_cron( bool $enabled ): void { $scheduled = wp_next_scheduled( self::CLEANUP_CRON_HOOK ); if ( $enabled && ! $scheduled ) { wp_schedule_event( time(), 'daily', self::CLEANUP_CRON_HOOK ); } elseif ( ! $enabled && $scheduled ) { wp_clear_scheduled_hook( self::CLEANUP_CRON_HOOK ); } } /** * Whether email logging is on. Static (and public) so other classes -- * `Module`, `LogAdminPage` -- can gate on it without instantiating a * `Feature`. * * @return bool */ public static function is_enabled(): bool { return (bool) aiou_get_setting( 'emails', 'logging', 'enabled', 0 ); } /** * Whether the full subject/body/headers are stored, or just metadata * (recipient, subject, status). * * @return bool */ private function log_body_enabled(): bool { return (bool) aiou_get_setting( 'emails', 'logging', 'log_body', 0 ); } /** * Configured retention window in days (0 = keep forever). * * @return int */ private function retention_days(): int { return (int) aiou_get_setting( 'emails', 'logging', 'retention', 30 ); } /** * Normalize wp_mail()'s `to` (string or array) into one display string. * * @param string|array $to Raw recipient(s). * @return string */ private function to_address_to_string( $to ): string { return is_array( $to ) ? implode( ', ', $to ) : (string) $to; } /** * Normalize wp_mail()'s `headers` (string or array) into one string. * * @param string|array $headers Raw headers. * @return string */ private function headers_to_string( $headers ): string { return is_array( $headers ) ? implode( "\n", $headers ) : (string) $headers; } }