PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.1
YayMail – WooCommerce Email Customizer v4.4.1
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.1, at src/Functions.php

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