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
|
<?php
use YayMail\Models\SettingModel; use YayMail\Utils\TemplateHelpers; use YayMail\Constants\TemplatesData; use YayMail\Elements\ColumnLayout; use YayMail\Elements\ElementsLoader; use YayMail\Integrations\TranslationModule; use YayMail\YayMailEmails; use YayMail\Utils\Logger; use YayMail\Utils\StyleInline; if ( ! function_exists( 'yaymail_get_emails' ) ) {
/** * Get all supported Emails * * @return BaseEmail[] */ function yaymail_get_emails() { $yaymail_emails = YayMailEmails::get_instance()->get_emails(); $emails_default = []; $emails_third_party = [];
foreach ( $yaymail_emails as $email ) { if ( in_array( $email->get_id(), TemplatesData::WOO_DEFAULT_EMAIL_IDS, true ) ) { $emails_default[] = $email; } else { $emails_third_party[] = $email; } } $sorted_emails = array_merge( $emails_default, $emails_third_party ); return $sorted_emails; } }//end if
if ( ! function_exists( 'yaymail_get_email' ) ) {
/** * Get email by email id * * @param string $email_id * * @return null|BaseEmail Return null when not found */ function yaymail_get_email( $email_id ) { $emails = yaymail_get_emails();
$find_email = null;
foreach ( $emails as $email ) { if ( $email_id === $email->get_id() ) { $find_email = $email; break; } }
return $find_email; } }//end if
if ( ! function_exists( 'yaymail_is_wc_installed' ) ) { function yaymail_is_wc_installed() { return function_exists( 'WC' ); } }
if ( ! function_exists( 'yaymail_settings' ) ) { function yaymail_settings() { global $yaymail_unsaved_settings; if ( ! empty( $yaymail_unsaved_settings ) ) { foreach ( $yaymail_unsaved_settings as $key => $value ) { if ( 'true' === $value ) { $yaymail_unsaved_settings[ $key ] = true; } if ( 'false' === $value ) { $yaymail_unsaved_settings[ $key ] = false; } } return $yaymail_unsaved_settings; } return SettingModel::get_instance()::find_all(); } }
if ( ! function_exists( 'yaymail_get_content' ) ) { function yaymail_get_content( $path, $args = [], $root = YAYMAIL_PLUGIN_PATH ) {
if ( empty( $path ) ) { return ''; }
$path = $root . $path;
if ( ! file_exists( $path ) ) { return ''; }
// TODO: do later ob_start(); include $path; $html = ob_get_contents(); ob_end_clean(); return yaymail_kses_post( $html ); } }//end if
if ( ! function_exists( 'yaymail_kses_post' ) ) { /** * The function yaymail_kses_post sanitizes HTML content using the allowed HTML tags defined in the * TemplateHelpers class. * * @param html The parameter is the input string that you want to sanitize and allow only * certain HTML tags and attributes. * * @return the result of the wp_kses() function, which is the sanitized version of the * parameter using the array as the allowed HTML tags and attributes. */ function yaymail_kses_post( $html ) { $allowed_html = TemplateHelpers::wp_kses_allowed_html(); return wp_kses( $html, $allowed_html ); } }
if ( ! function_exists( 'yaymail_kses_post_e' ) ) { /** * The function `yaymail_kses_post_e` echoes the HTML content after sanitizing it using the allowed * HTML tags defined in the `TemplateHelpers::wp_kses_allowed_html()` method. * * @param html The parameter is the content that you want to sanitize and filter using the * wp_kses() function. It could be any HTML content that you want to ensure is safe and free from * any potentially harmful or malicious code. */ function yaymail_kses_post_e( $html ) { if ( ! empty( $html ) ) { $allowed_html = TemplateHelpers::wp_kses_allowed_html(); echo wp_kses( $html, $allowed_html ); } else { echo ''; } } }
if ( ! function_exists( 'yaymail_get_text_align' ) ) { function yaymail_get_text_align() { $container_direction = yaymail_get_email_direction();
if ( 'rtl' === $container_direction ) { return 'right'; }
return is_rtl() ? 'right' : 'left'; } }
if ( ! function_exists( 'yaymail_get_default_elements' ) ) {
/** * Get default elements data of given email * * @param string $email_id * * @return array Return empty string when not found email */ function yaymail_get_default_elements( $email_id ) { $find_email = yaymail_get_email( $email_id );
if ( ! $find_email ) { return []; }
return $find_email->get_default_elements(); } }
if ( ! function_exists( 'yaymail_get_all_elements' ) ) {
/** * Get all registered elements * * @return BaseElement[] */ function yaymail_get_all_elements() { return ElementsLoader::get_instance()->get_all(); } }
if ( ! function_exists( 'yaymail_get_email_available_elements' ) ) {
/** * Get all available elements of given email * * @param string $email_id * * @return BaseElement[] */ function yaymail_get_email_available_elements( $email_id ) { $find_email = yaymail_get_email( $email_id );
if ( ! $find_email ) { return []; }
return $find_email->get_elements(); } }
if ( ! function_exists( 'yaymail_get_email_elements_data' ) ) {
/** * Get all elements data of given email * * @param string $email_id * * @return array */ function yaymail_get_email_elements_data( $email_id ) { $find_email = yaymail_get_email( $email_id );
if ( ! $find_email ) { return []; }
$all_elements = yaymail_get_all_elements(); $result = [];
foreach ( $all_elements as $element ) { $element_data = merge_extra_element_attributes( $element->get_data() ); $element_data['available'] = false; if ( $element->is_available_in_email( $find_email ) ) { $element_data['available'] = true; }
$result[] = $element_data;
/** * Add columns element */ if ( ColumnLayout::get_type() === $element::get_type() ) { foreach ( [ 2, 3, 4 ] as $col ) { $child_element_data = merge_extra_element_attributes( $element->get_data( $col ) ); $child_element_data['available'] = $element_data['available']; $result[] = $child_element_data; } } }//end foreach
return $result; }
/** * Merge extra attributes into element * * @param array $element_data * * @return array */ function merge_extra_element_attributes( $element ) { $extra_attributes = apply_filters( 'yaymail_extra_element_attributes', [], $element['type'] ); if ( empty( $extra_attributes ) ) { return $element; }
$data = &$element['data']; foreach ( $extra_attributes as $key => $value ) { if ( isset( $data[ $key ] ) || ! isset( $value ) ) { continue; } $data[ $key ] = $value; }
return $element; } }//end if
if ( ! function_exists( 'yaymail_get_element' ) ) {
/** * Get element by given type * * @param string $element_type * * @return null|BaseElement Return null when not found element */ function yaymail_get_element( $element_type ) {
$elements = yaymail_get_all_elements();
$find_element = null;
foreach ( $elements as $element ) { if ( $element::get_type() === $element_type ) { $find_element = $element; break; } }
return $find_element; } }//end if
if ( ! function_exists( 'yaymail_get_email_shortcodes' ) ) {
/** * Get all shortcodes of given email * * @param string $email_id * * @return array */ function yaymail_get_email_shortcodes( $email_id ) { $find_email = yaymail_get_email( $email_id );
if ( ! $find_email ) { return []; }
return $find_email->get_shortcodes(); } }
if ( ! function_exists( 'yaymail_get_logger' ) ) {
/** * Get logger instance */ function yaymail_get_logger( $message, $log_type = 'error', $additional_data = null ) { $logger = new Logger(); $logger->log_exception_message( new \Exception( $message ), $log_type, $additional_data ); } }
if ( ! function_exists( 'yaymail_get_wc_email_settings' ) ) { /** * Get WooCommerce email settings * * @return array An object of WooCommerce email settings which has these properties: * - 'header_image': The header image URL. * - 'base_color': The base color. * - 'background_color': The background color. * - 'body_background_color': The body background color. * - 'body_text_color': The body text color. * - 'footer_text': The footer text. * - 'footer_text_color': The footer text color. */ function yaymail_get_wc_email_settings() { return [ 'header_image' => get_option( 'woocommerce_email_header_image', '' ), 'base_color' => get_option( 'woocommerce_email_base_color', '#873EFF' ), 'background_color' => get_option( 'woocommerce_email_background_color', '#f7f7f7' ), 'body_background_color' => get_option( 'woocommerce_email_body_background_color', '#ffffff' ), 'body_text_color' => get_option( 'woocommerce_email_body_text_color', '#3c3c3c' ), 'footer_text' => get_option( 'woocommerce_email_footer_text', '[yaymail_site_name] — Built with WooCommerce' ), 'footer_text_color' => get_option( 'woocommerce_email_footer_text_color', '#3c3c3c' ), ]; } }//end if
if ( ! function_exists( 'yaymail_get_email_direction' ) ) { function yaymail_get_email_direction() { $yaymail_settings = yaymail_settings(); return isset( $yaymail_settings['direction'] ) && 'rtl' === $yaymail_settings['direction'] ? 'rtl' : 'ltr'; } }
/** * Get email recipient zone * * @param \WC_Email $email * @since 4.0.3 * * @return string */ function yaymail_get_email_recipient_zone( $email ) { $is_customer_email = $email instanceof \WC_Email && method_exists( $email, 'is_customer_email' ) ? $email->is_customer_email() : true; if ( $is_customer_email ) { return __( 'Customer', 'woocommerce' ); }
$recipient = ''; if ( $email instanceof \WC_Email ) { $recipient = ! empty( $email->recipient ) ? $email->recipient : $email->get_recipient(); if ( empty( $recipient ) ) { $recipient = __( 'Recipient', 'yaymail' ); } }
$recipients = array_map( function( $email_recipient ) { $recipient_user = get_user_by( 'email', $email_recipient ); if ( $recipient_user && user_can( $recipient_user, 'manage_options' ) ) { return __( 'Admin', 'woocommerce' ); } if ( empty( $email_recipient ) ) { return __( 'Recipient', 'yaymail' ); } return $email_recipient; }, explode( ',', $recipient ) ); $recipients = array_unique( $recipients ); return implode( ', ', $recipients ); }
|