` + EOL + ``, or an empty string when
* the record does not exist or has no error_message.
*
* @since 4.9.0
*
* @param string $connection_id Connection id.
*
* @return string
*/
public static function get_message( $connection_id ) {
return self::format_record( self::get( $connection_id ) );
}
/**
* Return formatted failure messages for every connection with a non-empty
* `error_message`. Each entry is formatted as `Mailer: ` + EOL +
* ``.
*
* @since 4.9.0
*
* @return string[]
*/
public static function get_messages() {
$messages = [];
foreach ( self::get() as $record ) {
$formatted = self::format_record( $record );
if ( $formatted !== '' ) {
$messages[] = $formatted;
}
}
return $messages;
}
/**
* Build the `Mailer: ` + EOL + `` string for a single
* failure record. Returns an empty string when the record is empty or has
* no `error_message`.
*
* @since 4.9.0
*
* @param array $record Failure-record payload.
*
* @return string
*/
private static function format_record( $record ) {
if ( empty( $record['error_message'] ) ) {
return '';
}
$options = ! empty( $record['mailer'] )
? wp_mail_smtp()->get_providers()->get_options( $record['mailer'] )
: null;
$mailer_title = ! empty( $options )
? $options->get_title()
: esc_html__( 'Unknown', 'wp-mail-smtp' );
return 'Mailer: ' . $mailer_title . WP::EOL . (string) $record['error_message'];
}
/**
* Read failure records. Returns the full map when `$connection_id` is null,
* otherwise that connection's record (or an empty array when none stored).
*
* @since 4.9.0
*
* @param string|null $connection_id Optional connection id.
*
* @return array
*/
public static function get( $connection_id = null ) {
$all = self::get_raw();
if ( $connection_id === null ) {
return $all;
}
return isset( $all[ $connection_id ] ) ? $all[ $connection_id ] : [];
}
/**
* Raw read with in-memory caching.
*
* @since 4.9.0
*
* @return array
*/
private static function get_raw() {
if ( self::$cached !== null ) {
return self::$cached;
}
$all = get_option( self::OPTION_KEY, [] );
if ( ! is_array( $all ) ) {
$all = [];
}
self::$cached = $all;
return $all;
}
}