PluginProbe
Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe / trunk
Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe vtrunk
4.17.3 trunk 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 4.10.0 4.11.1 4.12.2 4.14.1 4.14.2 4.14.3 4.15.0 4.16.0 All 59 releases
stripe / src / Emails / Mailer.php

Mailer.php in Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe trunk, at src/Emails/Mailer.php

374 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Emails: Mailer
4 *
5 * @package SimplePay
6 * @copyright Copyright (c) 2023, Sandhills Development, LLC
7 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
8 * @since 4.7.3
9 */
10
11 namespace SimplePay\Core\Emails;
12
13 use SimplePay\Core\Payments\Payment_Confirmation;
14 use SimplePay\Vendor\TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit;
18 }
19
20 /**
21 * Mailer class.
22 *
23 * @since 4.7.3
24 */
25 class Mailer {
26
27 /**
28 * The email being processed.
29 *
30 * @since 4.7.3
31 * @var \SimplePay\Core\Emails\Email\EmailInterface
32 */
33 protected $email;
34
35 /**
36 * Whether the mail is being sent in Live or Test Mode.
37 *
38 * @since 4.7.3
39 * @var bool
40 */
41 protected $is_livemode;
42
43 /**
44 * The data associated with the email.
45 *
46 * @since 4.7.3
47 * @var array<string, mixed>
48 */
49 protected $data;
50
51 /**
52 * The address(es) to send the email to.
53 *
54 * @since 4.7.3
55 * @var string|array<string>
56 */
57 protected $to;
58
59 /**
60 * The subject of the email.
61 *
62 * @since 4.7.3
63 * @var string
64 */
65 protected $subject;
66
67 /**
68 * The content of the email.
69 *
70 * @since 4.7.3
71 * @var string
72 */
73 protected $body;
74
75 /**
76 * Sets the email to be processed.
77 *
78 * @since 4.7.3
79 *
80 * @param \SimplePay\Core\Emails\Email\EmailInterface $email The email to be processed.
81 */
82 public function __construct( $email ) {
83 $this->email = $email;
84 }
85
86 /**
87 * Returns the payment mode.
88 *
89 * @since 4.7.3
90 *
91 * @return bool
92 */
93 public function is_livemode() {
94 if ( is_null( $this->is_livemode ) ) {
95 $this->is_livemode = simpay_is_livemode();
96 }
97
98 return $this->is_livemode;
99 }
100
101 /**
102 * Sets the relevant payment mode.
103 *
104 * @since 4.7.3
105 *
106 * @param bool $is_livemode Whether the mail is being sent in Live or Test Mode.
107 * @return void
108 */
109 public function set_livemode( $is_livemode ) {
110 $this->is_livemode = $is_livemode;
111 }
112
113 /**
114 * Returns the associated payment data.
115 *
116 * @since 4.7.3
117 *
118 * @return array<string, mixed>
119 */
120 public function get_data() {
121 return $this->data;
122 }
123
124 /**
125 * Sets the associated payment data.
126 *
127 * @since 4.7.3
128 *
129 * @param array<string, mixed> $data Payment data.
130 * @return void
131 */
132 public function set_data( $data ) {
133 $this->data = $data;
134 }
135
136 /**
137 * Returns email header information.
138 *
139 * Pulls name and address from stored settings.
140 *
141 * @since 4.7.3
142 *
143 * @return string
144 */
145 private function get_headers() {
146 $name = $this->get_from_name();
147 $address = $this->get_from_address();
148 $content_type = $this->get_content_type();
149
150 $headers = sprintf( "From: %s <%s>\r\n", $name, $address );
151 $headers .= sprintf(
152 "Content-Type: %s; charset=utf-8\r\n",
153 $content_type
154 );
155
156 return $headers;
157 }
158
159 /**
160 * Returns the address(es) to send the email to.
161 *
162 * @since 4.7.3
163 *
164 * @return string|array<string>
165 */
166 public function get_to() {
167 return $this->to;
168 }
169
170 /**
171 * Sets the address(es) to send the email to.
172 *
173 * @since 4.7.3
174 *
175 * @param string|array<string> $to The address(es) to send the email to.
176 * @return void
177 */
178 public function set_to( $to ) {
179 $this->to = $to;
180 }
181
182 /**
183 * Returns the subject of the email.
184 *
185 * @since 4.7.3
186 *
187 * @return string
188 */
189 public function get_subject() {
190 if ( true === $this->is_livemode() ) {
191 $subject = $this->subject;
192 } else {
193 $subject = sprintf(
194 /* translators: %s Email subject */
195 __( '[Test Mode] %s', 'stripe' ),
196 $this->subject
197 );
198 }
199
200 return html_entity_decode( $this->parse_smart_tags( $subject ) );
201 }
202
203 /**
204 * Sets the subject of the email.
205 *
206 * @since 4.7.3
207 *
208 * @param string $subject The subject of the email.
209 * @return void
210 */
211 public function set_subject( $subject ) {
212 $this->subject = $subject;
213 }
214
215 /**
216 * Returns the HTML for the email (body).
217 *
218 * @since 4.7.3
219 *
220 * @return string
221 */
222 public function get_body() {
223 $template = $this->email->get_template();
224
225 $autop = true;
226
227 /**
228 * Determines if an email's message body should have wpautop applied.
229 *
230 * @since 4.0.0
231 *
232 * @param bool $autop Determines if an email's message body should have
233 * wpautop applied.
234 */
235 $autop = apply_filters( 'simpay_emails_autop', $autop );
236
237 $body = $this->parse_smart_tags( $this->body );
238 $body = $autop ? wpautop( $body ) : $body;
239
240 $css_to_inline_styles = new CssToInlineStyles();
241
242 return $css_to_inline_styles->convert(
243 sprintf(
244 "%s\n%s\n%s",
245 $template->get_header( $this->email->get_header_content() ),
246 $template->get_body( $body ),
247 $template->get_footer( $this->email->get_footer_content() )
248 ),
249 $template->get_styles()
250 );
251 }
252
253 /**
254 * Sets the HTML for the email (body).
255 *
256 * @since 4.7.3
257 *
258 * @param string $body The HTML for the email (body).
259 * @return void
260 */
261 public function set_body( $body ) {
262 $this->body = $body;
263 }
264
265 /**
266 * Sends an email.
267 *
268 * @since 4.7.3
269 *
270 * @return bool
271 */
272 public function send() {
273 $this->send_before();
274
275 $sent = wp_mail(
276 $this->get_to(),
277 $this->get_subject(),
278 $this->get_body(),
279 $this->get_headers()
280 );
281
282 $this->send_after();
283
284 return $sent;
285 }
286
287 /**
288 * Returns the email address used to send emails.
289 *
290 * @since 4.7.3
291 *
292 * @return string
293 */
294 public function get_from_address() {
295 /** @var string $default */
296 $default = get_site_option( 'admin_email', '' );
297
298 /** @var string $address */
299 $address = simpay_get_setting( 'email_from_address', $default );
300
301 return $address;
302 }
303
304 /**
305 * Returns the name used to send emails.
306 *
307 * @since 4.7.3
308 *
309 * @return string
310 */
311 public function get_from_name() {
312 /** @var string $default */
313 $default = get_site_option( 'blogname', '' );
314
315 /** @var string $name */
316 $name = simpay_get_setting( 'email_from_name', $default );
317
318 return wp_specialchars_decode( $name, ENT_QUOTES );
319 }
320
321 /**
322 * Returns the content type used to send HTML emails.
323 *
324 * @since 4.0.0
325 *
326 * @return string
327 */
328 public function get_content_type() {
329 return 'text/html';
330 }
331
332 /**
333 * Updates how the email will be sent.
334 *
335 * @since 4.7.3
336 *
337 * @return void
338 */
339 public function send_before() {
340 add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
341 add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
342 add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
343 }
344
345 /**
346 * Removes our plugin-specific email changes after an email is sent.
347 *
348 * @since 4.7.3
349 *
350 * @return void
351 */
352 public function send_after() {
353 remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
354 remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
355 remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
356 }
357
358 /**
359 * Parses content and replaces Smart Tags with their values.
360 *
361 * @since 4.7.3
362 *
363 * @param string $content Content to parse and replace Smart Tags in.
364 * @return string
365 */
366 private function parse_smart_tags( $content ) {
367 return Payment_Confirmation\Template_Tags\parse_content(
368 $content,
369 $this->get_data()
370 );
371 }
372
373 }
374