PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.2
YayMail – WooCommerce Email Customizer v4.4.2
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 3.4.2 3.4.3 All 55 releases
yaymail / src / YayMailTemplate.php

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

384 lines 12.7 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 $old_version = get_option( 'yaymail_version' );
130 if ( $old_version && version_compare( $old_version, '4.0.0', '<' ) ) {
131 return false;
132 }
133
134 return $this->get_status() === 'active';
135 }
136
137 // GETTER METHOD
138
139 private function get_prop( $prop, $context = 'view' ) {
140 $value = null;
141
142 if ( array_key_exists( $prop, $this->data ) ) {
143 $value = $this->data[ $prop ];
144
145 if ( 'view' === $context ) {
146 $value = apply_filters( 'yaymail_template_get_' . $prop, $value, $this );
147 }
148 }
149
150 return $value;
151 }
152
153 public function get_id() {
154 return $this->id;
155 }
156
157 public function get_data() {
158 return array_merge(
159 [
160 'id' => $this->get_id(),
161 ],
162 $this->data
163 );
164 }
165
166 public function get_name( $context = 'view' ) {
167 return $this->get_prop( 'name', $context );
168 }
169
170 public function get_elements( $context = 'view' ) {
171 $elements = $this->get_prop( 'elements', $context );
172 return ElementsHelper::filter_available_elements( $elements, $this->get_name() );
173 }
174
175 public function get_status( $context = 'view' ) {
176 $value = $this->get_prop( 'status', $context );
177 if ( is_numeric( $value ) || is_bool( $value ) ) {
178 $value = empty( $value ) ? 'inactive' : 'active';
179 // Process old value
180 }
181 if ( 'inactve' !== $value && 'active' !== $value ) {
182 $value = 'inactive';
183 }
184 return $value;
185 }
186
187 public function get_background_color( $context = 'view' ) {
188 $color = $this->get_prop( 'background_color', $context );
189 return TemplateHelpers::convert_rgb_to_hex( $color );
190 }
191
192 public function get_text_link_color( $context = 'view' ) {
193 return $this->get_prop( 'text_link_color', $context );
194 }
195
196 public function get_language( $context = 'view' ) {
197 return $this->get_prop( 'language', $context );
198 }
199
200 public function get_title_color( $context = 'view' ) {
201 return $this->get_prop( 'title_color', $context );
202 }
203
204 public function get_attachments( $context = 'view' ) {
205 return $this->get_prop( 'attachments', $context );
206 }
207
208 public function get_preheader( $context = 'view' ) {
209 return $this->get_prop( 'preheader', $context );
210 }
211
212 /**
213 * Get global header
214 *
215 * @since 4.1.0
216 *
217 * @param string $context
218 * @return array
219 */
220 public function get_global_header_settings( $context = 'view' ) {
221 return $this->get_prop( 'global_header_settings', $context );
222 }
223
224 /**
225 * Get global header
226 *
227 * @since 4.1.0
228 *
229 * @param string $context
230 * @return array
231 */
232 public function get_global_footer_settings( $context = 'view' ) {
233 return $this->get_prop( 'global_footer_settings', $context );
234 }
235
236 // SETTER METHOD
237
238 public function set_props( $props ) {
239 foreach ( $props as $prop_key => $prop_value ) {
240 if ( is_null( $prop_value ) ) {
241 continue;
242 }
243 $set_method = "set_$prop_key";
244 if ( is_callable( [ $this, $set_method ] ) ) {
245 $this->{$set_method}( $prop_value );
246 }
247 }
248 }
249
250 private function set_prop( $prop, $value ) {
251 if ( array_key_exists( $prop, $this->data ) ) {
252 $this->data[ $prop ] = $value;
253 }
254 }
255
256 public function set_id( $id ) {
257 $this->id = absint( $id );
258 }
259
260 public function set_name( $value ) {
261 if ( ! is_null( $value ) && is_string( $value ) ) {
262 $this->set_prop( 'name', $value );
263 }
264 }
265
266 public function set_elements( $value ) {
267 if ( ! is_null( $value ) && is_array( $value ) ) {
268 $this->set_prop( 'elements', $value );
269 }
270 }
271
272 public function set_status( $value ) {
273 if ( ! is_null( $value ) ) {
274 if ( is_numeric( $value ) || is_bool( $value ) ) {
275 $value = empty( $value ) ? 'inactive' : 'active';
276 // Process old value
277 }
278 if ( 'inactive' === $value || 'active' === $value ) {
279 $this->set_prop( 'status', $value );
280 }
281 }
282 }
283
284 public function set_background_color( $value ) {
285 if ( ! is_null( $value ) && is_string( $value ) ) {
286 $this->set_prop( 'background_color', $value );
287 }
288 }
289
290 public function set_text_link_color( $value ) {
291 if ( ! is_null( $value ) && is_string( $value ) ) {
292 $this->set_prop( 'text_link_color', $value );
293 }
294 }
295
296 public function set_language( $value ) {
297 if ( ! is_null( $value ) && is_string( $value ) ) {
298 $this->set_prop( 'language', $value );
299 }
300 }
301
302 public function set_preheader( $value ) {
303 if ( is_string( $value ) ) {
304 $this->set_prop( 'preheader', $value );
305 }
306 }
307
308 public function set_title_color( $value ) {
309 if ( ! is_null( $value ) && is_string( $value ) ) {
310 $this->set_prop( 'title_color', $value );
311 }
312 }
313 public function set_content_background_color( $value ) {
314 if ( ! is_null( $value ) && is_string( $value ) ) {
315 $this->set_prop( 'content_background_color', $value );
316 }
317 }
318 public function set_content_text_color( $value ) {
319 if ( ! is_null( $value ) && is_string( $value ) ) {
320 $this->set_prop( 'content_text_color', $value );
321 }
322 }
323
324
325 /**
326 * Set global header
327 *
328 * @since 4.1.0
329 *
330 * @param array $value
331 */
332 public function set_global_header_settings( $value ) {
333 if ( ! is_null( $value ) && is_array( $value ) ) {
334 $this->set_prop( 'global_header_settings', $value );
335 }
336 }
337
338 /**
339 * Set global footer
340 *
341 * @since 4.1.0
342 *
343 * @param array $value
344 */
345 public function set_global_footer_settings( $value ) {
346 if ( ! is_null( $value ) && is_array( $value ) ) {
347 $this->set_prop( 'global_footer_settings', $value );
348 }
349 }
350
351 // UPDATE - DELETE METHOD
352
353 public function save() {
354 if ( $this->get_id() ) {
355 $this->model::update( $this->get_id(), $this->data );
356 }
357 return $this->get_id();
358 }
359
360 public function delete() {
361 if ( $this->get_id() ) {
362 $this->model::delete( $this->get_id() );
363 return true;
364 }
365 return false;
366 }
367
368 public function get_content( $data ) {
369 try {
370 if ( strpos( $this->get_name(), 'wp-core-' ) === 0 && ! empty( $this->renderer ) ) {
371 return $this->renderer->generate_content( $data );
372 }
373
374 if ( ! empty( $this->renderer ) ) {
375 return StyleInline::get_instance()->convert_style_inline( $this->renderer->generate_content( $data ) );
376 }
377 } catch ( \Exception $e ) {
378 yaymail_get_logger( $e->getMessage() );
379 } catch ( \Error $e ) {
380 yaymail_get_logger( $e->getMessage() );
381 }//end try
382 return '';
383 }
384 }