PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.0
YayMail – WooCommerce Email Customizer v4.4.0
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.0, at src/Functions.php

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