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 / Controllers / TemplateLibraryController.php

TemplateLibraryController.php in YayMail – WooCommerce Email Customizer trunk, at src/Controllers/TemplateLibraryController.php

302 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMail\Controllers;
4
5 use YayMail\Abstracts\BaseController;
6 use YayMail\Models\LibraryTemplateModel;
7 use YayMail\TemplateLibrary\TemplateLibraryService;
8 use YayMail\Utils\SingletonTrait;
9
10 /**
11 * Template Library Controller
12 *
13 * @method static TemplateLibraryController get_instance()
14 */
15 class TemplateLibraryController extends BaseController {
16 use SingletonTrait;
17
18 protected function __construct() {
19 $this->init_hooks();
20 }
21
22 /**
23 * Register REST routes.
24 *
25 * @return void
26 */
27 protected function init_hooks() {
28 register_rest_route(
29 YAYMAIL_REST_NAMESPACE,
30 '/template-library',
31 [
32 [
33 'methods' => \WP_REST_Server::READABLE,
34 'callback' => [ $this, 'exec_get_templates' ],
35 'permission_callback' => [ $this, 'permission_callback' ],
36 'args' => [
37 'email_type' => [
38 'type' => 'string',
39 'required' => true,
40 ],
41 ],
42 ],
43 ]
44 );
45
46 register_rest_route(
47 YAYMAIL_REST_NAMESPACE,
48 '/template-library/saved',
49 [
50 [
51 'methods' => \WP_REST_Server::CREATABLE,
52 'callback' => [ $this, 'exec_save_template' ],
53 'permission_callback' => [ $this, 'permission_callback' ],
54 'args' => $this->get_save_template_args(),
55 ],
56 ]
57 );
58
59 register_rest_route(
60 YAYMAIL_REST_NAMESPACE,
61 '/template-library/saved/(?P<public_id>[a-f0-9-]{36})',
62 [
63 [
64 'methods' => \WP_REST_Server::DELETABLE,
65 'callback' => [ $this, 'exec_delete_saved_template' ],
66 'permission_callback' => [ $this, 'permission_callback' ],
67 'args' => [
68 'public_id' => [
69 'type' => 'string',
70 'required' => true,
71 ],
72 ],
73 ],
74 ]
75 );
76 }
77
78 /**
79 * REST args for saving a template to the library.
80 *
81 * @return array
82 */
83 protected function get_save_template_args() {
84 return [
85 'scope' => [
86 'type' => 'string',
87 'required' => true,
88 'enum' => [ 'current', 'all' ],
89 ],
90 'name' => [
91 'type' => 'string',
92 'required' => true,
93 ],
94 'email_type' => [
95 'type' => 'string',
96 'required' => true,
97 ],
98 'elements' => [
99 'type' => 'array',
100 'required' => true,
101 ],
102 'email_settings' => [
103 'type' => 'object',
104 'required' => false,
105 ],
106 'global_header_settings' => [
107 'type' => 'object',
108 'required' => false,
109 ],
110 'global_footer_settings' => [
111 'type' => 'object',
112 'required' => false,
113 ],
114 ];
115 }
116
117 /**
118 * Exec wrapper for getting list of templates.
119 *
120 * @param \WP_REST_Request $request Request.
121 *
122 * @return \WP_REST_Response|\WP_Error
123 */
124 public function exec_get_templates( \WP_REST_Request $request ) {
125 return $this->exec( [ $this, 'get_templates' ], $request );
126 }
127
128 /**
129 * Get list of template summaries for current email type.
130 *
131 * @param \WP_REST_Request $request Request.
132 *
133 * @return array
134 */
135 public function get_templates( \WP_REST_Request $request ) {
136 $email_type = sanitize_text_field( $request->get_param( 'email_type' ) );
137
138 if ( empty( $email_type ) ) {
139 return [
140 'success' => false,
141 'message' => __( 'Email type is required.', 'yaymail' ),
142 ];
143 }
144
145 $templates = TemplateLibraryService::get_instance()->get_merged_list( $email_type );
146
147 return [
148 'success' => true,
149 'templates' => $templates,
150 ];
151 }
152
153 /**
154 * Exec wrapper for saving a template to the library.
155 *
156 * @param \WP_REST_Request $request Request.
157 *
158 * @return \WP_REST_Response|\WP_Error
159 */
160 public function exec_save_template( \WP_REST_Request $request ) {
161 return $this->exec( [ $this, 'save_template' ], $request );
162 }
163
164 /**
165 * Save current customizer layout as a library template.
166 *
167 * @param \WP_REST_Request $request Request.
168 *
169 * @return array
170 */
171 public function save_template( \WP_REST_Request $request ) {
172 $scope = sanitize_text_field( $request->get_param( 'scope' ) );
173 $name = sanitize_text_field( $request->get_param( 'name' ) );
174 $email_type = sanitize_text_field( $request->get_param( 'email_type' ) );
175
176 if ( '' === $name ) {
177 return [
178 'success' => false,
179 'message' => __( 'Template name is required.', 'yaymail' ),
180 ];
181 }
182
183 if ( empty( $email_type ) ) {
184 return [
185 'success' => false,
186 'message' => __( 'Email type is required.', 'yaymail' ),
187 ];
188 }
189
190 if ( ! in_array( $scope, [ 'current', 'all' ], true ) ) {
191 return [
192 'success' => false,
193 'message' => __( 'Invalid save scope.', 'yaymail' ),
194 ];
195 }
196
197 $elements = $request->get_param( 'elements' );
198 if ( ! is_array( $elements ) || empty( $elements ) ) {
199 return [
200 'success' => false,
201 'message' => __( 'Template elements are required.', 'yaymail' ),
202 ];
203 }
204
205 $base_payload = [
206 'name' => $name,
207 'elements' => $elements,
208 'email_settings' => $request->get_param( 'email_settings' ),
209 'global_header_settings' => $request->get_param( 'global_header_settings' ),
210 'global_footer_settings' => $request->get_param( 'global_footer_settings' ),
211 ];
212
213 $email_types = ( 'all' === $scope )
214 ? TemplateLibraryService::get_instance()->get_supported_email_types_for_library()
215 : [ $email_type ];
216
217 if ( empty( $email_types ) ) {
218 return [
219 'success' => false,
220 'message' => __( 'No supported email types found for bulk save.', 'yaymail' ),
221 ];
222 }
223
224 $base_row = LibraryTemplateModel::build_row_from_save_payload(
225 array_merge( $base_payload, [ 'email_type' => $email_types[0] ] )
226 );
227
228 $rows = [];
229 foreach ( $email_types as $type ) {
230 $row = $base_row;
231 $row['email_type'] = sanitize_text_field( $type );
232 $rows[] = $row;
233 }
234
235 if ( 1 === count( $rows ) ) {
236 $public_id = LibraryTemplateModel::create( $rows[0] );
237 $public_ids = false !== $public_id ? [ $public_id ] : [];
238 } else {
239 $public_ids = LibraryTemplateModel::create_many( $rows );
240 }
241
242 if ( empty( $public_ids ) ) {
243 return [
244 'success' => false,
245 'message' => count( $email_types ) > 1
246 ? __( 'Failed to save templates.', 'yaymail' )
247 : __( 'Failed to save template.', 'yaymail' ),
248 ];
249 }
250
251 return [
252 'success' => true,
253 'message' => __( 'Open templates to see your new one.', 'yaymail' ),
254 'created_count' => count( $public_ids ),
255 'public_ids' => $public_ids,
256 'effect_emails' => $email_types,
257 ];
258 }
259
260 /**
261 * Exec wrapper for deleting a saved library template.
262 *
263 * @param \WP_REST_Request $request Request.
264 *
265 * @return \WP_REST_Response|\WP_Error
266 */
267 public function exec_delete_saved_template( \WP_REST_Request $request ) {
268 return $this->exec( [ $this, 'delete_saved_template' ], $request );
269 }
270
271 /**
272 * Hard-delete a saved library template by public_id.
273 *
274 * @param \WP_REST_Request $request Request.
275 *
276 * @return array
277 */
278 public function delete_saved_template( \WP_REST_Request $request ) {
279 $public_id = sanitize_text_field( $request->get_param( 'public_id' ) );
280
281 if ( '' === $public_id ) {
282 return [
283 'success' => false,
284 'message' => __( 'Template id is required.', 'yaymail' ),
285 ];
286 }
287
288 $deleted = LibraryTemplateModel::delete_by_public_id( $public_id );
289
290 if ( ! $deleted ) {
291 return [
292 'success' => false,
293 'message' => __( 'Template not found or could not be deleted.', 'yaymail' ),
294 ];
295 }
296
297 return [
298 'success' => true,
299 'message' => __( 'Template deleted successfully.', 'yaymail' ),
300 ];
301 }
302 }