PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.5
YayMail – WooCommerce Email Customizer v4.4.5
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.5, at src/Functions.php

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