PluginProbe
YayMail – WooCommerce Email Customizer / 3.2.6
YayMail – WooCommerce Email Customizer v3.2.6
4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 All 57 releases
yaymail / includes / MailBuilder / WooTemplate.php

WooTemplate.php in YayMail – WooCommerce Email Customizer 3.2.6, at includes/MailBuilder/WooTemplate.php

450 lines 19.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMail\MailBuilder;
4
5 use YayMail\Page\Source\CustomPostType;
6 use YayMail\MailBuilder\PIPTemplate;
7
8 defined( 'ABSPATH' ) || exit;
9 /**
10 * Settings Page
11 */
12 class WooTemplate {
13
14
15 protected static $instance = null;
16 private $templateAccount;
17 private $templateSubscription;
18 private $automatewoo_info = null;
19 private $automatewoo_referrals_email_content = null;
20 private $trackShipArgs = null;
21 private $FUE_Sending_Email_Variables = null;
22 public static function getInstance() {
23 if ( null == self::$instance ) {
24 self::$instance = new self();
25 self::$instance->doHooks();
26 }
27
28 return self::$instance;
29 }
30
31 private function doHooks() {
32 $this->templateAccount = array( 'customer_new_account', 'customer_new_account_activation', 'customer_reset_password' );
33 $this->templateGermanizedForWC = array( 'sab_simple_invoice', 'sab_cancellation_invoice', 'sab_packing_slip', 'sab_document_admin', 'sab_document' );
34 add_filter( 'storeabill_get_template', array( $this, 'storeabill_get_template' ), 100, 5 );
35 add_filter( 'wc_get_template', array( $this, 'getTemplateMail' ), 100, 5 );
36
37 if ( has_filter( 'YaymailCreateFollowUpTemplates' ) ) {
38 add_action( 'fue_before_variable_replacements', array( $this, 'register_variable_replacements' ), 100, 4 );
39 add_filter( 'fue_before_sending_email', array( $this, 'getFollowUpTemplates' ), 100, 3 );
40 }
41
42 if ( class_exists( 'WC_PIP_Loader' ) && class_exists( 'YayMailWooPrintInvoices\\templateDefault\\DefaultInvoice' ) && class_exists( 'YayMailWooPrintInvoices\\templateDefault\\DefaultPickList' ) ) {
43 PIPTemplate::handle_trigger();
44 }
45 add_filter( 'retrieve_password_message', array( $this, 'admin_reset_password' ), 100, 4 );
46 add_action( 'automatewoo_before_action_run', array( $this, 'automatewoo_before_action_run' ), 10 );
47 add_filter( 'automatewoo/referrals/invite_email/mailer', array( $this, 'automatewoo_invite_email' ), 100, 2 );
48
49 if ( class_exists( 'WCFM' ) ) {
50 $WCFMWooFM_Template = CustomPostType::postIDByTemplate( 'WCFMWooFM_Template' );
51 if ( get_post_meta( $WCFMWooFM_Template, '_yaymail_status', true ) ) {
52 global $WCFM;
53 remove_action( 'wcfm_email_content_wrapper', array( $WCFM, 'wcfm_email_content_wrapper' ), 10 );
54 add_filter( 'wcfm_email_content_wrapper', array( &$this, 'wcfm_email_content_wrapper' ), 1, 2 );
55 }
56 }
57
58 add_filter( 'wpml_translate_single_string', array( $this, 'yaymail_ywces_new_template' ), 1, 3 );
59 // change german market template dir
60 $this->yaymail_get_german_market_templates();
61 }
62
63 public function yaymail_ywces_new_template( $mail_body_content, $admin_mail_body_text, $mail_body_text ) {
64 if ( class_exists( 'YayMailYITHWooCouponEmailSystem\templateDefault\DefaultCouponEmailSystem' ) && class_exists( 'YITH_WC_Coupon_Email_System' ) && false !== strpos( $mail_body_text, 'ywces_mailbody_' ) ) {
65 $mail_body_text_explode = explode( 'ywces_mailbody_', $mail_body_text );
66 $email_template_type = $mail_body_text_explode[1];
67 $template = 'YWCES_' . $email_template_type;
68 $postID = CustomPostType::postIDByTemplate( $template );
69 if ( $postID ) {
70 if ( get_post_meta( $postID, '_yaymail_status', true ) && ! empty( get_post_meta( $postID, '_yaymail_elements', true ) ) ) {
71 $check_YWCES = true;
72 $templateActive = file_exists( YAYMAIL_PLUGIN_PATH . 'views/templates/single-mail-template.php' ) ? YAYMAIL_PLUGIN_PATH . 'views/templates/single-mail-template.php' : false;
73 ob_start();
74 include $templateActive;
75 $template_body = ob_get_contents();
76 // Replace newline (\n) in a string to YAYMAIL_YWCES_YAY
77 $template_body = str_replace( "\n", 'YAYMAIL_YWCES_YAY', $template_body );
78 ob_end_clean();
79 return $template_body;
80 }
81 }
82 }
83
84 return $mail_body_content;
85 }
86
87 public function yaymail_get_german_market_templates() {
88 if ( class_exists( 'Woocommerce_German_Market' ) ) {
89 add_filter(
90 'wgm_locate_template',
91 function( $template, $template_name, $template_path ) {
92 if ( 'emails/customer-confirm-order.php' === $template_name ) {
93 $postID = CustomPostType::postIDByTemplate( 'wgm_confirm_order_email' );
94 $template_status = get_post_meta( $postID, '_yaymail_status', true );
95 if ( $template_status ) {
96 return YAYMAIL_PLUGIN_PATH . 'views/templates/emails/customer-confirm-order.php';
97 }
98 } elseif ( 'double-opt-in-customer-registration.php' === $template_name ) {
99 $postID = CustomPostType::postIDByTemplate( 'wgm_double_opt_in_customer_registration' );
100 $template_status = get_post_meta( $postID, '_yaymail_status', true );
101 if ( $template_status ) {
102 return YAYMAIL_PLUGIN_PATH . 'views/templates/emails/double-opt-in-customer-registration.php';
103 }
104 } elseif ( 'emails/sepa-mandate.php' === $template_name ) {
105 $postID = CustomPostType::postIDByTemplate( 'wgm_sepa' );
106 $template_status = get_post_meta( $postID, '_yaymail_status', true );
107 if ( $template_status ) {
108 return YAYMAIL_PLUGIN_PATH . 'views/templates/emails/sepa.php';
109 }
110 }
111 return $template;
112 },
113 100,
114 3
115 );
116 }
117 }
118
119 public function wcfm_email_content_wrapper( $content_body, $email_heading ) {
120 $template = 'WCFMWooFM_Template';
121 $postID = CustomPostType::postIDByTemplate( 'WCFMWooFM_Template' );
122 $templateActive = file_exists( YAYMAIL_PLUGIN_PATH . 'views/templates/single-follow-up-mail-template.php' ) ? YAYMAIL_PLUGIN_PATH . 'views/templates/single-follow-up-mail-template.php' : false;
123 $args = array(
124 'order' => null,
125 'content_body' => $content_body,
126 'email_heading' => $email_heading,
127 );
128 ob_start();
129 include $templateActive;
130 $template_body = ob_get_contents();
131 ob_end_clean();
132 return $template_body;
133 }
134
135 public function automatewoo_invite_email( $mailer, $invite_email ) {
136 if ( defined( 'YAYMAIL_ADDON_AUTOMATEWOO' ) && ! empty( YAYMAIL_ADDON_AUTOMATEWOO ) ) {
137 $template = 'AutomateWoo_Referrals_Email';
138 $postID = CustomPostType::postIDByTemplate( $template );
139 $template_status = get_post_meta( $postID, '_yaymail_status', true );
140 if ( $template_status ) {
141 $templateActive = file_exists( YAYMAIL_PLUGIN_PATH . 'views/templates/single-follow-up-mail-template.php' ) ? YAYMAIL_PLUGIN_PATH . 'views/templates/single-follow-up-mail-template.php' : false;
142 ob_start();
143 include $templateActive;
144 $template_body = ob_get_contents();
145 ob_end_clean();
146 if ( '' !== $template_body ) {
147 $template_body = $invite_email->replace_variables( $template_body );
148 $this->automatewoo_referrals_email_content = $template_body;
149 }
150 add_filter(
151 'woocommerce_mail_content',
152 function( $email_content ) {
153 return $this->automatewoo_referrals_email_content;
154 },
155 100,
156 1
157 );
158 }
159 }
160 return $mailer;
161 }
162 public function automatewoo_before_action_run( $action ) {
163 if ( defined( 'YAYMAIL_ADDON_AUTOMATEWOO' ) && ! empty( YAYMAIL_ADDON_AUTOMATEWOO ) ) {
164 $this->automatewoo_info = $action;
165 $template = 'AutomateWoo_' . $action->workflow->get_id();
166 $postID = CustomPostType::postIDByTemplate( $template );
167 $template_status = get_post_meta( $postID, '_yaymail_status', true );
168 if ( $template_status ) {
169 add_filter(
170 'woocommerce_mail_content',
171 function( $html ) {
172 $workflow = $this->automatewoo_info->workflow;
173 $template = 'AutomateWoo_' . $workflow->get_id();
174 $postID = CustomPostType::postIDByTemplate( $template );
175 $templateActive = file_exists( YAYMAIL_PLUGIN_PATH . 'views/templates/single-follow-up-mail-template.php' ) ? YAYMAIL_PLUGIN_PATH . 'views/templates/single-follow-up-mail-template.php' : false;
176 $raw_data = $workflow->data_layer()->get_raw_data();
177 $args = array(
178 'order' => isset( $raw_data['order'] ) ? $raw_data['order'] : null,
179 'subscription' => isset( $raw_data['subscription'] ) ? $raw_data['subscription'] : null,
180 'email' => '',
181 'workflow' => $workflow,
182 );
183
184 ob_start();
185 include $templateActive;
186 $template_body = ob_get_contents();
187 ob_end_clean();
188 if ( '' !== $template_body ) {
189 $template_body = $workflow->variable_processor()->process_field( $template_body, true );
190 $email = new \AutomateWoo\Workflow_Email( $workflow, '', '', $template_body );
191 $get_mailer = $email->get_mailer();
192 if ( $workflow->is_tracking_enabled() ) {
193 $template_body = $get_mailer->inject_tracking_pixel( $template_body );
194 $template_body = $get_mailer->replace_urls_in_content( $template_body );
195 }
196 $template_body = $get_mailer->style_inline( $template_body );
197 return $template_body;
198 }
199 return $html;
200 },
201 10
202 );
203 }
204 }
205 }
206
207 public function storeabill_get_template( $located, $template_name, $args, $template_path, $default_path ) {
208 $this_template = false;
209 $templateActive = file_exists( YAYMAIL_PLUGIN_PATH . 'views/templates/single-mail-template.php' ) ? YAYMAIL_PLUGIN_PATH . 'views/templates/single-mail-template.php' : false;
210 $template = isset( $args['email'] ) && isset( $args['email']->id ) && ! empty( $args['email']->id ) ? $args['email']->id : false;
211
212 if ( $template ) {
213 if ( CustomPostType::postIDByTemplate( $template ) ) {
214 $postID = CustomPostType::postIDByTemplate( $template );
215 if ( get_post_meta( $postID, '_yaymail_status', true ) && ! empty( get_post_meta( $postID, '_yaymail_elements', true ) ) ) {
216 if ( in_array( $template, $this->templateGermanizedForWC ) ) { // template mail with account
217 $this_template = $templateActive;
218 }
219 }
220 }
221 }
222 $this_template = $this_template ? $this_template : $located;
223 return $this_template;
224 }
225
226 private function __construct() {}
227 // define the woocommerce_new_order callback
228
229 public function admin_reset_password( $message, $key, $user_login, $user_data ) {
230 $this_template = false;
231 $templateActive = file_exists( YAYMAIL_PLUGIN_PATH . 'views/templates/single-mail-template.php' ) ? YAYMAIL_PLUGIN_PATH . 'views/templates/single-mail-template.php' : false;
232 $template = 'customer_reset_password';
233 $email = array(
234 'id' => 'customer_reset_password',
235 'user_login' => $user_login,
236 'user_id' => $user_data->ID,
237 'user_email' => $user_data->data->user_email,
238 'user_data' => $user_data,
239 'key' => $key,
240 );
241 $args = array(
242 'email' => (object) $email,
243 'sent_to_admin' => false,
244 'reset_key' => $key,
245 );
246 $holder_order = isset( $args['order'] ) ? $args['order'] : null;
247 if ( CustomPostType::postIDByTemplate( $template, $holder_order ) ) {
248 $postID = CustomPostType::postIDByTemplate( $template, $holder_order );
249 if ( get_post_meta( $postID, '_yaymail_status', true ) && ! empty( get_post_meta( $postID, '_yaymail_elements', true ) ) ) {
250 if ( isset( $args['order'] ) || in_array( $template, $this->templateAccount ) ) { // template mail with order
251 $this_template = $templateActive;
252 }
253 }
254 }
255 $template_path = '';
256 $template_name = 'emails/customer-reset-password.php';
257 if ( false !== $this_template ) {
258 ob_start();
259 include $this_template;
260 $message = ob_get_contents();
261 ob_end_clean();
262 $site_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
263 // translators: none.
264 $title = sprintf( __( '[%s] Password Reset', 'yaymail' ), $site_name );
265 wc_mail( $email['user_email'], $title, $message );
266 $message = false;
267 }
268 return $message;
269 }
270
271 // support addon TrackShip for WooCommerce
272 public function woocommerce_mail_content( $html ) {
273 $trackshipArgs = $this->trackShipArgs;
274 $template = 'trackship_' . $trackshipArgs['new_status'];
275 $postID = CustomPostType::postIDByTemplate( $template );
276 $templateActive = file_exists( YAYMAIL_PLUGIN_PATH . 'views/templates/single-follow-up-mail-template.php' ) ? YAYMAIL_PLUGIN_PATH . 'views/templates/single-follow-up-mail-template.php' : false;
277 $args = array(
278 'order_id' => isset( $trackshipArgs['order_id'] ) ? $trackshipArgs['order_id'] : '0',
279 'show_shipment_status' => isset( $trackshipArgs['show_shipment_status'] ) ? $trackshipArgs['show_shipment_status'] : false,
280 'new_status' => isset( $trackshipArgs['new_status'] ) ? $trackshipArgs['new_status'] : '',
281 'tracking_items' => isset( $trackshipArgs['tracking_items'] ) ? $trackshipArgs['tracking_items'] : array(),
282 'shipment_status' => isset( $trackshipArgs['shipment_status'] ) ? $trackshipArgs['shipment_status'] : array(),
283 );
284
285 ob_start();
286 include $templateActive;
287 $template_body = ob_get_contents();
288 ob_end_clean();
289 if ( '' !== $template_body ) {
290 return $template_body;
291 }
292 return $html;
293
294 }
295
296 public function getTemplateMail( $located, $template_name, $args, $template_path, $default_path ) {
297 // support addon TrackShip for WooCommerce
298 if ( 'emails/tracking-info.php' == $template_name ) {
299 $this->trackShipArgs = $args;
300 if ( isset( $args['new_status'] ) ) {
301 $template = 'trackship_' . $args['new_status'];
302 $postID = CustomPostType::postIDByTemplate( $template );
303 $template_status = get_post_meta( $postID, '_yaymail_status', true );
304 if ( $template_status ) {
305 add_filter( 'woocommerce_mail_content', array( $this, 'woocommerce_mail_content' ), 100 );
306 }
307 }
308 }
309 $this_template = false;
310 $templateActive = file_exists( YAYMAIL_PLUGIN_PATH . 'views/templates/single-mail-template.php' ) ? YAYMAIL_PLUGIN_PATH . 'views/templates/single-mail-template.php' : false;
311 if ( isset( $args['yith_wc_email'] ) && isset( $args['yith_wc_email']->id ) && ! empty( $args['yith_wc_email']->id ) ) {
312 // Get Email ID in yith-woocommerce-multi-vendor-premium
313 $template = $args['yith_wc_email']->id;
314 } else {
315 $template = isset( $args['email'] ) && isset( $args['email']->id ) && ! empty( $args['email']->id ) ? $args['email']->id : false;
316 if ( 'dokan-wholesale/' == $template_path ) {
317 $template = 'Dokan_Email_Wholesale_Register';
318 }
319 if ( 'new-user-registration.php' == $template_name ) {
320 $template = 'wp_crowdfunding_new_user';
321 }
322 if ( 'campaign-accepted.php' == $template_name ) {
323 $template = 'wp_crowdfunding_campaign_accept';
324 }
325 if ( 'submit-campaign.php' == $template_name ) {
326 $template = 'wp_crowdfunding_submit_campaign';
327 }
328 if ( 'campaign-updated.php' == $template_name ) {
329 $template = 'wp_crowdfunding_campaign_update_email';
330 }
331 if ( 'new-backed.php' == $template_name ) {
332 $template = 'wp_crowdfunding_new_backed';
333 }
334 if ( 'campaign-target-reached.php' == $template_name ) {
335 $template = 'wp_crowdfunding_target_reached_email';
336 }
337 if ( 'withdraw-request.php' == $template_name ) {
338 $template = 'wp_crowdfunding_withdraw_request';
339 }
340 if ( class_exists( 'WC_Smart_Coupons' ) ) {
341 if ( isset( $args['email'] ) && strpos( $located, plugin_dir_path( WC_SC_PLUGIN_FILE ) ) !== false ) {
342 $templateName = str_replace( plugin_dir_path( WC_SC_PLUGIN_FILE ) . 'templates/', '', $located );
343 if ( 'email.php' == $templateName ) {
344 $template = 'wc_sc_email_coupon';
345 $args['id'] = 'wc_sc_email_coupon';
346 }
347 if ( 'combined-email.php' == $templateName ) {
348 $template = 'wc_sc_combined_email_coupon';
349 $args['id'] = 'wc_sc_combined_email_coupon';
350 }
351 if ( 'acknowledgement-email.php' == $templateName ) {
352 $template = 'wc_sc_acknowledgement_email';
353 $args['id'] = 'wc_sc_acknowledgement_email';
354 }
355 }
356 }
357 if ( 'emails/waitlist-mailout.php' == $template_name ) {
358 $template = 'woocommerce_waitlist_mailout';
359 }
360 if ( 'emails/waitlist-left.php' == $template_name ) {
361 $template = 'woocommerce_waitlist_left_email';
362 }
363 if ( 'emails/waitlist-joined.php' == $template_name ) {
364 $template = 'woocommerce_waitlist_joined_email';
365 }
366 if ( 'emails/waitlist-new-signup.php' == $template_name ) {
367 $template = 'woocommerce_waitlist_signup_email';
368 }
369
370 if ( 'emails/dokan-admin-new-booking.php' == $template_name ) {
371 $template = 'Dokan_Email_Booking_New';
372 }
373 if ( 'emails/dokan-customer-booking-cancelled.php' == $template_name ) {
374 $template = 'Dokan_Email_Booking_Cancelled_NEW';
375 }
376 }
377
378 if ( isset( $args['email'] ) && isset( $args['email']->id ) && false !== strpos( get_class( $args['email'] ), 'ORDDD_Email_Delivery_Reminder' ) ) {
379 $template .= '_customer';
380 }
381
382 if ( isset( $args['email'] ) && is_object( $args['email'] ) && 'WC_GZD_Email_Customer_Cancelled_Order' === get_class( $args['email'] ) && 'customer_failed_order' === $template ) {
383 $template = 'customer_cancelled_order';
384 }
385
386 // can't load tempalte email-delivery-date.php because it will has error when check out, with plugin WooCommerce Order Delivery
387 if ( $template && 'emails/email-delivery-date.php' != $template_name && 'emails/email-order-details.php' != $template_name ) {
388
389 // Yith Stripe
390
391 if ( 'emails/expiring-card-email.php' === $template_name ) {
392 $template = 'expiring_card';
393 }
394 if ( 'emails/renew-needs-action-email.php' === $template_name ) {
395 $template = 'renew_needs_action';
396 }
397
398 if ( 'emails/admin-notify-approved.php' === $template_name ) {
399 $template = 'admin_notify_approved';
400 }
401 if ( 'customer_partially_refunded_order' === $template ) {
402 $template = 'customer_refunded_order';
403 }
404 if ( CustomPostType::postIDByTemplate( $template ) ) {
405 $postID = CustomPostType::postIDByTemplate( $template );
406 if ( get_post_meta( $postID, '_yaymail_status', true ) && ! empty( get_post_meta( $postID, '_yaymail_elements', true ) ) ) {
407 if ( isset( $args['order'] ) || in_array( $template, $this->templateAccount ) ) { // template mail with order
408 $this_template = $templateActive;
409 } else {
410 $checkHasTempalte = apply_filters( 'yaymail_addon_defined_template', false, $template );
411 if ( $checkHasTempalte ) { // template mail with account
412 $this_template = $templateActive;
413 }
414 }
415 }
416 }
417 }
418 $this_template = $this_template ? $this_template : $located;
419 return $this_template;
420 }
421
422 public function register_variable_replacements( $var, $email_data, $email, $queue_item ) {
423 $this->FUE_Sending_Email_Variables = $var;
424 }
425
426 public function getFollowUpTemplates( $email_data, $email, $queue_item ) {
427 if ( has_filter( 'yaymail_follow_up_shortcode' ) ) {
428 $templateActive = file_exists( YAYMAIL_PLUGIN_PATH . 'views/templates/single-follow-up-mail-template.php' ) ? YAYMAIL_PLUGIN_PATH . 'views/templates/single-follow-up-mail-template.php' : false;
429 $template = 'follow_up_email_' . $email->id;
430 $postID = CustomPostType::postIDByTemplate( $template );
431 $template_status = get_post_meta( $postID, '_yaymail_status', true );
432 $args = array(
433 'email_data' => $email_data,
434 'email' => $email,
435 'queue_item' => $queue_item,
436 );
437 if ( $template_status ) {
438 ob_start();
439 include $templateActive;
440 $template_body = ob_get_contents();
441 $FUE_Sending_Email_Variables = $this->FUE_Sending_Email_Variables;
442 $template_body = $FUE_Sending_Email_Variables->apply_replacements( $template_body );
443 ob_end_clean();
444 $email_data['message'] = $template_body;
445 }
446 }
447 return $email_data;
448 }
449 }
450