These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Class WC_Email file. |
||
4 | * |
||
5 | * @package WooCommerce\Emails |
||
6 | */ |
||
7 | |||
8 | 1 | if ( ! defined( 'ABSPATH' ) ) { |
|
9 | exit; |
||
10 | } |
||
11 | |||
12 | 1 | if ( class_exists( 'WC_Email', false ) ) { |
|
13 | 1 | return; |
|
14 | } |
||
15 | |||
16 | /** |
||
17 | * Email Class |
||
18 | * |
||
19 | * WooCommerce Email Class which is extended by specific email template classes to add emails to WooCommerce |
||
20 | * |
||
21 | * @class WC_Email |
||
22 | * @version 2.5.0 |
||
23 | * @package WooCommerce/Classes/Emails |
||
24 | * @extends WC_Settings_API |
||
25 | */ |
||
26 | class WC_Email extends WC_Settings_API { |
||
27 | |||
28 | /** |
||
29 | * Email method ID. |
||
30 | * |
||
31 | * @var String |
||
32 | */ |
||
33 | public $id; |
||
34 | |||
35 | /** |
||
36 | * Email method title. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | public $title; |
||
41 | |||
42 | /** |
||
43 | * 'yes' if the method is enabled. |
||
44 | * |
||
45 | * @var string yes, no |
||
46 | */ |
||
47 | public $enabled; |
||
48 | |||
49 | /** |
||
50 | * Description for the email. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | public $description; |
||
55 | |||
56 | /** |
||
57 | * Default heading. |
||
58 | * |
||
59 | * Supported for backwards compatibility but we recommend overloading the |
||
60 | * get_default_x methods instead so localization can be done when needed. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | public $heading = ''; |
||
65 | |||
66 | /** |
||
67 | * Default subject. |
||
68 | * |
||
69 | * Supported for backwards compatibility but we recommend overloading the |
||
70 | * get_default_x methods instead so localization can be done when needed. |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | public $subject = ''; |
||
75 | |||
76 | /** |
||
77 | * Plain text template path. |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | public $template_plain; |
||
82 | |||
83 | /** |
||
84 | * HTML template path. |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | public $template_html; |
||
89 | |||
90 | /** |
||
91 | * Template path. |
||
92 | * |
||
93 | * @var string |
||
94 | */ |
||
95 | public $template_base; |
||
96 | |||
97 | /** |
||
98 | * Recipients for the email. |
||
99 | * |
||
100 | * @var string |
||
101 | */ |
||
102 | public $recipient; |
||
103 | |||
104 | /** |
||
105 | * Object this email is for, for example a customer, product, or email. |
||
106 | * |
||
107 | * @var object|bool |
||
108 | */ |
||
109 | public $object; |
||
110 | |||
111 | /** |
||
112 | * Mime boundary (for multipart emails). |
||
113 | * |
||
114 | * @var string |
||
115 | */ |
||
116 | public $mime_boundary; |
||
117 | |||
118 | /** |
||
119 | * Mime boundary header (for multipart emails). |
||
120 | * |
||
121 | * @var string |
||
122 | */ |
||
123 | public $mime_boundary_header; |
||
124 | |||
125 | /** |
||
126 | * True when email is being sent. |
||
127 | * |
||
128 | * @var bool |
||
129 | */ |
||
130 | public $sending; |
||
131 | |||
132 | /** |
||
133 | * True when the email notification is sent manually only. |
||
134 | * |
||
135 | * @var bool |
||
136 | */ |
||
137 | protected $manual = false; |
||
138 | |||
139 | /** |
||
140 | * True when the email notification is sent to customers. |
||
141 | * |
||
142 | * @var bool |
||
143 | */ |
||
144 | protected $customer_email = false; |
||
145 | |||
146 | /** |
||
147 | * List of preg* regular expression patterns to search for, |
||
148 | * used in conjunction with $plain_replace. |
||
149 | * https://raw.github.com/ushahidi/wp-silcc/master/class.html2text.inc |
||
150 | * |
||
151 | * @var array $plain_search |
||
152 | * @see $plain_replace |
||
153 | */ |
||
154 | public $plain_search = array( |
||
155 | "/\r/", // Non-legal carriage return. |
||
156 | '/&(nbsp|#0*160);/i', // Non-breaking space. |
||
157 | '/&(quot|rdquo|ldquo|#0*8220|#0*8221|#0*147|#0*148);/i', // Double quotes. |
||
158 | '/&(apos|rsquo|lsquo|#0*8216|#0*8217);/i', // Single quotes. |
||
159 | '/>/i', // Greater-than. |
||
160 | '/</i', // Less-than. |
||
161 | '/�*38;/i', // Ampersand. |
||
162 | '/&/i', // Ampersand. |
||
163 | '/&(copy|#0*169);/i', // Copyright. |
||
164 | '/&(trade|#0*8482|#0*153);/i', // Trademark. |
||
165 | '/&(reg|#0*174);/i', // Registered. |
||
166 | '/&(mdash|#0*151|#0*8212);/i', // mdash. |
||
167 | '/&(ndash|minus|#0*8211|#0*8722);/i', // ndash. |
||
168 | '/&(bull|#0*149|#0*8226);/i', // Bullet. |
||
169 | '/&(pound|#0*163);/i', // Pound sign. |
||
170 | '/&(euro|#0*8364);/i', // Euro sign. |
||
171 | '/&(dollar|#0*36);/i', // Dollar sign. |
||
172 | '/&[^&\s;]+;/i', // Unknown/unhandled entities. |
||
173 | '/[ ]{2,}/', // Runs of spaces, post-handling. |
||
174 | ); |
||
175 | |||
176 | /** |
||
177 | * List of pattern replacements corresponding to patterns searched. |
||
178 | * |
||
179 | * @var array $plain_replace |
||
180 | * @see $plain_search |
||
181 | */ |
||
182 | public $plain_replace = array( |
||
183 | '', // Non-legal carriage return. |
||
184 | ' ', // Non-breaking space. |
||
185 | '"', // Double quotes. |
||
186 | "'", // Single quotes. |
||
187 | '>', // Greater-than. |
||
188 | '<', // Less-than. |
||
189 | '&', // Ampersand. |
||
190 | '&', // Ampersand. |
||
191 | '(c)', // Copyright. |
||
192 | '(tm)', // Trademark. |
||
193 | '(R)', // Registered. |
||
194 | '--', // mdash. |
||
195 | '-', // ndash. |
||
196 | '*', // Bullet. |
||
197 | '£', // Pound sign. |
||
198 | 'EUR', // Euro sign. € ?. |
||
199 | '$', // Dollar sign. |
||
200 | '', // Unknown/unhandled entities. |
||
201 | ' ', // Runs of spaces, post-handling. |
||
202 | ); |
||
203 | |||
204 | /** |
||
205 | * Strings to find/replace in subjects/headings. |
||
206 | * |
||
207 | * @var array |
||
208 | */ |
||
209 | protected $placeholders = array(); |
||
210 | |||
211 | /** |
||
212 | * Strings to find in subjects/headings. |
||
213 | * |
||
214 | * @deprecated 3.2.0 in favour of placeholders |
||
215 | * @var array |
||
216 | */ |
||
217 | public $find = array(); |
||
218 | |||
219 | /** |
||
220 | * Strings to replace in subjects/headings. |
||
221 | * |
||
222 | * @deprecated 3.2.0 in favour of placeholders |
||
223 | * @var array |
||
224 | */ |
||
225 | public $replace = array(); |
||
226 | |||
227 | /** |
||
228 | * Constructor. |
||
229 | */ |
||
230 | 1 | public function __construct() { |
|
231 | // Find/replace. |
||
232 | 1 | if ( empty( $this->placeholders ) ) { |
|
233 | 1 | $this->placeholders = array( |
|
234 | 1 | '{site_title}' => $this->get_blogname(), |
|
235 | ); |
||
236 | } |
||
237 | |||
238 | // Init settings. |
||
239 | 1 | $this->init_form_fields(); |
|
240 | 1 | $this->init_settings(); |
|
241 | |||
242 | // Default template base if not declared in child constructor. |
||
243 | 1 | if ( is_null( $this->template_base ) ) { |
|
244 | 1 | $this->template_base = WC()->plugin_path() . '/templates/'; |
|
245 | } |
||
246 | |||
247 | 1 | $this->email_type = $this->get_option( 'email_type' ); |
|
248 | 1 | $this->enabled = $this->get_option( 'enabled' ); |
|
249 | |||
250 | 1 | add_action( 'phpmailer_init', array( $this, 'handle_multipart' ) ); |
|
251 | 1 | add_action( 'woocommerce_update_options_email_' . $this->id, array( $this, 'process_admin_options' ) ); |
|
252 | } |
||
253 | |||
254 | /** |
||
255 | * Handle multipart mail. |
||
256 | * |
||
257 | * @param PHPMailer $mailer PHPMailer object. |
||
258 | * @return PHPMailer |
||
259 | */ |
||
260 | 1 | public function handle_multipart( $mailer ) { |
|
261 | 1 | if ( $this->sending && 'multipart' === $this->get_email_type() ) { |
|
262 | $mailer->AltBody = wordwrap( // phpcs:ignore WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar |
||
263 | preg_replace( $this->plain_search, $this->plain_replace, strip_tags( $this->get_content_plain() ) ) |
||
264 | ); |
||
265 | $this->sending = false; |
||
266 | } |
||
267 | 1 | return $mailer; |
|
268 | } |
||
269 | |||
270 | /** |
||
271 | * Format email string. |
||
272 | * |
||
273 | * @param mixed $string Text to replace placeholders in. |
||
274 | * @return string |
||
275 | */ |
||
276 | 1 | public function format_string( $string ) { |
|
277 | 1 | $find = array_keys( $this->placeholders ); |
|
278 | 1 | $replace = array_values( $this->placeholders ); |
|
279 | |||
280 | // If using legacy find replace, add those to our find/replace arrays first. @todo deprecate in 4.0.0. |
||
281 | 1 | $find = array_merge( (array) $this->find, $find ); |
|
282 | 1 | $replace = array_merge( (array) $this->replace, $replace ); |
|
283 | |||
284 | // Take care of blogname which is no longer defined as a valid placeholder. |
||
285 | 1 | $find[] = '{blogname}'; |
|
286 | 1 | $replace[] = $this->get_blogname(); |
|
287 | |||
288 | // If using the older style filters for find and replace, ensure the array is associative and then pass through filters. @todo deprecate in 4.0.0. |
||
289 | 1 | if ( has_filter( 'woocommerce_email_format_string_replace' ) || has_filter( 'woocommerce_email_format_string_find' ) ) { |
|
290 | $legacy_find = $this->find; |
||
291 | $legacy_replace = $this->replace; |
||
292 | |||
293 | foreach ( $this->placeholders as $find => $replace ) { |
||
294 | $legacy_key = sanitize_title( str_replace( '_', '-', trim( $find, '{}' ) ) ); |
||
295 | $legacy_find[ $legacy_key ] = $find; |
||
296 | $legacy_replace[ $legacy_key ] = $replace; |
||
297 | } |
||
298 | |||
299 | $string = str_replace( apply_filters( 'woocommerce_email_format_string_find', $legacy_find, $this ), apply_filters( 'woocommerce_email_format_string_replace', $legacy_replace, $this ), $string ); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Filter for main find/replace. |
||
304 | * |
||
305 | * @since 3.2.0 |
||
306 | */ |
||
307 | 1 | return apply_filters( 'woocommerce_email_format_string', str_replace( $find, $replace, $string ), $this ); |
|
308 | } |
||
309 | |||
310 | /** |
||
311 | * Set the locale to the store locale for customer emails to make sure emails are in the store language. |
||
312 | */ |
||
313 | 1 | public function setup_locale() { |
|
314 | 1 | if ( $this->is_customer_email() && apply_filters( 'woocommerce_email_setup_locale', true ) ) { |
|
315 | 1 | wc_switch_to_site_locale(); |
|
316 | } |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Restore the locale to the default locale. Use after finished with setup_locale. |
||
321 | */ |
||
322 | 1 | public function restore_locale() { |
|
323 | 1 | if ( $this->is_customer_email() && apply_filters( 'woocommerce_email_restore_locale', true ) ) { |
|
324 | 1 | wc_restore_locale(); |
|
325 | } |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Get email subject. |
||
330 | * |
||
331 | * @since 3.1.0 |
||
332 | * @return string |
||
333 | */ |
||
334 | public function get_default_subject() { |
||
335 | return $this->subject; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Get email heading. |
||
340 | * |
||
341 | * @since 3.1.0 |
||
342 | * @return string |
||
343 | */ |
||
344 | public function get_default_heading() { |
||
345 | return $this->heading; |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Get email subject. |
||
350 | * |
||
351 | * @return string |
||
352 | */ |
||
353 | 1 | public function get_subject() { |
|
354 | 1 | return apply_filters( 'woocommerce_email_subject_' . $this->id, $this->format_string( $this->get_option( 'subject', $this->get_default_subject() ) ), $this->object, $this ); |
|
355 | } |
||
356 | |||
357 | /** |
||
358 | * Get email heading. |
||
359 | * |
||
360 | * @return string |
||
361 | */ |
||
362 | 1 | public function get_heading() { |
|
363 | 1 | return apply_filters( 'woocommerce_email_heading_' . $this->id, $this->format_string( $this->get_option( 'heading', $this->get_default_heading() ) ), $this->object, $this ); |
|
364 | } |
||
365 | |||
366 | /** |
||
367 | * Get valid recipients. |
||
368 | * |
||
369 | * @return string |
||
370 | */ |
||
371 | 1 | public function get_recipient() { |
|
372 | 1 | $recipient = apply_filters( 'woocommerce_email_recipient_' . $this->id, $this->recipient, $this->object, $this ); |
|
373 | 1 | $recipients = array_map( 'trim', explode( ',', $recipient ) ); |
|
374 | 1 | $recipients = array_filter( $recipients, 'is_email' ); |
|
375 | 1 | return implode( ', ', $recipients ); |
|
376 | } |
||
377 | |||
378 | /** |
||
379 | * Get email headers. |
||
380 | * |
||
381 | * @return string |
||
382 | */ |
||
383 | 1 | public function get_headers() { |
|
384 | 1 | $header = 'Content-Type: ' . $this->get_content_type() . "\r\n"; |
|
385 | |||
386 | 1 | if ( in_array( $this->id, array( 'new_order', 'cancelled_order', 'failed_order' ), true ) ) { |
|
387 | if ( $this->object && $this->object->get_billing_email() && ( $this->object->get_billing_first_name() || $this->object->get_billing_last_name() ) ) { |
||
388 | $header .= 'Reply-to: ' . $this->object->get_billing_first_name() . ' ' . $this->object->get_billing_last_name() . ' <' . $this->object->get_billing_email() . ">\r\n"; |
||
389 | } |
||
390 | 1 | } elseif ( $this->get_from_address() && $this->get_from_name() ) { |
|
391 | 1 | $header .= 'Reply-to: ' . $this->get_from_name() . ' <' . $this->get_from_address() . ">\r\n"; |
|
392 | } |
||
393 | |||
394 | 1 | return apply_filters( 'woocommerce_email_headers', $header, $this->id, $this->object, $this ); |
|
395 | } |
||
396 | |||
397 | /** |
||
398 | * Get email attachments. |
||
399 | * |
||
400 | * @return array |
||
401 | */ |
||
402 | 1 | public function get_attachments() { |
|
403 | 1 | return apply_filters( 'woocommerce_email_attachments', array(), $this->id, $this->object, $this ); |
|
404 | } |
||
405 | |||
406 | /** |
||
407 | * Return email type. |
||
408 | * |
||
409 | * @return string |
||
410 | */ |
||
411 | 1 | public function get_email_type() { |
|
412 | 1 | return $this->email_type && class_exists( 'DOMDocument' ) ? $this->email_type : 'plain'; |
|
413 | } |
||
414 | |||
415 | /** |
||
416 | * Get email content type. |
||
417 | * |
||
418 | * @return string |
||
419 | */ |
||
420 | 1 | public function get_content_type() { |
|
421 | 1 | switch ( $this->get_email_type() ) { |
|
422 | case 'html': |
||
423 | 1 | return 'text/html'; |
|
424 | case 'multipart': |
||
425 | return 'multipart/alternative'; |
||
426 | default: |
||
427 | return 'text/plain'; |
||
428 | } |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * Return the email's title |
||
433 | * |
||
434 | * @return string |
||
435 | */ |
||
436 | public function get_title() { |
||
437 | return apply_filters( 'woocommerce_email_title', $this->title, $this ); |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Return the email's description |
||
442 | * |
||
443 | * @return string |
||
444 | */ |
||
445 | public function get_description() { |
||
446 | return apply_filters( 'woocommerce_email_description', $this->description, $this ); |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * Proxy to parent's get_option and attempt to localize the result using gettext. |
||
451 | * |
||
452 | * @param string $key Option key. |
||
453 | * @param mixed $empty_value Value to use when option is empty. |
||
454 | * @return string |
||
455 | */ |
||
456 | 1 | public function get_option( $key, $empty_value = null ) { |
|
457 | 1 | $value = parent::get_option( $key, $empty_value ); |
|
458 | 1 | return apply_filters( 'woocommerce_email_get_option', $value, $this, $value, $key, $empty_value ); |
|
459 | } |
||
460 | |||
461 | /** |
||
462 | * Checks if this email is enabled and will be sent. |
||
463 | * |
||
464 | * @return bool |
||
465 | */ |
||
466 | 1 | public function is_enabled() { |
|
467 | 1 | return apply_filters( 'woocommerce_email_enabled_' . $this->id, 'yes' === $this->enabled, $this->object, $this ); |
|
468 | } |
||
469 | |||
470 | /** |
||
471 | * Checks if this email is manually sent |
||
472 | * |
||
473 | * @return bool |
||
474 | */ |
||
475 | public function is_manual() { |
||
476 | return $this->manual; |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * Checks if this email is customer focussed. |
||
481 | * |
||
482 | * @return bool |
||
483 | */ |
||
484 | 1 | public function is_customer_email() { |
|
485 | 1 | return $this->customer_email; |
|
486 | } |
||
487 | |||
488 | /** |
||
489 | * Get WordPress blog name. |
||
490 | * |
||
491 | * @return string |
||
492 | */ |
||
493 | 1 | public function get_blogname() { |
|
494 | 1 | return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); |
|
495 | } |
||
496 | |||
497 | /** |
||
498 | * Get email content. |
||
499 | * |
||
500 | * @return string |
||
501 | */ |
||
502 | 1 | public function get_content() { |
|
503 | 1 | $this->sending = true; |
|
504 | |||
505 | 1 | if ( 'plain' === $this->get_email_type() ) { |
|
506 | $email_content = wordwrap( preg_replace( $this->plain_search, $this->plain_replace, strip_tags( $this->get_content_plain() ) ), 70 ); |
||
507 | } else { |
||
508 | 1 | $email_content = $this->get_content_html(); |
|
509 | } |
||
510 | |||
511 | 1 | return $email_content; |
|
512 | } |
||
513 | |||
514 | /** |
||
515 | * Apply inline styles to dynamic content. |
||
516 | * |
||
517 | * We only inline CSS for html emails, and to do so we use Emogrifier library (if supported). |
||
518 | * |
||
519 | * @param string|null $content Content that will receive inline styles. |
||
520 | * @return string |
||
521 | */ |
||
522 | 1 | public function style_inline( $content ) { |
|
523 | 1 | if ( in_array( $this->get_content_type(), array( 'text/html', 'multipart/alternative' ), true ) ) { |
|
524 | 1 | ob_start(); |
|
525 | 1 | wc_get_template( 'emails/email-styles.php' ); |
|
526 | 1 | $css = apply_filters( 'woocommerce_email_styles', ob_get_clean(), $this ); |
|
527 | |||
528 | 1 | if ( $this->supports_emogrifier() ) { |
|
529 | 1 | $emogrifier_class = '\\Pelago\\Emogrifier'; |
|
530 | 1 | if ( ! class_exists( $emogrifier_class ) ) { |
|
531 | 1 | include_once dirname( dirname( __FILE__ ) ) . '/libraries/class-emogrifier.php'; |
|
532 | } |
||
533 | try { |
||
534 | 1 | $emogrifier = new $emogrifier_class( $content, $css ); |
|
535 | 1 | $content = $emogrifier->emogrify(); |
|
536 | } catch ( Exception $e ) { |
||
537 | $logger = wc_get_logger(); |
||
538 | 1 | $logger->error( $e->getMessage(), array( 'source' => 'emogrifier' ) ); |
|
539 | } |
||
540 | } else { |
||
541 | $content = '<style type="text/css">' . $css . '</style>' . $content; |
||
542 | } |
||
543 | } |
||
544 | 1 | return $content; |
|
545 | } |
||
546 | |||
547 | /** |
||
548 | * Return if emogrifier library is supported. |
||
549 | * |
||
550 | * @since 3.5.0 |
||
551 | * @return bool |
||
552 | */ |
||
553 | 1 | protected function supports_emogrifier() { |
|
554 | 1 | return class_exists( 'DOMDocument' ) && version_compare( PHP_VERSION, '5.5', '>=' ); |
|
555 | } |
||
556 | |||
557 | /** |
||
558 | * Get the email content in plain text format. |
||
559 | * |
||
560 | * @return string |
||
561 | */ |
||
562 | public function get_content_plain() { |
||
563 | return ''; } |
||
564 | |||
565 | /** |
||
566 | * Get the email content in HTML format. |
||
567 | * |
||
568 | * @return string |
||
569 | */ |
||
570 | public function get_content_html() { |
||
571 | return ''; } |
||
572 | |||
573 | /** |
||
574 | * Get the from name for outgoing emails. |
||
575 | * |
||
576 | * @return string |
||
577 | */ |
||
578 | 1 | public function get_from_name() { |
|
579 | 1 | $from_name = apply_filters( 'woocommerce_email_from_name', get_option( 'woocommerce_email_from_name' ), $this ); |
|
580 | 1 | return wp_specialchars_decode( esc_html( $from_name ), ENT_QUOTES ); |
|
581 | } |
||
582 | |||
583 | /** |
||
584 | * Get the from address for outgoing emails. |
||
585 | * |
||
586 | * @return string |
||
587 | */ |
||
588 | 1 | public function get_from_address() { |
|
589 | 1 | $from_address = apply_filters( 'woocommerce_email_from_address', get_option( 'woocommerce_email_from_address' ), $this ); |
|
590 | 1 | return sanitize_email( $from_address ); |
|
591 | } |
||
592 | |||
593 | /** |
||
594 | * Send an email. |
||
595 | * |
||
596 | * @param string $to Email to. |
||
597 | * @param string $subject Email subject. |
||
598 | * @param string $message Email message. |
||
599 | * @param string $headers Email headers. |
||
600 | * @param array $attachments Email attachments. |
||
601 | * @return bool success |
||
602 | */ |
||
603 | 1 | public function send( $to, $subject, $message, $headers, $attachments ) { |
|
604 | 1 | add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); |
|
605 | 1 | add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); |
|
606 | 1 | add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) ); |
|
607 | |||
608 | 1 | $message = apply_filters( 'woocommerce_mail_content', $this->style_inline( $message ) ); |
|
609 | 1 | $mail_callback = apply_filters( 'woocommerce_mail_callback', 'wp_mail', $this ); |
|
610 | 1 | $mail_callback_params = apply_filters( 'woocommerce_mail_callback_params', array( $to, $subject, $message, $headers, $attachments ), $this ); |
|
611 | 1 | $return = call_user_func_array( $mail_callback, $mail_callback_params ); |
|
612 | |||
613 | 1 | remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); |
|
614 | 1 | remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); |
|
615 | 1 | remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) ); |
|
616 | |||
617 | 1 | return $return; |
|
618 | } |
||
619 | |||
620 | /** |
||
621 | * Initialise Settings Form Fields - these are generic email options most will use. |
||
622 | */ |
||
623 | 1 | public function init_form_fields() { |
|
624 | 1 | $this->form_fields = array( |
|
625 | 'enabled' => array( |
||
626 | 1 | 'title' => __( 'Enable/Disable', 'woocommerce' ), |
|
627 | 1 | 'type' => 'checkbox', |
|
628 | 1 | 'label' => __( 'Enable this email notification', 'woocommerce' ), |
|
629 | 1 | 'default' => 'yes', |
|
630 | ), |
||
631 | 'subject' => array( |
||
632 | 1 | 'title' => __( 'Subject', 'woocommerce' ), |
|
633 | 1 | 'type' => 'text', |
|
634 | 'desc_tip' => true, |
||
635 | /* translators: %s: list of placeholders */ |
||
636 | 1 | 'description' => sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>' . implode( '</code>, <code>', array_keys( $this->placeholders ) ) . '</code>' ), |
|
637 | 1 | 'placeholder' => $this->get_default_subject(), |
|
638 | 1 | 'default' => '', |
|
639 | ), |
||
640 | 'heading' => array( |
||
641 | 1 | 'title' => __( 'Email heading', 'woocommerce' ), |
|
642 | 1 | 'type' => 'text', |
|
643 | 'desc_tip' => true, |
||
644 | /* translators: %s: list of placeholders */ |
||
645 | 1 | 'description' => sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>' . implode( '</code>, <code>', array_keys( $this->placeholders ) ) . '</code>' ), |
|
646 | 1 | 'placeholder' => $this->get_default_heading(), |
|
647 | 1 | 'default' => '', |
|
648 | ), |
||
649 | 'email_type' => array( |
||
650 | 1 | 'title' => __( 'Email type', 'woocommerce' ), |
|
651 | 1 | 'type' => 'select', |
|
652 | 1 | 'description' => __( 'Choose which format of email to send.', 'woocommerce' ), |
|
653 | 1 | 'default' => 'html', |
|
654 | 1 | 'class' => 'email_type wc-enhanced-select', |
|
655 | 1 | 'options' => $this->get_email_type_options(), |
|
656 | 'desc_tip' => true, |
||
657 | ), |
||
658 | ); |
||
659 | } |
||
660 | |||
661 | /** |
||
662 | * Email type options. |
||
663 | * |
||
664 | * @return array |
||
665 | */ |
||
666 | 1 | public function get_email_type_options() { |
|
667 | 1 | $types = array( 'plain' => __( 'Plain text', 'woocommerce' ) ); |
|
668 | |||
669 | 1 | if ( class_exists( 'DOMDocument' ) ) { |
|
670 | 1 | $types['html'] = __( 'HTML', 'woocommerce' ); |
|
671 | 1 | $types['multipart'] = __( 'Multipart', 'woocommerce' ); |
|
672 | } |
||
673 | |||
674 | 1 | return $types; |
|
675 | } |
||
676 | |||
677 | /** |
||
678 | * Admin Panel Options Processing. |
||
679 | */ |
||
680 | public function process_admin_options() { |
||
681 | // Save regular options. |
||
682 | parent::process_admin_options(); |
||
683 | |||
684 | $post_data = $this->get_post_data(); |
||
685 | |||
686 | // Save templates. |
||
687 | if ( isset( $post_data['template_html_code'] ) ) { |
||
688 | $this->save_template( $post_data['template_html_code'], $this->template_html ); |
||
689 | } |
||
690 | if ( isset( $post_data['template_plain_code'] ) ) { |
||
691 | $this->save_template( $post_data['template_plain_code'], $this->template_plain ); |
||
692 | } |
||
693 | } |
||
694 | |||
695 | /** |
||
696 | * Get template. |
||
697 | * |
||
698 | * @param string $type Template type. Can be either 'template_html' or 'template_plain'. |
||
699 | * @return string |
||
700 | */ |
||
701 | public function get_template( $type ) { |
||
702 | $type = basename( $type ); |
||
703 | |||
704 | if ( 'template_html' === $type ) { |
||
705 | return $this->template_html; |
||
706 | } elseif ( 'template_plain' === $type ) { |
||
707 | return $this->template_plain; |
||
708 | } |
||
709 | return ''; |
||
710 | } |
||
711 | |||
712 | /** |
||
713 | * Save the email templates. |
||
714 | * |
||
715 | * @since 2.4.0 |
||
716 | * @param string $template_code Template code. |
||
717 | * @param string $template_path Template path. |
||
718 | */ |
||
719 | protected function save_template( $template_code, $template_path ) { |
||
720 | if ( current_user_can( 'edit_themes' ) && ! empty( $template_code ) && ! empty( $template_path ) ) { |
||
721 | $saved = false; |
||
722 | $file = get_stylesheet_directory() . '/' . WC()->template_path() . $template_path; |
||
723 | $code = wp_unslash( $template_code ); |
||
724 | |||
725 | if ( is_writeable( $file ) ) { // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writeable |
||
726 | $f = fopen( $file, 'w+' ); |
||
727 | |||
728 | if ( false !== $f ) { |
||
729 | fwrite( $f, $code ); |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
730 | fclose( $f ); |
||
731 | $saved = true; |
||
732 | } |
||
733 | } |
||
734 | |||
735 | if ( ! $saved ) { |
||
736 | $redirect = add_query_arg( 'wc_error', urlencode( __( 'Could not write to template file.', 'woocommerce' ) ) ); |
||
737 | wp_safe_redirect( $redirect ); |
||
738 | exit; |
||
739 | } |
||
740 | } |
||
741 | } |
||
742 | |||
743 | /** |
||
744 | * Get the template file in the current theme. |
||
745 | * |
||
746 | * @param string $template Template name. |
||
747 | * |
||
748 | * @return string |
||
749 | */ |
||
750 | public function get_theme_template_file( $template ) { |
||
751 | return get_stylesheet_directory() . '/' . apply_filters( 'woocommerce_template_directory', 'woocommerce', $template ) . '/' . $template; |
||
752 | } |
||
753 | |||
754 | /** |
||
755 | * Move template action. |
||
756 | * |
||
757 | * @param string $template_type Template type. |
||
758 | */ |
||
759 | protected function move_template_action( $template_type ) { |
||
760 | $template = $this->get_template( $template_type ); |
||
761 | if ( ! empty( $template ) ) { |
||
762 | $theme_file = $this->get_theme_template_file( $template ); |
||
763 | |||
764 | if ( wp_mkdir_p( dirname( $theme_file ) ) && ! file_exists( $theme_file ) ) { |
||
765 | |||
766 | // Locate template file. |
||
767 | $core_file = $this->template_base . $template; |
||
768 | $template_file = apply_filters( 'woocommerce_locate_core_template', $core_file, $template, $this->template_base, $this->id ); |
||
769 | |||
770 | // Copy template file. |
||
771 | copy( $template_file, $theme_file ); |
||
772 | |||
773 | /** |
||
774 | * Action hook fired after copying email template file. |
||
775 | * |
||
776 | * @param string $template_type The copied template type |
||
777 | * @param string $email The email object |
||
778 | */ |
||
779 | do_action( 'woocommerce_copy_email_template', $template_type, $this ); |
||
780 | |||
781 | ?> |
||
782 | <div class="updated"> |
||
783 | <p><?php echo esc_html__( 'Template file copied to theme.', 'woocommerce' ); ?></p> |
||
784 | </div> |
||
785 | <?php |
||
786 | } |
||
787 | } |
||
788 | } |
||
789 | |||
790 | /** |
||
791 | * Delete template action. |
||
792 | * |
||
793 | * @param string $template_type Template type. |
||
794 | */ |
||
795 | protected function delete_template_action( $template_type ) { |
||
796 | $template = $this->get_template( $template_type ); |
||
797 | |||
798 | if ( $template ) { |
||
799 | if ( ! empty( $template ) ) { |
||
800 | $theme_file = $this->get_theme_template_file( $template ); |
||
801 | |||
802 | if ( file_exists( $theme_file ) ) { |
||
803 | unlink( $theme_file ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_unlink |
||
804 | |||
805 | /** |
||
806 | * Action hook fired after deleting template file. |
||
807 | * |
||
808 | * @param string $template The deleted template type |
||
809 | * @param string $email The email object |
||
810 | */ |
||
811 | do_action( 'woocommerce_delete_email_template', $template_type, $this ); |
||
812 | ?> |
||
813 | <div class="updated"> |
||
814 | <p><?php echo esc_html__( 'Template file deleted from theme.', 'woocommerce' ); ?></p> |
||
815 | </div> |
||
816 | <?php |
||
817 | } |
||
818 | } |
||
819 | } |
||
820 | } |
||
821 | |||
822 | /** |
||
823 | * Admin actions. |
||
824 | */ |
||
825 | protected function admin_actions() { |
||
826 | // Handle any actions. |
||
827 | if ( |
||
828 | ( ! empty( $this->template_html ) || ! empty( $this->template_plain ) ) |
||
829 | && ( ! empty( $_GET['move_template'] ) || ! empty( $_GET['delete_template'] ) ) |
||
830 | && 'GET' === $_SERVER['REQUEST_METHOD'] // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated |
||
831 | ) { |
||
832 | View Code Duplication | if ( empty( $_GET['_wc_email_nonce'] ) || ! wp_verify_nonce( wc_clean( wp_unslash( $_GET['_wc_email_nonce'] ) ), 'woocommerce_email_template_nonce' ) ) { |
|
833 | wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) ); |
||
834 | } |
||
835 | |||
836 | if ( ! current_user_can( 'edit_themes' ) ) { |
||
837 | wp_die( esc_html__( 'You don’t have permission to do this.', 'woocommerce' ) ); |
||
838 | } |
||
839 | |||
840 | if ( ! empty( $_GET['move_template'] ) ) { |
||
841 | $this->move_template_action( wc_clean( wp_unslash( $_GET['move_template'] ) ) ); |
||
842 | } |
||
843 | |||
844 | if ( ! empty( $_GET['delete_template'] ) ) { |
||
845 | $this->delete_template_action( wc_clean( wp_unslash( $_GET['delete_template'] ) ) ); |
||
846 | } |
||
847 | } |
||
848 | } |
||
849 | |||
850 | /** |
||
851 | * Admin Options. |
||
852 | * |
||
853 | * Setup the email settings screen. |
||
854 | * Override this in your email. |
||
855 | * |
||
856 | * @since 1.0.0 |
||
857 | */ |
||
858 | public function admin_options() { |
||
859 | // Do admin actions. |
||
860 | $this->admin_actions(); |
||
861 | ?> |
||
862 | <h2><?php echo esc_html( $this->get_title() ); ?> <?php wc_back_link( __( 'Return to emails', 'woocommerce' ), admin_url( 'admin.php?page=wc-settings&tab=email' ) ); ?></h2> |
||
863 | |||
864 | <?php echo wpautop( wp_kses_post( $this->get_description() ) ); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped ?> |
||
865 | |||
866 | <?php |
||
867 | /** |
||
868 | * Action hook fired before displaying email settings. |
||
869 | * |
||
870 | * @param string $email The email object |
||
871 | */ |
||
872 | do_action( 'woocommerce_email_settings_before', $this ); |
||
873 | ?> |
||
874 | |||
875 | <table class="form-table"> |
||
876 | <?php $this->generate_settings_html(); ?> |
||
877 | </table> |
||
878 | |||
879 | <?php |
||
880 | /** |
||
881 | * Action hook fired after displaying email settings. |
||
882 | * |
||
883 | * @param string $email The email object |
||
884 | */ |
||
885 | do_action( 'woocommerce_email_settings_after', $this ); |
||
886 | ?> |
||
887 | |||
888 | <?php |
||
889 | |||
890 | if ( current_user_can( 'edit_themes' ) && ( ! empty( $this->template_html ) || ! empty( $this->template_plain ) ) ) { |
||
891 | ?> |
||
892 | <div id="template"> |
||
893 | <?php |
||
894 | $templates = array( |
||
895 | 'template_html' => __( 'HTML template', 'woocommerce' ), |
||
896 | 'template_plain' => __( 'Plain text template', 'woocommerce' ), |
||
897 | ); |
||
898 | |||
899 | foreach ( $templates as $template_type => $title ) : |
||
900 | $template = $this->get_template( $template_type ); |
||
901 | |||
902 | if ( empty( $template ) ) { |
||
903 | continue; |
||
904 | } |
||
905 | |||
906 | $local_file = $this->get_theme_template_file( $template ); |
||
907 | $core_file = $this->template_base . $template; |
||
908 | $template_file = apply_filters( 'woocommerce_locate_core_template', $core_file, $template, $this->template_base, $this->id ); |
||
909 | $template_dir = apply_filters( 'woocommerce_template_directory', 'woocommerce', $template ); |
||
910 | ?> |
||
911 | <div class="template <?php echo esc_attr( $template_type ); ?>"> |
||
912 | <h4><?php echo wp_kses_post( $title ); ?></h4> |
||
913 | |||
914 | <?php if ( file_exists( $local_file ) ) : ?> |
||
915 | <p> |
||
916 | <a href="#" class="button toggle_editor"></a> |
||
917 | |||
918 | View Code Duplication | <?php if ( is_writable( $local_file ) ) : // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable ?> |
|
919 | <a href="<?php echo esc_url( wp_nonce_url( remove_query_arg( array( 'move_template', 'saved' ), add_query_arg( 'delete_template', $template_type ) ), 'woocommerce_email_template_nonce', '_wc_email_nonce' ) ); ?>" class="delete_template button"> |
||
920 | <?php esc_html_e( 'Delete template file', 'woocommerce' ); ?> |
||
921 | </a> |
||
922 | <?php endif; ?> |
||
923 | |||
924 | <?php |
||
925 | /* translators: %s: Path to template file */ |
||
926 | printf( esc_html__( 'This template has been overridden by your theme and can be found in: %s.', 'woocommerce' ), '<code>' . esc_html( trailingslashit( basename( get_stylesheet_directory() ) ) . $template_dir . '/' . $template ) . '</code>' ); |
||
927 | ?> |
||
928 | </p> |
||
929 | |||
930 | <div class="editor" style="display:none"> |
||
931 | <textarea class="code" cols="25" rows="20" |
||
932 | <?php |
||
933 | if ( ! is_writable( $local_file ) ) : // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable |
||
934 | ?> |
||
935 | readonly="readonly" disabled="disabled" |
||
936 | <?php else : ?> |
||
937 | data-name="<?php echo esc_attr( $template_type ) . '_code'; ?>"<?php endif; ?>><?php echo esc_html( file_get_contents( $local_file ) ); ?></textarea> |
||
938 | </div> |
||
939 | <?php elseif ( file_exists( $template_file ) ) : ?> |
||
940 | <p> |
||
941 | <a href="#" class="button toggle_editor"></a> |
||
942 | |||
943 | <?php |
||
944 | $emails_dir = get_stylesheet_directory() . '/' . $template_dir . '/emails'; |
||
945 | $templates_dir = get_stylesheet_directory() . '/' . $template_dir; |
||
946 | $theme_dir = get_stylesheet_directory(); |
||
947 | |||
948 | if ( is_dir( $emails_dir ) ) { |
||
949 | $target_dir = $emails_dir; |
||
950 | } elseif ( is_dir( $templates_dir ) ) { |
||
951 | $target_dir = $templates_dir; |
||
952 | } else { |
||
953 | $target_dir = $theme_dir; |
||
954 | } |
||
955 | |||
956 | View Code Duplication | if ( is_writable( $target_dir ) ) : // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable |
|
957 | ?> |
||
958 | <a href="<?php echo esc_url( wp_nonce_url( remove_query_arg( array( 'delete_template', 'saved' ), add_query_arg( 'move_template', $template_type ) ), 'woocommerce_email_template_nonce', '_wc_email_nonce' ) ); ?>" class="button"> |
||
959 | <?php esc_html_e( 'Copy file to theme', 'woocommerce' ); ?> |
||
960 | </a> |
||
961 | <?php endif; ?> |
||
962 | |||
963 | <?php |
||
964 | /* translators: 1: Path to template file 2: Path to theme folder */ |
||
965 | printf( esc_html__( 'To override and edit this email template copy %1$s to your theme folder: %2$s.', 'woocommerce' ), '<code>' . esc_html( plugin_basename( $template_file ) ) . '</code>', '<code>' . esc_html( trailingslashit( basename( get_stylesheet_directory() ) ) . $template_dir . '/' . $template ) . '</code>' ); |
||
966 | ?> |
||
967 | </p> |
||
968 | |||
969 | <div class="editor" style="display:none"> |
||
970 | <textarea class="code" readonly="readonly" disabled="disabled" cols="25" rows="20"><?php echo esc_html( file_get_contents( $template_file ) ); ?></textarea> |
||
971 | </div> |
||
972 | <?php else : ?> |
||
973 | <p><?php esc_html_e( 'File was not found.', 'woocommerce' ); ?></p> |
||
974 | <?php endif; ?> |
||
975 | </div> |
||
976 | <?php endforeach; ?> |
||
977 | </div> |
||
978 | |||
979 | <?php |
||
980 | wc_enqueue_js( |
||
981 | "jQuery( 'select.email_type' ).change( function() { |
||
982 | |||
983 | var val = jQuery( this ).val(); |
||
984 | |||
985 | jQuery( '.template_plain, .template_html' ).show(); |
||
986 | |||
987 | if ( val != 'multipart' && val != 'html' ) { |
||
988 | jQuery('.template_html').hide(); |
||
989 | } |
||
990 | |||
991 | if ( val != 'multipart' && val != 'plain' ) { |
||
992 | jQuery('.template_plain').hide(); |
||
993 | } |
||
994 | |||
995 | }).change(); |
||
996 | |||
997 | var view = '" . esc_js( __( 'View template', 'woocommerce' ) ) . "'; |
||
998 | var hide = '" . esc_js( __( 'Hide template', 'woocommerce' ) ) . "'; |
||
999 | |||
1000 | jQuery( 'a.toggle_editor' ).text( view ).toggle( function() { |
||
1001 | jQuery( this ).text( hide ).closest(' .template' ).find( '.editor' ).slideToggle(); |
||
1002 | return false; |
||
1003 | }, function() { |
||
1004 | jQuery( this ).text( view ).closest( '.template' ).find( '.editor' ).slideToggle(); |
||
1005 | return false; |
||
1006 | } ); |
||
1007 | |||
1008 | jQuery( 'a.delete_template' ).click( function() { |
||
1009 | if ( window.confirm('" . esc_js( __( 'Are you sure you want to delete this template file?', 'woocommerce' ) ) . "') ) { |
||
1010 | return true; |
||
1011 | } |
||
1012 | |||
1013 | return false; |
||
1014 | }); |
||
1015 | |||
1016 | jQuery( '.editor textarea' ).change( function() { |
||
1017 | var name = jQuery( this ).attr( 'data-name' ); |
||
1018 | |||
1019 | if ( name ) { |
||
1020 | jQuery( this ).attr( 'name', name ); |
||
1021 | } |
||
1022 | });" |
||
1023 | ); |
||
1024 | } |
||
1025 | } |
||
1026 | } |
||
1027 |