PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
← All changes | inc/emails/class-lp-email.php +1052 -1052 4.3.94.4.8 View file →
@@ -1,1052 +1,1052 @@
1 -<?php
2 -/**
3 - * Class Email.
4 - *
5 - * @author ThimPress
6 - * @category Widgets
7 - * @package Learnpress/Email
8 - * @since 3.0.0
9 - * @version 3.0.4
10 - */
11 -
12 -/**
13 - * Prevent loading this file directly
14 - */
15 -
16 -use Pelago\Emogrifier\CssInliner;
17 -use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
18 -
19 -defined( 'ABSPATH' ) || exit();
20 -
21 -if ( ! class_exists( 'LP_Email' ) ) {
22 -
23 - /**
24 - * Class LP_Email
25 - */
26 - class LP_Email extends LP_Abstract_Settings {
27 - /**
28 - * Email method ID.
29 - *
30 - * @var String
31 - */
32 - public $id;
33 -
34 - /**
35 - * Email method title.
36 - *
37 - * @var string
38 - */
39 - public $title;
40 -
41 - /**
42 - * 'yes' if the method is enabled.
43 - *
44 - * @var string
45 - */
46 - //public $enabled;
47 -
48 - /**
49 - * Description for the email.
50 - *
51 - * @var string
52 - */
53 - public $description;
54 -
55 - /**
56 - * Plain text template path.
57 - *
58 - * @var string
59 - */
60 - public $template_plain;
61 -
62 - /**
63 - * HTML template path.
64 - *
65 - * @var string
66 - */
67 - public $template_html = '';
68 -
69 - /**
70 - * Template path.
71 - *
72 - * @var string
73 - */
74 - public $template_base = '';
75 -
76 - /**
77 - * Recipients for the email.
78 - *
79 - * @var string
80 - */
81 - public $recipient;
82 -
83 - /**
84 - * @var bool Enable recipients
85 - *
86 - * @since 4.2.6.4
87 - */
88 - public $enable_recipients = false;
89 -
90 - /**
91 - * For send CC or BB email.
92 - *
93 - * @var string
94 - */
95 - public $recipients = '';
96 -
97 - /**
98 - * Heading for the email content.
99 - *
100 - * @var string
101 - */
102 - public $heading = '';
103 -
104 - /**
105 - * Subject for the email.
106 - *
107 - * @var string
108 - */
109 - public $subject = '';
110 -
111 - /**
112 - * Default heading for the email content.
113 - *
114 - * @var string
115 - */
116 - public $default_heading = '';
117 -
118 - /**
119 - * Default subject for the email.
120 - *
121 - * @var string
122 - */
123 - public $default_subject = '';
124 -
125 - /**
126 - * Default content for the email.
127 - *
128 - * @var string
129 - */
130 - public $content;
131 -
132 - /**
133 - * Object this email is for, for example a customer, product, or email.
134 - *
135 - * @var object
136 - */
137 - public $object;
138 -
139 - /**
140 - * Strings to find in subjects/headings.
141 - *
142 - * @var array
143 - */
144 - public $find;
145 -
146 - /**
147 - * Strings to replace in subjects/headings.
148 - *
149 - * @var array
150 - */
151 - public $replace;
152 -
153 - /**
154 - * Mime boundary (for multipart emails).
155 - *
156 - * @var string
157 - */
158 - public $mime_boundary;
159 -
160 - /**
161 - * Mime boundary header (for multipart emails).
162 - *
163 - * @var string
164 - */
165 - public $mime_boundary_header;
166 -
167 - /**
168 - * True when email is being sent.
169 - *
170 - * @var bool
171 - */
172 - public $sending;
173 -
174 - public $debug = false;
175 -
176 - protected $_object_loaded = false;
177 -
178 - /**
179 - * List of preg* regular expression patterns to search for,
180 - * used in conjunction with $replace.
181 - * https://raw.github.com/ushahidi/wp-silcc/master/class.html2text.inc
182 - *
183 - * @var array $search
184 - * @see $replace
185 - */
186 - public $plain_search = array(
187 - "/\r/", // Non-legal carriage return
188 - '/&(nbsp|#160);/i', // Non-breaking space
189 - '/&(quot|rdquo|ldquo|#8220|#8221|#147|#148);/i', // Double quotes
190 - '/&(apos|rsquo|lsquo|#8216|#8217);/i', // Single quotes
191 - '/&gt;/i', // Greater-than
192 - '/&lt;/i', // Less-than
193 - '/&#38;/i', // Ampersand
194 - '/&#038;/i', // Ampersand
195 - '/&amp;/i', // Ampersand
196 - '/&(copy|#169);/i', // Copyright
197 - '/&(trade|#8482|#153);/i', // Trademark
198 - '/&(reg|#174);/i', // Registered
199 - '/&(mdash|#151|#8212);/i', // mdash
200 - '/&(ndash|minus|#8211|#8722);/i', // ndash
201 - '/&(bull|#149|#8226);/i', // Bullet
202 - '/&(pound|#163);/i', // Pound sign
203 - '/&(euro|#8364);/i', // Euro sign
204 - '/&#36;/', // Dollar sign
205 - '/&[^&\s;]+;/i', // Unknown/unhandled entities
206 - '/[ ]{2,}/', // Runs of spaces, post-handling
207 - );
208 -
209 - /**
210 - * List of pattern replacements corresponding to patterns searched.
211 - *
212 - * @var array $replace
213 - * @see $search
214 - */
215 - public $plain_replace = array(
216 - '', // Non-legal carriage return
217 - ' ', // Non-breaking space
218 - '"', // Double quotes
219 - "'", // Single quotes
220 - '>', // Greater-than
221 - '<', // Less-than
222 - '&', // Ampersand
223 - '&', // Ampersand
224 - '&', // Ampersand
225 - '(c)', // Copyright
226 - '(tm)', // Trademark
227 - '(R)', // Registered
228 - '--', // mdash
229 - '-', // ndash
230 - '*', // Bullet
231 - '�', // Pound sign
232 - 'EUR', // Euro sign. � ?
233 - '$', // Dollar sign
234 - '', // Unknown/unhandled entities
235 - ' ', // Runs of spaces, post-handling
236 - );
237 -
238 - /**
239 - * List of pattern search corresponding to patterns replace.
240 - *
241 - * @var array text shortcode
242 - */
243 - public $text_search = array();
244 -
245 - /**
246 - * List of pattern to replace
247 - *
248 - * @var array text replace
249 - */
250 - public $text_replace = array();
251 -
252 - /**
253 - * Text message description
254 - *
255 - * @var string
256 - */
257 - public $email_text_message_description = '';
258 -
259 - /**
260 - * @var string
261 - */
262 - public $template_path = '';
263 -
264 - /**
265 - * @var null
266 - */
267 - public $variables = array();
268 -
269 - /**
270 - * @var null
271 - */
272 - public $general_variables = [];
273 -
274 - /**
275 - * @var null
276 - */
277 - public $support_variables = [];
278 -
279 - /**
280 - * @var LP_Settings
281 - */
282 - public $settings = null;
283 -
284 - /**
285 - * @var string
286 - */
287 - public $email_format = 'html';
288 -
289 - /**
290 - * @var bool
291 - */
292 - public $enable = false;
293 -
294 - /**
295 - * @var string
296 - */
297 - public $group = '';
298 -
299 - /**
300 - * @var string
301 - */
302 - protected $_option_id = '';
303 -
304 - /**
305 - * LP_Email constructor.
306 - */
307 - public function __construct() {
308 - // Set template base path to LP templates path if it is not set.
309 - if ( empty( $this->template_base ) ) {
310 - $this->template_base = LearnPress::instance()->plugin_path( 'templates/' );
311 - }
312 -
313 - /**
314 - * Set template folder if it is not set. Default is 'learnpress'
315 - */
316 - if ( empty( $this->template_path ) ) {
317 - $this->template_path = learn_press_template_path();
318 - }
319 -
320 - if ( empty( $this->template_html ) ) {
321 - $this->template_html = "emails/{$this->id}.php";
322 - }
323 -
324 - if ( empty( $this->template_plain ) ) {
325 - $this->template_plain = "emails/plain/{$this->id}.php";
326 - }
327 -
328 - if ( ! $this->object ) {
329 - $this->object = array();
330 - }
331 -
332 - $this->_option_id = 'emails_' . $this->id;
333 -
334 - $this->settings = LP_Settings::instance()->get_group( $this->_option_id, '' );
335 -
336 - /**
337 - * Init general options
338 - */
339 - $this->heading = $this->settings->get( 'heading', $this->default_heading );
340 - $this->subject = $this->settings->get( 'subject', $this->default_subject );
341 - $this->enable = $this->settings->get( 'enable', 'no' ) === 'yes';
342 -
343 - /*if ( $this->settings->get( 'email_content.format' ) ) {
344 - $this->email_format = ( $this->settings->get( 'email_content.format' ) == 'plain_text' ) ? 'plain' : 'html';
345 - } else {
346 - if ( LP_Settings::instance()->get( 'emails_general.default_email_content', 'html' ) ) {
347 - $this->email_format = LP_Settings::instance()->get( 'emails_general.default_email_content', 'html' );
348 - }
349 - }*/
350 -
351 - $email_formats = array( 'plain', 'html' );
352 - if ( ! in_array( $this->email_format, $email_formats ) ) {
353 - $this->email_format = 'html';
354 - }
355 -
356 - // Set type variables can click add to content of email setting.
357 - $this->support_variables = [
358 - '{{site_url}}',
359 - '{{site_title}}',
360 - '{{login_url}}',
361 - '{{site_admin_email}}',
362 - '{{site_admin_name}}',
363 - '{{header}}',
364 - '{{footer}}',
365 - '{{footer_text}}',
366 - ];
367 - $this->general_variables = $this->support_variables;
368 - }
369 -
370 - /**
371 - * @param null $value
372 - *
373 - * @return bool
374 - */
375 - public function enable( $value = null ): bool {
376 - if ( is_bool( $value ) ) {
377 - $this->enable = $value;
378 -
379 - // Load default settings if the email is not configured
380 - if ( ! $this->is_configured() ) {
381 - $settings = $this->get_settings();
382 -
383 - foreach ( $settings as $field ) {
384 - if ( $field['type'] == 'heading' || $field['type'] == 'title' || $field['type'] == 'sectionend' ) {
385 - continue;
386 - }
387 -
388 - $id = str_replace( $this->_option_id, '', $field['id'] );
389 - $id = str_replace( array( '[', ']' ), '', $id );
390 -
391 - if ( $id == 'email_content' ) {
392 - $this->settings->set(
393 - 'email_content',
394 - array(
395 - 'format' => 'html',
396 - 'plain' => lp_get_email_content( 'plain', '', $field ),
397 - 'html' => lp_get_email_content( 'html', '', $field ),
398 - )
399 - );
400 - } else {
401 - $this->settings->set( $id, $field['default'] );
402 - }
403 - }
404 - }
405 - $this->settings->set( 'enable', $value ? 'yes' : 'no' );
406 - $this->settings->update( 'learn_press_' . $this->_option_id, $this->settings->get() );
407 - }
408 -
409 - return $this->enable;
410 - }
411 -
412 - /**
413 - * @return mixed
414 - */
415 - public function is_configured() {
416 - return LP_Settings::instance()->get( $this->_option_id );
417 - }
418 -
419 - /**
420 - * Get variables support in mail.
421 - *
422 - * @return mixed
423 - */
424 - public function get_variables_support() {
425 - return apply_filters( 'learn-press/email-variables-support', $this->support_variables, $this );
426 - }
427 -
428 - /**
429 - * Magic function
430 - *
431 - * @param $key
432 - *
433 - * @return mixed
434 - */
435 - public function __get( $key ) {
436 - if ( ! empty( $this->{$key} ) ) {
437 - return $this->{$key};
438 - } else {
439 - return $this->settings->get( $key );
440 - }
441 - }
442 -
443 - /**
444 - * @return bool
445 - */
446 - private function is_current() {
447 - return ! empty( $_REQUEST['section'] ) && sanitize_text_field( $_REQUEST['section'] ) == $this->id;
448 - }
449 -
450 - /**
451 - * @param $options
452 - * @param $key
453 - *
454 - * @return bool
455 - */
456 - public function _remove_email_content_from_option( $options, $key ) {
457 - if ( ! $this->is_current() ) {
458 - return false;
459 - }
460 -
461 - if ( is_array( $options ) && ( array_key_exists( 'email_content_html', $options ) || array_key_exists( 'email_content_plain', $options ) ) ) {
462 -
463 - if ( array_key_exists( 'email_content_html', $options ) ) {
464 - $this->save_template( $options['email_content_html'], $this->template_html );
465 - unset( $options['email_content_html'] );
466 - }
467 -
468 - if ( array_key_exists( 'email_content_plain', $options ) ) {
469 - $this->save_template( $options['email_content_plain'], $this->template_plain );
470 - unset( $options['email_content_plain'] );
471 - }
472 - }
473 -
474 - return $options;
475 - }
476 -
477 - /**
478 - * Apply the value of variables for string
479 - *
480 - * @param string $value
481 - *
482 - * @return string
483 - * @editor tungnx
484 - */
485 - public function format_string( string $value ): string {
486 - $search = [];
487 - $replace = [];
488 - if ( is_array( $this->variables ) ) {
489 - $search = array_keys( $this->variables );
490 - $replace = array_values( $this->variables );
491 - }
492 - $search = apply_filters( 'learn-press/email-format-string-find', $search, $this );
493 - $replace = apply_filters( 'learn-press/email-format-string-replace', $replace, $this );
494 -
495 - return str_replace( $search, $replace, $value );
496 - }
497 -
498 - /**
499 - * Get email recipient.
500 - *
501 - * @return string
502 - */
503 - public function get_recipient() {
504 - return apply_filters( 'learn-press/email-recipient-' . $this->id, $this->recipient, $this->object );
505 - }
506 -
507 - /**
508 - * Get email subject.
509 - *
510 - * @return string
511 - */
512 - public function get_subject(): string {
513 - return $this->format_string( $this->subject );
514 - }
515 -
516 - /**
517 - * Get email content.
518 - *
519 - * @return string
520 - * @editor tungnx
521 - */
522 - public function get_content(): string {
523 - $email_format = $this->get_email_format();
524 -
525 - if ( 'plain' == $email_format ) {
526 - $email_content = preg_replace( $this->plain_search, $this->plain_replace, strip_tags( $this->get_content_plain() ) );
527 - } elseif ( in_array( $email_format, array( 'html', 'multipart' ) ) ) {
528 - $email_content = $this->get_content_html();
529 - } else {
530 - $email_content = preg_replace( $this->text_search, $this->text_replace, $this->get_content_text_message() );
531 - }
532 -
533 - $email_content = $this->format_string( $email_content );
534 -
535 - return wordwrap( $email_content, 70 );
536 - }
537 -
538 - /**
539 - * Get email heading.
540 - *
541 - * @return string
542 - */
543 - public function get_heading(): string {
544 - return $this->format_string( $this->heading );
545 - }
546 -
547 - /**
548 - * Get email footer text.
549 - *
550 - * @return string
551 - */
552 - public function get_footer_text(): string {
553 - $text = LP_Settings::instance()->get( 'emails_general.footer_text', '' );
554 -
555 - return LP_Helper::sanitize_params_submitted( $text, 'html' );
556 - }
557 -
558 - /**
559 - * Get email content HTML.
560 - *
561 - * @return string
562 - */
563 - public function get_content_html(): string {
564 - $template = $this->get_template( 'template_html' );
565 - $template_file = $this->template_base . $template;
566 - $content = $this->settings->get( 'email_content.html', file_get_contents( $template_file ) );
567 -
568 - return stripslashes( $content );
569 - }
570 -
571 - /**
572 - * Get email content plain.
573 - *
574 - * @return string
575 - */
576 - public function get_content_plain(): string {
577 - $template = $this->get_template( 'template_plain' );
578 - $template_file = $this->template_base . $template;
579 - $content_tmp = LP_WP_Filesystem::instance()->file_get_contents( $template_file );
580 - $content = $this->settings->get( 'email_content.plain', $content_tmp );
581 - $content = stripslashes( $content );
582 -
583 - return $content;
584 - }
585 -
586 - /**
587 - * Get content context message.
588 - *
589 - * @return string
590 - */
591 - public function get_content_text_message(): string {
592 - return apply_filters( 'learn-press/email-text-message-' . $this->id, $this->settings->get( 'content_text_message' ) );
593 - }
594 -
595 - /**
596 - * Get email headers.
597 - *
598 - * @return string|array
599 - */
600 - public function get_headers() {
601 - $headers = [
602 - 'Content-Type: ' . $this->get_content_format() . "\r\n",
603 - ];
604 -
605 - $recipients = $this->settings->get( 'recipients', $this->recipients );
606 -
607 - if ( ! empty( $recipients ) ) {
608 - $cc_emails = explode( ',', $recipients );
609 - foreach ( $cc_emails as $cc_email ) {
610 - $headers[] = 'Cc: ' . $cc_email;
611 - }
612 - }
613 -
614 - return apply_filters( 'learn-press/email-headers', $headers, $this );
615 - }
616 -
617 - /**
618 - * Get email attachments.
619 - *
620 - * @return array
621 - */
622 - public function get_attachments() {
623 - return apply_filters( 'learn-press/email-attachments', array(), $this->id, $this->object );
624 - }
625 -
626 - /**
627 - * Get FROM address. Default is admin email.
628 - *
629 - * @return string
630 - * @editor tungnx
631 - * @version 1.0.1
632 - * @editor tungnx
633 - * @modify 4.1.4 - comment - not override hook
634 - */
635 - /*public function get_from_address(): string {
636 - $email = LP_Settings::instance()->get( 'emails_general.from_email', get_option( 'admin_email' ) );
637 -
638 - return sanitize_email( $email );
639 - }*/
640 -
641 - /**
642 - * Get FROM name. Default is Blog name.
643 - *
644 - * @return string
645 - * @editor tungnx
646 - * @version 1.0.1
647 - */
648 - public function get_from_name(): string {
649 - $name = LP_Settings::instance()->get( 'emails_general.from_name', get_option( 'blogname' ) );
650 -
651 - return LP_Helper::sanitize_params_submitted( $name );
652 - }
653 -
654 - /**
655 - * Get WP Blog name.
656 - *
657 - * @return string
658 - */
659 - public function get_blogname() {
660 - return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
661 - }
662 -
663 - /**
664 - * Get email content format.
665 - *
666 - * @return string
667 - */
668 - public function get_content_format() {
669 - switch ( $this->get_email_format() ) {
670 - case 'text_message':
671 - case 'html':
672 - return 'text/html';
673 - case 'multipart':
674 - return 'multipart/alternative';
675 - default:
676 - return 'text/plain';
677 - }
678 - }
679 -
680 - /**
681 - * @return string
682 - */
683 - public function get_email_format() {
684 - return $this->email_format && class_exists( 'DOMDocument' ) ? $this->email_format : 'plain';
685 - }
686 -
687 - /**
688 - * Apply css from external to inline.
689 - *
690 - * @param string $content
691 - *
692 - * @return string
693 - */
694 - public function apply_style_inline( $content ): string {
695 - try {
696 - if ( ! in_array( $this->get_content_format(), array( 'text/html', 'multipart/alternative' ) ) ) {
697 - return $content;
698 - }
699 -
700 - // Get CSS styles
701 - ob_start();
702 - learn_press_get_template( 'emails/email-styles.php' );
703 - $css = apply_filters( 'learn_press_email_styles', ob_get_clean(), $this->id, $this );
704 -
705 - // Apply CSS styles inline for picky email clients
706 - //$content = CssInliner::fromHtml( $content )->inlineCss( $css )->render();
707 - $cssToInlineStyles = new CssToInlineStyles();
708 - $content = $cssToInlineStyles->convert( $content, $css );
709 - } catch ( Exception $e ) {
710 -
711 - }
712 -
713 - return apply_filters( 'learn-press/email-content-inline-style', $content, $this->id );
714 - }
715 -
716 - /**
717 - * Get template file from type.
718 - *
719 - * @param string $type
720 - *
721 - * @return string
722 - */
723 - public function get_template( $type ) {
724 - $type = esc_attr( basename( $type ) );
725 -
726 - if ( 'template_html' == $type ) {
727 - return $this->template_html;
728 - } elseif ( 'template_plain' == $type ) {
729 - return $this->template_plain;
730 - }
731 -
732 - return '';
733 - }
734 -
735 - protected function save_template( $code, $path ) {
736 - return;
737 - }
738 -
739 - /**
740 - * Get template file.
741 - *
742 - * @param string $template
743 - * @param string $template_path
744 - *
745 - * @return string
746 - */
747 - public function get_theme_template_file( $template, $template_path = null ) {
748 - $template_dir = apply_filters( 'learn-press/template-directory', $template_path ? $template_path : learn_press_template_path(), $template );
749 -
750 - return join(
751 - '',
752 - array(
753 - trailingslashit( get_stylesheet_directory() ),
754 - trailingslashit( $template_dir ),
755 - $template,
756 - )
757 - );
758 - }
759 -
760 - /**
761 - * Send email.
762 - *
763 - * @param string|array $to
764 - * @param string $subject
765 - * @param string $message
766 - * @param string|string[] $headers
767 - * @param array $attachments
768 - *
769 - * @editor tungnx
770 - * @return bool
771 - * @version 1.0.1
772 - *
773 - */
774 - public function send( $to, string $subject, string $message, $headers, array $attachments ): bool {
775 - $return = false;
776 - $message = apply_filters( 'learn-press/email-content', $this->apply_style_inline( $message ), $this );
777 -
778 - if ( is_string( $to ) ) {
779 - $to = preg_split( '~\s?,\s?~', $to );
780 -
781 - $return = wp_mail( $to, $subject, $message, $headers, $attachments );
782 - } elseif ( is_array( $to ) ) {
783 - foreach ( $to as $t ) {
784 - $return = wp_mail( $t, $subject, $message, $headers, $attachments );
785 - }
786 - }
787 -
788 - return $return;
789 - }
790 -
791 - /**
792 - * Get all values set and send email
793 - *
794 - * @author tungnx
795 - * @since 4.1.3
796 - */
797 - public function send_email(): bool {
798 - add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
799 - add_filter( 'wp_mail_content_type', array( $this, 'get_content_format' ) );
800 -
801 - return $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
802 - }
803 -
804 - /**
805 - * Get common variables.
806 - * Variables use for click add to content of Email
807 - *
808 - * @param string $format
809 - *
810 - * @return array
811 - * @author tungnx
812 - * @since 4.1.1
813 - */
814 - public function get_common_variables( string $format = 'plain' ): array {
815 - $heading = strip_tags( $this->get_heading() );
816 - $footer_text = strip_tags( $this->get_footer_text() );
817 -
818 - $header = $heading;
819 - $footer = $footer_text;
820 -
821 - if ( $format != 'plain' ) {
822 - $header = $this->email_header( $heading );
823 - $footer = $this->email_footer( $footer_text );
824 - }
825 -
826 - $admin_user = get_user_by( 'email', get_option( 'admin_email' ) );
827 -
828 - return apply_filters(
829 - 'email_variables_common',
830 - [
831 - '{{header}}' => $header,
832 - '{{footer}}' => $footer,
833 - '{{footer_text}}' => $footer_text,
834 - '{{site_url}}' => get_home_url(),
835 - '{{site_title}}' => $this->get_blogname(),
836 - '{{site_admin_email}}' => get_option( 'admin_email' ),
837 - '{{site_admin_name}}' => learn_press_get_profile_display_name( $admin_user ),
838 - '{{login_url}}' => learn_press_get_login_url(),
839 - '{{plain_text}}' => $format == 'plain',
840 - ]
841 - );
842 - }
843 -
844 - /**
845 - * @param string $name
846 - *
847 - * @return string
848 - */
849 - public function get_field_name( $name ) {
850 - return $this->_option_id . "[$name]";
851 - }
852 -
853 - /**
854 - * Default options for all emails.
855 - * Almost the emails are the same with options.
856 - *
857 - * @return array
858 - */
859 - protected function _default_settings(): array {
860 - /**
861 - * In case the email is not for sending to specific admin (like user who has bought course or author of course, etc..)
862 - * So, we do not need this field.
863 - */
864 -
865 - $enable_recipients = $this->enable_recipients;
866 -
867 - $default = array_merge(
868 - array(
869 - array(
870 - 'type' => 'title',
871 - ),
872 - array(
873 - 'title' => esc_html__( 'Enable/Disable', 'learnpress' ),
874 - 'type' => 'checkbox',
875 - 'default' => 'no',
876 - 'id' => $this->get_field_name( 'enable' ),
877 - 'desc' => $this->description,
878 - ),
879 - ),
880 - $enable_recipients ? array(
881 - array(
882 - 'title' => esc_html__( 'Recipient(s)', 'learnpress' ),
883 - 'type' => 'text',
884 - 'default' => $this->recipients,
885 - 'id' => $this->get_field_name( 'recipients' ),
886 - 'desc' => esc_html__( 'Separate other recipients with commas.', 'learnpress' ),
887 - ),
888 - ) : array(),
889 - array(
890 - array(
891 - 'title' => esc_html__( 'Subject', 'learnpress' ),
892 - 'type' => 'text',
893 - 'default' => $this->default_subject,
894 - 'id' => $this->get_field_name( 'subject' ),
895 - 'css' => 'width:400px',
896 - ),
897 - array(
898 - 'title' => esc_html__( 'Email heading', 'learnpress' ),
899 - 'type' => 'text',
900 - 'default' => $this->default_heading,
901 - 'id' => $this->get_field_name( 'heading' ),
902 - 'css' => 'width:400px',
903 - ),
904 - array(
905 - 'title' => esc_html__( 'Content', 'learnpress' ),
906 - 'type' => 'email-content',
907 - 'default' => '',
908 - 'id' => $this->get_field_name( 'email_content' ),
909 - 'template_base' => $this->template_base,
910 - 'template_path' => $this->template_path,
911 - 'template_html' => $this->template_html,
912 - 'template_plain' => $this->template_plain,
913 - 'template_html_local' => $this->get_theme_template_file( 'html', $this->template_path ),
914 - 'template_plain_local' => $this->get_theme_template_file( 'plain', $this->template_path ),
915 - 'support_variables' => $this->get_variables_support(),
916 - ),
917 - array(
918 - 'type' => 'sectionend',
919 - ),
920 - )
921 - );
922 -
923 - return $default;
924 - }
925 -
926 - /**
927 - * Get settings in admin.
928 - *
929 - * @return bool|mixed
930 - * @since 3.0.0
931 - *
932 - */
933 - public function get_settings() {
934 - return apply_filters( 'learn-press/email-settings/' . $this->id . '/settings', $this->_default_settings() );
935 - }
936 -
937 - /**
938 - * Get instructors to send mail.
939 - *
940 - * @param null $order_id
941 - *
942 - * @return array
943 - * @since 3.0.0
944 - *
945 - */
946 - public function get_order_instructors( $order_id ) {
947 - if ( ! $order_id ) {
948 - return array();
949 - }
950 -
951 - $order = learn_press_get_order( $order_id );
952 -
953 - $items = $order->get_items();
954 - $instructors = array();
955 -
956 - if ( count( $items ) ) {
957 - foreach ( $items as $item ) {
958 - if ( ! isset( $item['course_id'] ) ) {
959 - continue;
960 - }
961 -
962 - $user_id = get_post_field( 'post_author', $item['course_id'] );
963 - if ( $user_id ) {
964 - $instructors[] = $user_id;
965 - }
966 - }
967 - }
968 -
969 - return $instructors;
970 - }
971 -
972 - protected function _get_admin_email() {
973 - return apply_filters( 'learn-press/email/admin-email', get_option( 'admin_email' ) );
974 - }
975 -
976 - /**
977 - * @return string
978 - */
979 - public function __toString() {
980 - return $this->title;
981 - }
982 -
983 - /**
984 - * Get image header in general settings.
985 - *
986 - * @return string
987 - */
988 - public function get_image_header(): string {
989 - $image = LP_Settings::instance()->get( 'emails_general.header_image', '' );
990 -
991 - if ( ! empty( $image ) ) {
992 - $image = wp_get_attachment_image_url( $image, 'full' );
993 - }
994 -
995 - return $image;
996 - }
997 -
998 - /**
999 - * Email header.
1000 - *
1001 - * @param string $heading
1002 - *
1003 - * @return string
1004 - */
1005 - public function email_header( string $heading ): string {
1006 - ob_start();
1007 - learn_press_get_template(
1008 - 'emails/email-header.php',
1009 - [
1010 - 'email_heading' => $heading,
1011 - 'image_header' => $this->get_image_header(),
1012 - ]
1013 - );
1014 -
1015 - return ob_get_clean();
1016 - }
1017 -
1018 - /**
1019 - * Email footer.
1020 - *
1021 - * @param string $footer_text
1022 - *
1023 - * @return string
1024 - */
1025 - public function email_footer( string $footer_text ): string {
1026 - ob_start();
1027 - learn_press_get_template( 'emails/email-footer.php', array( 'footer_text' => $footer_text ) );
1028 -
1029 - return ob_get_clean();
1030 - }
1031 -
1032 - /**
1033 - * Set receive email
1034 - *
1035 - * @param string $receive_email
1036 - */
1037 - public function set_receive( string $receive_email ) {
1038 - $this->recipient = $receive_email;
1039 - }
1040 -
1041 - /**
1042 - * Method called by background on LP_Background_Single_Email
1043 - *
1044 - * @param array $params
1045 - *
1046 - * @see LP_Background_Single_Email::handle()
1047 - *
1048 - */
1049 - public function handle( array $params ) {
1050 - }
1051 - }
1052 -}
1 +<?php
2 +/**
3 + * Class Email.
4 + *
5 + * @author ThimPress
6 + * @category Widgets
7 + * @package Learnpress/Email
8 + * @since 3.0.0
9 + * @version 3.0.4
10 + */
11 +
12 +/**
13 + * Prevent loading this file directly
14 + */
15 +
16 +use Pelago\Emogrifier\CssInliner;
17 +use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
18 +
19 +defined( 'ABSPATH' ) || exit();
20 +
21 +if ( ! class_exists( 'LP_Email' ) ) {
22 +
23 + /**
24 + * Class LP_Email
25 + */
26 + class LP_Email extends LP_Abstract_Settings {
27 + /**
28 + * Email method ID.
29 + *
30 + * @var String
31 + */
32 + public $id;
33 +
34 + /**
35 + * Email method title.
36 + *
37 + * @var string
38 + */
39 + public $title;
40 +
41 + /**
42 + * 'yes' if the method is enabled.
43 + *
44 + * @var string
45 + */
46 + //public $enabled;
47 +
48 + /**
49 + * Description for the email.
50 + *
51 + * @var string
52 + */
53 + public $description;
54 +
55 + /**
56 + * Plain text template path.
57 + *
58 + * @var string
59 + */
60 + public $template_plain;
61 +
62 + /**
63 + * HTML template path.
64 + *
65 + * @var string
66 + */
67 + public $template_html = '';
68 +
69 + /**
70 + * Template path.
71 + *
72 + * @var string
73 + */
74 + public $template_base = '';
75 +
76 + /**
77 + * Recipients for the email.
78 + *
79 + * @var string
80 + */
81 + public $recipient;
82 +
83 + /**
84 + * @var bool Enable recipients
85 + *
86 + * @since 4.2.6.4
87 + */
88 + public $enable_recipients = false;
89 +
90 + /**
91 + * For send CC or BB email.
92 + *
93 + * @var string
94 + */
95 + public $recipients = '';
96 +
97 + /**
98 + * Heading for the email content.
99 + *
100 + * @var string
101 + */
102 + public $heading = '';
103 +
104 + /**
105 + * Subject for the email.
106 + *
107 + * @var string
108 + */
109 + public $subject = '';
110 +
111 + /**
112 + * Default heading for the email content.
113 + *
114 + * @var string
115 + */
116 + public $default_heading = '';
117 +
118 + /**
119 + * Default subject for the email.
120 + *
121 + * @var string
122 + */
123 + public $default_subject = '';
124 +
125 + /**
126 + * Default content for the email.
127 + *
128 + * @var string
129 + */
130 + public $content;
131 +
132 + /**
133 + * Object this email is for, for example a customer, product, or email.
134 + *
135 + * @var object
136 + */
137 + public $object;
138 +
139 + /**
140 + * Strings to find in subjects/headings.
141 + *
142 + * @var array
143 + */
144 + public $find;
145 +
146 + /**
147 + * Strings to replace in subjects/headings.
148 + *
149 + * @var array
150 + */
151 + public $replace;
152 +
153 + /**
154 + * Mime boundary (for multipart emails).
155 + *
156 + * @var string
157 + */
158 + public $mime_boundary;
159 +
160 + /**
161 + * Mime boundary header (for multipart emails).
162 + *
163 + * @var string
164 + */
165 + public $mime_boundary_header;
166 +
167 + /**
168 + * True when email is being sent.
169 + *
170 + * @var bool
171 + */
172 + public $sending;
173 +
174 + public $debug = false;
175 +
176 + protected $_object_loaded = false;
177 +
178 + /**
179 + * List of preg* regular expression patterns to search for,
180 + * used in conjunction with $replace.
181 + * https://raw.github.com/ushahidi/wp-silcc/master/class.html2text.inc
182 + *
183 + * @var array $search
184 + * @see $replace
185 + */
186 + public $plain_search = array(
187 + "/\r/", // Non-legal carriage return
188 + '/&(nbsp|#160);/i', // Non-breaking space
189 + '/&(quot|rdquo|ldquo|#8220|#8221|#147|#148);/i', // Double quotes
190 + '/&(apos|rsquo|lsquo|#8216|#8217);/i', // Single quotes
191 + '/&gt;/i', // Greater-than
192 + '/&lt;/i', // Less-than
193 + '/&#38;/i', // Ampersand
194 + '/&#038;/i', // Ampersand
195 + '/&amp;/i', // Ampersand
196 + '/&(copy|#169);/i', // Copyright
197 + '/&(trade|#8482|#153);/i', // Trademark
198 + '/&(reg|#174);/i', // Registered
199 + '/&(mdash|#151|#8212);/i', // mdash
200 + '/&(ndash|minus|#8211|#8722);/i', // ndash
201 + '/&(bull|#149|#8226);/i', // Bullet
202 + '/&(pound|#163);/i', // Pound sign
203 + '/&(euro|#8364);/i', // Euro sign
204 + '/&#36;/', // Dollar sign
205 + '/&[^&\s;]+;/i', // Unknown/unhandled entities
206 + '/[ ]{2,}/', // Runs of spaces, post-handling
207 + );
208 +
209 + /**
210 + * List of pattern replacements corresponding to patterns searched.
211 + *
212 + * @var array $replace
213 + * @see $search
214 + */
215 + public $plain_replace = array(
216 + '', // Non-legal carriage return
217 + ' ', // Non-breaking space
218 + '"', // Double quotes
219 + "'", // Single quotes
220 + '>', // Greater-than
221 + '<', // Less-than
222 + '&', // Ampersand
223 + '&', // Ampersand
224 + '&', // Ampersand
225 + '(c)', // Copyright
226 + '(tm)', // Trademark
227 + '(R)', // Registered
228 + '--', // mdash
229 + '-', // ndash
230 + '*', // Bullet
231 + '�', // Pound sign
232 + 'EUR', // Euro sign. � ?
233 + '$', // Dollar sign
234 + '', // Unknown/unhandled entities
235 + ' ', // Runs of spaces, post-handling
236 + );
237 +
238 + /**
239 + * List of pattern search corresponding to patterns replace.
240 + *
241 + * @var array text shortcode
242 + */
243 + public $text_search = array();
244 +
245 + /**
246 + * List of pattern to replace
247 + *
248 + * @var array text replace
249 + */
250 + public $text_replace = array();
251 +
252 + /**
253 + * Text message description
254 + *
255 + * @var string
256 + */
257 + public $email_text_message_description = '';
258 +
259 + /**
260 + * @var string
261 + */
262 + public $template_path = '';
263 +
264 + /**
265 + * @var null
266 + */
267 + public $variables = array();
268 +
269 + /**
270 + * @var null
271 + */
272 + public $general_variables = [];
273 +
274 + /**
275 + * @var null
276 + */
277 + public $support_variables = [];
278 +
279 + /**
280 + * @var LP_Settings
281 + */
282 + public $settings = null;
283 +
284 + /**
285 + * @var string
286 + */
287 + public $email_format = 'html';
288 +
289 + /**
290 + * @var bool
291 + */
292 + public $enable = false;
293 +
294 + /**
295 + * @var string
296 + */
297 + public $group = '';
298 +
299 + /**
300 + * @var string
301 + */
302 + protected $_option_id = '';
303 +
304 + /**
305 + * LP_Email constructor.
306 + */
307 + public function __construct() {
308 + // Set template base path to LP templates path if it is not set.
309 + if ( empty( $this->template_base ) ) {
310 + $this->template_base = LearnPress::instance()->plugin_path( 'templates/' );
311 + }
312 +
313 + /**
314 + * Set template folder if it is not set. Default is 'learnpress'
315 + */
316 + if ( empty( $this->template_path ) ) {
317 + $this->template_path = learn_press_template_path();
318 + }
319 +
320 + if ( empty( $this->template_html ) ) {
321 + $this->template_html = "emails/{$this->id}.php";
322 + }
323 +
324 + if ( empty( $this->template_plain ) ) {
325 + $this->template_plain = "emails/plain/{$this->id}.php";
326 + }
327 +
328 + if ( ! $this->object ) {
329 + $this->object = array();
330 + }
331 +
332 + $this->_option_id = 'emails_' . $this->id;
333 +
334 + $this->settings = LP_Settings::instance()->get_group( $this->_option_id, '' );
335 +
336 + /**
337 + * Init general options
338 + */
339 + $this->heading = $this->settings->get( 'heading', $this->default_heading );
340 + $this->subject = $this->settings->get( 'subject', $this->default_subject );
341 + $this->enable = $this->settings->get( 'enable', 'no' ) === 'yes';
342 +
343 + /*if ( $this->settings->get( 'email_content.format' ) ) {
344 + $this->email_format = ( $this->settings->get( 'email_content.format' ) == 'plain_text' ) ? 'plain' : 'html';
345 + } else {
346 + if ( LP_Settings::instance()->get( 'emails_general.default_email_content', 'html' ) ) {
347 + $this->email_format = LP_Settings::instance()->get( 'emails_general.default_email_content', 'html' );
348 + }
349 + }*/
350 +
351 + $email_formats = array( 'plain', 'html' );
352 + if ( ! in_array( $this->email_format, $email_formats ) ) {
353 + $this->email_format = 'html';
354 + }
355 +
356 + // Set type variables can click add to content of email setting.
357 + $this->support_variables = [
358 + '{{site_url}}',
359 + '{{site_title}}',
360 + '{{login_url}}',
361 + '{{site_admin_email}}',
362 + '{{site_admin_name}}',
363 + '{{header}}',
364 + '{{footer}}',
365 + '{{footer_text}}',
366 + ];
367 + $this->general_variables = $this->support_variables;
368 + }
369 +
370 + /**
371 + * @param null $value
372 + *
373 + * @return bool
374 + */
375 + public function enable( $value = null ): bool {
376 + if ( is_bool( $value ) ) {
377 + $this->enable = $value;
378 +
379 + // Load default settings if the email is not configured
380 + if ( ! $this->is_configured() ) {
381 + $settings = $this->get_settings();
382 +
383 + foreach ( $settings as $field ) {
384 + if ( $field['type'] == 'heading' || $field['type'] == 'title' || $field['type'] == 'sectionend' ) {
385 + continue;
386 + }
387 +
388 + $id = str_replace( $this->_option_id, '', $field['id'] );
389 + $id = str_replace( array( '[', ']' ), '', $id );
390 +
391 + if ( $id == 'email_content' ) {
392 + $this->settings->set(
393 + 'email_content',
394 + array(
395 + 'format' => 'html',
396 + 'plain' => lp_get_email_content( 'plain', '', $field ),
397 + 'html' => lp_get_email_content( 'html', '', $field ),
398 + )
399 + );
400 + } else {
401 + $this->settings->set( $id, $field['default'] );
402 + }
403 + }
404 + }
405 + $this->settings->set( 'enable', $value ? 'yes' : 'no' );
406 + $this->settings->update( 'learn_press_' . $this->_option_id, $this->settings->get() );
407 + }
408 +
409 + return $this->enable;
410 + }
411 +
412 + /**
413 + * @return mixed
414 + */
415 + public function is_configured() {
416 + return LP_Settings::instance()->get( $this->_option_id );
417 + }
418 +
419 + /**
420 + * Get variables support in mail.
421 + *
422 + * @return mixed
423 + */
424 + public function get_variables_support() {
425 + return apply_filters( 'learn-press/email-variables-support', $this->support_variables, $this );
426 + }
427 +
428 + /**
429 + * Magic function
430 + *
431 + * @param $key
432 + *
433 + * @return mixed
434 + */
435 + public function __get( $key ) {
436 + if ( ! empty( $this->{$key} ) ) {
437 + return $this->{$key};
438 + } else {
439 + return $this->settings->get( $key );
440 + }
441 + }
442 +
443 + /**
444 + * @return bool
445 + */
446 + private function is_current() {
447 + return ! empty( $_REQUEST['section'] ) && sanitize_text_field( $_REQUEST['section'] ) == $this->id;
448 + }
449 +
450 + /**
451 + * @param $options
452 + * @param $key
453 + *
454 + * @return bool
455 + */
456 + public function _remove_email_content_from_option( $options, $key ) {
457 + if ( ! $this->is_current() ) {
458 + return false;
459 + }
460 +
461 + if ( is_array( $options ) && ( array_key_exists( 'email_content_html', $options ) || array_key_exists( 'email_content_plain', $options ) ) ) {
462 +
463 + if ( array_key_exists( 'email_content_html', $options ) ) {
464 + $this->save_template( $options['email_content_html'], $this->template_html );
465 + unset( $options['email_content_html'] );
466 + }
467 +
468 + if ( array_key_exists( 'email_content_plain', $options ) ) {
469 + $this->save_template( $options['email_content_plain'], $this->template_plain );
470 + unset( $options['email_content_plain'] );
471 + }
472 + }
473 +
474 + return $options;
475 + }
476 +
477 + /**
478 + * Apply the value of variables for string
479 + *
480 + * @param string $value
481 + *
482 + * @return string
483 + * @editor tungnx
484 + */
485 + public function format_string( string $value ): string {
486 + $search = [];
487 + $replace = [];
488 + if ( is_array( $this->variables ) ) {
489 + $search = array_keys( $this->variables );
490 + $replace = array_values( $this->variables );
491 + }
492 + $search = apply_filters( 'learn-press/email-format-string-find', $search, $this );
493 + $replace = apply_filters( 'learn-press/email-format-string-replace', $replace, $this );
494 +
495 + return str_replace( $search, $replace, $value );
496 + }
497 +
498 + /**
499 + * Get email recipient.
500 + *
501 + * @return string
502 + */
503 + public function get_recipient() {
504 + return apply_filters( 'learn-press/email-recipient-' . $this->id, $this->recipient, $this->object );
505 + }
506 +
507 + /**
508 + * Get email subject.
509 + *
510 + * @return string
511 + */
512 + public function get_subject(): string {
513 + return $this->format_string( $this->subject );
514 + }
515 +
516 + /**
517 + * Get email content.
518 + *
519 + * @return string
520 + * @editor tungnx
521 + */
522 + public function get_content(): string {
523 + $email_format = $this->get_email_format();
524 +
525 + if ( 'plain' == $email_format ) {
526 + $email_content = preg_replace( $this->plain_search, $this->plain_replace, strip_tags( $this->get_content_plain() ) );
527 + } elseif ( in_array( $email_format, array( 'html', 'multipart' ) ) ) {
528 + $email_content = $this->get_content_html();
529 + } else {
530 + $email_content = preg_replace( $this->text_search, $this->text_replace, $this->get_content_text_message() );
531 + }
532 +
533 + $email_content = $this->format_string( $email_content );
534 +
535 + return wordwrap( $email_content, 70 );
536 + }
537 +
538 + /**
539 + * Get email heading.
540 + *
541 + * @return string
542 + */
543 + public function get_heading(): string {
544 + return $this->format_string( $this->heading );
545 + }
546 +
547 + /**
548 + * Get email footer text.
549 + *
550 + * @return string
551 + */
552 + public function get_footer_text(): string {
553 + $text = LP_Settings::instance()->get( 'emails_general.footer_text', '' );
554 +
555 + return LP_Helper::sanitize_params_submitted( $text, 'html' );
556 + }
557 +
558 + /**
559 + * Get email content HTML.
560 + *
561 + * @return string
562 + */
563 + public function get_content_html(): string {
564 + $template = $this->get_template( 'template_html' );
565 + $template_file = $this->template_base . $template;
566 + $content = $this->settings->get( 'email_content.html', file_get_contents( $template_file ) );
567 +
568 + return stripslashes( $content );
569 + }
570 +
571 + /**
572 + * Get email content plain.
573 + *
574 + * @return string
575 + */
576 + public function get_content_plain(): string {
577 + $template = $this->get_template( 'template_plain' );
578 + $template_file = $this->template_base . $template;
579 + $content_tmp = LP_WP_Filesystem::instance()->file_get_contents( $template_file );
580 + $content = $this->settings->get( 'email_content.plain', $content_tmp );
581 + $content = stripslashes( $content );
582 +
583 + return $content;
584 + }
585 +
586 + /**
587 + * Get content context message.
588 + *
589 + * @return string
590 + */
591 + public function get_content_text_message(): string {
592 + return apply_filters( 'learn-press/email-text-message-' . $this->id, $this->settings->get( 'content_text_message' ) );
593 + }
594 +
595 + /**
596 + * Get email headers.
597 + *
598 + * @return string|array
599 + */
600 + public function get_headers() {
601 + $headers = [
602 + 'Content-Type: ' . $this->get_content_format() . "\r\n",
603 + ];
604 +
605 + $recipients = $this->settings->get( 'recipients', $this->recipients );
606 +
607 + if ( ! empty( $recipients ) ) {
608 + $cc_emails = explode( ',', $recipients );
609 + foreach ( $cc_emails as $cc_email ) {
610 + $headers[] = 'Cc: ' . $cc_email;
611 + }
612 + }
613 +
614 + return apply_filters( 'learn-press/email-headers', $headers, $this );
615 + }
616 +
617 + /**
618 + * Get email attachments.
619 + *
620 + * @return array
621 + */
622 + public function get_attachments() {
623 + return apply_filters( 'learn-press/email-attachments', array(), $this->id, $this->object );
624 + }
625 +
626 + /**
627 + * Get FROM address. Default is admin email.
628 + *
629 + * @return string
630 + * @editor tungnx
631 + * @version 1.0.1
632 + * @editor tungnx
633 + * @modify 4.1.4 - comment - not override hook
634 + */
635 + /*public function get_from_address(): string {
636 + $email = LP_Settings::instance()->get( 'emails_general.from_email', get_option( 'admin_email' ) );
637 +
638 + return sanitize_email( $email );
639 + }*/
640 +
641 + /**
642 + * Get FROM name. Default is Blog name.
643 + *
644 + * @return string
645 + * @editor tungnx
646 + * @version 1.0.1
647 + */
648 + public function get_from_name(): string {
649 + $name = LP_Settings::instance()->get( 'emails_general.from_name', get_option( 'blogname' ) );
650 +
651 + return LP_Helper::sanitize_params_submitted( $name );
652 + }
653 +
654 + /**
655 + * Get WP Blog name.
656 + *
657 + * @return string
658 + */
659 + public function get_blogname() {
660 + return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
661 + }
662 +
663 + /**
664 + * Get email content format.
665 + *
666 + * @return string
667 + */
668 + public function get_content_format() {
669 + switch ( $this->get_email_format() ) {
670 + case 'text_message':
671 + case 'html':
672 + return 'text/html';
673 + case 'multipart':
674 + return 'multipart/alternative';
675 + default:
676 + return 'text/plain';
677 + }
678 + }
679 +
680 + /**
681 + * @return string
682 + */
683 + public function get_email_format() {
684 + return $this->email_format && class_exists( 'DOMDocument' ) ? $this->email_format : 'plain';
685 + }
686 +
687 + /**
688 + * Apply css from external to inline.
689 + *
690 + * @param string $content
691 + *
692 + * @return string
693 + */
694 + public function apply_style_inline( $content ): string {
695 + try {
696 + if ( ! in_array( $this->get_content_format(), array( 'text/html', 'multipart/alternative' ) ) ) {
697 + return $content;
698 + }
699 +
700 + // Get CSS styles
701 + ob_start();
702 + learn_press_get_template( 'emails/email-styles.php' );
703 + $css = apply_filters( 'learn_press_email_styles', ob_get_clean(), $this->id, $this );
704 +
705 + // Apply CSS styles inline for picky email clients
706 + //$content = CssInliner::fromHtml( $content )->inlineCss( $css )->render();
707 + $cssToInlineStyles = new CssToInlineStyles();
708 + $content = $cssToInlineStyles->convert( $content, $css );
709 + } catch ( Exception $e ) {
710 +
711 + }
712 +
713 + return apply_filters( 'learn-press/email-content-inline-style', $content, $this->id );
714 + }
715 +
716 + /**
717 + * Get template file from type.
718 + *
719 + * @param string $type
720 + *
721 + * @return string
722 + */
723 + public function get_template( $type ) {
724 + $type = esc_attr( basename( $type ) );
725 +
726 + if ( 'template_html' == $type ) {
727 + return $this->template_html;
728 + } elseif ( 'template_plain' == $type ) {
729 + return $this->template_plain;
730 + }
731 +
732 + return '';
733 + }
734 +
735 + protected function save_template( $code, $path ) {
736 + return;
737 + }
738 +
739 + /**
740 + * Get template file.
741 + *
742 + * @param string $template
743 + * @param string $template_path
744 + *
745 + * @return string
746 + */
747 + public function get_theme_template_file( $template, $template_path = null ) {
748 + $template_dir = apply_filters( 'learn-press/template-directory', $template_path ? $template_path : learn_press_template_path(), $template );
749 +
750 + return join(
751 + '',
752 + array(
753 + trailingslashit( get_stylesheet_directory() ),
754 + trailingslashit( $template_dir ),
755 + $template,
756 + )
757 + );
758 + }
759 +
760 + /**
761 + * Send email.
762 + *
763 + * @param string|array $to
764 + * @param string $subject
765 + * @param string $message
766 + * @param string|string[] $headers
767 + * @param array $attachments
768 + *
769 + * @editor tungnx
770 + * @return bool
771 + * @version 1.0.1
772 + *
773 + */
774 + public function send( $to, string $subject, string $message, $headers, array $attachments ): bool {
775 + $return = false;
776 + $message = apply_filters( 'learn-press/email-content', $this->apply_style_inline( $message ), $this );
777 +
778 + if ( is_string( $to ) ) {
779 + $to = preg_split( '~\s?,\s?~', $to );
780 +
781 + $return = wp_mail( $to, $subject, $message, $headers, $attachments );
782 + } elseif ( is_array( $to ) ) {
783 + foreach ( $to as $t ) {
784 + $return = wp_mail( $t, $subject, $message, $headers, $attachments );
785 + }
786 + }
787 +
788 + return $return;
789 + }
790 +
791 + /**
792 + * Get all values set and send email
793 + *
794 + * @author tungnx
795 + * @since 4.1.3
796 + */
797 + public function send_email(): bool {
798 + add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
799 + add_filter( 'wp_mail_content_type', array( $this, 'get_content_format' ) );
800 +
801 + return $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
802 + }
803 +
804 + /**
805 + * Get common variables.
806 + * Variables use for click add to content of Email
807 + *
808 + * @param string $format
809 + *
810 + * @return array
811 + * @author tungnx
812 + * @since 4.1.1
813 + */
814 + public function get_common_variables( string $format = 'plain' ): array {
815 + $heading = strip_tags( $this->get_heading() );
816 + $footer_text = strip_tags( $this->get_footer_text() );
817 +
818 + $header = $heading;
819 + $footer = $footer_text;
820 +
821 + if ( $format != 'plain' ) {
822 + $header = $this->email_header( $heading );
823 + $footer = $this->email_footer( $footer_text );
824 + }
825 +
826 + $admin_user = get_user_by( 'email', get_option( 'admin_email' ) );
827 +
828 + return apply_filters(
829 + 'email_variables_common',
830 + [
831 + '{{header}}' => $header,
832 + '{{footer}}' => $footer,
833 + '{{footer_text}}' => $footer_text,
834 + '{{site_url}}' => get_home_url(),
835 + '{{site_title}}' => $this->get_blogname(),
836 + '{{site_admin_email}}' => get_option( 'admin_email' ),
837 + '{{site_admin_name}}' => learn_press_get_profile_display_name( $admin_user ),
838 + '{{login_url}}' => learn_press_get_login_url(),
839 + '{{plain_text}}' => $format == 'plain',
840 + ]
841 + );
842 + }
843 +
844 + /**
845 + * @param string $name
846 + *
847 + * @return string
848 + */
849 + public function get_field_name( $name ) {
850 + return $this->_option_id . "[$name]";
851 + }
852 +
853 + /**
854 + * Default options for all emails.
855 + * Almost the emails are the same with options.
856 + *
857 + * @return array
858 + */
859 + protected function _default_settings(): array {
860 + /**
861 + * In case the email is not for sending to specific admin (like user who has bought course or author of course, etc..)
862 + * So, we do not need this field.
863 + */
864 +
865 + $enable_recipients = $this->enable_recipients;
866 +
867 + $default = array_merge(
868 + array(
869 + array(
870 + 'type' => 'title',
871 + ),
872 + array(
873 + 'title' => esc_html__( 'Enable/Disable', 'learnpress' ),
874 + 'type' => 'checkbox',
875 + 'default' => 'no',
876 + 'id' => $this->get_field_name( 'enable' ),
877 + 'desc' => $this->description,
878 + ),
879 + ),
880 + $enable_recipients ? array(
881 + array(
882 + 'title' => esc_html__( 'Recipient(s)', 'learnpress' ),
883 + 'type' => 'text',
884 + 'default' => $this->recipients,
885 + 'id' => $this->get_field_name( 'recipients' ),
886 + 'desc' => esc_html__( 'Separate other recipients with commas.', 'learnpress' ),
887 + ),
888 + ) : array(),
889 + array(
890 + array(
891 + 'title' => esc_html__( 'Subject', 'learnpress' ),
892 + 'type' => 'text',
893 + 'default' => $this->default_subject,
894 + 'id' => $this->get_field_name( 'subject' ),
895 + 'css' => 'width:400px',
896 + ),
897 + array(
898 + 'title' => esc_html__( 'Email heading', 'learnpress' ),
899 + 'type' => 'text',
900 + 'default' => $this->default_heading,
901 + 'id' => $this->get_field_name( 'heading' ),
902 + 'css' => 'width:400px',
903 + ),
904 + array(
905 + 'title' => esc_html__( 'Content', 'learnpress' ),
906 + 'type' => 'email-content',
907 + 'default' => '',
908 + 'id' => $this->get_field_name( 'email_content' ),
909 + 'template_base' => $this->template_base,
910 + 'template_path' => $this->template_path,
911 + 'template_html' => $this->template_html,
912 + 'template_plain' => $this->template_plain,
913 + 'template_html_local' => $this->get_theme_template_file( 'html', $this->template_path ),
914 + 'template_plain_local' => $this->get_theme_template_file( 'plain', $this->template_path ),
915 + 'support_variables' => $this->get_variables_support(),
916 + ),
917 + array(
918 + 'type' => 'sectionend',
919 + ),
920 + )
921 + );
922 +
923 + return $default;
924 + }
925 +
926 + /**
927 + * Get settings in admin.
928 + *
929 + * @return bool|mixed
930 + * @since 3.0.0
931 + *
932 + */
933 + public function get_settings() {
934 + return apply_filters( 'learn-press/email-settings/' . $this->id . '/settings', $this->_default_settings() );
935 + }
936 +
937 + /**
938 + * Get instructors to send mail.
939 + *
940 + * @param null $order_id
941 + *
942 + * @return array
943 + * @since 3.0.0
944 + *
945 + */
946 + public function get_order_instructors( $order_id ) {
947 + if ( ! $order_id ) {
948 + return array();
949 + }
950 +
951 + $order = learn_press_get_order( $order_id );
952 +
953 + $items = $order->get_items();
954 + $instructors = array();
955 +
956 + if ( count( $items ) ) {
957 + foreach ( $items as $item ) {
958 + if ( ! isset( $item['course_id'] ) ) {
959 + continue;
960 + }
961 +
962 + $user_id = get_post_field( 'post_author', $item['course_id'] );
963 + if ( $user_id ) {
964 + $instructors[] = $user_id;
965 + }
966 + }
967 + }
968 +
969 + return $instructors;
970 + }
971 +
972 + protected function _get_admin_email() {
973 + return apply_filters( 'learn-press/email/admin-email', get_option( 'admin_email' ) );
974 + }
975 +
976 + /**
977 + * @return string
978 + */
979 + public function __toString() {
980 + return $this->title;
981 + }
982 +
983 + /**
984 + * Get image header in general settings.
985 + *
986 + * @return string
987 + */
988 + public function get_image_header(): string {
989 + $image = LP_Settings::instance()->get( 'emails_general.header_image', '' );
990 +
991 + if ( ! empty( $image ) ) {
992 + $image = wp_get_attachment_image_url( $image, 'full' );
993 + }
994 +
995 + return $image;
996 + }
997 +
998 + /**
999 + * Email header.
1000 + *
1001 + * @param string $heading
1002 + *
1003 + * @return string
1004 + */
1005 + public function email_header( string $heading ): string {
1006 + ob_start();
1007 + learn_press_get_template(
1008 + 'emails/email-header.php',
1009 + [
1010 + 'email_heading' => $heading,
1011 + 'image_header' => $this->get_image_header(),
1012 + ]
1013 + );
1014 +
1015 + return ob_get_clean();
1016 + }
1017 +
1018 + /**
1019 + * Email footer.
1020 + *
1021 + * @param string $footer_text
1022 + *
1023 + * @return string
1024 + */
1025 + public function email_footer( string $footer_text ): string {
1026 + ob_start();
1027 + learn_press_get_template( 'emails/email-footer.php', array( 'footer_text' => $footer_text ) );
1028 +
1029 + return ob_get_clean();
1030 + }
1031 +
1032 + /**
1033 + * Set receive email
1034 + *
1035 + * @param string $receive_email
1036 + */
1037 + public function set_receive( string $receive_email ) {
1038 + $this->recipient = $receive_email;
1039 + }
1040 +
1041 + /**
1042 + * Method called by background on LP_Background_Single_Email
1043 + *
1044 + * @param array $params
1045 + *
1046 + * @see LP_Background_Single_Email::handle()
1047 + *
1048 + */
1049 + public function handle( array $params ) {
1050 + }
1051 + }
1052 +}