Admin
1 year ago
Compatibility
1 year ago
Helpers
1 year ago
Providers
1 year ago
Queue
1 year ago
Reports
1 year ago
Tasks
1 year ago
UsageTracking
1 year ago
AbstractConnection.php
1 year ago
Conflicts.php
1 year ago
Connect.php
1 year ago
Connection.php
1 year ago
ConnectionInterface.php
1 year ago
ConnectionsManager.php
1 year ago
Core.php
1 year ago
DBRepair.php
1 year ago
Debug.php
1 year ago
Geo.php
1 year ago
MailCatcher.php
1 year ago
MailCatcherInterface.php
1 year ago
MailCatcherTrait.php
1 year ago
MailCatcherV6.php
1 year ago
Migration.php
1 year ago
MigrationAbstract.php
1 year ago
Migrations.php
1 year ago
OptimizedEmailSending.php
1 year ago
Options.php
1 year ago
Processor.php
1 year ago
SiteHealth.php
1 year ago
Upgrade.php
1 year ago
Uploads.php
1 year ago
WP.php
1 year ago
WPMailArgs.php
1 year ago
WPMailInitiator.php
1 year ago
WPMailInitiator.php
272 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP; |
| 4 | |
| 5 | /** |
| 6 | * The `wp_mail` function initiator. It has centralized initiator data that can be used across all processes. |
| 7 | * |
| 8 | * @since 3.7.0 |
| 9 | */ |
| 10 | class WPMailInitiator { |
| 11 | |
| 12 | /** |
| 13 | * The path where the `wp_mail` function was called. |
| 14 | * |
| 15 | * @since 3.7.0 |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | private $file; |
| 20 | |
| 21 | /** |
| 22 | * Line in the file where the `wp_mail` function was called. |
| 23 | * |
| 24 | * @since 3.7.0 |
| 25 | * |
| 26 | * @var int |
| 27 | */ |
| 28 | private $line; |
| 29 | |
| 30 | /** |
| 31 | * The `wp_mail` function call backtrace. |
| 32 | * |
| 33 | * @since 3.7.0 |
| 34 | * |
| 35 | * @var array |
| 36 | */ |
| 37 | private $backtrace; |
| 38 | |
| 39 | /** |
| 40 | * Initiator name (plugin or theme name). |
| 41 | * |
| 42 | * @since 3.7.0 |
| 43 | * |
| 44 | * @var string |
| 45 | */ |
| 46 | private $name; |
| 47 | |
| 48 | /** |
| 49 | * Initiator type. Available options: plugin, mu-plugin, theme, wp-core, unknown. |
| 50 | * |
| 51 | * @since 3.7.0 |
| 52 | * |
| 53 | * @var string |
| 54 | */ |
| 55 | private $type; |
| 56 | |
| 57 | /** |
| 58 | * Initiator slug (plugin or theme slug). |
| 59 | * |
| 60 | * @since 3.7.0 |
| 61 | * |
| 62 | * @var string |
| 63 | */ |
| 64 | private $slug; |
| 65 | |
| 66 | /** |
| 67 | * Whether performance-costly properties were initialized. |
| 68 | * |
| 69 | * @since 3.7.0 |
| 70 | * |
| 71 | * @var array |
| 72 | */ |
| 73 | private $initialized = false; |
| 74 | |
| 75 | /** |
| 76 | * Register hooks. |
| 77 | * |
| 78 | * @since 3.7.0 |
| 79 | */ |
| 80 | public function hooks() { |
| 81 | |
| 82 | // Capture `wp_mail` function call. |
| 83 | add_action( 'wp_mail_smtp_processor_capture_wp_mail_call', [ $this, 'capture_wp_mail_call' ], 0 ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Capture `wp_mail` function call. |
| 88 | * |
| 89 | * @since 4.0.0 |
| 90 | */ |
| 91 | public function capture_wp_mail_call() { |
| 92 | |
| 93 | $this->set_initiator(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get the path where the `wp_mail` function was called. |
| 98 | * |
| 99 | * @since 3.7.0 |
| 100 | * |
| 101 | * @return string |
| 102 | */ |
| 103 | public function get_file() { |
| 104 | |
| 105 | return $this->file; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get the line in the file where the `wp_mail` function was called. |
| 110 | * |
| 111 | * @since 3.7.0 |
| 112 | * |
| 113 | * @return int |
| 114 | */ |
| 115 | public function get_line() { |
| 116 | |
| 117 | return $this->line; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get the `wp_mail` function call backtrace. |
| 122 | * |
| 123 | * @since 3.7.0 |
| 124 | * |
| 125 | * @return array |
| 126 | */ |
| 127 | public function get_backtrace() { |
| 128 | |
| 129 | return $this->backtrace; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Get the initiator name (plugin or theme name). |
| 134 | * |
| 135 | * @since 3.7.0 |
| 136 | * |
| 137 | * @return string |
| 138 | */ |
| 139 | public function get_name() { |
| 140 | |
| 141 | $this->lazy_init(); |
| 142 | |
| 143 | return $this->name; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Get the initiator type. Available options: plugin, mu-plugin, theme, wp-core, unknown. |
| 148 | * |
| 149 | * @since 3.7.0 |
| 150 | * |
| 151 | * @return string |
| 152 | */ |
| 153 | public function get_type() { |
| 154 | |
| 155 | $this->lazy_init(); |
| 156 | |
| 157 | return $this->type; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Get the initiator slug (plugin or theme slug). |
| 162 | * |
| 163 | * @since 3.7.0 |
| 164 | * |
| 165 | * @return string |
| 166 | */ |
| 167 | public function get_slug() { |
| 168 | |
| 169 | $this->lazy_init(); |
| 170 | |
| 171 | return $this->slug; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Initialize initiator data. |
| 176 | * |
| 177 | * @since 3.7.0 |
| 178 | */ |
| 179 | public function set_initiator() { |
| 180 | |
| 181 | // Reset previous values. |
| 182 | $this->reset(); |
| 183 | |
| 184 | $backtrace = $this->get_wpmail_backtrace(); |
| 185 | |
| 186 | /** |
| 187 | * Filter the `wp_mail` function initiator data. |
| 188 | * |
| 189 | * @since 4.0.0 |
| 190 | * |
| 191 | * @param array $backtrace Backtrace data. |
| 192 | */ |
| 193 | $backtrace = apply_filters( 'wp_mail_smtp_wp_mail_initiator_set_initiator', $backtrace ); |
| 194 | |
| 195 | if ( empty( $backtrace['file'] ) ) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | $this->file = $backtrace['file']; |
| 200 | $this->backtrace = $backtrace['backtrace']; |
| 201 | |
| 202 | if ( ! empty( $backtrace['line'] ) ) { |
| 203 | $this->line = $backtrace['line']; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Initialize performance-costly properties. |
| 209 | * |
| 210 | * @since 3.7.0 |
| 211 | */ |
| 212 | private function lazy_init() { |
| 213 | |
| 214 | if ( empty( $this->file ) || $this->initialized ) { |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | $data = WP::get_initiator( $this->file ); |
| 219 | |
| 220 | $this->name = $data['name']; |
| 221 | $this->type = $data['type']; |
| 222 | |
| 223 | if ( isset( $data['slug'] ) ) { |
| 224 | $this->slug = $data['slug']; |
| 225 | } |
| 226 | |
| 227 | $this->initialized = true; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Reset previous initiator data before the new email sending. |
| 232 | * |
| 233 | * @since 3.7.0 |
| 234 | */ |
| 235 | private function reset() { |
| 236 | |
| 237 | $this->initialized = false; |
| 238 | $this->file = null; |
| 239 | $this->line = null; |
| 240 | $this->backtrace = null; |
| 241 | $this->name = null; |
| 242 | $this->type = null; |
| 243 | $this->slug = null; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Get the `wp_mail` function backtrace data, if it exists. |
| 248 | * |
| 249 | * @since 3.7.0 |
| 250 | * |
| 251 | * @return array |
| 252 | */ |
| 253 | private function get_wpmail_backtrace() { |
| 254 | |
| 255 | $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
| 256 | |
| 257 | foreach ( $backtrace as $i => $item ) { |
| 258 | if ( $item['function'] === 'wp_mail' ) { |
| 259 | if ( isset( $item['function'] ) ) { |
| 260 | unset( $item['function'] ); |
| 261 | } |
| 262 | |
| 263 | $item['backtrace'] = array_slice( $backtrace, $i ); |
| 264 | |
| 265 | return $item; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return []; |
| 270 | } |
| 271 | } |
| 272 |