| 1 |
<?php |
| 2 |
namespace YayMail; |
| 3 |
|
| 4 |
use YayMail\Elements\ElementsHelper; |
| 5 |
use YayMail\Models\TemplateModel; |
| 6 |
use YayMail\Utils\Helpers; |
| 7 |
use YayMail\Utils\TemplateHelpers; |
| 8 |
use YayMail\Utils\TemplateRenderer; |
| 9 |
use YayMail\Utils\StyleInline; |
| 10 |
|
| 11 |
/** |
| 12 |
* YayMail Template |
| 13 |
*/ |
| 14 |
class YayMailTemplate { |
| 15 |
|
| 16 |
/** |
| 17 |
* TemplateModel |
| 18 |
* |
| 19 |
* @var TemplateModel |
| 20 |
*/ |
| 21 |
private $model = null; |
| 22 |
|
| 23 |
/** |
| 24 |
* Contains template id |
| 25 |
* |
| 26 |
* @var number |
| 27 |
*/ |
| 28 |
private $id = 0; |
| 29 |
|
| 30 |
public $renderer = null; |
| 31 |
|
| 32 |
public const META_KEYS = [ |
| 33 |
'name' => '_yaymail_template', |
| 34 |
'elements' => '_yaymail_elements', |
| 35 |
'status' => '_yaymail_status', |
| 36 |
'background_color' => '_yaymail_email_backgroundColor_settings', |
| 37 |
'text_link_color' => '_yaymail_email_textLinkColor_settings', |
| 38 |
'content_background_color' => '_yaymail_email_content_background_color', |
| 39 |
'content_text_color' => '_yaymail_email_content_text_color', |
| 40 |
'title_color' => '_yaymail_email_title_color', |
| 41 |
'language' => '_yaymail_template_language', |
| 42 |
'variant' => '_yaymail_template_variant', |
| 43 |
'modified_by' => '_yaymail_modified_by', |
| 44 |
'is_v4_supported' => '_yaymail_is_v4_supported', |
| 45 |
'global_header_settings' => '_yaymail_global_header_settings', |
| 46 |
'global_footer_settings' => '_yaymail_global_footer_settings', |
| 47 |
'preheader' => '_yaymail_preheader', |
| 48 |
]; |
| 49 |
|
| 50 |
public const DEFAULT_DATA = [ |
| 51 |
'name' => '', |
| 52 |
'elements' => [], |
| 53 |
'status' => 0, |
| 54 |
'background_color' => '', |
| 55 |
'text_link_color' => '', |
| 56 |
'content_background_color' => '', |
| 57 |
'content_text_color' => '', |
| 58 |
'title_color' => '', |
| 59 |
'language' => '', |
| 60 |
'variant' => '', |
| 61 |
'modified_by' => '', |
| 62 |
'is_v4_supported' => false, |
| 63 |
'attachments' => [], |
| 64 |
'preheader' => '', |
| 65 |
'global_header_settings' => [ |
| 66 |
'content_override' => false, |
| 67 |
'heading_content' => '<h1 style="font-size: 30px; font-weight: 300; line-height: normal; margin: 0px; color: inherit;">Hello YayMail</h1>', |
| 68 |
'hidden' => false, |
| 69 |
], |
| 70 |
'global_footer_settings' => [ |
| 71 |
'content_override' => false, |
| 72 |
'footer_content' => '<p style="font-size: 14px; margin: 0px 0px 16px; text-align: center;">[yaymail_site_name] - Built with <a style="color: #873eff; font-weight: normal; text-decoration: underline;" href="https://woocommerce.com" target="_blank" rel="noopener">WooCommerce</a></p>', |
| 73 |
'hidden' => false, |
| 74 |
], |
| 75 |
]; |
| 76 |
|
| 77 |
/** |
| 78 |
* Contains template data |
| 79 |
*/ |
| 80 |
private $data = [ |
| 81 |
'name' => self::DEFAULT_DATA['name'], |
| 82 |
'elements' => self::DEFAULT_DATA['elements'], |
| 83 |
'status' => self::DEFAULT_DATA['status'], |
| 84 |
'background_color' => self::DEFAULT_DATA['background_color'], |
| 85 |
'text_link_color' => self::DEFAULT_DATA['text_link_color'], |
| 86 |
'language' => self::DEFAULT_DATA['language'], |
| 87 |
'variant' => self::DEFAULT_DATA['variant'], |
| 88 |
'title_color' => self::DEFAULT_DATA['title_color'], |
| 89 |
'attachments' => self::DEFAULT_DATA['attachments'], |
| 90 |
'preheader' => self::DEFAULT_DATA['preheader'], |
| 91 |
'global_header_settings' => self::DEFAULT_DATA['global_header_settings'], |
| 92 |
'global_footer_settings' => self::DEFAULT_DATA['global_footer_settings'], |
| 93 |
]; |
| 94 |
|
| 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 = '' ) { |
| 101 |
|
| 102 |
$this->model = TemplateModel::get_instance(); |
| 103 |
|
| 104 |
if ( is_string( $template_name ) && ! empty( $template_name ) ) { |
| 105 |
$template_data = $this->model::find_by_name( $template_name, $language, $variant ); |
| 106 |
if ( empty( $template_data['id'] ) && SupportedPlugins::get_instance()->get_support_info( $template_name )['status'] === 'already_supported' ) { |
| 107 |
/** Insert new template when not exists */ |
| 108 |
$template_data = $this->model::insert( |
| 109 |
[ |
| 110 |
'name' => $template_name, |
| 111 |
'elements' => yaymail_get_default_elements( $template_name ), |
| 112 |
'language' => $language, |
| 113 |
'variant' => $variant, |
| 114 |
'background_color' => self::DEFAULT_DATA['background_color'], |
| 115 |
'text_link_color' => self::DEFAULT_DATA['text_link_color'], |
| 116 |
'content_background_color' => self::DEFAULT_DATA['content_background_color'], |
| 117 |
'content_text_color' => self::DEFAULT_DATA['content_text_color'], |
| 118 |
'title_color' => self::DEFAULT_DATA['title_color'], |
| 119 |
'global_header_settings' => self::DEFAULT_DATA['global_header_settings'], |
| 120 |
'global_footer_settings' => self::DEFAULT_DATA['global_footer_settings'], |
| 121 |
] |
| 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 |
} |
| 128 |
} |
| 129 |
// Missing, not-yet-supported templates come back without an id; 0 keeps is_exists() false |
| 130 |
$this->set_id( $template_data['id'] ?? 0 ); |
| 131 |
$this->set_props( $template_data ); |
| 132 |
// TODO: Consider filter available elements before pass to props |
| 133 |
$this->renderer = new TemplateRenderer( $this ); |
| 134 |
}//end if |
| 135 |
} |
| 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 |
|
| 151 |
public function is_exists() { |
| 152 |
return is_numeric( $this->id ) && $this->id > 0; |
| 153 |
} |
| 154 |
|
| 155 |
public function is_enabled() { |
| 156 |
// Check if YayMail core is migrated |
| 157 |
// If not, consider template is not activated |
| 158 |
if ( ! defined( 'YAYMAIL_VERSION' ) ) { |
| 159 |
return $this->get_status() === 'active'; |
| 160 |
} |
| 161 |
$old_version = get_option( 'yaymail_version' ); |
| 162 |
if ( $old_version && version_compare( $old_version, '4.0.0', '<' ) ) { |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
return $this->get_status() === 'active'; |
| 167 |
} |
| 168 |
|
| 169 |
// GETTER METHOD |
| 170 |
|
| 171 |
private function get_prop( $prop, $context = 'view' ) { |
| 172 |
$value = null; |
| 173 |
|
| 174 |
if ( array_key_exists( $prop, $this->data ) ) { |
| 175 |
$value = $this->data[ $prop ]; |
| 176 |
|
| 177 |
if ( 'view' === $context ) { |
| 178 |
$value = apply_filters( 'yaymail_template_get_' . $prop, $value, $this ); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
return $value; |
| 183 |
} |
| 184 |
|
| 185 |
public function get_id() { |
| 186 |
return $this->id; |
| 187 |
} |
| 188 |
|
| 189 |
public function get_data() { |
| 190 |
return array_merge( |
| 191 |
[ |
| 192 |
'id' => $this->get_id(), |
| 193 |
], |
| 194 |
$this->data |
| 195 |
); |
| 196 |
} |
| 197 |
|
| 198 |
public function get_name( $context = 'view' ) { |
| 199 |
return $this->get_prop( 'name', $context ); |
| 200 |
} |
| 201 |
|
| 202 |
public function get_elements( $context = 'view' ) { |
| 203 |
$elements = $this->get_prop( 'elements', $context ); |
| 204 |
return ElementsHelper::filter_available_elements( $elements, $this->get_name() ); |
| 205 |
} |
| 206 |
|
| 207 |
public function get_status( $context = 'view' ) { |
| 208 |
$value = $this->get_prop( 'status', $context ); |
| 209 |
if ( is_numeric( $value ) || is_bool( $value ) ) { |
| 210 |
$value = empty( $value ) ? 'inactive' : 'active'; |
| 211 |
// Process old value |
| 212 |
} |
| 213 |
if ( 'inactve' !== $value && 'active' !== $value ) { |
| 214 |
$value = 'inactive'; |
| 215 |
} |
| 216 |
return $value; |
| 217 |
} |
| 218 |
|
| 219 |
public function get_background_color( $context = 'view' ) { |
| 220 |
$color = $this->get_prop( 'background_color', $context ); |
| 221 |
return TemplateHelpers::convert_rgb_to_hex( $color ); |
| 222 |
} |
| 223 |
|
| 224 |
public function get_text_link_color( $context = 'view' ) { |
| 225 |
return $this->get_prop( 'text_link_color', $context ); |
| 226 |
} |
| 227 |
|
| 228 |
public function get_language( $context = 'view' ) { |
| 229 |
return $this->get_prop( 'language', $context ); |
| 230 |
} |
| 231 |
|
| 232 |
public function get_variant( $context = 'view' ) { |
| 233 |
return $this->get_prop( 'variant', $context ); |
| 234 |
} |
| 235 |
|
| 236 |
public function get_title_color( $context = 'view' ) { |
| 237 |
return $this->get_prop( 'title_color', $context ); |
| 238 |
} |
| 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 |
|
| 248 |
/** |
| 249 |
* Get global header |
| 250 |
* |
| 251 |
* @since 4.1.0 |
| 252 |
* |
| 253 |
* @param string $context |
| 254 |
* @return array |
| 255 |
*/ |
| 256 |
public function get_global_header_settings( $context = 'view' ) { |
| 257 |
return $this->get_prop( 'global_header_settings', $context ); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Get global header |
| 262 |
* |
| 263 |
* @since 4.1.0 |
| 264 |
* |
| 265 |
* @param string $context |
| 266 |
* @return array |
| 267 |
*/ |
| 268 |
public function get_global_footer_settings( $context = 'view' ) { |
| 269 |
return $this->get_prop( 'global_footer_settings', $context ); |
| 270 |
} |
| 271 |
|
| 272 |
// SETTER METHOD |
| 273 |
|
| 274 |
public function set_props( $props ) { |
| 275 |
foreach ( $props as $prop_key => $prop_value ) { |
| 276 |
if ( is_null( $prop_value ) ) { |
| 277 |
continue; |
| 278 |
} |
| 279 |
$set_method = "set_$prop_key"; |
| 280 |
if ( is_callable( [ $this, $set_method ] ) ) { |
| 281 |
$this->{$set_method}( $prop_value ); |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
private function set_prop( $prop, $value ) { |
| 287 |
if ( array_key_exists( $prop, $this->data ) ) { |
| 288 |
$this->data[ $prop ] = $value; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
public function set_id( $id ) { |
| 293 |
$this->id = absint( $id ); |
| 294 |
} |
| 295 |
|
| 296 |
public function set_name( $value ) { |
| 297 |
if ( ! is_null( $value ) && is_string( $value ) ) { |
| 298 |
$this->set_prop( 'name', $value ); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
public function set_elements( $value ) { |
| 303 |
if ( ! is_null( $value ) && is_array( $value ) ) { |
| 304 |
$this->set_prop( 'elements', $value ); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
public function set_status( $value ) { |
| 309 |
if ( ! is_null( $value ) ) { |
| 310 |
if ( is_numeric( $value ) || is_bool( $value ) ) { |
| 311 |
$value = empty( $value ) ? 'inactive' : 'active'; |
| 312 |
// Process old value |
| 313 |
} |
| 314 |
if ( 'inactive' === $value || 'active' === $value ) { |
| 315 |
$this->set_prop( 'status', $value ); |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
public function set_background_color( $value ) { |
| 321 |
if ( ! is_null( $value ) && is_string( $value ) ) { |
| 322 |
$this->set_prop( 'background_color', $value ); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
public function set_text_link_color( $value ) { |
| 327 |
if ( ! is_null( $value ) && is_string( $value ) ) { |
| 328 |
$this->set_prop( 'text_link_color', $value ); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
public function set_language( $value ) { |
| 333 |
if ( ! is_null( $value ) && is_string( $value ) ) { |
| 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 ); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
public function set_title_color( $value ) { |
| 351 |
if ( ! is_null( $value ) && is_string( $value ) ) { |
| 352 |
$this->set_prop( 'title_color', $value ); |
| 353 |
} |
| 354 |
} |
| 355 |
public function set_content_background_color( $value ) { |
| 356 |
if ( ! is_null( $value ) && is_string( $value ) ) { |
| 357 |
$this->set_prop( 'content_background_color', $value ); |
| 358 |
} |
| 359 |
} |
| 360 |
public function set_content_text_color( $value ) { |
| 361 |
if ( ! is_null( $value ) && is_string( $value ) ) { |
| 362 |
$this->set_prop( 'content_text_color', $value ); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
/** |
| 368 |
* Set global header |
| 369 |
* |
| 370 |
* @since 4.1.0 |
| 371 |
* |
| 372 |
* @param array $value |
| 373 |
*/ |
| 374 |
public function set_global_header_settings( $value ) { |
| 375 |
if ( ! is_null( $value ) && is_array( $value ) ) { |
| 376 |
$this->set_prop( 'global_header_settings', $value ); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Set global footer |
| 382 |
* |
| 383 |
* @since 4.1.0 |
| 384 |
* |
| 385 |
* @param array $value |
| 386 |
*/ |
| 387 |
public function set_global_footer_settings( $value ) { |
| 388 |
if ( ! is_null( $value ) && is_array( $value ) ) { |
| 389 |
$this->set_prop( 'global_footer_settings', $value ); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
// UPDATE - DELETE METHOD |
| 394 |
|
| 395 |
public function save() { |
| 396 |
if ( $this->get_id() ) { |
| 397 |
$this->model::update( $this->get_id(), $this->data ); |
| 398 |
} |
| 399 |
return $this->get_id(); |
| 400 |
} |
| 401 |
|
| 402 |
public function delete() { |
| 403 |
if ( $this->get_id() ) { |
| 404 |
$this->model::delete( $this->get_id() ); |
| 405 |
return true; |
| 406 |
} |
| 407 |
return false; |
| 408 |
} |
| 409 |
|
| 410 |
public function get_content( $data ) { |
| 411 |
try { |
| 412 |
if ( strpos( $this->get_name(), 'wp-core-' ) === 0 && ! empty( $this->renderer ) ) { |
| 413 |
return $this->renderer->generate_content( $data ); |
| 414 |
} |
| 415 |
|
| 416 |
if ( ! empty( $this->renderer ) ) { |
| 417 |
return StyleInline::get_instance()->convert_style_inline( $this->renderer->generate_content( $data ) ); |
| 418 |
} |
| 419 |
} catch ( \Exception $e ) { |
| 420 |
yaymail_get_logger( $e->getMessage() ); |
| 421 |
} catch ( \Error $e ) { |
| 422 |
yaymail_get_logger( $e->getMessage() ); |
| 423 |
}//end try |
| 424 |
return ''; |
| 425 |
} |
| 426 |
} |
| 427 |
|