PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.6.9.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.6.9.2
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
learnpress / inc / emails / class-lp-email.php

class-lp-email.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.6.9.2, at inc/emails/class-lp-email.php

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