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

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