PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3.1
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.7.3.1, at inc/emails/class-lp-email.php

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