Abilities
5 days ago
Admin
5 days ago
Compatibility
5 days ago
Helpers
5 days ago
Integrations
5 days ago
Providers
5 days ago
Queue
5 days ago
Reports
5 days ago
Tasks
5 days ago
TestEmail
5 days ago
UsageTracking
5 days ago
WPCLI
5 days ago
AbstractConnection.php
5 days ago
Conflicts.php
5 days ago
Connect.php
5 days ago
Connection.php
5 days ago
ConnectionInterface.php
5 days ago
ConnectionsManager.php
5 days ago
Core.php
5 days ago
DBRepair.php
5 days ago
Debug.php
5 days ago
EmailSendingDebug.php
5 days ago
Geo.php
5 days ago
MailCatcher.php
5 days ago
MailCatcherInterface.php
5 days ago
MailCatcherTrait.php
5 days ago
MailCatcherV6.php
5 days ago
Migration.php
5 days ago
MigrationAbstract.php
5 days ago
Migrations.php
5 days ago
OptimizedEmailSending.php
5 days ago
Options.php
5 days ago
Processor.php
5 days ago
SiteHealth.php
5 days ago
Upgrade.php
5 days ago
Uploads.php
5 days ago
WP.php
5 days ago
WPMailArgs.php
5 days ago
WPMailInitiator.php
5 days ago
EmailSendingDebug.php
229 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP; |
| 4 | |
| 5 | /** |
| 6 | * Per-connection email-sending-failure store. |
| 7 | * |
| 8 | * @since 4.9.0 |
| 9 | */ |
| 10 | class EmailSendingDebug { |
| 11 | |
| 12 | /** |
| 13 | * Option key for the per-connection failure map. |
| 14 | * |
| 15 | * @since 4.9.0 |
| 16 | */ |
| 17 | const OPTION_KEY = 'wp_mail_smtp_email_sending_debug'; |
| 18 | |
| 19 | /** |
| 20 | * In-memory cache to avoid repeated option reads in one request. |
| 21 | * |
| 22 | * @since 4.9.0 |
| 23 | * |
| 24 | * @var array|null |
| 25 | */ |
| 26 | private static $cached = null; |
| 27 | |
| 28 | /** |
| 29 | * Write or overwrite the failure record for a single connection. |
| 30 | * |
| 31 | * @since 4.9.0 |
| 32 | * |
| 33 | * @param string $connection_id Connection id ('primary' or an additional connection's id). |
| 34 | * @param array $record Failure-record payload. |
| 35 | */ |
| 36 | public static function set( $connection_id, $record ) { |
| 37 | |
| 38 | if ( empty( $connection_id ) ) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | $all = self::get_raw(); |
| 43 | $all[ $connection_id ] = $record; |
| 44 | self::$cached = $all; |
| 45 | |
| 46 | update_option( self::OPTION_KEY, $all, false ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Merge partial fields into the existing failure record for a single connection. |
| 51 | * |
| 52 | * Unlike {@see self::set()}, merge annotates an already-stored record - it does |
| 53 | * not create one when none exists. Provided fields overwrite stored ones; every |
| 54 | * other field is preserved. |
| 55 | * |
| 56 | * @since 4.9.0 |
| 57 | * |
| 58 | * @param string $connection_id Connection id ('primary' or an additional connection's id). |
| 59 | * @param array $partial_record Fields to merge over the existing record. |
| 60 | */ |
| 61 | public static function merge( $connection_id, $partial_record ) { |
| 62 | |
| 63 | if ( empty( $connection_id ) || empty( $partial_record ) ) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | $all = self::get_raw(); |
| 68 | |
| 69 | if ( empty( $all[ $connection_id ] ) ) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | $all[ $connection_id ] = array_merge( $all[ $connection_id ], $partial_record ); |
| 74 | self::$cached = $all; |
| 75 | |
| 76 | update_option( self::OPTION_KEY, $all, false ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Remove the failure record for a single connection. |
| 81 | * |
| 82 | * @since 4.9.0 |
| 83 | * |
| 84 | * @param string $connection_id Connection id. |
| 85 | */ |
| 86 | public static function clear( $connection_id ) { |
| 87 | |
| 88 | if ( empty( $connection_id ) ) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | $all = self::get_raw(); |
| 93 | |
| 94 | if ( ! array_key_exists( $connection_id, $all ) ) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | unset( $all[ $connection_id ] ); |
| 99 | self::$cached = $all; |
| 100 | |
| 101 | update_option( self::OPTION_KEY, $all, false ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Wipe every connection's failure record. |
| 106 | * |
| 107 | * @since 4.9.0 |
| 108 | */ |
| 109 | public static function clear_all() { |
| 110 | |
| 111 | self::$cached = []; |
| 112 | |
| 113 | update_option( self::OPTION_KEY, [], false ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Return a single connection's failure as a human-readable string in the |
| 118 | * form `Mailer: <title>` + EOL + `<error_message>`, or an empty string when |
| 119 | * the record does not exist or has no error_message. |
| 120 | * |
| 121 | * @since 4.9.0 |
| 122 | * |
| 123 | * @param string $connection_id Connection id. |
| 124 | * |
| 125 | * @return string |
| 126 | */ |
| 127 | public static function get_message( $connection_id ) { |
| 128 | |
| 129 | return self::format_record( self::get( $connection_id ) ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Return formatted failure messages for every connection with a non-empty |
| 134 | * `error_message`. Each entry is formatted as `Mailer: <title>` + EOL + |
| 135 | * `<error_message>`. |
| 136 | * |
| 137 | * @since 4.9.0 |
| 138 | * |
| 139 | * @return string[] |
| 140 | */ |
| 141 | public static function get_messages() { |
| 142 | |
| 143 | $messages = []; |
| 144 | |
| 145 | foreach ( self::get() as $record ) { |
| 146 | $formatted = self::format_record( $record ); |
| 147 | |
| 148 | if ( $formatted !== '' ) { |
| 149 | $messages[] = $formatted; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return $messages; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Build the `Mailer: <title>` + EOL + `<error_message>` string for a single |
| 158 | * failure record. Returns an empty string when the record is empty or has |
| 159 | * no `error_message`. |
| 160 | * |
| 161 | * @since 4.9.0 |
| 162 | * |
| 163 | * @param array $record Failure-record payload. |
| 164 | * |
| 165 | * @return string |
| 166 | */ |
| 167 | private static function format_record( $record ) { |
| 168 | |
| 169 | if ( empty( $record['error_message'] ) ) { |
| 170 | return ''; |
| 171 | } |
| 172 | |
| 173 | $options = ! empty( $record['mailer'] ) |
| 174 | ? wp_mail_smtp()->get_providers()->get_options( $record['mailer'] ) |
| 175 | : null; |
| 176 | |
| 177 | $mailer_title = ! empty( $options ) |
| 178 | ? $options->get_title() |
| 179 | : esc_html__( 'Unknown', 'wp-mail-smtp' ); |
| 180 | |
| 181 | return 'Mailer: ' . $mailer_title . WP::EOL . (string) $record['error_message']; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Read failure records. Returns the full map when `$connection_id` is null, |
| 186 | * otherwise that connection's record (or an empty array when none stored). |
| 187 | * |
| 188 | * @since 4.9.0 |
| 189 | * |
| 190 | * @param string|null $connection_id Optional connection id. |
| 191 | * |
| 192 | * @return array |
| 193 | */ |
| 194 | public static function get( $connection_id = null ) { |
| 195 | |
| 196 | $all = self::get_raw(); |
| 197 | |
| 198 | if ( $connection_id === null ) { |
| 199 | return $all; |
| 200 | } |
| 201 | |
| 202 | return isset( $all[ $connection_id ] ) ? $all[ $connection_id ] : []; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Raw read with in-memory caching. |
| 207 | * |
| 208 | * @since 4.9.0 |
| 209 | * |
| 210 | * @return array |
| 211 | */ |
| 212 | private static function get_raw() { |
| 213 | |
| 214 | if ( self::$cached !== null ) { |
| 215 | return self::$cached; |
| 216 | } |
| 217 | |
| 218 | $all = get_option( self::OPTION_KEY, [] ); |
| 219 | |
| 220 | if ( ! is_array( $all ) ) { |
| 221 | $all = []; |
| 222 | } |
| 223 | |
| 224 | self::$cached = $all; |
| 225 | |
| 226 | return $all; |
| 227 | } |
| 228 | } |
| 229 |