PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.2
YayMail – WooCommerce Email Customizer v4.4.2
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 3.4.2 3.4.3 All 55 releases
yaymail / src / Functions.php

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

451 lines 13.0 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_is_wp_mail_installed' ) ) {
73 function yaymail_is_wp_mail_installed() {
74 return defined( 'YAYMAIL_WP_VERSION' );
75 }
76 }
77
78 if ( ! function_exists( 'yaymail_version' ) ) {
79 function yaymail_version() {
80 if ( defined( 'YAYMAIL_VERSION' ) ) {
81 return YAYMAIL_VERSION;
82 }
83 return false;
84 }
85 }
86
87
88
89 if ( ! function_exists( 'yaymail_settings' ) ) {
90 function yaymail_settings() {
91 global $yaymail_unsaved_settings;
92 if ( ! empty( $yaymail_unsaved_settings ) ) {
93 foreach ( $yaymail_unsaved_settings as $key => $value ) {
94 if ( 'true' === $value ) {
95 $yaymail_unsaved_settings[ $key ] = true;
96 }
97 if ( 'false' === $value ) {
98 $yaymail_unsaved_settings[ $key ] = false;
99 }
100 }
101 return $yaymail_unsaved_settings;
102 }
103 return SettingModel::get_instance()::find_all();
104 }
105 }
106
107 if ( ! function_exists( 'yaymail_get_content' ) ) {
108 function yaymail_get_content( $path, $args = [], $root = YAYMAIL_PLUGIN_PATH ) {
109
110 if ( empty( $path ) ) {
111 return '';
112 }
113
114 $path = $root . $path;
115
116 if ( $path === false || ! file_exists( $path ) ) {
117 return '';
118 }
119
120 // TODO: do later
121 ob_start();
122 include $path;
123 // nosemgrep
124 $html = ob_get_contents();
125 ob_end_clean();
126 return yaymail_kses_post( $html );
127 }
128 }//end if
129
130 if ( ! function_exists( 'yaymail_kses_post' ) ) {
131 /**
132 * The function yaymail_kses_post sanitizes HTML content using the allowed HTML tags defined in the
133 * TemplateHelpers class.
134 *
135 * @param html The parameter is the input string that you want to sanitize and allow only
136 * certain HTML tags and attributes.
137 *
138 * @return the result of the wp_kses() function, which is the sanitized version of the
139 * parameter using the array as the allowed HTML tags and attributes.
140 */
141 function yaymail_kses_post( $html ) {
142 $allowed_html = TemplateHelpers::wp_kses_allowed_html();
143 return wp_kses( $html, $allowed_html );
144 }
145 }
146
147
148 if ( ! function_exists( 'yaymail_kses_post_e' ) ) {
149 /**
150 * The function `yaymail_kses_post_e` echoes the HTML content after sanitizing it using the allowed
151 * HTML tags defined in the `TemplateHelpers::wp_kses_allowed_html()` method.
152 *
153 * @param html The parameter is the content that you want to sanitize and filter using the
154 * wp_kses() function. It could be any HTML content that you want to ensure is safe and free from
155 * any potentially harmful or malicious code.
156 */
157 function yaymail_kses_post_e( $html ) {
158 if ( ! empty( $html ) ) {
159 $allowed_html = TemplateHelpers::wp_kses_allowed_html();
160 echo wp_kses( $html, $allowed_html );
161 } else {
162 echo '';
163 }
164 }
165 }
166
167 if ( ! function_exists( 'yaymail_get_text_align' ) ) {
168 function yaymail_get_text_align() {
169 $container_direction = yaymail_get_email_direction();
170
171 if ( 'rtl' === $container_direction ) {
172 return 'right';
173 }
174
175 return is_rtl() ? 'right' : 'left';
176 }
177 }
178
179 if ( ! function_exists( 'yaymail_get_default_elements' ) ) {
180
181 /**
182 * Get default elements data of given email
183 *
184 * @param string $email_id
185 *
186 * @return array Return empty string when not found email
187 */
188 function yaymail_get_default_elements( $email_id ) {
189 $find_email = yaymail_get_email( $email_id );
190
191 if ( ! $find_email ) {
192 return [];
193 }
194
195 return $find_email->get_default_elements();
196 }
197 }
198
199 if ( ! function_exists( 'yaymail_get_all_elements' ) ) {
200
201 /**
202 * Get all registered elements
203 *
204 * @return BaseElement[]
205 */
206 function yaymail_get_all_elements() {
207 return ElementsLoader::get_instance()->get_all();
208 }
209 }
210
211 if ( ! function_exists( 'yaymail_get_email_available_elements' ) ) {
212
213 /**
214 * Get all available elements of given email
215 *
216 * @param string $email_id
217 *
218 * @return BaseElement[]
219 */
220 function yaymail_get_email_available_elements( $email_id ) {
221 $find_email = yaymail_get_email( $email_id );
222
223 if ( ! $find_email ) {
224 return [];
225 }
226
227 return $find_email->get_elements();
228 }
229 }
230
231 if ( ! function_exists( 'yaymail_get_email_elements_data' ) ) {
232
233 /**
234 * Get all elements data of given email
235 *
236 * @param string $email_id
237 *
238 * @return array
239 */
240 function yaymail_get_email_elements_data( $email_id ) {
241 $find_email = yaymail_get_email( $email_id );
242
243 if ( ! $find_email ) {
244 return [];
245 }
246
247 $all_elements = yaymail_get_all_elements();
248 $result = [];
249
250 foreach ( $all_elements as $element ) {
251 $element_data = merge_extra_element_attributes( $element->get_data() );
252 $element_data['available'] = false;
253 if ( $element->is_available_in_email( $find_email ) ) {
254 $element_data['available'] = true;
255 }
256
257 $result[] = $element_data;
258
259 /**
260 * Add columns element
261 */
262 if ( ColumnLayout::get_type() === $element::get_type() ) {
263 foreach ( [ 2, 3, 4 ] as $col ) {
264 $child_element_data = merge_extra_element_attributes( $element->get_data( $col ) );
265 $child_element_data['available'] = $element_data['available'];
266 $result[] = $child_element_data;
267 }
268 }
269 }//end foreach
270
271 return $result;
272 }
273
274 /**
275 * Merge extra attributes into element
276 *
277 * @param array $element_data
278 *
279 * @return array
280 */
281 function merge_extra_element_attributes( $element ) {
282 $extra_attributes = apply_filters( 'yaymail_extra_element_attributes', [], $element['type'] );
283 if ( empty( $extra_attributes ) ) {
284 return $element;
285 }
286
287 $data = &$element['data'];
288 foreach ( $extra_attributes as $key => $value ) {
289 if ( isset( $data[ $key ] ) || ! isset( $value ) ) {
290 continue;
291 }
292 $data[ $key ] = $value;
293 }
294
295 return $element;
296 }
297 }//end if
298
299 if ( ! function_exists( 'yaymail_get_element' ) ) {
300
301 /**
302 * Get element by given type
303 *
304 * @param string $element_type
305 *
306 * @return null|BaseElement Return null when not found element
307 */
308 function yaymail_get_element( $element_type ) {
309
310 $elements = yaymail_get_all_elements();
311
312 $find_element = null;
313
314 foreach ( $elements as $element ) {
315 if ( $element::get_type() === $element_type ) {
316 $find_element = $element;
317 break;
318 }
319 }
320
321 return $find_element;
322 }
323 }//end if
324
325 if ( ! function_exists( 'yaymail_get_email_shortcodes' ) ) {
326
327 /**
328 * Get all shortcodes of given email
329 *
330 * @param string $email_id
331 *
332 * @return array
333 */
334 function yaymail_get_email_shortcodes( $email_id ) {
335 $find_email = yaymail_get_email( $email_id );
336
337 if ( ! $find_email ) {
338 return [];
339 }
340
341 return $find_email->get_shortcodes();
342 }
343 }
344
345 if ( ! function_exists( 'yaymail_get_logger' ) ) {
346
347 /**
348 * Get logger instance
349 */
350 function yaymail_get_logger( $message, $log_type = 'error', $additional_data = null ) {
351 $logger = new Logger();
352 $logger->log_exception_message( new \Exception( $message ), $log_type, $additional_data );
353 }
354 }
355
356 if ( ! function_exists( 'yaymail_get_wc_email_settings' ) ) {
357 /**
358 * Get WooCommerce email settings
359 *
360 * @return array An object of WooCommerce email settings which has these properties:
361 * - 'header_image': The header image URL.
362 * - 'base_color': The base color.
363 * - 'background_color': The background color.
364 * - 'body_background_color': The body background color.
365 * - 'body_text_color': The body text color.
366 * - 'footer_text': The footer text.
367 * - 'footer_text_color': The footer text color.
368 */
369 function yaymail_get_wc_email_settings() {
370 return [
371 'header_image' => get_option( 'woocommerce_email_header_image', '' ),
372 'base_color' => get_option( 'woocommerce_email_base_color', '#873EFF' ),
373 'background_color' => get_option( 'woocommerce_email_background_color', '#f7f7f7' ),
374 'body_background_color' => get_option( 'woocommerce_email_body_background_color', '#ffffff' ),
375 'body_text_color' => get_option( 'woocommerce_email_body_text_color', '#3c3c3c' ),
376 'footer_text' => get_option( 'woocommerce_email_footer_text', '[yaymail_site_name] &mdash; Built with WooCommerce' ),
377 'footer_text_color' => get_option( 'woocommerce_email_footer_text_color', '#3c3c3c' ),
378 ];
379 }
380 }//end if
381
382
383 if ( ! function_exists( 'yaymail_get_email_direction' ) ) {
384 function yaymail_get_email_direction() {
385 $yaymail_settings = yaymail_settings();
386 return isset( $yaymail_settings['direction'] ) && 'rtl' === $yaymail_settings['direction'] ? 'rtl' : 'ltr';
387 }
388 }//end if
389
390 /**
391 * Get email recipient zone
392 *
393 * @param \WC_Email $email
394 * @since 4.0.3
395 *
396 * @return string
397 */
398 if ( ! function_exists( 'yaymail_get_email_recipient_zone' ) ) {
399 function yaymail_get_email_recipient_zone( $email ) {
400 $is_customer_email = $email instanceof \WC_Email && method_exists( $email, 'is_customer_email' ) ? $email->is_customer_email() : true;
401 if ( $is_customer_email ) {
402 return __( 'Customer', 'woocommerce' );
403 }
404
405 $recipient = '';
406 if ( $email instanceof \WC_Email ) {
407 $recipient = ! empty( $email->recipient ) ? $email->recipient : $email->get_recipient();
408 if ( empty( $recipient ) ) {
409 $recipient = __( 'Recipient', 'yaymail' );
410 }
411 }
412
413 $recipients = array_map(
414 function( $email_recipient ) {
415 $recipient_user = get_user_by( 'email', $email_recipient );
416 if ( $recipient_user && user_can( $recipient_user, 'manage_options' ) ) {
417 return __( 'Admin', 'woocommerce' );
418 }
419 if ( empty( $email_recipient ) ) {
420 return __( 'Recipient', 'yaymail' );
421 }
422 return $email_recipient;
423 },
424 explode( ',', $recipient )
425 );
426 $recipients = array_unique( $recipients );
427 return implode( ', ', $recipients );
428 }
429 }//end if
430
431 if ( ! function_exists( 'yaymail_get_template' ) ) {
432 function yaymail_get_template( $template_name, $template_path = '', $default_path = '' ) {
433 if ( ! $template_path ) {
434 $template_path = 'yaymail';
435 }
436
437 $template = locate_template(
438 [
439 trailingslashit( $template_path ) . $template_name,
440 $template_name,
441 ]
442 );
443
444 if ( ! $template ) {
445 $template = $default_path . $template_name;
446 }
447
448 return $template;
449 }
450 }
451