| @@ -7,15 +7,14 @@ | ||
| 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; |
| 16 | 15 | use YayMail\SupportedPlugins; |
| 17 | -use YayMail\Utils\TemplateHelpers; | |
| 16 | + | |
| 18 | 17 | /** |
| 19 | 18 | * Template Model |
| 20 | 19 | * |
| 21 | 20 | * @method static TemplateModel get_instance() |
| @@ -55,13 +54,9 @@ | ||
| 55 | 54 | |
| 56 | 55 | if ( isset( $args['name'] ) ) { |
| 57 | 56 | $template_name = $args['name']; |
| 58 | 57 | $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 | - ); | |
| 58 | + $clauses['where'] .= " AND ( postmeta.meta_key='$template_meta_key' AND postmeta.meta_value = '$template_name' )"; | |
| 64 | 59 | } |
| 65 | 60 | |
| 66 | 61 | $query_string = implode( ' ', $clauses ); |
| 67 | 62 | $post_id = $wpdb->get_var( $query_string ); |
| @@ -88,40 +83,36 @@ | ||
| 88 | 83 | $templates = []; |
| 89 | 84 | |
| 90 | 85 | $excluded_templates = apply_filters( 'yaymail_excluded_templates', [ 'yaymail_global_header_footer' ] ); |
| 91 | 86 | |
| 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(); | |
| 87 | + /* Wc Emails, may or may not be supported by us */ | |
| 88 | + $wc_emails = \WC_Emails::instance()->get_emails(); | |
| 95 | 89 | |
| 96 | - foreach ( $wc_emails as $wc_email ) { | |
| 97 | - $template_id = $wc_email->id ?? null; | |
| 90 | + foreach ( $wc_emails as $wc_email ) { | |
| 91 | + $template_id = $wc_email->id ?? null; | |
| 98 | 92 | |
| 99 | - if ( ! isset( $template_id ) ) { | |
| 100 | - continue; | |
| 101 | - } | |
| 93 | + if ( ! isset( $template_id ) ) { | |
| 94 | + continue; | |
| 95 | + } | |
| 102 | 96 | |
| 103 | - if ( in_array( $template_id, $excluded_templates, true ) ) { | |
| 104 | - continue; | |
| 105 | - } | |
| 97 | + if ( in_array( $template_id, $excluded_templates, true ) ) { | |
| 98 | + continue; | |
| 99 | + } | |
| 106 | 100 | |
| 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 | - } | |
| 101 | + if ( SupportedPlugins::get_instance()->get_support_info( $template_id )['status'] !== 'already_supported' ) { | |
| 102 | + // Templates is currently not editable, but could be supported by pro/addon | |
| 103 | + $template_data = self::get_uneditable_template( $wc_email, $templates ); | |
| 104 | + $templates[] = $template_data; | |
| 105 | + continue; | |
| 106 | + } | |
| 113 | 107 | |
| 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 | |
| 108 | + $email_data = SupportedPlugins::get_instance()->get_yaymail_template_data( $template_id ); | |
| 109 | + $template_data = self::get_yaymail_template( $email_data ); | |
| 110 | + if ( isset( $template_data ) ) { | |
| 111 | + unset( $template_data['elements'] ); | |
| 112 | + $templates[] = $template_data; | |
| 113 | + } | |
| 114 | + }//end foreach | |
| 124 | 115 | |
| 125 | 116 | // Make sure it will show templates for Automatewoo ... |
| 126 | 117 | // Because those templates don't appear in wc_emails |
| 127 | 118 | $template_ids = array_map( fn( $template ) => $template['name'], $templates ); |
| @@ -298,10 +289,9 @@ | ||
| 298 | 289 | } |
| 299 | 290 | |
| 300 | 291 | public static function update( $template_id, $data, $is_save_revision = false ) { |
| 301 | 292 | if ( isset( $data['elements'] ) && is_array( $data['elements'] ) && isset( self::$meta_keys['elements'] ) ) { |
| 302 | - $elements_data = TemplateHelpers::sanitize_elements_recursive( $data['elements'] ); | |
| 303 | - update_post_meta( $template_id, self::$meta_keys['elements'], $elements_data ); | |
| 293 | + update_post_meta( $template_id, self::$meta_keys['elements'], $data['elements'] ); | |
| 304 | 294 | } |
| 305 | 295 | |
| 306 | 296 | if ( ! empty( $data['background_color'] ) && isset( self::$meta_keys['background_color'] ) ) { |
| 307 | 297 | update_post_meta( $template_id, self::$meta_keys['background_color'], $data['background_color'] ); |
| @@ -334,12 +324,8 @@ | ||
| 334 | 324 | if ( ! empty( $data['global_footer_settings'] ) && isset( self::$meta_keys['global_footer_settings'] ) ) { |
| 335 | 325 | update_post_meta( $template_id, self::$meta_keys['global_footer_settings'], $data['global_footer_settings'] ); |
| 336 | 326 | } |
| 337 | 327 | |
| 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 | 328 | // Update post_modified |
| 343 | 329 | $post_data = [ |
| 344 | 330 | 'ID' => $template_id, |
| 345 | 331 | 'post_modified' => current_time( 'mysql' ), |
| @@ -375,20 +361,18 @@ | ||
| 375 | 361 | * @return array An array containing the global header and footer elements: |
| 376 | 362 | * - 'global_header_elements': array. |
| 377 | 363 | * - 'global_footer_elements': array. |
| 378 | 364 | */ |
| 379 | - public static function get_global_header_and_footer( $language = '', $template_name = 'yaymail_global_header_footer' ) { | |
| 365 | + public static function get_global_header_and_footer() { | |
| 380 | 366 | $query_args = [ |
| 381 | - 'name' => $template_name, | |
| 382 | - 'language' => in_array( $language, [ 'en', 'en_US' ], true ) ? '' : $language, | |
| 367 | + 'name' => 'yaymail_global_header_footer', | |
| 383 | 368 | ]; |
| 384 | 369 | $template_id = self::query_template( $query_args ); |
| 385 | 370 | if ( empty( $template_id ) ) { |
| 386 | 371 | self::insert( |
| 387 | 372 | [ |
| 388 | - 'name' => $template_name, | |
| 389 | - 'elements' => yaymail_get_default_elements( $template_name ), | |
| 390 | - 'language' => $language, | |
| 373 | + 'name' => 'yaymail_global_header_footer', | |
| 374 | + 'elements' => yaymail_get_default_elements( 'yaymail_global_header_footer' ), | |
| 391 | 375 | ] |
| 392 | 376 | ); |
| 393 | 377 | $template_id = self::query_template( $query_args ); |
| 394 | 378 | } |
| @@ -418,43 +402,8 @@ | ||
| 418 | 402 | 'global_footer_elements' => $global_footer_elements, |
| 419 | 403 | ]; |
| 420 | 404 | } |
| 421 | 405 | |
| 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 | 406 | /** |
| 458 | 407 | * Get template meta data from Database |
| 459 | 408 | * |
| 460 | 409 | * @param number $template_post_id |
| @@ -473,9 +422,9 @@ | ||
| 473 | 422 | // /** For editable templates */ |
| 474 | 423 | $template_elements = self::query_meta_data( $template_post_id, self::$meta_keys['elements'], [] ); |
| 475 | 424 | |
| 476 | 425 | $support_info = SupportedPlugins::get_instance()->get_support_info( $template_name ); |
| 477 | - if ( $support_info['status'] !== 'already_supported' && 'wp-core-edd-password_reset' !== $template_name ) { | |
| 426 | + if ( $support_info['status'] !== 'already_supported' ) { | |
| 478 | 427 | // Template is not editable |
| 479 | 428 | $template_elements = self::get_uneditable_template_placeholder_elements( $support_info ); |
| 480 | 429 | } elseif ( isset( $post ) ) { |
| 481 | 430 | // TODO: how to store global default value |
| @@ -483,11 +432,10 @@ | ||
| 483 | 432 | $text_link_color = self::query_meta_data( $template_post_id, self::$meta_keys['text_link_color'], YayMailTemplate::DEFAULT_DATA['text_link_color'] ); |
| 484 | 433 | $title_color = self::query_meta_data( $template_post_id, self::$meta_keys['title_color'], YayMailTemplate::DEFAULT_DATA['title_color'] ); |
| 485 | 434 | $content_background_color = self::query_meta_data( $template_post_id, self::$meta_keys['content_background_color'], YayMailTemplate::DEFAULT_DATA['content_background_color'] ); |
| 486 | 435 | $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'] ); | |
| 436 | + $global_header_settings = self::query_meta_data( $template_post_id, self::$meta_keys['global_header_settings'], YayMailTemplate::DEFAULT_DATA['global_header_settings'] ); | |
| 437 | + $global_footer_settings = self::query_meta_data( $template_post_id, self::$meta_keys['global_footer_settings'], YayMailTemplate::DEFAULT_DATA['global_footer_settings'] ); | |
| 490 | 438 | } |
| 491 | 439 | |
| 492 | 440 | return [ |
| 493 | 441 | 'id' => $template_post_id, |
| @@ -502,59 +450,13 @@ | ||
| 502 | 450 | 'content_text_color' => $content_text_color ?? '#000000', |
| 503 | 451 | 'support_status' => $support_info['status'] ?? 'already_supported', |
| 504 | 452 | 'addon_info' => $support_info['addon'] ?? '', |
| 505 | 453 | '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 ), | |
| 454 | + 'global_header_settings' => $global_header_settings, | |
| 455 | + 'global_footer_settings' => $global_footer_settings, | |
| 510 | 456 | ]; |
| 511 | 457 | } |
| 512 | 458 | |
| 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 | 459 | private static function get_uneditable_template_placeholder_elements( array $support_info ): array { |
| 558 | 460 | $elements = []; |
| 559 | 461 | |
| 560 | 462 | $elements[] = BaseElement::reduce_new_element( Logo::get_data() ); |
| @@ -565,25 +467,9 @@ | ||
| 565 | 467 | return $elements; |
| 566 | 468 | } |
| 567 | 469 | |
| 568 | 470 | |
| 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 ) { | |
| 471 | + private static function query_meta_data( $post_id, $meta_name, $default ) { | |
| 586 | 472 | if ( ! isset( $post_id ) ) { |
| 587 | 473 | return $default; |
| 588 | 474 | } |
| 589 | 475 | |
| @@ -627,17 +513,14 @@ | ||
| 627 | 513 | |
| 628 | 514 | public static function get_elements_for_template( $template_id ): array { |
| 629 | 515 | $support_info = SupportedPlugins::get_instance()->get_support_info( $template_id ); |
| 630 | 516 | 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( | |
| 517 | + $elements = array_map( | |
| 635 | 518 | function( $element ) { |
| 636 | 519 | $element['available'] = false; |
| 637 | 520 | return $element; |
| 638 | 521 | }, |
| 639 | - yaymail_get_email_elements_data( $fallback_email_id ) | |
| 522 | + yaymail_get_email_elements_data( 'new_order' ) | |
| 640 | 523 | ); |
| 641 | 524 | return $elements; |
| 642 | 525 | } |
| 643 | 526 | return yaymail_get_email_elements_data( $template_id ); |