| 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 |
'modified_by' => '_yaymail_modified_by', |
| 43 |
'is_v4_supported' => '_yaymail_is_v4_supported', |
| 44 |
'global_header_settings' => '_yaymail_global_header_settings', |
| 45 |
'global_footer_settings' => '_yaymail_global_footer_settings', |
| 46 |
]; |
| 47 |
|
| 48 |
public const DEFAULT_DATA = [ |
| 49 |
'name' => '', |
| 50 |
'elements' => [], |
| 51 |
'status' => 0, |
| 52 |
'background_color' => '', |
| 53 |
'text_link_color' => '', |
| 54 |
'content_background_color' => '', |
| 55 |
'content_text_color' => '', |
| 56 |
'title_color' => '', |
| 57 |
'language' => '', |
| 58 |
'modified_by' => '', |
| 59 |
'is_v4_supported' => false, |
| 60 |
'global_header_settings' => [ |
| 61 |
'content_override' => false, |
| 62 |
'heading_content' => '<h1 style="font-size: 30px; font-weight: 300; line-height: normal; margin: 0px; color: inherit;">Hello YayMail</h1>', |
| 63 |
'hidden' => false, |
| 64 |
], |
| 65 |
'global_footer_settings' => [ |
| 66 |
'content_override' => false, |
| 67 |
'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>', |
| 68 |
'hidden' => false, |
| 69 |
], |
| 70 |
]; |
| 71 |
|
| 72 |
/** |
| 73 |
* Contains template data |
| 74 |
*/ |
| 75 |
private $data = [ |
| 76 |
'name' => self::DEFAULT_DATA['name'], |
| 77 |
'elements' => self::DEFAULT_DATA['elements'], |
| 78 |
'status' => self::DEFAULT_DATA['status'], |
| 79 |
'background_color' => self::DEFAULT_DATA['background_color'], |
| 80 |
'text_link_color' => self::DEFAULT_DATA['text_link_color'], |
| 81 |
'language' => self::DEFAULT_DATA['language'], |
| 82 |
'title_color' => self::DEFAULT_DATA['title_color'], |
| 83 |
'global_header_settings' => self::DEFAULT_DATA['global_header_settings'], |
| 84 |
'global_footer_settings' => self::DEFAULT_DATA['global_footer_settings'], |
| 85 |
]; |
| 86 |
|
| 87 |
public function __construct( $template_name = '', $language = '' ) { |
| 88 |
|
| 89 |
$this->model = TemplateModel::get_instance(); |
| 90 |
|
| 91 |
if ( is_string( $template_name ) && ! empty( $template_name ) && Helpers::is_yaymail_email( $template_name ) ) { |
| 92 |
$template_data = $this->model::find_by_name( $template_name, $language ); |
| 93 |
if ( empty( $template_data['id'] ) && SupportedPlugins::get_instance()->get_support_info( $template_name )['status'] === 'already_supported' ) { |
| 94 |
/** Insert new template when not exists */ |
| 95 |
$template_data = $this->model::insert( |
| 96 |
[ |
| 97 |
'name' => $template_name, |
| 98 |
'elements' => yaymail_get_default_elements( $template_name ), |
| 99 |
'language' => $language, |
| 100 |
'background_color' => self::DEFAULT_DATA['background_color'], |
| 101 |
'text_link_color' => self::DEFAULT_DATA['text_link_color'], |
| 102 |
'content_background_color' => self::DEFAULT_DATA['content_background_color'], |
| 103 |
'content_text_color' => self::DEFAULT_DATA['content_text_color'], |
| 104 |
'title_color' => self::DEFAULT_DATA['title_color'], |
| 105 |
'global_header_settings' => self::DEFAULT_DATA['global_header_settings'], |
| 106 |
'global_footer_settings' => self::DEFAULT_DATA['global_footer_settings'], |
| 107 |
] |
| 108 |
); |
| 109 |
} |
| 110 |
$this->set_id( $template_data['id'] ); |
| 111 |
$this->set_props( $template_data ); |
| 112 |
// TODO: Consider filter available elements before pass to props |
| 113 |
$this->renderer = new TemplateRenderer( $this ); |
| 114 |
}//end if |
| 115 |
} |
| 116 |
|
| 117 |
public function is_exists() { |
| 118 |
return is_numeric( $this->id ) && $this->id > 0; |
| 119 |
} |
| 120 |
|
| 121 |
public function is_enabled() { |
| 122 |
// Check if YayMail core is migrated |
| 123 |
// If not, consider template is not activated |
| 124 |
$old_version = get_option( 'yaymail_version' ); |
| 125 |
if ( $old_version && version_compare( $old_version, '4.0.0', '<' ) ) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
return $this->get_status() === 'active'; |
| 130 |
} |
| 131 |
|
| 132 |
// GETTER METHOD |
| 133 |
|
| 134 |
private function get_prop( $prop, $context = 'view' ) { |
| 135 |
$value = null; |
| 136 |
|
| 137 |
if ( array_key_exists( $prop, $this->data ) ) { |
| 138 |
$value = $this->data[ $prop ]; |
| 139 |
|
| 140 |
if ( 'view' === $context ) { |
| 141 |
$value = apply_filters( 'yaymail_template_get_' . $prop, $value, $this ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return $value; |
| 146 |
} |
| 147 |
|
| 148 |
public function get_id() { |
| 149 |
return $this->id; |
| 150 |
} |
| 151 |
|
| 152 |
public function get_data() { |
| 153 |
return array_merge( |
| 154 |
[ |
| 155 |
'id' => $this->get_id(), |
| 156 |
], |
| 157 |
$this->data |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
public function get_name( $context = 'view' ) { |
| 162 |
return $this->get_prop( 'name', $context ); |
| 163 |
} |
| 164 |
|
| 165 |
public function get_elements( $context = 'view' ) { |
| 166 |
$elements = $this->get_prop( 'elements', $context ); |
| 167 |
return ElementsHelper::filter_available_elements( $elements, $this->get_name() ); |
| 168 |
} |
| 169 |
|
| 170 |
public function get_status( $context = 'view' ) { |
| 171 |
$value = $this->get_prop( 'status', $context ); |
| 172 |
if ( is_numeric( $value ) || is_bool( $value ) ) { |
| 173 |
$value = empty( $value ) ? 'inactive' : 'active'; |
| 174 |
// Process old value |
| 175 |
} |
| 176 |
if ( 'inactve' !== $value && 'active' !== $value ) { |
| 177 |
$value = 'inactive'; |
| 178 |
} |
| 179 |
return $value; |
| 180 |
} |
| 181 |
|
| 182 |
public function get_background_color( $context = 'view' ) { |
| 183 |
$color = $this->get_prop( 'background_color', $context ); |
| 184 |
return TemplateHelpers::convert_rgb_to_hex( $color ); |
| 185 |
} |
| 186 |
|
| 187 |
public function get_text_link_color( $context = 'view' ) { |
| 188 |
return $this->get_prop( 'text_link_color', $context ); |
| 189 |
} |
| 190 |
|
| 191 |
public function get_language( $context = 'view' ) { |
| 192 |
return $this->get_prop( 'language', $context ); |
| 193 |
} |
| 194 |
|
| 195 |
public function get_title_color( $context = 'view' ) { |
| 196 |
return $this->get_prop( 'title_color', $context ); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Get global header |
| 201 |
* |
| 202 |
* @since 4.1.0 |
| 203 |
* |
| 204 |
* @param string $context |
| 205 |
* @return array |
| 206 |
*/ |
| 207 |
public function get_global_header_settings( $context = 'view' ) { |
| 208 |
return $this->get_prop( 'global_header_settings', $context ); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Get global header |
| 213 |
* |
| 214 |
* @since 4.1.0 |
| 215 |
* |
| 216 |
* @param string $context |
| 217 |
* @return array |
| 218 |
*/ |
| 219 |
public function get_global_footer_settings( $context = 'view' ) { |
| 220 |
return $this->get_prop( 'global_footer_settings', $context ); |
| 221 |
} |
| 222 |
|
| 223 |
// SETTER METHOD |
| 224 |
|
| 225 |
public function set_props( $props ) { |
| 226 |
foreach ( $props as $prop_key => $prop_value ) { |
| 227 |
if ( is_null( $prop_value ) ) { |
| 228 |
continue; |
| 229 |
} |
| 230 |
$set_method = "set_$prop_key"; |
| 231 |
if ( is_callable( [ $this, $set_method ] ) ) { |
| 232 |
$this->{$set_method}( $prop_value ); |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
private function set_prop( $prop, $value ) { |
| 238 |
if ( array_key_exists( $prop, $this->data ) ) { |
| 239 |
$this->data[ $prop ] = $value; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
public function set_id( $id ) { |
| 244 |
$this->id = absint( $id ); |
| 245 |
} |
| 246 |
|
| 247 |
public function set_name( $value ) { |
| 248 |
if ( ! is_null( $value ) && is_string( $value ) ) { |
| 249 |
$this->set_prop( 'name', $value ); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
public function set_elements( $value ) { |
| 254 |
if ( ! is_null( $value ) && is_array( $value ) ) { |
| 255 |
$this->set_prop( 'elements', $value ); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
public function set_status( $value ) { |
| 260 |
if ( ! is_null( $value ) ) { |
| 261 |
if ( is_numeric( $value ) || is_bool( $value ) ) { |
| 262 |
$value = empty( $value ) ? 'inactive' : 'active'; |
| 263 |
// Process old value |
| 264 |
} |
| 265 |
if ( 'inactive' === $value || 'active' === $value ) { |
| 266 |
$this->set_prop( 'status', $value ); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
public function set_background_color( $value ) { |
| 272 |
if ( ! is_null( $value ) && is_string( $value ) ) { |
| 273 |
$this->set_prop( 'background_color', $value ); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
public function set_text_link_color( $value ) { |
| 278 |
if ( ! is_null( $value ) && is_string( $value ) ) { |
| 279 |
$this->set_prop( 'text_link_color', $value ); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
public function set_language( $value ) { |
| 284 |
if ( ! is_null( $value ) && is_string( $value ) ) { |
| 285 |
$this->set_prop( 'language', $value ); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
public function set_title_color( $value ) { |
| 290 |
if ( ! is_null( $value ) && is_string( $value ) ) { |
| 291 |
$this->set_prop( 'title_color', $value ); |
| 292 |
} |
| 293 |
} |
| 294 |
public function set_content_background_color( $value ) { |
| 295 |
if ( ! is_null( $value ) && is_string( $value ) ) { |
| 296 |
$this->set_prop( 'content_background_color', $value ); |
| 297 |
} |
| 298 |
} |
| 299 |
public function set_content_text_color( $value ) { |
| 300 |
if ( ! is_null( $value ) && is_string( $value ) ) { |
| 301 |
$this->set_prop( 'content_text_color', $value ); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
/** |
| 307 |
* Set global header |
| 308 |
* |
| 309 |
* @since 4.1.0 |
| 310 |
* |
| 311 |
* @param array $value |
| 312 |
*/ |
| 313 |
public function set_global_header_settings( $value ) { |
| 314 |
if ( ! is_null( $value ) && is_array( $value ) ) { |
| 315 |
$this->set_prop( 'global_header_settings', $value ); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Set global footer |
| 321 |
* |
| 322 |
* @since 4.1.0 |
| 323 |
* |
| 324 |
* @param array $value |
| 325 |
*/ |
| 326 |
public function set_global_footer_settings( $value ) { |
| 327 |
if ( ! is_null( $value ) && is_array( $value ) ) { |
| 328 |
$this->set_prop( 'global_footer_settings', $value ); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
// UPDATE - DELETE METHOD |
| 333 |
|
| 334 |
public function save() { |
| 335 |
if ( $this->get_id() ) { |
| 336 |
$this->model::update( $this->get_id(), $this->data ); |
| 337 |
} |
| 338 |
return $this->get_id(); |
| 339 |
} |
| 340 |
|
| 341 |
public function delete() { |
| 342 |
if ( $this->get_id() ) { |
| 343 |
$this->model::delete( $this->get_id() ); |
| 344 |
return true; |
| 345 |
} |
| 346 |
return false; |
| 347 |
} |
| 348 |
|
| 349 |
public function get_content( $data ) { |
| 350 |
try { |
| 351 |
if ( ! empty( $this->renderer ) ) { |
| 352 |
return StyleInline::get_instance()->convert_style_inline( $this->renderer->generate_content( $data ) ); |
| 353 |
} |
| 354 |
} catch ( \Exception $e ) { |
| 355 |
yaymail_get_logger( $e->getMessage() ); |
| 356 |
} catch ( \Error $e ) { |
| 357 |
yaymail_get_logger( $e->getMessage() ); |
| 358 |
} |
| 359 |
return ''; |
| 360 |
} |
| 361 |
} |
| 362 |
|