PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.4
YayMail – WooCommerce Email Customizer v4.4.4
4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 All 57 releases
yaymail / src / YayMailTemplate.php

YayMailTemplate.php in YayMail – WooCommerce Email Customizer 4.4.4, at src/YayMailTemplate.php

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