LLMS_Email

Email Base Class


Source Source

File: includes/emails/class.llms.email.php

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
class LLMS_Email {
 
    /**
     * @var array
     * @since 3.15.0
     */
    private $attachments = array();
 
    /**
     * @var string
     * @since 3.8.0
     */
    protected $body = '';
 
    /**
     * @var string
     * @since 3.8.0
     */
    protected $content_type = 'text/html';
 
    /**
     * @var WP_Post
     * @since 3.26.1
     */
    public $email_post;
 
    /**
     * @var array
     * @since 1.0.0
     */
    private $find = array();
 
    /**
     * @var array
     * @since 3.8.0
     */
    private $headers = array();
 
    /**
     * @var string
     * @since 1.0.0
     */
    protected $heading = '';
 
    /**
     * @var string
     * @since 1.0.0
     */
    protected $id = 'generic';
 
    /**
     * @var array
     * @since 1.0.0
     */
    private $recipient = array();
 
    /**
     * @var array
     * @since 1.0.0
     */
    private $replace = array();
 
    /**
     * @var string
     * @since 1.0.0
     */
    protected $subject = '';
 
    /**
     * @var string
     * @since 3.8.0
     */
    protected $template_html = 'emails/template.php';
 
    /**
     * Initializer
     * Children can configure the email in this function called by the __construct() function
     *
     * @param    array $args  optional arguments passed in from the constructor
     * @return   void
     * @since    3.8.0
     * @version  3.8.0
     */
    protected function init( $args ) {}
 
    /**
     * Constructor
     * Sets up data needed to generate email content
     *
     * @since    1.0.0
     * @version  3.8.0
     */
    public function __construct( $args = array() ) {
 
        $this->add_header( 'Content-Type', $this->get_content_type() );
 
        $this->add_merge_data(
            array(
                '{blogname}'     => get_bloginfo( 'name', 'display' ),
                '{site_title}'   => get_bloginfo( 'name', 'display' ),
                '{divider}'      => llms()->mailer()->get_divider_html(),
                '{button_style}' => llms()->mailer()->get_button_style(),
            )
        );
 
        $this->init( $args );
 
    }
 
    /**
     * Add an attachment to the email
     *
     * @param    string $attachment  full system path to a file to attach
     * @return   void
     * @since    3.15.0
     * @version  3.15.0
     */
    public function add_attachment( $attachment ) {
 
        array_push( $this->attachments, $attachment );
 
    }
 
    /**
     * Add a single header to the email headers array
     *
     * @param    string $key   header key eg: 'Cc'
     * @param    string $val   header value eg: 'noreply@website.tld'
     * @since    3.8.0
     * @version  3.8.0
     */
    public function add_header( $key, $val ) {
 
        array_push( $this->headers, sprintf( '%1$s: %2$s', $key, $val ) );
 
    }
 
    /**
     * Add merge data that will be used in the email
     *
     * @param    array $data    associative array where
     *                             $key = merge field
     *                             $val = merge value
     * @since    3.8.0
     * @version  3.8.0
     */
    public function add_merge_data( $data = array() ) {
 
        foreach ( $data as $find => $replace ) {
 
            array_push( $this->find, $find );
            array_push( $this->replace, $replace );
 
        }
 
    }
 
    /**
     * Add a single recipient for sending to, cc, or bcc
     *
     * @param    int|string $address  if string, must be a valid email address
     *                                if int, must be the WP User ID of a user
     * @param    string     $type     recipient type [to,cc,bcc]
     * @param    string     $name     recipient name (optional)
     * @return   boolean
     * @since    3.8.0
     * @version  3.10.1
     */
    public function add_recipient( $address, $type = 'to', $name = '' ) {
 
        // If an ID was supplied, get the information from the student object.
        if ( is_numeric( $address ) ) {
            $student = llms_get_student( $address );
            if ( ! $student ) {
                return false;
            }
            $address = $student->get( 'user_email' );
            $name    = $student->get_name();
        }
 
        // Ensure address is a valid email.
        if ( ! filter_var( $address, FILTER_VALIDATE_EMAIL ) ) {
            return false;
        }
 
        // If a name is supplied format the name & address.
        if ( $name ) {
            $address = sprintf( '%1$s <%2$s>', $name, $address );
        }
 
        if ( 'to' === $type ) {
 
            array_push( $this->recipient, $address );
            return true;
 
        } elseif ( 'cc' === $type || 'bcc' === $type ) {
 
            $this->add_header( ucfirst( $type ), $address );
            return true;
 
        }
 
        return false;
 
    }
 
    /**
     * Add multiple recipients
     *
     * @param    array $recipients  array of recipient information
     * @return   void
     * @since    3.8.0
     * @version  3.8.0
     */
    public function add_recipients( $recipients = array() ) {
 
        foreach ( $recipients as $data ) {
 
            $data = wp_parse_args(
                $data,
                array(
                    'address' => '',
                    'type'    => 'to',
                    'name'    => '',
                )
            );
 
            if ( $data['address'] ) {
                $this->add_recipient( $data['address'], $data['type'], $data['name'] );
            }
        }
 
    }
 
    /**
     *  Format string method
     *
     *  Finds and replaces merge fields with appropriate data.
     *
     * @since 1.0.0
     * @since 5.0.0 Process shortocdes when formatting a string.
     *
     * @param string $string String to be formatted.
     * @return string
     */
    public function format_string( $string ) {
        return do_shortcode( str_replace( $this->find, $this->replace, $string ) );
    }
 
