| @@ -38,12 +38,14 @@ | ||
| 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', |
| 47 | + 'preheader' => '_yaymail_preheader', | |
| 46 | 48 | ]; |
| 47 | 49 | |
| 48 | 50 | public const DEFAULT_DATA = [ |
| 49 | 51 | 'name' => '', |
| @@ -54,10 +56,13 @@ | ||
| 54 | 56 | 'content_background_color' => '', |
| 55 | 57 | 'content_text_color' => '', |
| 56 | 58 | 'title_color' => '', |
| 57 | 59 | 'language' => '', |
| 60 | + 'variant' => '', | |
| 58 | 61 | 'modified_by' => '', |
| 59 | 62 | 'is_v4_supported' => false, |
| 63 | + 'attachments' => [], | |
| 64 | + 'preheader' => '', | |
| 60 | 65 | 'global_header_settings' => [ |
| 61 | 66 | 'content_override' => false, |
| 62 | 67 | 'heading_content' => '<h1 style="font-size: 30px; font-weight: 300; line-height: normal; margin: 0px; color: inherit;">Hello YayMail</h1>', |
| 63 | 68 | 'hidden' => false, |
| @@ -78,19 +83,27 @@ | ||
| 78 | 83 | 'status' => self::DEFAULT_DATA['status'], |
| 79 | 84 | 'background_color' => self::DEFAULT_DATA['background_color'], |
| 80 | 85 | 'text_link_color' => self::DEFAULT_DATA['text_link_color'], |
| 81 | 86 | 'language' => self::DEFAULT_DATA['language'], |
| 87 | + 'variant' => self::DEFAULT_DATA['variant'], | |
| 82 | 88 | 'title_color' => self::DEFAULT_DATA['title_color'], |
| 89 | + 'attachments' => self::DEFAULT_DATA['attachments'], | |
| 90 | + 'preheader' => self::DEFAULT_DATA['preheader'], | |
| 83 | 91 | 'global_header_settings' => self::DEFAULT_DATA['global_header_settings'], |
| 84 | 92 | 'global_footer_settings' => self::DEFAULT_DATA['global_footer_settings'], |
| 85 | 93 | ]; |
| 86 | 94 | |
| 87 | - 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 = '' ) { | |
| 88 | 101 | |
| 89 | 102 | $this->model = TemplateModel::get_instance(); |
| 90 | 103 | |
| 91 | 104 | if ( is_string( $template_name ) && ! empty( $template_name ) ) { |
| 92 | - $template_data = $this->model::find_by_name( $template_name, $language ); | |
| 105 | + $template_data = $this->model::find_by_name( $template_name, $language, $variant ); | |
| 93 | 106 | if ( empty( $template_data['id'] ) && SupportedPlugins::get_instance()->get_support_info( $template_name )['status'] === 'already_supported' ) { |
| 94 | 107 | /** Insert new template when not exists */ |
| 95 | 108 | $template_data = $this->model::insert( |
| 96 | 109 | [ |
| @@ -96,8 +109,9 @@ | ||
| 96 | 109 | [ |
| 97 | 110 | 'name' => $template_name, |
| 98 | 111 | 'elements' => yaymail_get_default_elements( $template_name ), |
| 99 | 112 | 'language' => $language, |
| 113 | + 'variant' => $variant, | |
| 100 | 114 | 'background_color' => self::DEFAULT_DATA['background_color'], |
| 101 | 115 | 'text_link_color' => self::DEFAULT_DATA['text_link_color'], |
| 102 | 116 | 'content_background_color' => self::DEFAULT_DATA['content_background_color'], |
| 103 | 117 | 'content_text_color' => self::DEFAULT_DATA['content_text_color'], |
| @@ -105,10 +119,16 @@ | ||
| 105 | 119 | 'global_header_settings' => self::DEFAULT_DATA['global_header_settings'], |
| 106 | 120 | 'global_footer_settings' => self::DEFAULT_DATA['global_footer_settings'], |
| 107 | 121 | ] |
| 108 | 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 | + } | |
| 109 | 128 | } |
| 110 | - $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 ); | |
| 111 | 131 | $this->set_props( $template_data ); |
| 112 | 132 | // TODO: Consider filter available elements before pass to props |
| 113 | 133 | $this->renderer = new TemplateRenderer( $this ); |
| 114 | 134 | }//end if |
| @@ -113,8 +133,22 @@ | ||
| 113 | 133 | $this->renderer = new TemplateRenderer( $this ); |
| 114 | 134 | }//end if |
| 115 | 135 | } |
| 116 | 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 | + | |
| 117 | 151 | public function is_exists() { |
| 118 | 152 | return is_numeric( $this->id ) && $this->id > 0; |
| 119 | 153 | } |
| 120 | 154 | |
| @@ -120,8 +154,11 @@ | ||
| 120 | 154 | |
| 121 | 155 | public function is_enabled() { |
| 122 | 156 | // Check if YayMail core is migrated |
| 123 | 157 | // If not, consider template is not activated |
| 158 | + if ( ! defined( 'YAYMAIL_VERSION' ) ) { | |
| 159 | + return $this->get_status() === 'active'; | |
| 160 | + } | |
| 124 | 161 | $old_version = get_option( 'yaymail_version' ); |
| 125 | 162 | if ( $old_version && version_compare( $old_version, '4.0.0', '<' ) ) { |
| 126 | 163 | return false; |
| 127 | 164 | } |
| @@ -191,12 +228,24 @@ | ||
| 191 | 228 | public function get_language( $context = 'view' ) { |
| 192 | 229 | return $this->get_prop( 'language', $context ); |
| 193 | 230 | } |
| 194 | 231 | |
| 232 | + public function get_variant( $context = 'view' ) { | |
| 233 | + return $this->get_prop( 'variant', $context ); | |
| 234 | + } | |
| 235 | + | |
| 195 | 236 | public function get_title_color( $context = 'view' ) { |
| 196 | 237 | return $this->get_prop( 'title_color', $context ); |
| 197 | 238 | } |
| 198 | 239 | |
| 240 | + public function get_attachments( $context = 'view' ) { | |
| 241 | + return $this->get_prop( 'attachments', $context ); | |
| 242 | + } | |
| 243 | + | |
| 244 | + public function get_preheader( $context = 'view' ) { | |
| 245 | + return $this->get_prop( 'preheader', $context ); | |
| 246 | + } | |
| 247 | + | |
| 199 | 248 | /** |
| 200 | 249 | * Get global header |
| 201 | 250 | * |
| 202 | 251 | * @since 4.1.0 |
| @@ -282,8 +331,20 @@ | ||
| 282 | 331 | |
| 283 | 332 | public function set_language( $value ) { |
| 284 | 333 | if ( ! is_null( $value ) && is_string( $value ) ) { |
| 285 | 334 | $this->set_prop( 'language', $value ); |
| 335 | + } | |
| 336 | + } | |
| 337 | + | |
| 338 | + public function set_variant( $value ) { | |
| 339 | + if ( is_string( $value ) ) { | |
| 340 | + $this->set_prop( 'variant', $value ); | |
| 341 | + } | |
| 342 | + } | |
| 343 | + | |
| 344 | + public function set_preheader( $value ) { | |
| 345 | + if ( is_string( $value ) ) { | |
| 346 | + $this->set_prop( 'preheader', $value ); | |
| 286 | 347 | } |
| 287 | 348 | } |
| 288 | 349 | |
| 289 | 350 | public function set_title_color( $value ) { |