class-mail.php
19.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
<?php
namespace um\core;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'um\core\Mail' ) ) {
/**
* Class Mail
* @package um\core
*/
class Mail {
/**
* @var array
*/
public $email_templates = array();
/**
* @var array
*/
public $path_by_slug = array();
/**
* @var array
*/
public $attachments = array();
/**
* @var string
*/
public $headers = '';
/**
* @var string
*/
public $subject = '';
/**
* @var string
*/
public $message = '';
/**
* Mail constructor.
*/
public function __construct() {
//mandrill compatibility
add_filter( 'mandrill_nl2br', array( &$this, 'mandrill_nl2br' ) );
add_action( 'plugins_loaded', array( &$this, 'init_paths' ), 99 );
}
/**
* Mandrill compatibility
*
* @param $nl2br
* @param string $message
* @return bool
*/
public function mandrill_nl2br( $nl2br, $message = '' ) {
// text emails
if ( ! UM()->options()->get( 'email_html' ) ) {
$nl2br = true;
}
return $nl2br;
}
/**
* Init paths for email notifications
*/
public function init_paths() {
/**
* UM hook
*
* @type filter
* @title um_email_templates_path_by_slug
* @description Extend email templates path
* @input_vars
* [{"var":"$paths","type":"array","desc":"Email slug -> Template Path"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_email_templates_path_by_slug', 'function_name', 10, 1 ); ?>
* @example
* <?php
* add_filter( 'um_email_templates_path_by_slug', 'my_email_templates_path_by_slug', 10, 1 );
* function my_email_templates_path_by_slug( $paths ) {
* // your code here
* return $paths;
* }
* ?>
*/
$this->path_by_slug = apply_filters( 'um_email_templates_path_by_slug', $this->path_by_slug );
}
/**
* Check blog ID on multisite, return '' if single site
*
* @return string
*/
public function get_blog_id() {
$blog_id = '';
if ( is_multisite() ) {
$blog_id = '/' . get_current_blog_id();
}
return $blog_id;
}
/**
* Locate a template and return the path for inclusion.
*
* @access public
* @param string $template_name
* @return string
*/
public function locate_template( $template_name ) {
// check if there is template at theme folder
$blog_id = $this->get_blog_id();
//get template file from current blog ID folder
$template = locate_template(
array(
trailingslashit( 'ultimate-member/email' . $blog_id ) . $template_name . '.php',
)
);
// If there isn't template at theme folder for current blog ID get template file from theme folder
if ( is_multisite() && ! $template ) {
$template = locate_template(
array(
trailingslashit( 'ultimate-member/email' ) . $template_name . '.php',
)
);
}
// If there isn't template at theme folder, get template file from plugin dir
if ( ! $template ) {
$path = ! empty( $this->path_by_slug[ $template_name ] ) ? $this->path_by_slug[ $template_name ] : um_path . 'templates/email';
$template = trailingslashit( $path ) . $template_name . '.php';
}
// Return what we found.
/**
* UM hook
*
* @type filter
* @title um_locate_email_template
* @description Change email notification template path
* @input_vars
* [{"var":"$template","type":"string","desc":"Template Path"},
* {"var":"$template_name","type":"string","desc":"Template Name"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_locate_email_template', 'function_name', 10, 2 ); ?>
* @example
* <?php
* add_filter( 'um_locate_email_template', 'my_locate_email_template', 10, 2 );
* function my_email_template_body_attrs( $template, $template_name ) {
* // your code here
* return $template;
* }
* ?>
*/
return apply_filters( 'um_locate_email_template', $template, $template_name );
}
/**
* @param $slug
* @param $args
* @return bool|string
*/
public function get_email_template( $slug, $args = array() ) {
$located = $this->locate_template( $slug );
/**
* UM hook
*
* @type filter
* @title um_email_template_path
* @description Change email template location
* @input_vars
* [{"var":"$located","type":"string","desc":"Template Location"},
* {"var":"$slug","type":"string","desc":"Template Key"},
* {"var":"$args","type":"array","desc":"Template settings"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_email_template_path', 'function_name', 10, 3 ); ?>
* @example
* <?php
* add_filter( 'um_email_template_path', 'my_email_send_subject', 10, 3 );
* function my_email_send_subject( $located, $slug, $args ) {
* // your code here
* return $located;
* }
* ?>
*/
$located = apply_filters( 'um_email_template_path', $located, $slug, $args );
if ( ! file_exists( $located ) ) {
_doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' );
return false;
}
ob_start();
/**
* UM hook
*
* @type action
* @title um_before_email_template_part
* @description Action before email template loading
* @input_vars
* [{"var":"$slug","type":"string","desc":"Email template slug"},
* {"var":"$located","type":"string","desc":"Email template location"},
* {"var":"$args","type":"array","desc":"Email template arguments"}]
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_before_email_template_part', 'function_name', 10, 3 );
* @example
* <?php
* add_action( 'um_before_email_template_part', 'my_before_email_template_part', 10, 3 );
* function my_before_email_template_part( $slug, $located, $args ) {
* // your code here
* }
* ?>
*/
do_action( 'um_before_email_template_part', $slug, $located, $args );
include( $located );
/**
* UM hook
*
* @type action
* @title um_after_email_template_part
* @description Action after email template loading
* @input_vars
* [{"var":"$slug","type":"string","desc":"Email template slug"},
* {"var":"$located","type":"string","desc":"Email template location"},
* {"var":"$args","type":"array","desc":"Email template arguments"}]
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_after_email_template_part', 'function_name', 10, 3 );
* @example
* <?php
* add_action( 'um_after_email_template_part', 'my_after_email_template_part', 10, 3 );
* function my_after_email_template_part( $slug, $located, $args ) {
* // your code here
* }
* ?>
*/
do_action( 'um_after_email_template_part', $slug, $located, $args );
return ob_get_clean();
}
/**
* Prepare email template to send
*
* @param $slug
* @param $args
* @return mixed|string
*/
function prepare_template( $slug, $args = array() ) {
ob_start();
if ( UM()->options()->get( 'email_html' ) ) {
/**
* UM hook
*
* @type filter
* @title um_email_template_html_formatting
* @description Change email notification template header
* @input_vars
* [{"var":"$header","type":"string","desc":"Email notification header. '<html>' by default"},
* {"var":"$slug","type":"string","desc":"Template Key"},
* {"var":"$args","type":"array","desc":"Template settings"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_email_template_html_formatting', 'function_name', 10, 3 ); ?>
* @example
* <?php
* add_filter( 'um_email_template_html_formatting', 'my_email_template_html_formatting', 10, 3 );
* function my_email_template_html_formatting( $header, $slug, $args ) {
* // your code here
* return $header;
* }
* ?>
*/
echo apply_filters( 'um_email_template_html_formatting', '<html>', $slug, $args );
/**
* UM hook
*
* @type action
* @title um_before_email_template_body
* @description Action before email template body display
* @input_vars
* [{"var":"$slug","type":"string","desc":"Email template slug"},
* {"var":"$args","type":"array","desc":"Email template arguments"}]
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_before_email_template_body', 'function_name', 10, 2 );
* @example
* <?php
* add_action( 'um_before_email_template_body', 'my_before_email_template_body', 10, 2 );
* function my_before_email_template_body( $slug, $args ) {
* // your code here
* }
* ?>
*/
do_action( 'um_before_email_template_body', $slug, $args );
/**
* UM hook
*
* @type filter
* @title um_email_template_body_attrs
* @description Change email notification template body additional attributes
* @input_vars
* [{"var":"$body_atts","type":"string","desc":"Email notification body attributes"},
* {"var":"$slug","type":"string","desc":"Template Key"},
* {"var":"$args","type":"array","desc":"Template settings"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_email_template_body_attrs', 'function_name', 10, 3 ); ?>
* @example
* <?php
* add_filter( 'um_email_template_body_attrs', 'my_email_template_body_attrs', 10, 3 );
* function my_email_template_body_attrs( $body_atts, $slug, $args ) {
* // your code here
* return $body_atts;
* }
* ?>
*/
$body_attrs = apply_filters( 'um_email_template_body_attrs', 'style="background: #f2f2f2;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;"', $slug, $args );
?>
<body <?php echo $body_attrs ?>>
<?php echo $this->get_email_template( $slug, $args ); ?>
</body>
</html>
<?php } else {
//strip tags in plain text email
//important don't use HTML in plain text emails!
$raw_email_template = $this->get_email_template( $slug, $args );
$plain_email_template = strip_tags( $raw_email_template );
if( $plain_email_template !== $raw_email_template ){
$plain_email_template = preg_replace( array('/ /mi', '/^\s+/mi'), array(' ', ''), $plain_email_template );
}
echo $plain_email_template;
}
$message = ob_get_clean();
/**
* UM hook
*
* @type filter
* @title um_email_send_message_content
* @description Change email notification message content
* @input_vars
* [{"var":"$message","type":"string","desc":"Message Content"},
* {"var":"$template","type":"string","desc":"Template Key"},
* {"var":"$args","type":"string","desc":"Notification Arguments"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_email_send_message_content', 'function_name', 10, 3 ); ?>
* @example
* <?php
* add_filter( 'um_email_send_message_content', 'my_email_send_message_content', 10, 3 );
* function my_email_send_message_content( $message, $template, $args ) {
* // your code here
* return $message;
* }
* ?>
*/
$message = apply_filters( 'um_email_send_message_content', $message, $slug, $args );
add_filter( 'um_template_tags_patterns_hook', array( &$this, 'add_placeholder' ), 10, 1 );
add_filter( 'um_template_tags_replaces_hook', array( &$this, 'add_replace_placeholder' ), 10, 1 );
// Convert tags in email template
return um_convert_tags( $message, $args );
}
/**
* Send Email function
*
* @param string $email
* @param null $template
* @param array $args
*/
public function send( $email, $template, $args = array() ) {
if ( ! is_email( $email ) ) {
return;
}
if ( empty( UM()->options()->get( $template . '_on' ) ) ) {
return;
}
/**
* Filters for disabling email notifications programmatically.
*
* @since 2.6.1
* @hook um_disable_email_notification_sending
*
* @param {bool} $disabled Does an email is disabled programmatically. By default it is false.
* @param {string} $email Email address for sending.
* @param {string} $template Email template key.
* @param {array} $args Arguments for sending email.
*
* @return {bool} `true` if email is disabled programmatically.
*/
$hook_disabled = apply_filters( 'um_disable_email_notification_sending', false, $email, $template, $args );
if ( false !== $hook_disabled ) {
return;
}
do_action( 'um_before_email_notification_sending', $email, $template, $args );
$this->attachments = array();
$mail_from = UM()->options()->get( 'mail_from' ) ? UM()->options()->get( 'mail_from' ) : get_bloginfo( 'name' );
$mail_from_addr = UM()->options()->get( 'mail_from_addr' ) ? UM()->options()->get( 'mail_from_addr' ) : get_bloginfo( 'admin_email' );
$this->headers = 'From: ' . stripslashes( $mail_from ) . ' <' . $mail_from_addr . '>' . "\r\n";
add_filter( 'um_template_tags_patterns_hook', array( UM()->mail(), 'add_placeholder' ), 10, 1 );
add_filter( 'um_template_tags_replaces_hook', array( UM()->mail(), 'add_replace_placeholder' ), 10, 1 );
/**
* UM hook
*
* @type filter
* @title um_email_send_subject
* @description Change email notification subject
* @input_vars
* [{"var":"$subject","type":"string","desc":"Subject"},
* {"var":"$key","type":"string","desc":"Template Key"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_email_send_subject', 'function_name', 10, 2 ); ?>
* @example
* <?php
* add_filter( 'um_email_send_subject', 'my_email_send_subject', 10, 2 );
* function my_email_send_subject( $subject, $key ) {
* // your code here
* return $paths;
* }
* ?>
*/
$subject = apply_filters( 'um_email_send_subject', UM()->options()->get( $template . '_sub' ), $template );
$subject = wp_unslash( um_convert_tags( $subject, $args ) );
$this->subject = html_entity_decode( $subject, ENT_QUOTES, 'UTF-8' );
$this->message = $this->prepare_template( $template, $args );
if ( UM()->options()->get( 'email_html' ) ) {
$this->headers .= "Content-Type: text/html\r\n";
} else {
$this->headers .= "Content-Type: text/plain\r\n";
}
// Send mail
wp_mail( $email, $this->subject, $this->message, $this->headers, $this->attachments );
do_action( 'um_after_email_notification_sending', $email, $template, $args );
}
/**
* @param $template_name
*
* @return mixed|void
*/
public function get_template_filename( $template_name ) {
/**
* UM hook
*
* @type filter
* @title um_change_email_template_file
* @description Change email notification template path
* @input_vars
* [{"var":"$template_name","type":"string","desc":"Template Name"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_change_email_template_file', 'function_name', 10, 1 ); ?>
* @example
* <?php
* add_filter( 'um_change_email_template_file', 'my_change_email_template_file', 10, 1 );
* function my_change_email_template_file( $template, $template_name ) {
* // your code here
* return $template;
* }
* ?>
*/
return apply_filters( 'um_change_email_template_file', $template_name );
}
/**
* Locate a template and return the path for inclusion.
*
* @access public
* @param string $template_name
* @return string
*/
public function template_in_theme( $template_name ) {
$template_name_file = $this->get_template_filename( $template_name );
$blog_id = $this->get_blog_id();
// check if there is a template at theme blog ID folder
$template = locate_template(
array(
trailingslashit( 'ultimate-member/email' . $blog_id ) . $template_name_file . '.php',
)
);
// Return what we found.
if ( get_template_directory() === get_stylesheet_directory() ) {
return ! $template ? false : true;
} else {
return strstr( $template, get_stylesheet_directory() );
}
}
/**
* Method returns an expected path for template
*
* @access public
*
* @param string $location
* @param string $template_name
*
* @return string
*/
public function get_template_file( $location, $template_name ) {
$template_path = '';
$template_name_file = $this->get_template_filename( $template_name );
switch ( $location ) {
case 'theme':
//save email template in blog ID folder if we use multisite
$blog_id = $this->get_blog_id();
$template_path = trailingslashit( get_stylesheet_directory() . '/ultimate-member/email' . $blog_id ) . $template_name_file . '.php';
break;
case 'plugin':
$path = ! empty( $this->path_by_slug[ $template_name ] ) ? $this->path_by_slug[ $template_name ] : um_path . 'templates/email';
$template_path = trailingslashit( $path ) . $template_name . '.php';
break;
}
/**
* Filters an expected path for email template in theme or plugin.
*
* @since 2.6.1
* @hook um_email_get_template_file_path
*
* @param {string} $template_path Expected template path.
* @param {string} $location Where to search 'theme||plugin'.
* @param {string} $template_name_file Expected filename.
*
* @return {string} Expected template path.
*/
return apply_filters( 'um_email_get_template_file_path', $template_path, $location, $template_name_file );
}
/**
* Copy template to the theme
*
* @param string $template
* @return bool
*/
public function copy_email_template( $template ) {
$in_theme = $this->template_in_theme( $template );
if ( $in_theme ) {
return false;
}
$plugin_template_path = wp_normalize_path( $this->get_template_file( 'plugin', $template ) );
$theme_template_path = wp_normalize_path( $this->get_template_file( 'theme', $template ) );
$template_filename = $this->get_template_filename( $template ) . '.php';
$template_dir = wp_normalize_path( str_replace( $template_filename, '', $theme_template_path ) );
$result = wp_mkdir_p( $template_dir );
if ( ! $result ) {
return false;
}
if ( file_exists( $plugin_template_path ) && copy( $plugin_template_path, $theme_template_path ) ) {
return true;
} else {
return false;
}
}
/**
* UM Placeholders for site url, admin email, submit registration
*
* @param $placeholders
*
* @return array
*/
public function add_placeholder( $placeholders ) {
$placeholders[] = '{user_profile_link}';
$placeholders[] = '{site_url}';
$placeholders[] = '{admin_email}';
$placeholders[] = '{submitted_registration}';
$placeholders[] = '{login_url}';
$placeholders[] = '{password}';
$placeholders[] = '{account_activation_link}';
return $placeholders;
}
/**
* UM Replace Placeholders for site url, admin email, submit registration
*
* @param $replace_placeholders
*
* @return array
*/
public function add_replace_placeholder( $replace_placeholders ) {
$replace_placeholders[] = um_user_profile_url();
$replace_placeholders[] = get_bloginfo( 'url' );
$replace_placeholders[] = um_admin_email();
$replace_placeholders[] = um_user_submitted_registration_formatted();
$replace_placeholders[] = um_get_core_page( 'login' );
$replace_placeholders[] = esc_html__( 'Your set password', 'ultimate-member' );
$replace_placeholders[] = um_user( 'account_activation_link' );
return $replace_placeholders;
}
}
}