    /**
     * Get attachments
     *
     * @return   array
     * @since    3.15.0
     * @version  3.15.0
     */
    public function get_attachments() {
        return apply_filters( 'llms_email_get_attachments', $this->attachments, $this );
    }
 
    /**
     * Get the body content of the email
     *
     * @return   string
     * @since    3.8.0
     * @version  3.8.0
     */
    public function get_body() {
        return apply_filters( 'llms_email_body', $this->format_string( $this->body ), $this );
    }
 
    /**
     * Get email content
     *
     * @return string
     * @since    1.0.0
     * @version  3.8.0
     */
    public function get_content() {
 
        $content = apply_filters( 'llms_email_content_get_content', $this->get_content_html(), $this );
        return wordwrap( $content, 70 );
 
    }
 
    /**
     * Get the HTML email content
     *
     * @return   string
     * @since    3.8.0
     * @version  3.26.1
     */
    public function get_content_html() {
 
        global $post;
        $temp = null;
 
        // Override the $post global with the email post content (if it exists).
        // This fixes Elementor / WC conflict outlined at https://github.com/gocodebox/lifterlms/issues/730.
        if ( isset( $this->email_post ) ) {
            $temp = $post;
            $post = $this->email_post;
        }
 
        ob_start();
        llms_get_template(
            $this->template_html,
            array(
                'email_heading' => $this->get_heading(),
                'email_message' => $this->get_body(),
            )
        );
 
        $html = apply_filters( 'llms_email_content_get_content_html', ob_get_clean(), $this );
 
        // Restore the default $post global.
        if ( $temp ) {
            $post = $temp;
        }
 
        return $html;
 
    }
 
    /**
     * Get the content type
     *
     * @return string
     * @since    1.0.0
     * @version  3.8.0
     */
    public function get_content_type() {
        return apply_filters( 'llms_email_content_type', $this->content_type, $this );
    }
 
    /**
     * Get from email option data
     *
     * @since 1.0.0
     * @since 4.0.0 Use the address provided by `wp_mail_from` as the default if no option is stored.
     *
     * @return string
     */
    public function get_from_address( $from_address ) {
        return sanitize_email( get_option( 'lifterlms_email_from_address', $from_address ) );
    }
 
    /**
     * Get from name option data
     *
     * @return   string
     * @since    1.0.0
     * @version  1.0.0
     */
    public function get_from_name() {
        return wp_specialchars_decode( esc_html( get_option( 'lifterlms_email_from_name' ) ), ENT_QUOTES );
    }
 
    /**
     * Get email headers
     *
     * @return   string|array
     * @since    1.0.0
     * @version  3.8.0
     */
    public function get_headers() {
        return apply_filters( 'lifterlms_email_headers', $this->headers, $this->id );
    }
 
    /**
     * Get the text of the email "heading"
     *
     * @return string
     * @since    1.0.0
     * @version  3.8.0
     */
    public function get_heading() {
        return apply_filters( 'lifterlms_email_heading', $this->format_string( $this->heading ), $this );
    }
 
    /**
     * Get recipient email address
     *
     * @return   string|array
     * @since    1.0.0
     * @version  3.8.0
     */
    public function get_recipient() {
        return apply_filters( 'lifterlms_email_recipient', $this->recipient, $this );
    }
 
    /**
     * Get email subject
     *
     * @return   string
     * @since    1.0.0
     * @version  3.8.0
     */
    public function get_subject() {
        return apply_filters( 'lifterlms_email_subject', $this->format_string( $this->subject ), $this );
    }
 
    /**
     * Set the body for the email
     *
     * @param    string $body   text or html body content for the email
     * @return   $this
     * @since    3.8.0
     * @version  3.8.0
     */
    public function set_body( $body = '' ) {
        $this->body = $body;
        return $this;
    }
 
    /**
     * set the content_type for the email
     *
     * @param    string $content_type   content type (for the header)
     * @return   $this
     * @since    3.8.0
     * @version  3.8.0
     */
    public function set_content_type( $content_type = 'text/html' ) {
        $this->content_type = $content_type;
        return $this;
    }
 
    /**
     * set the heading for the email
     *
     * @param    string $heading    text string to use for the email heading
     * @return   $this
     * @since    3.8.0
     * @version  3.8.0
     */
    public function set_heading( $heading = '' ) {
        $this->heading = $heading;
        return $this;
    }
 
    /**
     * Set the ID of the email
     *
     * @param    string $id   id string
     * @return   $this
     * @since    3.8.0
     * @version  3.8.0
     */
    public function set_id( $id = '' ) {
        $this->id = $id;
        return $this;
    }
 
    /**
     * set the subject for the email
     *
     * @param    string $subject Text string to use for the email subject.
     * @return   $this
     * @since    3.8.0
     * @version  3.24.0
     */
    public function set_subject( $subject = '' ) {
        $this->subject = html_entity_decode( $subject, ENT_QUOTES );
        return $this;
    }
 
    /**
     * Send email
     *
     * @return bool
     * @since    1.0.0
     * @version  3.15.0
     */
    public function send() {
 
        do_action( 'lifterlms_email_' . $this->id . '_before_send', $this );
 
        add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
        add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
        add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
 
        $return = wp_mail( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
 
        remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
        remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
        remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
 
        do_action( 'lifterlms_email_' . $this->id . '_after_send', $this, $return );
 
        return $return;
 
    }
 
}


Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
4.0.0 Always supply a from address even if the option is empty.
3.30.3 Explicitly define class properties.
1.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.