PluginProbe
YayMail – WooCommerce Email Customizer / trunk
YayMail – WooCommerce Email Customizer vtrunk
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 / Models / TemplateModel.php

TemplateModel.php in YayMail – WooCommerce Email Customizer trunk, at src/Models/TemplateModel.php

657 lines 28.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMail\Models;
4
5 use YayMail\Abstracts\BaseElement;
6 use YayMail\Elements\ElementsHelper;
7 use YayMail\Elements\Heading;
8 use YayMail\Elements\Logo;
9 use YayMail\Elements\Text;
10 use YayMail\Elements\Footer;
11 use YayMail\Integrations\TranslationModule;
12 use YayMail\YayMailTemplate;
13 use YayMail\Utils\SingletonTrait;
14 use YayMail\PostTypes\TemplatePostType;
15 use YayMail\Shortcodes\ShortcodesExecutor;
16 use YayMail\SupportedPlugins;
17 use YayMail\Utils\TemplateHelpers;
18 /**
19 * Template Model
20 *
21 * @method static TemplateModel get_instance()
22 */
23 class TemplateModel {
24 use SingletonTrait;
25
26 private const POST_TABLE = 'posts';
27 private const POST_META_TABLE = 'postmeta';
28 private static $meta_keys = YayMailTemplate::META_KEYS;
29
30 /**
31 * Query post with given arguments.
32 * Build query string base on given arguments
33 * then use $wpdb to query it.
34 *
35 * Returns post id or null if not found
36 *
37 * @param array $args Given args.
38 */
39 private static function query_template( $args = [] ) {
40
41 if ( ! is_array( $args ) ) {
42 return null;
43 }
44
45 global $wpdb;
46 $posts = $wpdb->prefix . self::POST_TABLE;
47 $post_meta = $wpdb->prefix . self::POST_META_TABLE;
48 $post_type = TemplatePostType::POST_TYPE;
49
50 $clauses = [
51 'select' => "SELECT posts.ID FROM $posts AS posts",
52 'join' => "JOIN $post_meta AS postmeta ON ( posts.ID = postmeta.post_id )",
53 'where' => "WHERE posts.post_type = '$post_type' AND posts.post_status IN ('publish', 'pending', 'future')",
54 ];
55
56 if ( isset( $args['name'] ) ) {
57 $template_name = $args['name'];
58 $template_meta_key = self::$meta_keys['name'];
59 $clauses['where'] .= $wpdb->prepare(
60 ' AND ( postmeta.meta_key=%s AND postmeta.meta_value = %s )',
61 $template_meta_key,
62 $template_name
63 );
64 }
65
66 $query_string = implode( ' ', $clauses );
67 $post_id = $wpdb->get_var( $query_string );
68
69 return $post_id;
70 }
71
72 /**
73 * Finds all YayMail templates and retrieves relevant information about each template.
74 *
75 * @return array An array of YayMail templates with their corresponding data.
76 * Each template is represented as an associative array containing the following keys:
77 * - 'key': The unique identifier for the template (same as 'id').
78 * - 'template_title': The title of the template.
79 * - 'status': The status of the template.
80 * - 'recipient': The recipient associated with the template.
81 * - 'source': The plugin name that the template originates from.
82 * - 'last_updated': The date and time of the last modification to the template
83 * (formatted according to the WordPress date and time settings) or 'N/A' if not available.
84 * - 'id': The unique identifier for the template (same as 'key').
85 * - '...': Other template-specific data retrieved from YayMail.
86 */
87 public static function find_all() {
88 $templates = [];
89
90 $excluded_templates = apply_filters( 'yaymail_excluded_templates', [ 'yaymail_global_header_footer' ] );
91
92 if ( yaymail_is_wc_installed() ) {
93 /* Wc Emails, may or may not be supported by us */
94 $wc_emails = \WC_Emails::instance()->get_emails();
95
96 foreach ( $wc_emails as $wc_email ) {
97 $template_id = $wc_email->id ?? null;
98
99 if ( ! isset( $template_id ) ) {
100 continue;
101 }
102
103 if ( in_array( $template_id, $excluded_templates, true ) ) {
104 continue;
105 }
106
107 if ( SupportedPlugins::get_instance()->get_support_info( $template_id )['status'] !== 'already_supported' ) {
108 // Templates is currently not editable, but could be supported by pro/addon
109 $template_data = self::get_uneditable_template( $wc_email, $templates );
110 $templates[] = $template_data;
111 continue;
112 }
113
114 $email_data = SupportedPlugins::get_instance()->get_yaymail_template_data( $template_id );
115 $template_data = self::get_yaymail_template( $email_data );
116 if ( isset( $template_data ) ) {
117 unset( $template_data['elements'] );
118 $templates[] = $template_data;
119 }
120 }//end foreach
121 } else {
122 $templates = [];
123 }//end if
124
125 // Make sure it will show templates for Automatewoo ...
126 // Because those templates don't appear in wc_emails
127 $template_ids = array_map( fn( $template ) => $template['name'], $templates );
128 foreach ( yaymail_get_emails() as $yaymail_email ) {
129 $template_id = $yaymail_email->get_id();
130 if ( in_array( $template_id, $excluded_templates, true ) ) {
131 continue;
132 }
133 if ( in_array( $template_id, $template_ids, true ) ) {
134 continue;
135 }
136
137 $email_data = SupportedPlugins::get_instance()->get_yaymail_template_data( $template_id );
138 $template_data = self::get_yaymail_template( $email_data );
139 if ( isset( $template_data ) ) {
140 unset( $template_data['elements'] );
141 $templates[] = $template_data;
142 }
143 }
144
145 return $templates;
146 }
147
148 private static function get_yaymail_template( $email_data ) {
149 /* Template is supported and ready to be edited */
150 $template = new YayMailTemplate( $email_data->get_id() );
151 if ( ! $template->is_exists() || empty( $email_data ) ) {
152 return null;
153 }
154
155 $template_data = $template->get_data();
156 $template_data['elements'] = $template->get_elements();
157 $template_data['key'] = $template_data['id'];
158 $template_data['template_title'] = $email_data->get_title();
159 $template_data['status'] = $template_data['status'];
160 $template_data['recipient'] = $email_data->get_recipient();
161 $template_data['source'] = $email_data->get_source()['plugin_name'] ?? '';
162 $template_data['last_updated'] = isset( get_post( $template_data['id'] )->post_modified ) ? date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( get_post( $template_data['id'] )->post_modified ) ) : esc_html__( 'N/A', 'yaymail' );
163 return $template_data;
164 }
165
166 /**
167 * Get template data for template that is currently not editable by YayMail and YayMail addons
168 * (It might or might not be supported by YayMail)
169 *
170 * @param object $wc_email The WooCommerce email object.
171 * @param array $templates The list of templates.
172 * @return array The template data.
173 */
174 private static function get_uneditable_template( $wc_email, $templates ): array {
175
176 $existed_post_ids = array_map( fn( $template ) => $template['id'], $templates );
177 do {
178 // Create a mockup post id
179 $mock_post_id = wp_rand( 1000, 10000 );
180 } while ( in_array(
181 $mock_post_id,
182 $existed_post_ids,
183 true
184 ) );
185
186 $template_id = $wc_email->id;
187 $support_info = SupportedPlugins::get_instance()->get_support_info( $template_id );
188 $support_status = $support_info['status'];
189 $source = $wc_email->source ?? $support_info['addon']['plugin_name'] ?? '';
190
191 // Get title
192
193 $support_status_labels = [
194 'pro_needed' => __( 'Pro', 'yaymail' ),
195 'addon_needed' => __( 'Addon', 'yaymail' ),
196 'not_supported' => __( 'N/A', 'yaymail' ),
197 ];
198
199 $template_title = $wc_email->title;
200 if ( isset( $support_status_labels[ $support_status ] ) ) {
201 $template_title .= ' (' . $support_status_labels[ $support_status ] . ')';
202 }
203
204 // Get last_updated
205 $post = get_post( $template_id );
206 $last_updated = $post && isset( $post->post_modified )
207 ? date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $post->post_modified ) )
208 : esc_html__( 'N/A', 'yaymail' );
209
210 return [
211 'id' => $mock_post_id,
212 'key' => $mock_post_id,
213 'name' => $template_id,
214 'support_status' => $support_status,
215 'addon_info' => $support_info['addon'],
216 'template_title' => $template_title,
217 'status' => 'inactive',
218 'recipient' => yaymail_get_email_recipient_zone( $wc_email ),
219 'source' => $source,
220 'last_updated' => $last_updated,
221 ];
222 }
223
224 /**
225 * Find template by given name
226 * Returns null if not found
227 *
228 * @param string $name
229 */
230 public static function find_by_name( $name ) {
231 $template_id = self::query_template(
232 [
233 'name' => $name,
234 ]
235 );
236 if ( empty( $template_id ) ) {
237 $support_info = SupportedPlugins::get_instance()->get_support_info( $name );
238 return [
239 'support_status' => $support_info['status'] ?? '',
240 'addon_info' => $support_info['addon'] ?? '',
241 ];
242 }
243 return self::get_meta_data( $template_id, $name );
244 }
245
246 /**
247 * Find template by post id
248 * Returns null if not found
249 *
250 * @param number $id
251 */
252 public static function find_by_id( $id ) {
253 if ( ! empty( $id ) && ! is_null( get_post( $id ) ) ) {
254 return self::get_meta_data( $id );
255 }
256 return null;
257 // Returns null on failure
258 }
259
260 public static function insert( $args = false ) {
261 // Insert template data to posts table
262 $template_args = [
263 'post_content' => '',
264 'post_type' => TemplatePostType::POST_TYPE,
265 'post_title' => '',
266 'post_status' => 'publish',
267 ];
268 $new_template_id = wp_insert_post( $template_args );
269
270 // Insert data to post meta table
271 update_post_meta( $new_template_id, self::$meta_keys['name'], $args['name'] );
272 update_post_meta( $new_template_id, self::$meta_keys['elements'], $args['elements'] );
273 update_post_meta( $new_template_id, self::$meta_keys['status'], 'inactive' );
274 update_post_meta( $new_template_id, self::$meta_keys['background_color'], YAYMAIL_COLOR_BACKGROUND_DEFAULT );
275 update_post_meta( $new_template_id, self::$meta_keys['is_v4_supported'], true );
276 if ( ! empty( $args['text_link_color'] ) ) {
277 update_post_meta( $new_template_id, self::$meta_keys['text_link_color'], YAYMAIL_COLOR_WC_DEFAULT );
278 }
279 if ( ! empty( $args['content_background_color'] ) ) {
280 update_post_meta( $new_template_id, self::$meta_keys['content_background_color'], '#ffffff' );
281 }
282 if ( ! empty( $args['content_text_color'] ) ) {
283 update_post_meta( $new_template_id, self::$meta_keys['content_text_color'], '#000000' );
284 }
285 if ( ! empty( $args['title_color'] ) ) {
286 update_post_meta( $new_template_id, self::$meta_keys['title_color'], '#000000' );
287 }
288 if ( ! empty( $args['global_header_settings'] ) ) {
289 update_post_meta( $new_template_id, self::$meta_keys['global_header_settings'], $args['global_header_settings'] );
290 }
291 if ( ! empty( $args['global_footer_settings'] ) ) {
292 update_post_meta( $new_template_id, self::$meta_keys['global_footer_settings'], $args['global_footer_settings'] );
293 }
294
295 // Hook insert data of integrations
296
297 return self::get_meta_data( $new_template_id );
298 }
299
300 public static function update( $template_id, $data, $is_save_revision = false ) {
301 if ( isset( $data['elements'] ) && is_array( $data['elements'] ) && isset( self::$meta_keys['elements'] ) ) {
302 $elements_data = TemplateHelpers::sanitize_elements_recursive( $data['elements'] );
303 update_post_meta( $template_id, self::$meta_keys['elements'], $elements_data );
304 }
305
306 if ( ! empty( $data['background_color'] ) && isset( self::$meta_keys['background_color'] ) ) {
307 update_post_meta( $template_id, self::$meta_keys['background_color'], $data['background_color'] );
308 }
309
310 if ( ! empty( $data['text_link_color'] ) && isset( self::$meta_keys['text_link_color'] ) ) {
311 update_post_meta( $template_id, self::$meta_keys['text_link_color'], $data['text_link_color'] );
312 }
313
314 if ( ! empty( $data['content_background_color'] ) && isset( self::$meta_keys['content_background_color'] ) ) {
315 update_post_meta( $template_id, self::$meta_keys['content_background_color'], $data['content_background_color'] );
316 }
317
318 if ( ! empty( $data['content_text_color'] ) && isset( self::$meta_keys['content_text_color'] ) ) {
319 update_post_meta( $template_id, self::$meta_keys['content_text_color'], $data['content_text_color'] );
320 }
321
322 if ( ! empty( $data['title_color'] ) && isset( self::$meta_keys['title_color'] ) ) {
323 update_post_meta( $template_id, self::$meta_keys['title_color'], $data['title_color'] );
324 }
325
326 if ( isset( $data['status'] ) && isset( self::$meta_keys['status'] ) ) {
327 update_post_meta( $template_id, self::$meta_keys['status'], $data['status'] );
328 }
329
330 if ( ! empty( $data['global_header_settings'] ) && isset( self::$meta_keys['global_header_settings'] ) ) {
331 update_post_meta( $template_id, self::$meta_keys['global_header_settings'], $data['global_header_settings'] );
332 }
333
334 if ( ! empty( $data['global_footer_settings'] ) && isset( self::$meta_keys['global_footer_settings'] ) ) {
335 update_post_meta( $template_id, self::$meta_keys['global_footer_settings'], $data['global_footer_settings'] );
336 }
337
338 if ( isset( $data['preheader'] ) && isset( self::$meta_keys['preheader'] ) ) {
339 update_post_meta( $template_id, self::$meta_keys['preheader'], sanitize_text_field( $data['preheader'] ) );
340 }
341
342 // Update post_modified
343 $post_data = [
344 'ID' => $template_id,
345 'post_modified' => current_time( 'mysql' ),
346 'post_modified_gmt' => current_time( 'mysql', 1 ),
347 ];
348 wp_update_post( $post_data, true );
349
350 if ( $is_save_revision ) {
351 try {
352 $revision_model = RevisionModel::get_instance();
353 $new_revision = $revision_model->save( $template_id, $data );
354 } catch ( \Throwable $th ) {
355 $new_revision = null;
356 }
357
358 return [
359 'updated_data' => self::get_meta_data( $template_id ),
360 'new_revision' => $new_revision,
361 ];
362 }
363
364 return self::get_meta_data( $template_id );
365 }
366
367 public static function delete( $template_id ) {
368 wp_delete_post( $template_id, true );
369 // TODO: remove relatives.
370 }
371
372 /**
373 * Retrieve the global header and footer elements.
374 *
375 * @return array An array containing the global header and footer elements:
376 * - 'global_header_elements': array.
377 * - 'global_footer_elements': array.
378 */
379 public static function get_global_header_and_footer( $language = '', $template_name = 'yaymail_global_header_footer' ) {
380 $query_args = [
381 'name' => $template_name,
382 'language' => in_array( $language, [ 'en', 'en_US' ], true ) ? '' : $language,
383 ];
384 $template_id = self::query_template( $query_args );
385 if ( empty( $template_id ) ) {
386 self::insert(
387 [
388 'name' => $template_name,
389 'elements' => yaymail_get_default_elements( $template_name ),
390 'language' => $language,
391 ]
392 );
393 $template_id = self::query_template( $query_args );
394 }
395 $retrieved_meta_data = self::get_meta_data( $template_id );
396 $elements = isset( $retrieved_meta_data['elements'] ) ? $retrieved_meta_data['elements'] : [];
397
398 if ( empty( $elements ) ) {
399 /**
400 * Get default elements
401 */
402 $global_header_footer_instance = \YayMail\Emails\GlobalHeaderFooter::get_instance();
403 $elements = $global_header_footer_instance->get_default_elements();
404 }
405
406 $divider_index = array_search( 'skeleton_divider', array_column( $elements, 'type' ), true );
407
408 $global_header_elements = [];
409 $global_footer_elements = [];
410
411 if ( false !== $divider_index ) {
412 $global_header_elements = array_slice( $elements, 0, $divider_index );
413 $global_footer_elements = array_slice( $elements, $divider_index + 1 );
414 }
415
416 return [
417 'global_header_elements' => $global_header_elements,
418 'global_footer_elements' => $global_footer_elements,
419 ];
420 }
421
422 public static function get_global_headers_and_footers_for_all_available_languages( $template_name = 'yaymail_global_header_footer' ) {
423 $multi_languages_instances = TranslationModule::get_instance()->current_integration;
424
425 if ( ! empty( $multi_languages_instances ) ) {
426 $languages = $multi_languages_instances->get_available_languages();
427 } else {
428 $languages = [ [ 'code' => 'en' ] ];
429 }
430
431 /**
432 * Each language should have 3 fields: code, name, flag
433 *
434 * @Example [
435 * code: 'en',
436 * name: 'English',
437 * flag: 'http://localhost:8080/wp-content/plugins/sitepress-multilingual-cms/res/flags/en.svg'
438 * ]
439 */
440 $result = array_map(
441 function ( $language ) use ( $template_name ) {
442 $code = isset( $language['code'] ) ? $language['code'] : '';
443 $global_header_and_footer = self::get_global_header_and_footer( 'en' !== $code ? $code : '', $template_name );
444
445 return [
446 'language' => $code,
447 'global_header_elements' => $global_header_and_footer['global_header_elements'] ? $global_header_and_footer['global_header_elements'] : [],
448 'global_footer_elements' => $global_header_and_footer['global_footer_elements'] ? $global_header_and_footer['global_footer_elements'] : [],
449 ];
450 },
451 $languages
452 );
453
454 return array_values( $result );
455 }
456
457 /**
458 * Get template meta data from Database
459 *
460 * @param number $template_post_id
461 * @param string $template_name
462 */
463 private static function get_meta_data( $template_post_id, $template_name = null ) {
464
465 if ( empty( $template_name ) ) {
466 $template_name = self::query_meta_data( $template_post_id, self::$meta_keys['name'], '' );
467 }
468 $status = self::query_meta_data( $template_post_id, self::$meta_keys['status'], 'inactive' );
469
470 $post = isset( $template_post_id ) ? get_post( $template_post_id ) : null;
471 $post_modified = isset( $post ) ? $post->post_modified : '';
472
473 // /** For editable templates */
474 $template_elements = self::query_meta_data( $template_post_id, self::$meta_keys['elements'], [] );
475
476 $support_info = SupportedPlugins::get_instance()->get_support_info( $template_name );
477 if ( $support_info['status'] !== 'already_supported' && 'wp-core-edd-password_reset' !== $template_name ) {
478 // Template is not editable
479 $template_elements = self::get_uneditable_template_placeholder_elements( $support_info );
480 } elseif ( isset( $post ) ) {
481 // TODO: how to store global default value
482 $background_color = self::query_meta_data( $template_post_id, self::$meta_keys['background_color'], YayMailTemplate::DEFAULT_DATA['background_color'] );
483 $text_link_color = self::query_meta_data( $template_post_id, self::$meta_keys['text_link_color'], YayMailTemplate::DEFAULT_DATA['text_link_color'] );
484 $title_color = self::query_meta_data( $template_post_id, self::$meta_keys['title_color'], YayMailTemplate::DEFAULT_DATA['title_color'] );
485 $content_background_color = self::query_meta_data( $template_post_id, self::$meta_keys['content_background_color'], YayMailTemplate::DEFAULT_DATA['content_background_color'] );
486 $content_text_color = self::query_meta_data( $template_post_id, self::$meta_keys['content_text_color'], YayMailTemplate::DEFAULT_DATA['content_text_color'] );
487 $global_header_settings = self::query_meta_data( $template_post_id, self::$meta_keys['global_header_settings'], self::get_default_global_header_settings() );
488 $global_footer_settings = self::query_meta_data( $template_post_id, self::$meta_keys['global_footer_settings'], self::get_default_global_footer_settings() );
489 $preheader = self::query_meta_data( $template_post_id, self::$meta_keys['preheader'], YayMailTemplate::DEFAULT_DATA['preheader'] );
490 }
491
492 return [
493 'id' => $template_post_id,
494 'key' => $template_post_id,
495 'name' => $template_name,
496 'elements' => ElementsHelper::filter_available_elements( $template_elements, $template_name ),
497 'status' => $status,
498 'title_color' => $title_color ?? '#000000',
499 'background_color' => $background_color ?? YAYMAIL_COLOR_BACKGROUND_DEFAULT,
500 'text_link_color' => $text_link_color ?? YAYMAIL_COLOR_WC_DEFAULT,
501 'content_background_color' => $content_background_color ?? '#ffffff',
502 'content_text_color' => $content_text_color ?? '#000000',
503 'support_status' => $support_info['status'] ?? 'already_supported',
504 'addon_info' => $support_info['addon'] ?? '',
505 'post_modified' => $post_modified,
506 'global_header_settings' => isset( $global_header_settings ) ? $global_header_settings : [],
507 'global_footer_settings' => isset( $global_footer_settings ) ? $global_footer_settings : [],
508 'preheader' => isset( $preheader ) ? $preheader : YayMailTemplate::DEFAULT_DATA['preheader'],
509 'email_subject' => self::get_email_subject( $template_name ),
510 ];
511 }
512
513 /**
514 * Get email subject from WooCommerce email settings.
515 *
516 * @param string $template_name Email / template id.
517 * @return string|null
518 */
519 private static function get_email_subject( $template_name ) {
520 $settings = self::get_wc_email_settings( $template_name );
521 return is_array( $settings ) && isset( $settings['subject'] ) ? (string) $settings['subject'] : null;
522 }
523
524 /**
525 * Save email subject to WooCommerce email settings (same option as Woo admin).
526 *
527 * @param string $template_name Email / template id.
528 * @param string $subject Subject line.
529 */
530 public static function save_email_subject( $template_name, $subject ) {
531 if ( ! is_string( $template_name ) || '' === $template_name ) {
532 return;
533 }
534
535 $settings = self::get_wc_email_settings( $template_name );
536 $settings['subject'] = sanitize_text_field( $subject );
537 update_option( self::get_wc_email_settings_option_name( $template_name ), $settings );
538 }
539
540 /**
541 * @param string $template_name Email / template id.
542 * @return string
543 */
544 private static function get_wc_email_settings_option_name( $template_name ) {
545 return "woocommerce_{$template_name}_settings";
546 }
547
548 /**
549 * @param string $template_name Email / template id.
550 * @return array
551 */
552 private static function get_wc_email_settings( $template_name ) {
553 $settings = get_option( self::get_wc_email_settings_option_name( $template_name ), [] );
554 return is_array( $settings ) ? $settings : [];
555 }
556
557 private static function get_uneditable_template_placeholder_elements( array $support_info ): array {
558 $elements = [];
559
560 $elements[] = BaseElement::reduce_new_element( Logo::get_data() );
561 $elements[] = BaseElement::reduce_new_element( Heading::get_data() );
562 $elements[] = BaseElement::reduce_new_element( Text::get_data() );
563 $elements[] = BaseElement::reduce_new_element( Footer::get_data( [ 'rich_text' => '<p style="font-size: 14px;margin: 0px 0px 16px; text-align: center;">' . esc_html( get_bloginfo( 'name' ) ) . '&nbsp;- Built with <a style="color: ' . esc_attr( YAYMAIL_COLOR_WC_DEFAULT ) . '; font-weight: normal; text-decoration: underline;" href="https://woocommerce.com" target="_blank" rel="noopener">WooCommerce</a></p>' ] ) );
564
565 return $elements;
566 }
567
568
569 private static function get_default_global_footer_settings(): array {
570 $default = YayMailTemplate::DEFAULT_DATA['global_footer_settings'];
571 if ( ! function_exists( 'WC' ) ) {
572 $default['footer_content'] = '<p style="font-size: 14px;margin: 0px 0px 16px; text-align: center;">[yaymail_site_name]</p>';
573 }
574 return apply_filters( 'yaymail_default_global_footer_settings', $default );
575 }
576
577 private static function get_default_global_header_settings(): array {
578 $default = YayMailTemplate::DEFAULT_DATA['global_header_settings'];
579 if ( ! function_exists( 'WC' ) ) {
580 $default['heading_content'] = '<h1 style="font-size: 30px; font-weight: 300; line-height: normal; margin: 0px; color: inherit;">[yaymail_site_name]</h1>';
581 }
582 return apply_filters( 'yaymail_default_global_header_settings', $default );
583 }
584
585 protected static function query_meta_data( $post_id, $meta_name, $default ) {
586 if ( ! isset( $post_id ) ) {
587 return $default;
588 }
589
590 $meta_value = get_post_meta( $post_id, $meta_name, true );
591
592 if ( empty( $meta_value ) ) {
593 return $default;
594 }
595 return $meta_value;
596 }
597
598 public static function get_shortcodes_by_template_name_and_order_id( $template_name, $order_id ) {
599 do_action( 'yaymail_before_order_id_changed', $order_id );
600
601 $shortcodes = yaymail_get_email_shortcodes( $template_name );
602
603 // TODO: check shortcodes
604 // $shortcodes = apply_filters( 'yaymail_extra_shortcodes', $shortcodes, $template_name, $order_id );
605
606 $executor_data = self::get_shortcode_executor_data( $template_name, $order_id );
607 $executor = new ShortcodesExecutor( $shortcodes, $executor_data );
608 return $executor->get_shortcodes_content();
609 }
610
611 /**
612 * Gets data needed for executing a YayMail shortcode.
613 *
614 * @param string $template_name The template name.
615 * @param string $order_id The order ID. Uses sample data if empty or 'sample_order'.
616 * @return array [template, render_data, settings, is_placeholder]
617 */
618 public static function get_shortcode_executor_data( $template_name, $order_id ) {
619
620 return [
621 'template' => new YayMailTemplate( $template_name ),
622 'render_data' => empty( $order_id ) || ( ! empty( $order_id ) && 'sample_order' === $order_id ) ? [ 'is_sample' => true ] : [ 'order' => wc_get_order( $order_id ) ],
623 'settings' => yaymail_settings(),
624 'is_placeholder' => true,
625 ];
626 }
627
628 public static function get_elements_for_template( $template_id ): array {
629 $support_info = SupportedPlugins::get_instance()->get_support_info( $template_id );
630 if ( $support_info['status'] !== 'already_supported' ) {
631 // 'new_order' only exists on WooCommerce sites; fall back to the WP-core
632 // email so unsupported templates still resolve an elements catalog.
633 $fallback_email_id = yaymail_get_email( 'new_order' ) ? 'new_order' : 'wp-core-mail';
634 $elements = array_map(
635 function( $element ) {
636 $element['available'] = false;
637 return $element;
638 },
639 yaymail_get_email_elements_data( $fallback_email_id )
640 );
641 return $elements;
642 }
643 return yaymail_get_email_elements_data( $template_id );
644 }
645
646 public static function get_short_data_by_name( $name ) {
647 $template_id = self::query_template( [ 'name' => $name ] );
648 if ( empty( $template_id ) ) {
649 return null;
650 }
651 return [
652 'id' => $template_id,
653 'status' => self::query_meta_data( $template_id, self::$meta_keys['status'], 'inactive' ),
654 ];
655 }
656 }
657