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 / TemplateLibrary / TemplateLibraryService.php

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

153 lines 3.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\TemplateLibrary;
4
5 use YayMail\Abstracts\BaseTemplate;
6 use YayMail\Models\LibraryTemplateModel;
7 use YayMail\Utils\SingletonTrait;
8
9 /**
10 * Service responsible for registering and exposing code-defined email templates
11 * for the Template Library feature.
12 */
13 class TemplateLibraryService {
14 use SingletonTrait;
15
16 /**
17 * @var BaseTemplate[]
18 */
19 protected $templates = [];
20
21 /**
22 * Constructor.
23 *
24 * @return void
25 */
26 protected function __construct() {
27 }
28
29 /**
30 * Programmatically register a template class.
31 *
32 * @param BaseTemplate $template Template instance.
33 *
34 * @return void
35 */
36 public function register( BaseTemplate $template ) {
37 if ( ! $template instanceof BaseTemplate ) {
38 return;
39 }
40
41 if ( in_array( $template, $this->templates, true ) ) {
42 return;
43 }
44
45 $template_key = $template->get_id();
46
47 $this->templates[ $template_key ] = $template;
48 }
49
50 /**
51 * Get list of template summaries for given email type
52 *
53 * @param string $email_type YayMail email/template name. Example: 'new_order'.
54 *
55 * @return array[]
56 */
57 public function get_list( $email_type ) {
58 $results = [];
59
60 foreach ( $this->templates as $template ) {
61 if ( $template->get_email_type() !== $email_type ) {
62 continue;
63 }
64
65 $results[] = $template->get_template_data();
66 }
67
68 usort(
69 $results,
70 function( $a, $b ) {
71 $pos_a = isset( $a['position'] ) ? (int) $a['position'] : 10;
72 $pos_b = isset( $b['position'] ) ? (int) $b['position'] : 10;
73
74 if ( $pos_a === $pos_b ) {
75 return strcmp( (string) ( $a['name'] ?? '' ), (string) ( $b['name'] ?? '' ) );
76 }
77
78 return $pos_a - $pos_b;
79 }
80 );
81
82 return $results;
83 }
84
85 /**
86 * Built-in + user-saved templates for the library modal.
87 * Order: saved (newest first via DB query), then builtin (by position).
88 *
89 * @param string $email_type Email type slug.
90 *
91 * @return array[]
92 */
93 public function get_merged_list( $email_type ) {
94 $builtin = $this->get_list( $email_type );
95
96 $saved = LibraryTemplateModel::list_by_email_type( $email_type );
97
98 $saved = array_map(
99 function( $template ) {
100 $template['source'] = 'saved';
101 return $template;
102 },
103 $saved
104 );
105
106 return array_merge( $saved, $builtin );
107 }
108
109 /**
110 * Email types eligible for scope "all" bulk save.
111 * Uses registered YayMail emails (same pool as editable customizer emails).
112 *
113 * @return string[]
114 */
115 public function get_supported_email_types_for_library() {
116 $types = [];
117
118 if ( ! function_exists( 'yaymail_get_emails' ) ) {
119 return $types;
120 }
121
122 foreach ( yaymail_get_emails() as $email ) {
123 $template_id = $email->get_id();
124 if ( ! self::is_library_bulk_email_type( $template_id ) ) {
125 continue;
126 }
127 $types[] = $template_id;
128 }
129
130 return $types;
131 }
132
133 /**
134 * @param string|null $template_id Template id.
135 *
136 * @return bool
137 */
138 private static function is_library_bulk_email_type( $template_id ) {
139 if ( empty( $template_id ) || ! is_string( $template_id ) ) {
140 return false;
141 }
142
143 if ( 0 === strpos( $template_id, 'pattern_' ) ) {
144 return false;
145 }
146
147 if ( 0 === strpos( $template_id, 'wp-core-' ) ) {
148 return false;
149 }
150
151 return true;
152 }
153 }