Admin
6 months ago
Compatibility
6 months ago
Helpers
6 months ago
Providers
6 months ago
Queue
6 months ago
Reports
6 months ago
Tasks
6 months ago
UsageTracking
6 months ago
AbstractConnection.php
6 months ago
Conflicts.php
6 months ago
Connect.php
6 months ago
Connection.php
6 months ago
ConnectionInterface.php
6 months ago
ConnectionsManager.php
6 months ago
Core.php
6 months ago
DBRepair.php
6 months ago
Debug.php
6 months ago
Geo.php
6 months ago
MailCatcher.php
6 months ago
MailCatcherInterface.php
6 months ago
MailCatcherTrait.php
6 months ago
MailCatcherV6.php
6 months ago
Migration.php
6 months ago
MigrationAbstract.php
6 months ago
Migrations.php
6 months ago
OptimizedEmailSending.php
6 months ago
Options.php
6 months ago
Processor.php
6 months ago
SiteHealth.php
6 months ago
Upgrade.php
6 months ago
Uploads.php
6 months ago
WP.php
6 months ago
WPMailArgs.php
6 months ago
WPMailInitiator.php
6 months ago
WPMailArgs.php
253 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP; |
| 4 | |
| 5 | /** |
| 6 | * Class WPMailArgs. This class responsible for `wp_mail` function arguments parsing. |
| 7 | * |
| 8 | * Parsing algorithms copied from `wp_mail` function. |
| 9 | * |
| 10 | * @since 3.7.0 |
| 11 | */ |
| 12 | class WPMailArgs { |
| 13 | |
| 14 | /** |
| 15 | * Array of the `wp_mail` function arguments. |
| 16 | * |
| 17 | * @since 3.7.0 |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | private $args; |
| 22 | |
| 23 | /** |
| 24 | * Parsed headers. |
| 25 | * |
| 26 | * @since 3.7.0 |
| 27 | * |
| 28 | * @var array |
| 29 | */ |
| 30 | private $headers = null; |
| 31 | |
| 32 | /** |
| 33 | * Constructor. |
| 34 | * |
| 35 | * @since 3.7.0 |
| 36 | * |
| 37 | * @param array $args { |
| 38 | * Array of the `wp_mail` function arguments. |
| 39 | * |
| 40 | * @type string|string[] $to Array or comma-separated list of email addresses to send message. |
| 41 | * @type string $subject Email subject. |
| 42 | * @type string $message Message contents. |
| 43 | * @type string|string[] $headers Additional headers. |
| 44 | * @type string|string[] $attachments Paths to files to attach. |
| 45 | * } |
| 46 | */ |
| 47 | public function __construct( $args ) { |
| 48 | |
| 49 | $this->args = $args; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get arguments. |
| 54 | * |
| 55 | * @since 3.7.0 |
| 56 | * |
| 57 | * @return array |
| 58 | */ |
| 59 | public function get_args() { |
| 60 | |
| 61 | return $this->args; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get to email. |
| 66 | * |
| 67 | * @since 3.7.0 |
| 68 | * |
| 69 | * @return string |
| 70 | */ |
| 71 | public function get_to_email() { |
| 72 | |
| 73 | return $this->get_arg( 'to' ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get subject. |
| 78 | * |
| 79 | * @since 3.7.0 |
| 80 | * |
| 81 | * @return string |
| 82 | */ |
| 83 | public function get_subject() { |
| 84 | |
| 85 | return $this->get_arg( 'subject' ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get message. |
| 90 | * |
| 91 | * @since 3.7.0 |
| 92 | * |
| 93 | * @return string |
| 94 | */ |
| 95 | public function get_message() { |
| 96 | |
| 97 | return $this->get_arg( 'message' ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get from email. |
| 102 | * |
| 103 | * @since 3.7.0 |
| 104 | * |
| 105 | * @return string |
| 106 | */ |
| 107 | public function get_from_email() { |
| 108 | |
| 109 | $from = $this->get_from(); |
| 110 | |
| 111 | return $from['email']; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get from name. |
| 116 | * |
| 117 | * @since 3.7.0 |
| 118 | * |
| 119 | * @return string |
| 120 | */ |
| 121 | public function get_from_name() { |
| 122 | |
| 123 | $from = $this->get_from(); |
| 124 | |
| 125 | return $from['name']; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get parsed headers. |
| 130 | * |
| 131 | * @since 3.7.0 |
| 132 | * |
| 133 | * @return array |
| 134 | */ |
| 135 | public function get_headers() { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh |
| 136 | |
| 137 | if ( ! is_null( $this->headers ) ) { |
| 138 | return $this->headers; |
| 139 | } |
| 140 | |
| 141 | $this->headers = []; |
| 142 | |
| 143 | if ( ! empty( $this->args['headers'] ) ) { |
| 144 | if ( ! is_array( $this->args['headers'] ) ) { |
| 145 | $headers = explode( "\n", str_replace( "\r\n", "\n", $this->args['headers'] ) ); |
| 146 | } else { |
| 147 | $headers = $this->args['headers']; |
| 148 | } |
| 149 | |
| 150 | foreach ( (array) $headers as $header ) { |
| 151 | if ( strpos( $header, ':' ) === false ) { |
| 152 | continue; |
| 153 | } |
| 154 | |
| 155 | list( $name, $content ) = array_map( 'trim', explode( ':', trim( $header ), 2 ) ); |
| 156 | |
| 157 | $name = strtolower( $name ); |
| 158 | |
| 159 | if ( isset( $this->headers[ $name ] ) && in_array( $name, [ 'cc', 'bcc', 'reply-to' ], true ) ) { |
| 160 | $this->headers[ $name ] .= ', ' . $content; |
| 161 | } else { |
| 162 | $this->headers[ $name ] = $content; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return $this->headers; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Get particular header value. |
| 172 | * |
| 173 | * @since 3.7.0 |
| 174 | * |
| 175 | * @param string $name Header name. |
| 176 | * |
| 177 | * @return null|string |
| 178 | */ |
| 179 | public function get_header( $name ) { |
| 180 | |
| 181 | $name = strtolower( $name ); |
| 182 | $headers = $this->get_headers(); |
| 183 | |
| 184 | return isset( $headers[ $name ] ) ? $headers[ $name ] : null; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Get argument value. |
| 189 | * |
| 190 | * @since 3.7.0 |
| 191 | * |
| 192 | * @param string $key Argument key. |
| 193 | * @param mixed $default Default value. |
| 194 | * |
| 195 | * @return string |
| 196 | */ |
| 197 | private function get_arg( $key, $default = '' ) { |
| 198 | |
| 199 | return isset( $this->args[ $key ] ) ? $this->args[ $key ] : $default; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Get from address. |
| 204 | * |
| 205 | * @since 3.7.0 |
| 206 | * |
| 207 | * @return array |
| 208 | */ |
| 209 | private function get_from() { |
| 210 | |
| 211 | $from_email = ''; |
| 212 | $from_name = ''; |
| 213 | $value = $this->get_header( 'from' ); |
| 214 | $value = is_null( $value ) ? '' : $value; |
| 215 | |
| 216 | $bracket_pos = strpos( $value, '<' ); |
| 217 | |
| 218 | if ( $bracket_pos !== false ) { |
| 219 | // Text before the bracketed email is the "From" name. |
| 220 | if ( $bracket_pos > 0 ) { |
| 221 | $from_name = substr( $value, 0, $bracket_pos - 1 ); |
| 222 | $from_name = str_replace( '"', '', $from_name ); |
| 223 | $from_name = trim( $from_name ); |
| 224 | } |
| 225 | |
| 226 | $from_email = substr( $value, $bracket_pos + 1 ); |
| 227 | $from_email = str_replace( '>', '', $from_email ); |
| 228 | $from_email = trim( $from_email ); |
| 229 | |
| 230 | // Avoid setting an empty $from_email. |
| 231 | } elseif ( trim( $value ) !== '' ) { |
| 232 | $from_email = trim( $value ); |
| 233 | } |
| 234 | |
| 235 | return [ |
| 236 | 'email' => $from_email, |
| 237 | 'name' => $from_name, |
| 238 | ]; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Get attachments. |
| 243 | * |
| 244 | * @since 4.0.0 |
| 245 | * |
| 246 | * @return array |
| 247 | */ |
| 248 | public function get_attachments() { |
| 249 | |
| 250 | return $this->get_arg( 'attachments', [] ); |
| 251 | } |
| 252 | } |
| 253 |