settings = Plugin::get_settings( 'updates-and-notifications' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( isset( $this->settings['email_failure_alert_enabled'] ) && true === (bool) $this->settings['email_failure_alert_enabled'] ) { add_action( 'wp_mail_failed', [ $this, 'record_failure' ] ); add_action( 'admin_notices', [ $this, 'maybe_render_notice' ] ); add_action( 'admin_post_' . self::DISMISS_ACTION, [ $this, 'handle_dismiss' ] ); } } /** * Adds the email failure alert field to the Email tab. It is still stored under updates-and-notifications. * * @param array $fields An array of existing settings fields, keyed by tab. * * @return array The modified array of settings fields. */ public function adminease_settings_fields( array $fields ): array { $fields['email']['fields'][] = [ 'type' => 'switch', 'id' => 'email-failure-alert-enabled', 'name' => 'adminease[updates-and-notifications][email_failure_alert_enabled]', 'value' => $this->settings['email_failure_alert_enabled'] ?? '', 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'label' => __( 'Email failure alert', 'adminease' ), 'description' => __( 'A failed email makes no sound. Nothing breaks, nothing turns red, nobody is told. The order simply goes unconfirmed, the customer never gets their password reset link, and you hear about it days later from someone who gave up waiting.

Turn this on and WordPress tells you the moment a message fails. A red notice appears at the top of your admin screens with the address it was going to and the exact reason the mail server gave, plus a link straight to the Email Log so you can see the rest of the story.

💡 Why a notice and not an email? Because when your email is broken, an email telling you your email is broken goes nowhere.', 'adminease' ), ]; return $fields; } /** * Records the most recent mail failure so the next admin page load can show a notice. * * Unlike the alert email this replaced, this writes directly to an option instead of * deferring to shutdown: there is no wp_mail() call here to recurse back through * wp_mail_failed, and no cooldown transient to sequence ahead of a second mail round trip, * so there is nothing left that a mid-request write could get wrong. * * @param WP_Error $error The error object passed by the wp_mail_failed action. * * @return void */ public function record_failure( $error ): void { if( !$error instanceof WP_Error ) { return; } $error_data = $error->get_error_data(); $recipients = $this->normalize_recipients( $error_data['to'] ?? '' ); if( empty( $recipients ) ) { return; } if( true !== (bool) apply_filters( 'adminease_email_failure_alert_should_send', true, $error ) ) { return; } $previous = get_option( self::NOTICE_OPTION ); update_option( self::NOTICE_OPTION, [ 'to' => implode( ', ', $recipients ), 'error' => $error->get_error_message(), 'time' => time(), 'dismissed_at' => is_array( $previous ) ? (int) ( $previous['dismissed_at'] ?? 0 ) : 0, ] ); } /** * Renders the admin notice for the most recently recorded failure, unless it has already * been dismissed since it happened. * * @return void */ public function maybe_render_notice(): void { if( !current_user_can( 'manage_options' ) ) { return; } $notice = get_option( self::NOTICE_OPTION ); if( !is_array( $notice ) || empty( $notice['time'] ) ) { return; } if( (int) ( $notice['dismissed_at'] ?? 0 ) >= (int) $notice['time'] ) { return; } $email_log_url = admin_url( 'admin.php?page=' . ADMINEASE_SLUG . '#tab-debug:email-log' ); ?>


settings['smtp_enabled'] ) && true === (bool) $this->settings['smtp_enabled'] ) { ?>  |   | 

self::DISMISS_ACTION, '_wp_http_referer' => $referer, ], admin_url( 'admin-post.php' ) ); return wp_nonce_url( $url, self::DISMISS_ACTION ); } /** * Marks the current failure as dismissed and returns the admin to where they were. * A real, server-persisted dismiss rather than a client-side "is-dismissible" notice, so the * choice survives the next page load instead of reappearing immediately after. * * @return void */ public function handle_dismiss(): void { if( !current_user_can( 'manage_options' ) || !check_admin_referer( self::DISMISS_ACTION ) ) { wp_die( esc_html__( 'Invalid request.', 'adminease' ) ); } $notice = get_option( self::NOTICE_OPTION, [] ); $notice['dismissed_at'] = time(); update_option( self::NOTICE_OPTION, $notice ); wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url() ); exit; } }