PluginProbe
YayMail – WooCommerce Email Customizer / 4.3.3
YayMail – WooCommerce Email Customizer v4.3.3
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
← All changes | src/Models/TemplateModel.php +37 -152 trunk4.3.3 View file →
@@ -7,9 +7,8 @@
7 7 use YayMail\Elements\Heading;
8 8 use YayMail\Elements\Logo;
9 9 use YayMail\Elements\Text;
10 10 use YayMail\Elements\Footer;
11 -use YayMail\Integrations\TranslationModule;
12 11 use YayMail\YayMailTemplate;
13 12 use YayMail\Utils\SingletonTrait;
14 13 use YayMail\PostTypes\TemplatePostType;
15 14 use YayMail\Shortcodes\ShortcodesExecutor;
@@ -14,8 +13,9 @@
14 13 use YayMail\PostTypes\TemplatePostType;
15 14 use YayMail\Shortcodes\ShortcodesExecutor;
16 15 use YayMail\SupportedPlugins;
17 16 use YayMail\Utils\TemplateHelpers;
17 +
18 18 /**
19 19 * Template Model
20 20 *
21 21 * @method static TemplateModel get_instance()
@@ -55,13 +55,9 @@
55 55
56 56 if ( isset( $args['name'] ) ) {
57 57 $template_name = $args['name'];
58 58 $template_meta_key = self::$meta_keys['name'];
59 - $clauses['where'] .= $wpdb->prepare(
60 - ' AND ( postmeta.meta_key=%s AND postmeta.meta_value = %s )',
61 - $template_meta_key,
62 - $template_name
63 - );
59 + $clauses['where'] .= " AND ( postmeta.meta_key='$template_meta_key' AND postmeta.meta_value = '$template_name' )";
64 60 }
65 61
66 62 $query_string = implode( ' ', $clauses );
67 63 $post_id = $wpdb->get_var( $query_string );
@@ -88,40 +84,36 @@
88 84 $templates = [];
89 85
90 86 $excluded_templates = apply_filters( 'yaymail_excluded_templates', [ 'yaymail_global_header_footer' ] );
91 87
92 - if ( yaymail_is_wc_installed() ) {
93 - /* Wc Emails, may or may not be supported by us */
94 - $wc_emails = \WC_Emails::instance()->get_emails();
88 + /* Wc Emails, may or may not be supported by us */
89 + $wc_emails = \WC_Emails::instance()->get_emails();
95 90
96 - foreach ( $wc_emails as $wc_email ) {
97 - $template_id = $wc_email->id ?? null;
91 + foreach ( $wc_emails as $wc_email ) {
92 + $template_id = $wc_email->id ?? null;
98 93
99 - if ( ! isset( $template_id ) ) {
100 - continue;
101 - }
94 + if ( ! isset( $template_id ) ) {
95 + continue;
96 + }
102 97
103 - if ( in_array( $template_id, $excluded_templates, true ) ) {
104 - continue;
105 - }
98 + if ( in_array( $template_id, $excluded_templates, true ) ) {
99 + continue;
100 + }
106 101
107 - if ( SupportedPlugins::get_instance()->get_support_info( $template_id )['status'] !== 'already_supported' ) {
108 - // Templates is currently not editable, but could be supported by pro/addon
109 - $template_data = self::get_uneditable_template( $wc_email, $templates );
110 - $templates[] = $template_data;
111 - continue;
112 - }
102 + if ( SupportedPlugins::get_instance()->get_support_info( $template_id )['status'] !== 'already_supported' ) {
103 + // Templates is currently not editable, but could be supported by pro/addon
104 + $template_data = self::get_uneditable_template( $wc_email, $templates );
105 + $templates[] = $template_data;
106 + continue;
107 + }
113 108
114 - $email_data = SupportedPlugins::get_instance()->get_yaymail_template_data( $template_id );
115 - $template_data = self::get_yaymail_template( $email_data );
116 - if ( isset( $template_data ) ) {
117 - unset( $template_data['elements'] );
118 - $templates[] = $template_data;
119 - }
120 - }//end foreach
121 - } else {
122 - $templates = [];
123 - }//end if
109 + $email_data = SupportedPlugins::get_instance()->get_yaymail_template_data( $template_id );
110 + $template_data = self::get_yaymail_template( $email_data );
111 + if ( isset( $template_data ) ) {
112 + unset( $template_data['elements'] );
113 + $templates[] = $template_data;
114 + }
115 + }//end foreach
124 116
125 117 // Make sure it will show templates for Automatewoo ...
126 118 // Because those templates don't appear in wc_emails
127 119 $template_ids = array_map( fn( $template ) => $template['name'], $templates );
@@ -334,12 +326,8 @@
334 326 if ( ! empty( $data['global_footer_settings'] ) && isset( self::$meta_keys['global_footer_settings'] ) ) {
335 327 update_post_meta( $template_id, self::$meta_keys['global_footer_settings'], $data['global_footer_settings'] );
336 328 }
337 329
338 - if ( isset( $data['preheader'] ) && isset( self::$meta_keys['preheader'] ) ) {
339 - update_post_meta( $template_id, self::$meta_keys['preheader'], sanitize_text_field( $data['preheader'] ) );
340 - }
341 -
342 330 // Update post_modified
343 331 $post_data = [
344 332 'ID' => $template_id,
345 333 'post_modified' => current_time( 'mysql' ),
@@ -375,20 +363,18 @@
375 363 * @return array An array containing the global header and footer elements:
376 364 * - 'global_header_elements': array.
377 365 * - 'global_footer_elements': array.
378 366 */
379 - public static function get_global_header_and_footer( $language = '', $template_name = 'yaymail_global_header_footer' ) {
367 + public static function get_global_header_and_footer() {
380 368 $query_args = [
381 - 'name' => $template_name,
382 - 'language' => in_array( $language, [ 'en', 'en_US' ], true ) ? '' : $language,
369 + 'name' => 'yaymail_global_header_footer',
383 370 ];
384 371 $template_id = self::query_template( $query_args );
385 372 if ( empty( $template_id ) ) {
386 373 self::insert(
387 374 [
388 - 'name' => $template_name,
389 - 'elements' => yaymail_get_default_elements( $template_name ),
390 - 'language' => $language,
375 + 'name' => 'yaymail_global_header_footer',
376 + 'elements' => yaymail_get_default_elements( 'yaymail_global_header_footer' ),
391 377 ]
392 378 );
393 379 $template_id = self::query_template( $query_args );
394 380 }
@@ -418,43 +404,8 @@
418 404 'global_footer_elements' => $global_footer_elements,
419 405 ];
420 406 }
421 407
422 - public static function get_global_headers_and_footers_for_all_available_languages( $template_name = 'yaymail_global_header_footer' ) {
423 - $multi_languages_instances = TranslationModule::get_instance()->current_integration;
424 -
425 - if ( ! empty( $multi_languages_instances ) ) {
426 - $languages = $multi_languages_instances->get_available_languages();
427 - } else {
428 - $languages = [ [ 'code' => 'en' ] ];
429 - }
430 -
431 - /**
432 - * Each language should have 3 fields: code, name, flag
433 - *
434 - * @Example [
435 - * code: 'en',
436 - * name: 'English',
437 - * flag: 'http://localhost:8080/wp-content/plugins/sitepress-multilingual-cms/res/flags/en.svg'
438 - * ]
439 - */
440 - $result = array_map(
441 - function ( $language ) use ( $template_name ) {
442 - $code = isset( $language['code'] ) ? $language['code'] : '';
443 - $global_header_and_footer = self::get_global_header_and_footer( 'en' !== $code ? $code : '', $template_name );
444 -
445 - return [
446 - 'language' => $code,
447 - 'global_header_elements' => $global_header_and_footer['global_header_elements'] ? $global_header_and_footer['global_header_elements'] : [],
448 - 'global_footer_elements' => $global_header_and_footer['global_footer_elements'] ? $global_header_and_footer['global_footer_elements'] : [],
449 - ];
450 - },
451 - $languages
452 - );
453 -
454 - return array_values( $result );
455 - }
456 -
457 408 /**
458 409 * Get template meta data from Database
459 410 *
460 411 * @param number $template_post_id
@@ -473,9 +424,9 @@
473 424 // /** For editable templates */
474 425 $template_elements = self::query_meta_data( $template_post_id, self::$meta_keys['elements'], [] );
475 426
476 427 $support_info = SupportedPlugins::get_instance()->get_support_info( $template_name );
477 - if ( $support_info['status'] !== 'already_supported' && 'wp-core-edd-password_reset' !== $template_name ) {
428 + if ( $support_info['status'] !== 'already_supported' ) {
478 429 // Template is not editable
479 430 $template_elements = self::get_uneditable_template_placeholder_elements( $support_info );
480 431 } elseif ( isset( $post ) ) {
481 432 // TODO: how to store global default value
@@ -483,11 +434,10 @@
483 434 $text_link_color = self::query_meta_data( $template_post_id, self::$meta_keys['text_link_color'], YayMailTemplate::DEFAULT_DATA['text_link_color'] );
484 435 $title_color = self::query_meta_data( $template_post_id, self::$meta_keys['title_color'], YayMailTemplate::DEFAULT_DATA['title_color'] );
485 436 $content_background_color = self::query_meta_data( $template_post_id, self::$meta_keys['content_background_color'], YayMailTemplate::DEFAULT_DATA['content_background_color'] );
486 437 $content_text_color = self::query_meta_data( $template_post_id, self::$meta_keys['content_text_color'], YayMailTemplate::DEFAULT_DATA['content_text_color'] );
487 - $global_header_settings = self::query_meta_data( $template_post_id, self::$meta_keys['global_header_settings'], self::get_default_global_header_settings() );
488 - $global_footer_settings = self::query_meta_data( $template_post_id, self::$meta_keys['global_footer_settings'], self::get_default_global_footer_settings() );
489 - $preheader = self::query_meta_data( $template_post_id, self::$meta_keys['preheader'], YayMailTemplate::DEFAULT_DATA['preheader'] );
438 + $global_header_settings = self::query_meta_data( $template_post_id, self::$meta_keys['global_header_settings'], YayMailTemplate::DEFAULT_DATA['global_header_settings'] );
439 + $global_footer_settings = self::query_meta_data( $template_post_id, self::$meta_keys['global_footer_settings'], YayMailTemplate::DEFAULT_DATA['global_footer_settings'] );
490 440 }
491 441
492 442 return [
493 443 'id' => $template_post_id,
@@ -502,59 +452,13 @@
502 452 'content_text_color' => $content_text_color ?? '#000000',
503 453 'support_status' => $support_info['status'] ?? 'already_supported',
504 454 'addon_info' => $support_info['addon'] ?? '',
505 455 'post_modified' => $post_modified,
506 - 'global_header_settings' => isset( $global_header_settings ) ? $global_header_settings : [],
507 - 'global_footer_settings' => isset( $global_footer_settings ) ? $global_footer_settings : [],
508 - 'preheader' => isset( $preheader ) ? $preheader : YayMailTemplate::DEFAULT_DATA['preheader'],
509 - 'email_subject' => self::get_email_subject( $template_name ),
456 + 'global_header_settings' => $global_header_settings,
457 + 'global_footer_settings' => $global_footer_settings,
510 458 ];
511 459 }
512 460
513 - /**
514 - * Get email subject from WooCommerce email settings.
515 - *
516 - * @param string $template_name Email / template id.
517 - * @return string|null
518 - */
519 - private static function get_email_subject( $template_name ) {
520 - $settings = self::get_wc_email_settings( $template_name );
521 - return is_array( $settings ) && isset( $settings['subject'] ) ? (string) $settings['subject'] : null;
522 - }
523 -
524 - /**
525 - * Save email subject to WooCommerce email settings (same option as Woo admin).
526 - *
527 - * @param string $template_name Email / template id.
528 - * @param string $subject Subject line.
529 - */
530 - public static function save_email_subject( $template_name, $subject ) {
531 - if ( ! is_string( $template_name ) || '' === $template_name ) {
532 - return;
533 - }
534 -
535 - $settings = self::get_wc_email_settings( $template_name );
536 - $settings['subject'] = sanitize_text_field( $subject );
537 - update_option( self::get_wc_email_settings_option_name( $template_name ), $settings );
538 - }
539 -
540 - /**
541 - * @param string $template_name Email / template id.
542 - * @return string
543 - */
544 - private static function get_wc_email_settings_option_name( $template_name ) {
545 - return "woocommerce_{$template_name}_settings";
546 - }
547 -
548 - /**
549 - * @param string $template_name Email / template id.
550 - * @return array
551 - */
552 - private static function get_wc_email_settings( $template_name ) {
553 - $settings = get_option( self::get_wc_email_settings_option_name( $template_name ), [] );
554 - return is_array( $settings ) ? $settings : [];
555 - }
556 -
557 461 private static function get_uneditable_template_placeholder_elements( array $support_info ): array {
558 462 $elements = [];
559 463
560 464 $elements[] = BaseElement::reduce_new_element( Logo::get_data() );
@@ -565,25 +469,9 @@
565 469 return $elements;
566 470 }
567 471
568 472
569 - private static function get_default_global_footer_settings(): array {
570 - $default = YayMailTemplate::DEFAULT_DATA['global_footer_settings'];
571 - if ( ! function_exists( 'WC' ) ) {
572 - $default['footer_content'] = '<p style="font-size: 14px;margin: 0px 0px 16px; text-align: center;">[yaymail_site_name]</p>';
573 - }
574 - return apply_filters( 'yaymail_default_global_footer_settings', $default );
575 - }
576 -
577 - private static function get_default_global_header_settings(): array {
578 - $default = YayMailTemplate::DEFAULT_DATA['global_header_settings'];
579 - if ( ! function_exists( 'WC' ) ) {
580 - $default['heading_content'] = '<h1 style="font-size: 30px; font-weight: 300; line-height: normal; margin: 0px; color: inherit;">[yaymail_site_name]</h1>';
581 - }
582 - return apply_filters( 'yaymail_default_global_header_settings', $default );
583 - }
584 -
585 - protected static function query_meta_data( $post_id, $meta_name, $default ) {
473 + private static function query_meta_data( $post_id, $meta_name, $default ) {
586 474 if ( ! isset( $post_id ) ) {
587 475 return $default;
588 476 }
589 477
@@ -627,17 +515,14 @@
627 515
628 516 public static function get_elements_for_template( $template_id ): array {
629 517 $support_info = SupportedPlugins::get_instance()->get_support_info( $template_id );
630 518 if ( $support_info['status'] !== 'already_supported' ) {
631 - // 'new_order' only exists on WooCommerce sites; fall back to the WP-core
632 - // email so unsupported templates still resolve an elements catalog.
633 - $fallback_email_id = yaymail_get_email( 'new_order' ) ? 'new_order' : 'wp-core-mail';
634 - $elements = array_map(
519 + $elements = array_map(
635 520 function( $element ) {
636 521 $element['available'] = false;
637 522 return $element;
638 523 },
639 - yaymail_get_email_elements_data( $fallback_email_id )
524 + yaymail_get_email_elements_data( 'new_order' )
640 525 );
641 526 return $elements;
642 527 }
643 528 return yaymail_get_email_elements_data( $template_id );