-
Notifications
You must be signed in to change notification settings - Fork 22
/
email-templates.php
executable file
·852 lines (737 loc) · 31.3 KB
/
email-templates.php
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
<?php
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Email Templates
*
* @link https://www.wpexperts.io/
* @since 2.0
* @package Mailtpl
*
* @wordpress-plugin
* Plugin Name: Email Templates
* Plugin URI: http://wordpress.org/plugins/email-templates
* Description: Beautify WordPress default emails
* Version: 1.4
* Author: wpexpertsio
* Author URI: https://www.wpexperts.io/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: email-templates
* Domain Path: /languages
*/
if ( ! class_exists( 'Mailtpl_Woomail_Composer' ) ) {
class Mailtpl_Woomail_Composer {
/**
* Instance Control
*
* @var null
*/
private static $instance = null;
/**
* User Role
*
* @var null
*/
private static $admin_capability = null;
/**
* Overide Var
*
* @var null
*/
private static $overwrite_options = null;
/**
* Instance Control
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Construct
*/
public function __construct() {
global $woo_send;
$woo_send = true;
define( 'MAILTPL_VERSION' , '1.4');
define( 'MAILTPL_PLUGIN_FILE' , __FILE__);
define( 'MAILTPL_PLUGIN_DIR' , plugin_dir_path(__FILE__) );
define( 'MAILTPL_WOOMAIL_URL' , plugin_dir_url(__FILE__) );
define( 'MAILTPL_PLUGIN_HOOK' , basename( dirname( __FILE__ ) ) . '/' . basename( __FILE__ ) );
define( 'MAILTPL_WOOMAIL_PATH', realpath( plugin_dir_path( __FILE__ ) ) . DIRECTORY_SEPARATOR );
add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ) );
add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded_woomail' ) );
}
/**
* Function on plugins loaded
*/
public function on_plugins_loaded() {
require plugin_dir_path( __FILE__ ) . 'includes/class-mailtpl.php';
$plugin = Mailtpl::instance();
$plugin->run();
}
public function on_plugins_loaded_woomail() {
if ( ! mailtpl_woomail_is_woo_active() ) {
add_action( 'admin_notices', array( $this, 'admin_notice_need_woocommerce' ) );
return;
}
require_once MAILTPL_WOOMAIL_PATH . 'includes/class-mailtpl-woomail-settings.php'; // Gets all settings.
require_once MAILTPL_WOOMAIL_PATH . 'includes/class-mailtpl-woomail-customizer.php'; // Gets custom customizer set up.
require_once MAILTPL_WOOMAIL_PATH . 'includes/class-mailtpl-woomail-import-export.php'; // Adds Customizer control.
require_once MAILTPL_WOOMAIL_PATH . 'includes/class-customizer-range-value-control.php'; // Adds Customizer control.
require_once MAILTPL_WOOMAIL_PATH . 'includes/class-customizer-mailtpltemplateload-control.php'; // Adds Customizer control.
require_once MAILTPL_WOOMAIL_PATH . 'includes/class-customizer-mailtplsendemail-control.php'; // Adds Customizer control.
require_once MAILTPL_WOOMAIL_PATH . 'includes/class-customizer-repeater-control.php'; // Adds Customizer control.
require_once MAILTPL_WOOMAIL_PATH . 'includes/class-customizer-mailtplinfoblock-control.php'; // Adds Customizer control.
require_once MAILTPL_WOOMAIL_PATH . 'includes/class-customizer-mailtplimportexport-control.php'; // Adds Customizer control.
require_once MAILTPL_WOOMAIL_PATH . 'includes/class-customizer-toggle-control.php'; // Adds customizer control.
require_once MAILTPL_WOOMAIL_PATH . 'includes/class-mailtpl-woomail-preview.php'; // Builds the email preview for customizer.
require_once MAILTPL_WOOMAIL_PATH . 'includes/class-mailtpl-woomail-woo.php'; // Add settings to woocommerce email settings page.
add_action( 'init', array( $this, 'on_init' ), 80 );
// Get translation set up.
load_plugin_textdomain( 'mailtpl-woocommerce-email-composer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
// Add link for plugin page.
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugins_page_links' ) );
}
/**
* Trigger Load on init.
*/
public function on_init() {
// Remove the woocommerce call for email header.
if ( function_exists( 'WC' ) ) {
remove_action( 'woocommerce_email_header', array( WC()->mailer(), 'email_header' ) );
// $remove_schema = Mailtpl_Woomail_Customizer::opt( 'email_schema' );
// if ( true == $remove_schema ) {
// remove_action( 'woocommerce_email_order_details', array( WC()->structured_data, 'output_email_structured_data' ), 30 ); // This removes structured data from all Emails sent by WooCommerce.
// }
}
// Add our custom call for email header.
add_action( 'woocommerce_email_header', array( $this, 'add_email_header' ), 20, 2 );
// Use our templates instead of woocommerce.
add_filter( 'woocommerce_locate_template', array( $this, 'filter_locate_template' ), 10, 3 );
// Add extra placeholder support for subject and title fields.
add_filter( 'woocommerce_email_format_string', array( $this, 'add_extra_placeholders' ), 20, 2 );
// Hook in main text areas for customized emails.
add_action( 'Mailtpl_Woomailemail_details', array( $this, 'email_main_text_area' ), 10, 4 );
// Hook in main text areas for customized emails.
add_action( 'Mailtpl_Woomailemail_text', array( $this, 'email_main_text_area_no_order' ), 10, 1 );
// Hook in footer container.
add_action( 'Mailtpl_Woomailemail_footer', array( $this, 'email_footer_content' ), 100 );
// hook in email photo option.
add_filter( 'woocommerce_email_order_items_args', array( $this, 'add_wc_order_email_args_images' ), 10 );
// Hook for replacing {year} in email-footer.
add_filter( 'woocommerce_email_footer_text', array( $this, 'email_footer_replace_year' ) );
add_filter( 'woocommerce_email_setup_locale', array( $this, 'switch_to_site_locale' ) );
add_filter( 'woocommerce_email_restore_locale', array( $this, 'restore_to_user_locale' ) );
//add_filter( 'woocommerce_email_styles', array( $this, 'check_to_add_gmail_hack' ), 50, 2 );
//require_once MAILTPL_WOOMAIL_PATH . 'includes/class-kwed-cartflows-ca-email.php'; // Add CartFlows, soon.
// Forces the WordPress to use the correct language file if switched.
add_action( 'change_locale', array( $this, 'load_plugin_textdomain' ) );
}
/**
* Adds the filter for email hack if this filter runs.
*
* @param string $css the Email css.
* @param object $email the Email object.
* @return string the Email css.
*/
public function check_to_add_gmail_hack( $css, $email = '' ) {
add_filter( 'woocommerce_mail_content', array( $this, 'add_gmail_hack' ), 50 );
return $css;
}
/**
* Adds a bit of css to fix a rendering issue where gmail breaks email templates.
*
* @param string $content the Email content.
* @return string Email content with string possibly added on.
*/
public function add_gmail_hack( $content ) {
$responsive_check = Mailtpl_Woomail_Customizer::opt( 'responsive_mode' );
if ( true != $responsive_check ) {
$content = '<style type="text/css">.gm-remove-late{ display:none;}</style>' . $content;
}
remove_filter( 'woocommerce_mail_content', array( $this, 'add_gmail_hack' ), 50 );
return $content;
}
/**
* Filter callback to replace {year} in email footer
*
* @param string $string Email footer text.
* @return string Email footer text with any replacements done.
*/
public function email_footer_replace_year( $string ) {
return str_replace( '{year}', date( 'Y' ), $string );
}
/**
* Add a notice about woocommerce being needed.
*
* @param array $args the order detials args.
*/
public function add_wc_order_email_args_images( $args ) {
$product_photo = Mailtpl_Woomail_Customizer::opt( 'order_items_image' );
$size = Mailtpl_Woomail_Customizer::opt( 'order_items_image_size' );
if ( 'show' === $product_photo ) {
$args['show_image'] = true;
if ( '100x100' === $size ) {
$args['image_size'] = array( 100, 100 );
} else if ( '150x150' === $size ) {
$args['image_size'] = array( 150, 150 );
} else if ( '40x40' === $size ) {
$args['image_size'] = array( 40, 40 );
} else if ( '50x50' === $size ) {
$args['image_size'] = array( 50, 50 );
} else if ( 'woocommerce_thumbnail' === $size ) {
$args['image_size'] = 'woocommerce_thumbnail';
} else {
$args['image_size'] = array( 100, 50 );
}
}
return $args;
}
/**
* Add a notice about woocommerce being needed.
*/
public function admin_notice_need_woocommerce() {
echo '<div class="notice notice-error is-dismissible">';
echo '<p>' . esc_html__( 'Mailtpl Woocommerce Email Composer requires WooCommerce to be active to work', 'mailtpl-woocommerce-email-composer' ) . '</p>';
echo '</div>';
}
/**
* Set up the footer content
*/
public function email_footer_content() {
$content_width = Mailtpl_Woomail_Customizer::opt( 'content_width' );
if ( empty( $content_width ) ) {
$content_width = '600';
}
$content_width = str_replace( 'px', '', $content_width );
$social_enable = Mailtpl_Woomail_Customizer::opt( 'footer_social_enable' );
$social_links = Mailtpl_Woomail_Customizer::opt( 'footer_social_repeater' );
$social_links = json_decode( $social_links );
?>
<table border="0" cellpadding="0" cellspacing="0" width="100%" id="template_footer_container">
<tr>
<td valign="top" align="center">
<table border="0" cellpadding="10" cellspacing="0" width="<?php echo esc_attr( $content_width ); ?>" id="template_footer">
<tr>
<td valign="top" id="template_footer_inside">
<table border="0" cellpadding="10" cellspacing="0" width="100%">
<?php if ( false != $social_enable && ! empty( $social_links ) && is_array( $social_links ) ) { ?>
<tr>
<td valign="top">
<table id="footersocial" border="0" cellpadding="10" cellspacing="0" width="100%">
<tr>
<?php
$items = count( $social_links );
foreach ( $social_links as $social_link ) {
?>
<td valign="middle" style="text-align:center; width:<?php echo esc_attr( round( 100 / $items, 2 ) ); ?>%">
<a href="<?php echo esc_url( $social_link->link ); ?>" class="ft-social-link" style="display:block; text-decoration: none;">
<?php
if ( 'customizer_repeater_image' == $social_link->choice ) {
echo '<img src="' . esc_attr( $social_link->image_url ) . '" width="24" style="vertical-align: bottom;">';
} else if ( 'customizer_repeater_icon' == $social_link->choice ) {
$img_string = str_replace( 'mailtpl-woomail-', '', $social_link->icon_value );
if ( isset( $social_link->icon_color ) && ! empty( $social_link->icon_color ) ) {
$color = $social_link->icon_color;
} else {
$color = 'black';
}
echo '<img alt="' . esc_attr( $img_string ) . '" src="' . esc_attr( MAILTPL_WOOMAIL_URL . 'assets/images/' . $color . '/' . $img_string ) . '.png" width="24" style="vertical-align: bottom;">';
}
?>
<span class="ft-social-title"><?php echo esc_html( $social_link->title ); ?></span>
</a>
</td>
<?php
}
?>
</tr>
</table>
</td>
</tr>
<?php } ?>
<tr>
<td valign="top">
<table border="0" cellpadding="10" cellspacing="0" width="100%">
<tr>
<td colspan="2" valign="middle" id="credit">
<?php echo wp_kses_post( wpautop( wptexturize( apply_filters( 'woocommerce_email_footer_text', get_option( 'woocommerce_email_footer_text' ) ) ) ) ); ?>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<?php
}
/**
* Check if WooCommerce settings need to be overwritten and custom styles applied
* This is true when plugin is active and at least one custom option is stored in the database
*
* @access public
* @return bool
*/
public static function overwrite_options() {
// Check if any settings were saved.
if ( null === self::$overwrite_options ) {
$option = get_option( 'mailtpl_woomail', array() );
self::$overwrite_options = ! empty( $option );
}
// Return result.
return self::$overwrite_options;
}
/**
* Hook in main text areas for customized emails
*
* @param object $order the order object.
* @param boolean $sent_to_admin if sent to admin.
* @param boolean $plain_text if plan text.
* @param object $email the Email object.
* @return void
*/
public function email_main_text_area( $order, $sent_to_admin, $plain_text, $email ) {
// Get Email ID.
$key = $email->id;
if ( 'customer_refunded_order' == $key ) {
if ( $email->partial_refund ) {
$body_text = Mailtpl_Woomail_Customizer::opt( $key . '_body_partial' );
} else {
$body_text = Mailtpl_Woomail_Customizer::opt( $key . '_body_full' );
}
} elseif ( 'customer_partially_refunded_order' == $key ) {
$body_text = Mailtpl_Woomail_Customizer::opt( 'customer_refunded_order_body_partial' );
} elseif ( 'customer_invoice' == $key ) {
if ( $order->has_status( 'pending' ) ) {
$body_text = Mailtpl_Woomail_Customizer::opt( $key . '_body' );
$btn_switch = Mailtpl_Woomail_Customizer::opt( $key . '_btn_switch' );
if ( true == $btn_switch ) {
$pay_link = '<p class="btn-container"><a class="btn" href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'Pay for this order', 'mailtpl-woocommerce-email-composer' ) . '</a></p>';
} else {
$pay_link = '<a href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'Pay for this order', 'mailtpl-woocommerce-email-composer' ) . '</a>';
}
$body_text = str_replace( '{invoice_pay_link}', $pay_link, $body_text );
} else {
$body_text = Mailtpl_Woomail_Customizer::opt( $key . '_body_paid' );
}
} elseif ( 'customer_renewal_invoice' == $key ) {
if ( $order->has_status( 'pending' ) ) {
$body_text = Mailtpl_Woomail_Customizer::opt( $key . '_body' );
$btn_switch = Mailtpl_Woomail_Customizer::opt( $key . '_btn_switch' );
if ( true == $btn_switch ) {
$pay_link = '<p class="btn-container"><a class="btn" href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'Pay Now »', 'mailtpl-woocommerce-email-composer' ) . '</a></p>';
} else {
$pay_link = '<a href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'Pay Now »', 'mailtpl-woocommerce-email-composer' ) . '</a>';
}
$body_text = str_replace( '{invoice_pay_link}', $pay_link, $body_text );
} else {
$body_text = Mailtpl_Woomail_Customizer::opt( $key . '_body_failed' );
$btn_switch = Mailtpl_Woomail_Customizer::opt( $key . '_btn_switch' );
if ( true == $btn_switch ) {
$pay_link = '<p class="btn-container"><a class="btn" href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'Pay Now »', 'mailtpl-woocommerce-email-composer' ) . '</a></p>';
} else {
$pay_link = '<a href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'Pay Now »', 'mailtpl-woocommerce-email-composer' ) . '</a>';
}
$body_text = str_replace( '{invoice_pay_link}', $pay_link, $body_text );
}
} elseif ( 'customer_payment_retry' == $key ) {
$body_text = Mailtpl_Woomail_Customizer::opt( $key . '_body' );
$btn_switch = Mailtpl_Woomail_Customizer::opt( $key . '_btn_switch' );
if ( true == $btn_switch ) {
$pay_link = '<p class="btn-container"><a class="btn" href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'Pay Now »', 'mailtpl-woocommerce-email-composer' ) . '</a></p>';
} else {
$pay_link = '<a href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'Pay Now »', 'mailtpl-woocommerce-email-composer' ) . '</a>';
}
$body_text = str_replace( '{invoice_pay_link}', $pay_link, $body_text );
} else {
$body_text = Mailtpl_Woomail_Customizer::opt( $key . '_body' );
}
$body_text = str_replace( '{site_title}', get_bloginfo( 'name', 'display' ), $body_text );
$body_text = str_replace( '{site_address}', wp_parse_url( home_url(), PHP_URL_HOST ), $body_text );
$body_text = str_replace( '{site_url}', wp_parse_url( home_url(), PHP_URL_HOST ), $body_text );
if ( $order ) {
if ( 0 === ( $user_id = (int) get_post_meta( $order->get_id(), '_customer_user', true ) ) ) {
$user_id = 'guest';
}
// Check for placeholders.
$body_text = str_replace( '{order_date}', wc_format_datetime( $order->get_date_created() ), $body_text );
$body_text = str_replace( '{order_number}', $order->get_order_number(), $body_text );
$body_text = str_replace( '{customer_first_name}', $order->get_billing_first_name(), $body_text );
$body_text = str_replace( '{customer_last_name}', $order->get_billing_last_name(), $body_text );
$body_text = str_replace( '{customer_full_name}', $order->get_formatted_billing_full_name(), $body_text );
$body_text = str_replace( '{customer_company}', $order->get_billing_company(), $body_text );
$body_text = str_replace( '{customer_email}', $order->get_billing_email(), $body_text );
$body_text = str_replace( '{customer_username}', self::get_username_from_id( $user_id ), $body_text );
}
$body_text = apply_filters( 'mailtpl_woomail_order_body_text', $body_text, $order, $sent_to_admin, $plain_text, $email );
// auto wrap text.
$body_text = wpautop( $body_text );
echo wp_kses_post( $body_text );
}
/**
* Get username from user id.
*
* @param string $id the user id.
* @access public
* @return string
*/
public static function get_username_from_id( $id ) {
if ( empty( $id ) || 'guest' === $id ) {
return __( 'Guest', 'mailtpl-woocommerce-email-composer' );
}
$user = get_user_by( 'id', $id );
if ( is_object( $user ) ) {
$username = $user->user_login;
} else {
$username = __( 'Guest', 'mailtpl-woocommerce-email-composer' );
}
return $username;
}
/**
* Filter Subtitle for Placeholders
*
* @param string $subtitle the email subtitle.
* @param object $email the email object.
* @access public
* @return string
*/
public static function filter_subtitle( $subtitle, $email ) {
// Check for placeholders.
$subtitle = str_replace( '{site_title}', get_bloginfo( 'name', 'display' ), $subtitle );
$subtitle = str_replace( '{site_address}', wp_parse_url( home_url(), PHP_URL_HOST ), $subtitle );
$subtitle = str_replace( '{site_url}', wp_parse_url( home_url(), PHP_URL_HOST ), $subtitle );
if ( is_a( $email->object, 'WP_User' ) ) {
$first_name = get_user_meta( $email->object->ID, 'billing_first_name', true );
if ( empty( $first_name ) ) {
// Fall back to user display name.
$first_name = $email->object->display_name;
}
$last_name = get_user_meta( $email->object->ID, 'billing_last_name', true );
if ( empty( $last_name ) ) {
// Fall back to user display name.
$last_name = $email->object->display_name;
}
$full_name = get_user_meta( $email->object->ID, 'formatted_billing_full_name', true );
if ( empty( $full_name ) ) {
// Fall back to user display name.
$full_name = $email->object->display_name;
}
$subtitle = str_replace( '{customer_first_name}', $first_name, $subtitle );
$subtitle = str_replace( '{customer_last_name}', $last_name, $subtitle );
$subtitle = str_replace( '{customer_full_name}', $full_name, $subtitle );
$subtitle = str_replace( '{customer_username}', $email->user_login, $subtitle );
$subtitle = str_replace( '{customer_email}', $email->object->user_email, $subtitle );
} elseif ( is_a( $email->object, 'WC_Order' ) ) {
if ( 0 === ( $user_id = (int) get_post_meta( $email->object->get_id(), '_customer_user', true ) ) ) {
$user_id = 'guest';
}
$subtitle = str_replace( '{order_date}', wc_format_datetime( $email->object->get_date_created() ), $subtitle );
$subtitle = str_replace( '{order_number}', $email->object->get_order_number(), $subtitle );
$subtitle = str_replace( '{customer_first_name}', $email->object->get_billing_first_name(), $subtitle );
$subtitle = str_replace( '{customer_last_name}', $email->object->get_billing_last_name(), $subtitle );
$subtitle = str_replace( '{customer_full_name}', $email->object->get_formatted_billing_full_name(), $subtitle );
$subtitle = str_replace( '{customer_company}', $email->object->get_billing_company(), $subtitle );
$subtitle = str_replace( '{customer_email}', $email->object->get_billing_email(), $subtitle );
$subtitle = str_replace( '{customer_username}', self::get_username_from_id( $user_id ), $subtitle );
} elseif ( is_a( $email->object, 'WC_Product' ) ) {
$subtitle = str_replace( '{product_title}', $email->object->get_title(), $subtitle );
}
return $subtitle;
}
/**
* Hook in main text areas for customized emails.
*
* @param object $email the email object.
* @access public
* @return void
*/
public function email_main_text_area_no_order( $email ) {
// Get Email ID.
$key = $email->id;
$body_text = Mailtpl_Woomail_Customizer::opt( $key . '_body' );
// Check for placeholders.
$body_text = str_replace( '{site_title}', get_bloginfo( 'name', 'display' ), $body_text );
$body_text = str_replace( '{site_address}', wp_parse_url( home_url(), PHP_URL_HOST ), $body_text );
$body_text = str_replace( '{site_url}', wp_parse_url( home_url(), PHP_URL_HOST ), $body_text );
if ( is_a( $email->object, 'WP_User' ) ) {
$first_name = get_user_meta( $email->object->ID, 'billing_first_name', true );
if ( empty( $first_name ) ) {
$first_name = get_user_meta( $email->object->ID, 'first_name', true );
if ( empty( $first_name ) ) {
// Fall back to user display name.
$first_name = $email->object->display_name;
}
}
$last_name = get_user_meta( $email->object->ID, 'billing_last_name', true );
if ( empty( $last_name ) ) {
$last_name = get_user_meta( $email->object->ID, 'last_name', true );
if ( empty( $last_name ) ) {
// Fall back to user display name.
$last_name = $email->object->display_name;
}
}
$full_name = get_user_meta( $email->object->ID, 'formatted_billing_full_name', true );
if ( empty( $full_name ) ) {
// Fall back to user display name.
$full_name = $email->object->display_name;
}
$body_text = str_replace( '{customer_first_name}', $first_name, $body_text );
$body_text = str_replace( '{customer_last_name}', $last_name, $body_text );
$body_text = str_replace( '{customer_full_name}', $full_name, $body_text );
$body_text = str_replace( '{customer_username}', $email->user_login, $body_text );
$body_text = str_replace( '{customer_email}', $email->object->user_email, $body_text );
} elseif ( is_a( $email->object, 'WC_Product' ) ) {
$body_text = str_replace( '{product_title}', $email->object->get_title(), $body_text );
$body_text = str_replace( '{product_link}', $email->object->get_permalink(), $body_text );
}
$body_text = apply_filters( 'mailtpl_woomail_no_order_body_text', $body_text, $email );
// auto wrap text.
$body_text = wpautop( $body_text );
echo wp_kses_post( $body_text );
}
/**
* Filter through strings to add support for extra placeholders
*
* @param string $string string of text.
* @param object $email the email object.
* @access public
* @return string
*/
public function add_extra_placeholders( $string, $email ) {
if ( is_a( $email->object, 'WP_User' ) ) {
$first_name = get_user_meta( $email->object->ID, 'billing_first_name', true );
if ( empty( $first_name ) ) {
$first_name = get_user_meta( $email->object->ID, 'first_name', true );
if ( empty( $first_name ) ) {
// Fall back to user display name.
$first_name = $email->object->display_name;
}
}
$last_name = get_user_meta( $email->object->ID, 'billing_last_name', true );
if ( empty( $last_name ) ) {
$last_name = get_user_meta( $email->object->ID, 'last_name', true );
if ( empty( $last_name ) ) {
// Fall back to user display name.
$last_name = $email->object->display_name;
}
}
$full_name = get_user_meta( $email->object->ID, 'formatted_billing_full_name', true );
if ( empty( $full_name ) ) {
// Fall back to user display name.
$full_name = $email->object->display_name;
}
$string = str_replace( '{customer_first_name}', $first_name, $string );
$string = str_replace( '{customer_last_name}', $last_name, $string );
$string = str_replace( '{customer_full_name}', $full_name, $string );
$string = str_replace( '{customer_username}', $email->user_login, $string );
$string = str_replace( '{customer_email}', $email->object->user_email, $string );
} else if ( is_a( $email->object, 'WC_Order' ) ) {
if ( 0 === ( $user_id = (int) get_post_meta( $email->object->get_id(), '_customer_user', true ) ) ) {
$user_id = 'guest';
}
$string = str_replace( '{customer_first_name}', $email->object->get_billing_first_name(), $string );
$string = str_replace( '{customer_last_name}', $email->object->get_billing_last_name(), $string );
$string = str_replace( '{customer_full_name}', $email->object->get_formatted_billing_full_name(), $string );
$string = str_replace( '{customer_company}', $email->object->get_billing_company(), $string );
$string = str_replace( '{customer_email}', $email->object->get_billing_email(), $string );
$string = str_replace( '{customer_username}', self::get_username_from_id( $user_id ), $string );
}
return $string;
}
/**
* Add submenu under woocommerce for email customizer
*
* @access public
* @return void
*/
public function wwp_wholesale_reports_callback() {
}
/**
* Add submenu under woocommerce for email customizer
*
* @param array $links plugin links.
* @access public
* @return array
*/
public function plugins_page_links( $links ) {
$settings_link = '<a href="' . Mailtpl_Woomail_Customizer::get_customizer_url() . '">' . __( 'Open Email Composer', 'mailtpl-woocommerce-email-composer' ) . '</a>';
array_unshift( $links, $settings_link );
return $links;
}
/**
* Checks to see if we are opening our custom customizer preview
*
* @access public
* @return bool
*/
public static function is_own_preview_request() {
return isset( $_REQUEST['mailtpl-woomail-preview'] ) && '1' === $_REQUEST['mailtpl-woomail-preview'];
}
/**
* Checks to see if we are opening our custom customizer controls
*
* @access public
* @return bool
*/
public static function is_own_customizer_request() {
return isset( $_REQUEST['mailtpl-woomail-customize'] ) && '1' === $_REQUEST['mailtpl-woomail-customize'];
}
/**
* Gets the capability setting needed to edit in the email customizer
*
* @access public
* @return string
*/
public static function get_admin_capability() {
// Get capability.
if ( is_null( self::$admin_capability ) ) {
self::$admin_capability = apply_filters( 'mailtpl_woomail_capability', 'manage_woocommerce' );
}
// Return capability.
return self::$admin_capability;
}
/**
* Check if user is authorized to use the email customizer
*
* @access public
* @return bool
*/
public static function is_admin() {
return current_user_can( self::get_admin_capability() );
}
/**
* Hook in email header with access to the email object
*
* @param string $email_heading email heading.
* @param object $email the email object.
* @access public
* @return void
*/
public function add_email_header( $email_heading, $email = '' ) {
wc_get_template( 'emails/email-header.php', array( 'email_heading' => $email_heading, 'email' => $email ) );
}
/**
* Filter in custom email templates with priority to child themes
*
* @param string $template the email template file.
* @param string $template_name name of email template.
* @param string $template_path path to email template.
* @access public
* @return string
*/
public function filter_locate_template( $template, $template_name, $template_path ) {
// Make sure we are working with an email template.
if ( ! in_array( 'emails', explode( '/', $template_name ) ) ) {
return $template;
}
// clone template.
$_template = $template;
// Get the woocommerce template path if empty.
if ( ! $template_path ) {
global $woocommerce;
$template_path = $woocommerce->template_url;
}
// Get our template path.
$plugin_path = MAILTPL_WOOMAIL_PATH . 'templates/woo/';
// Look within passed path within the theme - this is priority.
$template = locate_template( array( $template_path . $template_name, $template_name ) );
// If theme isn't trying to override get the template from this plugin, if it exists.
if ( ! $template && file_exists( $plugin_path . $template_name ) ) {
$template = $plugin_path . $template_name;
}
// else if we still don't have a template use default.
if ( ! $template ) {
$template = $_template;
}
// Return template.
return $template;
}
/**
* Filter in custom email templates with priority to child themes
*
* @param string $template the email template file.
* @param string $template_name name of email template.
* @param string $template_path path to email template.
* @access public
* @return string
*/
public function filter_locate_template_language( $template, $template_name, $template_path ) {
// Make sure we are working with an email template.
if ( ! in_array( 'emails', explode( '/', $template_name ) ) ) {
return $template;
}
$this->load_plugin_textdomain();
// Return template.
return $template;
}
/**
* Filter when email languages are set, adds in a switch if needed
*
* @access public
* @param bool $switch whether or not it should switch.
* @return bool
*/
public function switch_to_site_locale( $switch ) {
if ( $switch ) {
if ( function_exists( 'switch_to_locale' ) ) {
add_filter( 'woocommerce_locate_template', array( $this, 'filter_locate_template_language' ), 5, 3 );
}
}
return $switch;
}
/**
* Load Localisation files.
*/
public function load_plugin_textdomain() {
load_plugin_textdomain( 'mailtpl-woocommerce-email-composer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Restore the locale to the default locale. Use after finished with setup_locale.
*
* @param boolean $switch whether or not it should switch.
* @return boolean
*/
public function restore_to_user_locale( $switch ) {
if ( $switch ) {
if ( function_exists( 'restore_previous_locale' ) ) {
remove_filter( 'woocommerce_locate_template', array( $this, 'filter_locate_template_language' ), 5, 3 );
}
}
return $switch;
}
}
Mailtpl_Woomail_Composer::get_instance();
/**
* Checks if WooCommerce is enabled
*/
class Mailtpl_Woomail_Plugin_Check {
private static $active_plugins;
public static function init() {
self::$active_plugins = (array) get_option( 'active_plugins', array() );
if ( is_multisite() ) {
self::$active_plugins = array_merge( self::$active_plugins, get_site_option( 'active_sitewide_plugins', array() ) );
}
}
public static function active_check_woo() {
if ( ! self::$active_plugins ) {
self::init();
}
return in_array( 'woocommerce/woocommerce.php', self::$active_plugins ) || array_key_exists( 'woocommerce/woocommerce.php', self::$active_plugins );
}
}
}
/**
* Checks if WooCommerce is enabled
*/
function mailtpl_woomail_is_woo_active() {
return Mailtpl_Woomail_Plugin_Check::active_check_woo();
}