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 / Functions.php

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

527 lines 16.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use YayMail\Models\SettingModel;
4 use YayMail\Utils\TemplateHelpers;
5 use YayMail\Constants\TemplatesData;
6 use YayMail\Elements\ColumnLayout;
7 use YayMail\Elements\ElementsLoader;
8 use YayMail\YayMailEmails;
9 use YayMail\Utils\Logger;
10
11 if ( ! function_exists( 'yaymail_get_emails' ) ) {
12
13 /**
14 * Get all supported Emails
15 *
16 * @return BaseEmail[]
17 */
18 function yaymail_get_emails() {
19 $yaymail_emails = YayMailEmails::get_instance()->get_emails();
20 $emails_default = [];
21 $emails_third_party = [];
22
23 foreach ( $yaymail_emails as $email ) {
24 if ( in_array( $email->get_id(), TemplatesData::WOO_DEFAULT_EMAIL_IDS, true ) ) {
25 $emails_default[] = $email;
26 } else {
27 $emails_third_party[] = $email;
28 }
29 }
30 $sorted_emails = array_merge( $emails_default, $emails_third_party );
31 return $sorted_emails;
32 }
33 }//end if
34
35
36 if ( ! function_exists( 'yaymail_get_email' ) ) {
37
38 /**
39 * Get email by email id
40 *
41 * @param string $email_id
42 *
43 * @return null|BaseEmail Return null when not found
44 */
45 function yaymail_get_email( $email_id ) {
46 $emails = yaymail_get_emails();
47
48 $find_email = null;
49
50 foreach ( $emails as $email ) {
51 if ( $email_id === $email->get_id() ) {
52 $find_email = $email;
53 break;
54 }
55 }
56
57 return $find_email;
58 }
59 }//end if
60
61 if ( ! function_exists( 'yaymail_is_wc_installed' ) ) {
62 function yaymail_is_wc_installed() {
63 if ( ! function_exists( 'WC' ) ) {
64 return false;
65 }
66
67 $plugin_work = \YayMail\Utils\Helpers::get_plugin_work_info();
68 return $plugin_work['yaymail'];
69 }
70 }
71
72 if ( ! function_exists( 'yaymail_version' ) ) {
73 function yaymail_version() {
74 if ( defined( 'YAYMAIL_VERSION' ) ) {
75 return YAYMAIL_VERSION;
76 }
77 return false;
78 }
79 }
80
81
82
83 if ( ! function_exists( 'yaymail_settings' ) ) {
84 function yaymail_settings() {
85 global $yaymail_unsaved_settings;
86 if ( ! empty( $yaymail_unsaved_settings ) ) {
87 foreach ( $yaymail_unsaved_settings as $key => $value ) {
88 if ( 'true' === $value ) {
89 $yaymail_unsaved_settings[ $key ] = true;
90 }
91 if ( 'false' === $value ) {
92 $yaymail_unsaved_settings[ $key ] = false;
93 }
94 }
95 return $yaymail_unsaved_settings;
96 }
97 return SettingModel::get_instance()::find_all();
98 }
99 }
100
101 if ( ! function_exists( 'yaymail_get_content' ) ) {
102 function yaymail_get_content( $path, $args = [], $root = YAYMAIL_PLUGIN_PATH ) {
103
104 if ( empty( $path ) ) {
105 return '';
106 }
107
108 $path = $root . $path;
109
110 if ( $path === false || ! file_exists( $path ) ) {
111 return '';
112 }
113
114 // TODO: do later
115 ob_start();
116 // nosemgrep: audit.php.lang.security.file.inclusion-arg
117 include $path;
118 // nosemgrep
119 $html = ob_get_contents();
120 ob_end_clean();
121 return yaymail_kses_post( $html );
122 }
123 }//end if
124
125 if ( ! function_exists( 'yaymail_kses_post' ) ) {
126 /**
127 * The function yaymail_kses_post sanitizes HTML content using the allowed HTML tags defined in the
128 * TemplateHelpers class.
129 *
130 * @param html The parameter is the input string that you want to sanitize and allow only
131 * certain HTML tags and attributes.
132 *
133 * @return the result of the wp_kses() function, which is the sanitized version of the
134 * parameter using the array as the allowed HTML tags and attributes.
135 */
136 function yaymail_kses_post( $html ) {
137 $allowed_html = TemplateHelpers::wp_kses_allowed_html();
138 return wp_kses( $html, $allowed_html );
139 }
140 }
141
142
143 if ( ! function_exists( 'yaymail_kses_post_e' ) ) {
144 /**
145 * The function `yaymail_kses_post_e` echoes the HTML content after sanitizing it using the allowed
146 * HTML tags defined in the `TemplateHelpers::wp_kses_allowed_html()` method.
147 *
148 * @param html The parameter is the content that you want to sanitize and filter using the
149 * wp_kses() function. It could be any HTML content that you want to ensure is safe and free from
150 * any potentially harmful or malicious code.
151 */
152 function yaymail_kses_post_e( $html ) {
153 if ( ! empty( $html ) ) {
154 $allowed_html = TemplateHelpers::wp_kses_allowed_html();
155 echo wp_kses( $html, $allowed_html );
156 } else {
157 echo '';
158 }
159 }
160 }
161
162 if ( ! function_exists( 'yaymail_get_text_align' ) ) {
163 function yaymail_get_text_align() {
164 $container_direction = yaymail_get_email_direction();
165
166 if ( 'rtl' === $container_direction ) {
167 return 'right';
168 }
169
170 return is_rtl() ? 'right' : 'left';
171 }
172 }
173
174 if ( ! function_exists( 'yaymail_get_default_elements' ) ) {
175
176 /**
177 * Get default elements data of given email
178 *
179 * @param string $email_id
180 *
181 * @return array Return empty string when not found email
182 */
183 function yaymail_get_default_elements( $email_id ) {
184 $find_email = yaymail_get_email( $email_id );
185
186 if ( ! $find_email ) {
187 return [];
188 }
189
190 return $find_email->get_default_elements();
191 }
192 }
193
194 if ( ! function_exists( 'yaymail_get_all_elements' ) ) {
195
196 /**
197 * Get all registered elements
198 *
199 * @return BaseElement[]
200 */
201 function yaymail_get_all_elements() {
202 return ElementsLoader::get_instance()->get_all();
203 }
204 }
205
206 if ( ! function_exists( 'yaymail_get_email_available_elements' ) ) {
207
208 /**
209 * Get all available elements of given email
210 *
211 * @param string $email_id
212 *
213 * @return BaseElement[]
214 */
215 function yaymail_get_email_available_elements( $email_id ) {
216 $find_email = yaymail_get_email( $email_id );
217
218 if ( ! $find_email ) {
219 return [];
220 }
221
222 return $find_email->get_elements();
223 }
224 }
225
226 if ( ! function_exists( 'yaymail_get_email_elements_data' ) ) {
227
228 /**
229 * Get all elements data of given email
230 *
231 * @param string $email_id
232 *
233 * @return array
234 */
235 function yaymail_get_email_elements_data( $email_id ) {
236 $find_email = yaymail_get_email( $email_id );
237
238 if ( ! $find_email ) {
239 return [];
240 }
241
242 // Not passed as a get_data() argument: ColumnLayout::get_data( $amount, $attributes )
243 // uses that same first/second position for its own params, so any element's
244 // default color must read this out-of-band instead. Scoped to this one request.
245 global $yaymail_current_email_id;
246 $yaymail_current_email_id = $email_id;
247
248 $all_elements = yaymail_get_all_elements();
249 $result = [];
250
251 foreach ( $all_elements as $element ) {
252 $element_data = merge_extra_element_attributes( $element->get_data() );
253 $element_data['available'] = false;
254 if ( $element->is_available_in_email( $find_email ) ) {
255 $element_data['available'] = true;
256 }
257
258 $result[] = $element_data;
259
260 /**
261 * Add columns element
262 */
263 if ( ColumnLayout::get_type() === $element::get_type() ) {
264 foreach ( [ 2, 3, 4 ] as $col ) {
265 $child_element_data = merge_extra_element_attributes( $element->get_data( $col ) );
266 $child_element_data['available'] = $element_data['available'];
267 $result[] = $child_element_data;
268 }
269 }
270 }//end foreach
271
272 return $result;
273 }
274
275 /**
276 * Merge extra attributes into element
277 *
278 * @param array $element_data
279 *
280 * @return array
281 */
282 function merge_extra_element_attributes( $element ) {
283 $extra_attributes = apply_filters( 'yaymail_extra_element_attributes', [], $element['type'] );
284 if ( empty( $extra_attributes ) ) {
285 return $element;
286 }
287
288 $data = &$element['data'];
289 foreach ( $extra_attributes as $key => $value ) {
290 if ( isset( $data[ $key ] ) || ! isset( $value ) ) {
291 continue;
292 }
293 $data[ $key ] = $value;
294 }
295
296 return $element;
297 }
298 }//end if
299
300 if ( ! function_exists( 'yaymail_get_default_brand_color' ) ) {
301
302 /**
303 * Default accent color for newly dragged elements (Heading background, Button
304 * background, etc.), matching the frontend's brand color per platform
305 * (constants/theme.ts WP_COLORS vs YAYMAIL_TOKENS.color.wcPurple).
306 *
307 * Element get_data() methods share BaseElement's abstract contract, but
308 * ColumnLayout::get_data( $amount, $attributes ) repurposes that same
309 * position for a non-attributes param -- so the email/template id can't be
310 * threaded through as a get_data() argument without breaking it. Falls back
311 * to the id yaymail_get_email_elements_data() stashed for the current request.
312 *
313 * @param string|null $email_id Email/template id to check; defaults to the
314 * current request's when omitted.
315 * @return string Hex color.
316 */
317 function yaymail_get_default_brand_color( $email_id = null ) {
318 if ( null === $email_id ) {
319 global $yaymail_current_email_id;
320 $email_id = $yaymail_current_email_id;
321 }
322 $email_id = (string) $email_id;
323
324 $is_wp_template = 0 === strpos( $email_id, 'wp-core-' ) || 'wp_global_header_footer' === $email_id;
325
326 return $is_wp_template ? YAYMAIL_COLOR_WP_DEFAULT : YAYMAIL_COLOR_WC_DEFAULT;
327 }
328 }
329
330 if ( ! function_exists( 'yaymail_get_ghf_disallowed_element_types' ) ) {
331
332 /**
333 * Element types that cannot be used in global header/footer.
334 * Derived from the same availability rules as the GHF customizer sidebar.
335 *
336 * @return string[]
337 */
338 function yaymail_get_ghf_disallowed_element_types() {
339 $elements = yaymail_get_email_elements_data( 'yaymail_global_header_footer' );
340
341 $disallowed = [];
342 foreach ( $elements as $element ) {
343 if ( empty( $element['available'] ) && ! empty( $element['type'] ) ) {
344 $disallowed[] = $element['type'];
345 }
346 }
347
348 return array_values( array_unique( $disallowed ) );
349 }
350 }//end if
351
352 if ( ! function_exists( 'yaymail_get_element' ) ) {
353
354 /**
355 * Get element by given type
356 *
357 * @param string $element_type
358 *
359 * @return null|BaseElement Return null when not found element
360 */
361 function yaymail_get_element( $element_type ) {
362
363 $elements = yaymail_get_all_elements();
364
365 $find_element = null;
366
367 foreach ( $elements as $element ) {
368 if ( $element::get_type() === $element_type ) {
369 $find_element = $element;
370 break;
371 }
372 }
373
374 return $find_element;
375 }
376 }//end if
377
378 if ( ! function_exists( 'yaymail_get_email_shortcodes' ) ) {
379
380 /**
381 * Get all shortcodes of given email
382 *
383 * @param string $email_id
384 *
385 * @return array
386 */
387 function yaymail_get_email_shortcodes( $email_id ) {
388 $find_email = yaymail_get_email( $email_id );
389
390 if ( ! $find_email ) {
391 return [];
392 }
393
394 return $find_email->get_shortcodes();
395 }
396 }
397
398 if ( ! function_exists( 'yaymail_get_logger' ) ) {
399
400 /**
401 * Get logger instance
402 */
403 function yaymail_get_logger( $message, $log_type = 'error', $additional_data = null ) {
404 $logger = new Logger();
405 $logger->log_exception_message( new \Exception( $message ), $log_type, $additional_data );
406 }
407 }
408
409 if ( ! function_exists( 'yaymail_get_attachment_image_url' ) ) {
410
411 /**
412 * Safely resolve an attachment image URL.
413 *
414 * Returns '' for an empty id or a dangling/deleted attachment (where
415 * wp_get_attachment_image_src() returns false). Never throws, so a missing
416 * image degrades gracefully instead of breaking the request that triggered
417 * rendering (e.g. WooCommerce checkout).
418 *
419 * @param int|string $image_id Attachment ID.
420 * @param string $size Registered image size.
421 * @return string Image URL or '' when unavailable.
422 */
423 function yaymail_get_attachment_image_url( $image_id, $size = 'full' ) {
424 if ( empty( $image_id ) ) {
425 return '';
426 }
427 $image = wp_get_attachment_image_src( $image_id, $size );
428 return ( is_array( $image ) && ! empty( $image[0] ) ) ? $image[0] : '';
429 }
430 }//end if
431
432 if ( ! function_exists( 'yaymail_get_wc_email_settings' ) ) {
433 /**
434 * Get WooCommerce email settings
435 *
436 * @return array An object of WooCommerce email settings which has these properties:
437 * - 'header_image': The header image URL.
438 * - 'base_color': The base color.
439 * - 'background_color': The background color.
440 * - 'body_background_color': The body background color.
441 * - 'body_text_color': The body text color.
442 * - 'footer_text': The footer text.
443 * - 'footer_text_color': The footer text color.
444 */
445 function yaymail_get_wc_email_settings() {
446 return [
447 'header_image' => get_option( 'woocommerce_email_header_image', '' ),
448 'base_color' => get_option( 'woocommerce_email_base_color', '#873EFF' ),
449 'background_color' => get_option( 'woocommerce_email_background_color', '#f7f7f7' ),
450 'body_background_color' => get_option( 'woocommerce_email_body_background_color', '#ffffff' ),
451 'body_text_color' => get_option( 'woocommerce_email_body_text_color', '#3c3c3c' ),
452 'footer_text' => get_option( 'woocommerce_email_footer_text', '[yaymail_site_name] &mdash; Built with WooCommerce' ),
453 'footer_text_color' => get_option( 'woocommerce_email_footer_text_color', '#3c3c3c' ),
454 ];
455 }
456 }//end if
457
458
459 if ( ! function_exists( 'yaymail_get_email_direction' ) ) {
460 function yaymail_get_email_direction() {
461 $yaymail_settings = yaymail_settings();
462 return isset( $yaymail_settings['direction'] ) && 'rtl' === $yaymail_settings['direction'] ? 'rtl' : 'ltr';
463 }
464 }//end if
465
466 /**
467 * Get email recipient zone
468 *
469 * @param \WC_Email $email
470 * @since 4.0.3
471 *
472 * @return string
473 */
474 if ( ! function_exists( 'yaymail_get_email_recipient_zone' ) ) {
475 function yaymail_get_email_recipient_zone( $email ) {
476 $is_customer_email = $email instanceof \WC_Email && method_exists( $email, 'is_customer_email' ) ? $email->is_customer_email() : true;
477 if ( $is_customer_email ) {
478 return __( 'Customer', 'woocommerce' );
479 }
480
481 $recipient = '';
482 if ( $email instanceof \WC_Email ) {
483 $recipient = ! empty( $email->recipient ) ? $email->recipient : $email->get_recipient();
484 if ( empty( $recipient ) ) {
485 $recipient = __( 'Recipient', 'yaymail' );
486 }
487 }
488
489 $recipients = array_map(
490 function( $email_recipient ) {
491 $recipient_user = get_user_by( 'email', $email_recipient );
492 if ( $recipient_user && user_can( $recipient_user, 'manage_options' ) ) {
493 return __( 'Admin', 'woocommerce' );
494 }
495 if ( empty( $email_recipient ) ) {
496 return __( 'Recipient', 'yaymail' );
497 }
498 return $email_recipient;
499 },
500 explode( ',', $recipient )
501 );
502 $recipients = array_unique( $recipients );
503 return implode( ', ', $recipients );
504 }
505 }//end if
506
507 if ( ! function_exists( 'yaymail_get_template' ) ) {
508 function yaymail_get_template( $template_name, $template_path = '', $default_path = '' ) {
509 if ( ! $template_path ) {
510 $template_path = 'yaymail';
511 }
512
513 $template = locate_template(
514 [
515 trailingslashit( $template_path ) . $template_name,
516 $template_name,
517 ]
518 );
519
520 if ( ! $template ) {
521 $template = $default_path . $template_name;
522 }
523
524 return $template;
525 }
526 }
527