PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.4
YayMail – WooCommerce Email Customizer v4.4.4
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 / src / PreviewEmail / PreviewEmailWoo.php

PreviewEmailWoo.php in YayMail – WooCommerce Email Customizer 4.4.4, at src/PreviewEmail/PreviewEmailWoo.php

194 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMail\PreviewEmail;
4
5 use YayMail\Utils\SingletonTrait;
6 use YayMail\YayMailTemplate;
7 use YayMail\SupportedPlugins;
8 use YayMail\Models\TemplateModel;
9
10 /**
11 *
12 * @method static PreviewEmailWoo get_instance()
13 */
14 class PreviewEmailWoo {
15 use SingletonTrait;
16
17 public static $recipient;
18
19 private function __construct() {}
20
21 public static function email_preview_output( $order_id, $email_id, $email_address = '' ) {
22 $current_email = yaymail_get_email( $email_id );
23 $current_email = ! empty( $current_email ) && method_exists( $current_email, 'get_root_email' ) ? $current_email->get_root_email() : '';
24
25 if ( empty( $current_email ) ) {
26 return [
27 'html' => yaymail_get_content( '/templates/preview-email/notice.php' ),
28 'subject' => str_replace( '(N/A)', '', str_replace( '{blogname}', wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), '[' . wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) . '] - ' . self::get_email_title_by_id( $email_id ) ) ),
29 'is_disabled_send_mail' => true,
30 ];
31 }
32
33 $email_class = get_class( $current_email );
34
35 $order = wc_get_order( $order_id );
36 $template_data = new YayMailTemplate( $email_id );
37
38 if ( ! $template_data->is_enabled() ) {
39 return self::render_default_preview( $current_email, $email_class, $order );
40 }
41
42 self::$recipient = is_email( $email_address ) ? $email_address : '';
43
44 WC()->payment_gateways();
45 WC()->shipping();
46
47 add_filter( 'woocommerce_email_recipient_' . $current_email->id, [ __CLASS__, 'no_recipient' ] );
48 add_filter( 'woocommerce_new_order_email_allows_resend', '__return_true' );
49 add_filter( 'yaymail_is_preview_email', '__return_true' );
50
51 $core_template_ids = SupportedPlugins::get_instance()->get_template_ids_from_core();
52 $addon_template_ids = SupportedPlugins::get_instance()->get_all_addon_supported_template_ids();
53 $supported_template_ids = array_merge( $core_template_ids, $addon_template_ids );
54
55 if ( in_array( $email_id, $supported_template_ids, true ) ) {
56 // Sample order fallback
57 if ( empty( $order ) ) {
58 return self::render_sample_preview( $template_data, $current_email );
59 }
60
61 self::trigger_email( $email_class, $current_email, $order_id );
62 $content = self::get_email_content( $current_email );
63 $is_disabled_send_mail = false;
64 } else {
65 $content = yaymail_get_content( '/templates/preview-email/notice.php', [ 'template_data' => $template_data ] );
66 $is_disabled_send_mail = true;
67 }
68
69 remove_filter( 'woocommerce_new_order_email_allows_resend', '__return_true', 10 );
70
71 return [
72 'html' => yaymail_kses_post( $content ),
73 'subject' => self::get_subject( $current_email, $order ),
74 'is_disabled_send_mail' => $is_disabled_send_mail,
75 ];
76 }
77
78 public static function get_email_title_by_id( $email_id ) {
79 $templates = TemplateModel::get_instance()->find_all();
80 foreach ( $templates as $template ) {
81 if ( $template['name'] === $email_id ) {
82 return $template['template_title'];
83 }
84 }
85 return __( 'Email Subject', 'yaymail' );
86 }
87
88 private static function get_subject( $current_email, $order ) {
89 $text_replace = [
90 '{order_number}' => ! empty( $order ) ? '#' . $order->get_order_number() : '#1',
91 '{order_date}' => ! empty( $order ) ? gmdate( get_option( 'date_format' ), strtotime( $order->get_date_created() ) ) : gmdate( get_option( 'date_format' ) ),
92 '{order_id}' => ! empty( $order ) ? '#' . $order->get_id() : '#1',
93 '{blogname}' => wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ),
94 ];
95
96 $subject = ! empty( $current_email->subject ) ? str_replace( array_keys( $text_replace ), $text_replace, $current_email->subject ) : $current_email->get_subject();
97 return $subject;
98 }
99
100 private static function render_sample_preview( $template_data, $current_email ) {
101 $render_data = [
102 'is_sample' => true,
103 'is_customized_preview' => true,
104 ];
105 $content = $template_data->get_content( $render_data );
106
107 return [
108 'html' => yaymail_kses_post( $content ),
109 'subject' => self::get_subject( $current_email, '' ),
110 ];
111 }
112
113 private static function render_default_preview( $current_email, $email_class, $order ) {
114 add_filter(
115 'woocommerce_email_preview_dummy_order',
116 function ( $dummy_order ) use ( $order ) {
117 return ! empty( $order ) ? $order : $dummy_order;
118 },
119 10,
120 2
121 );
122 try {
123 $email_preview = wc_get_container()->get( \Automattic\WooCommerce\Internal\Admin\EmailPreview\EmailPreview::class );
124 $email_preview->set_email_type( $email_class );
125 $message = $email_preview->render();
126 $message = $email_preview->ensure_links_open_in_new_tab( $message );
127 $content = $message;
128 $is_disabled_send_mail = false;
129 } catch ( \Throwable $e ) {
130 ob_end_clean();
131 $content = yaymail_get_content( '/templates/preview-email/notice.php' );
132 $is_disabled_send_mail = true;
133 }
134
135 return [
136 'html' => yaymail_kses_post( $content ),
137 'subject' => self::get_subject( $current_email, $order ),
138 'is_disabled_send_mail' => $is_disabled_send_mail,
139 ];
140 }
141
142 private static function trigger_email( $email_class, $email, $order_id ) {
143 try {
144 /**
145 * Filter: yaymail_trigger_to_preview_email
146 *
147 * Allows developers to control whether to use custom email trigger logic
148 * instead of the default WooCommerce email trigger.
149 *
150 * @param bool $should_use_custom_trigger Whether to use custom trigger logic (default: false)
151 * @param object $email The email object instance
152 * @param int $order_id The order ID
153 *
154 * @return bool True to use custom trigger via 'yaymail_trigger_email' action, false to use default WooCommerce trigger
155 */
156 if ( apply_filters( 'yaymail_trigger_to_preview_email', false, $email, $order_id ) ) {
157 /**
158 * Action: yaymail_trigger_email
159 *
160 * Fired when custom email trigger logic should be used instead of
161 * the default WooCommerce email trigger. This allows addons and
162 * custom implementations to handle email triggering differently.
163 *
164 * @param object $email The email object instance
165 * @param int $order_id The order ID
166 */
167 do_action( 'yaymail_trigger_email', $email, $order_id );
168 } else {
169 // Default WooCommerce email trigger logic
170 $order = wc_get_order( $order_id );
171 $email->set_object( $order );
172 $email->trigger( $order_id );
173 }//end if
174 } catch ( \Exception $e ) {
175 return [ 'error' => $e ];
176 }//end try
177 }
178
179 private static function get_email_content( $email ) {
180 $email->email_type = 'html';
181 $content = $email->get_content();
182 return apply_filters( 'woocommerce_mail_content', $email->style_inline( $content ) );
183 }
184
185 public static function no_recipient( $recipient ): string {
186 if ( self::$recipient !== '' ) {
187 $recipient = self::$recipient;
188 } else {
189 $recipient = '';
190 }
191 return $recipient;
192 }
193 }
194