| @@ -38,8 +38,9 @@ | ||
| 38 | 38 | 'content_background_color' => '_yaymail_email_content_background_color', |
| 39 | 39 | 'content_text_color' => '_yaymail_email_content_text_color', |
| 40 | 40 | 'title_color' => '_yaymail_email_title_color', |
| 41 | 41 | 'language' => '_yaymail_template_language', |
| 42 | + 'variant' => '_yaymail_template_variant', | |
| 42 | 43 | 'modified_by' => '_yaymail_modified_by', |
| 43 | 44 | 'is_v4_supported' => '_yaymail_is_v4_supported', |
| 44 | 45 | 'global_header_settings' => '_yaymail_global_header_settings', |
| 45 | 46 | 'global_footer_settings' => '_yaymail_global_footer_settings', |
| @@ -55,8 +56,9 @@ | ||
| 55 | 56 | 'content_background_color' => '', |
| 56 | 57 | 'content_text_color' => '', |
| 57 | 58 | 'title_color' => '', |
| 58 | 59 | 'language' => '', |
| 60 | + 'variant' => '', | |
| 59 | 61 | 'modified_by' => '', |
| 60 | 62 | 'is_v4_supported' => false, |
| 61 | 63 | 'attachments' => [], |
| 62 | 64 | 'preheader' => '', |
| @@ -81,8 +83,9 @@ | ||
| 81 | 83 | 'status' => self::DEFAULT_DATA['status'], |
| 82 | 84 | 'background_color' => self::DEFAULT_DATA['background_color'], |
| 83 | 85 | 'text_link_color' => self::DEFAULT_DATA['text_link_color'], |
| 84 | 86 | 'language' => self::DEFAULT_DATA['language'], |
| 87 | + 'variant' => self::DEFAULT_DATA['variant'], | |
| 85 | 88 | 'title_color' => self::DEFAULT_DATA['title_color'], |
| 86 | 89 | 'attachments' => self::DEFAULT_DATA['attachments'], |
| 87 | 90 | 'preheader' => self::DEFAULT_DATA['preheader'], |
| 88 | 91 | 'global_header_settings' => self::DEFAULT_DATA['global_header_settings'], |
| @@ -88,14 +91,19 @@ | ||
| 88 | 91 | 'global_header_settings' => self::DEFAULT_DATA['global_header_settings'], |
| 89 | 92 | 'global_footer_settings' => self::DEFAULT_DATA['global_footer_settings'], |
| 90 | 93 | ]; |
| 91 | 94 | |
| 92 | - public function __construct( $template_name = '', $language = '' ) { | |
| 95 | + /** | |
| 96 | + * @param string $template_name Email id. | |
| 97 | + * @param string $language Unused in Lite; kept so callers match the Pro signature. | |
| 98 | + * @param string $variant Variant slug, '' for the default design. | |
| 99 | + */ | |
| 100 | + public function __construct( $template_name = '', $language = '', $variant = '' ) { | |
| 93 | 101 | |
| 94 | 102 | $this->model = TemplateModel::get_instance(); |
| 95 | 103 | |
| 96 | 104 | if ( is_string( $template_name ) && ! empty( $template_name ) ) { |
| 97 | - $template_data = $this->model::find_by_name( $template_name, $language ); | |
| 105 | + $template_data = $this->model::find_by_name( $template_name, $language, $variant ); | |
| 98 | 106 | if ( empty( $template_data['id'] ) && SupportedPlugins::get_instance()->get_support_info( $template_name )['status'] === 'already_supported' ) { |
| 99 | 107 | /** Insert new template when not exists */ |
| 100 | 108 | $template_data = $this->model::insert( |
| 101 | 109 | [ |
| @@ -101,8 +109,9 @@ | ||
| 101 | 109 | [ |
| 102 | 110 | 'name' => $template_name, |
| 103 | 111 | 'elements' => yaymail_get_default_elements( $template_name ), |
| 104 | 112 | 'language' => $language, |
| 113 | + 'variant' => $variant, | |
| 105 | 114 | 'background_color' => self::DEFAULT_DATA['background_color'], |
| 106 | 115 | 'text_link_color' => self::DEFAULT_DATA['text_link_color'], |
| 107 | 116 | 'content_background_color' => self::DEFAULT_DATA['content_background_color'], |
| 108 | 117 | 'content_text_color' => self::DEFAULT_DATA['content_text_color'], |
| @@ -110,10 +119,16 @@ | ||
| 110 | 119 | 'global_header_settings' => self::DEFAULT_DATA['global_header_settings'], |
| 111 | 120 | 'global_footer_settings' => self::DEFAULT_DATA['global_footer_settings'], |
| 112 | 121 | ] |
| 113 | 122 | ); |
| 123 | + // A new variant post starts from the default design instead of the stock elements. | |
| 124 | + $seed = '' !== $variant ? $this->get_variant_seed( $template_name ) : []; | |
| 125 | + if ( ! empty( $seed ) && ! empty( $template_data['id'] ) ) { | |
| 126 | + $template_data = $this->model::update( $template_data['id'], $seed ); | |
| 127 | + } | |
| 114 | 128 | } |
| 115 | - $this->set_id( $template_data['id'] ); | |
| 129 | + // Missing, not-yet-supported templates come back without an id; 0 keeps is_exists() false | |
| 130 | + $this->set_id( $template_data['id'] ?? 0 ); | |
| 116 | 131 | $this->set_props( $template_data ); |
| 117 | 132 | // TODO: Consider filter available elements before pass to props |
| 118 | 133 | $this->renderer = new TemplateRenderer( $this ); |
| 119 | 134 | }//end if |
| @@ -118,8 +133,22 @@ | ||
| 118 | 133 | $this->renderer = new TemplateRenderer( $this ); |
| 119 | 134 | }//end if |
| 120 | 135 | } |
| 121 | 136 | |
| 137 | + /** | |
| 138 | + * Design to copy into a freshly created variant post: the default design of the same email. | |
| 139 | + * | |
| 140 | + * @return array Subset of template data accepted by TemplateModel::update(), or [] when nothing to copy. | |
| 141 | + */ | |
| 142 | + private function get_variant_seed( $template_name ) { | |
| 143 | + $source = $this->model::find_by_name( $template_name ); | |
| 144 | + if ( empty( $source['id'] ) ) { | |
| 145 | + return []; | |
| 146 | + } | |
| 147 | + $seed_keys = [ 'elements', 'background_color', 'text_link_color', 'content_background_color', 'content_text_color', 'title_color', 'global_header_settings', 'global_footer_settings', 'preheader' ]; | |
| 148 | + return array_intersect_key( $source, array_flip( $seed_keys ) ); | |
| 149 | + } | |
| 150 | + | |
| 122 | 151 | public function is_exists() { |
| 123 | 152 | return is_numeric( $this->id ) && $this->id > 0; |
| 124 | 153 | } |
| 125 | 154 | |
| @@ -125,8 +154,11 @@ | ||
| 125 | 154 | |
| 126 | 155 | public function is_enabled() { |
| 127 | 156 | // Check if YayMail core is migrated |
| 128 | 157 | // If not, consider template is not activated |
| 158 | + if ( ! defined( 'YAYMAIL_VERSION' ) ) { | |
| 159 | + return $this->get_status() === 'active'; | |
| 160 | + } | |
| 129 | 161 | $old_version = get_option( 'yaymail_version' ); |
| 130 | 162 | if ( $old_version && version_compare( $old_version, '4.0.0', '<' ) ) { |
| 131 | 163 | return false; |
| 132 | 164 | } |
| @@ -196,8 +228,12 @@ | ||
| 196 | 228 | public function get_language( $context = 'view' ) { |
| 197 | 229 | return $this->get_prop( 'language', $context ); |
| 198 | 230 | } |
| 199 | 231 | |
| 232 | + public function get_variant( $context = 'view' ) { | |
| 233 | + return $this->get_prop( 'variant', $context ); | |
| 234 | + } | |
| 235 | + | |
| 200 | 236 | public function get_title_color( $context = 'view' ) { |
| 201 | 237 | return $this->get_prop( 'title_color', $context ); |
| 202 | 238 | } |
| 203 | 239 | |
| @@ -298,8 +334,14 @@ | ||
| 298 | 334 | $this->set_prop( 'language', $value ); |
| 299 | 335 | } |
| 300 | 336 | } |
| 301 | 337 | |
| 338 | + public function set_variant( $value ) { | |
| 339 | + if ( is_string( $value ) ) { | |
| 340 | + $this->set_prop( 'variant', $value ); | |
| 341 | + } | |
| 342 | + } | |
| 343 | + | |
| 302 | 344 | public function set_preheader( $value ) { |
| 303 | 345 | if ( is_string( $value ) ) { |
| 304 | 346 | $this->set_prop( 'preheader', $value ); |
| 305 | 347 | } |
| @@ -380,5 +422,5 @@ | ||
| 380 | 422 | yaymail_get_logger( $e->getMessage() ); |
| 381 | 423 | }//end try |
| 382 | 424 | return ''; |
| 383 | 425 | } |
| 384 | -} | |
| 426 | +} | |