PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.5
YayMail – WooCommerce Email Customizer v4.4.5
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 / Controllers / TemplateController.php

TemplateController.php in YayMail – WooCommerce Email Customizer 4.4.5, at src/Controllers/TemplateController.php

409 lines 17.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\Controllers;
4
5 use YayMail\Abstracts\BaseController;
6 use YayMail\Models\SettingModel;
7 use YayMail\Models\TemplateModel;
8 use YayMail\Utils\SingletonTrait;
9 use YayMail\YayMailTemplate;
10
11 /**
12 * Template Controller
13 *
14 * @method static TemplateController get_instance()
15 */
16 class TemplateController extends BaseController {
17 use SingletonTrait;
18
19 private $model = null;
20
21 protected function __construct() {
22 $this->model = TemplateModel::get_instance();
23 $this->init_hooks();
24 }
25
26 protected function init_hooks() {
27 $template_id_args = [
28 'template_id' => [
29 'type' => 'string',
30 'required' => true,
31 ],
32 ];
33 register_rest_route(
34 YAYMAIL_REST_NAMESPACE,
35 '/templates',
36 [
37 [
38 'methods' => \WP_REST_Server::READABLE,
39 'callback' => [ $this, 'exec_get_all_templates' ],
40 'permission_callback' => [ $this, 'permission_callback' ],
41 ],
42 ]
43 );
44
45 register_rest_route(
46 YAYMAIL_REST_NAMESPACE,
47 '/templates/(?P<template_id>\d+)',
48 [
49 [
50 'methods' => \WP_REST_Server::READABLE,
51 'callback' => [ $this, 'exec_get_template_by_id' ],
52 'permission_callback' => [ $this, 'permission_callback' ],
53 'args' => $template_id_args,
54 ],
55 [
56 'methods' => \WP_REST_Server::EDITABLE,
57 'callback' => [ $this, 'exec_update_template' ],
58 'permission_callback' => [ $this, 'permission_callback' ],
59 'args' => $template_id_args,
60 ],
61 [
62 'methods' => \WP_REST_Server::DELETABLE,
63 'callback' => [ $this, 'exec_delete_template' ],
64 'permission_callback' => [ $this, 'permission_callback' ],
65 'args' => $template_id_args,
66 ],
67 ]
68 );
69
70 register_rest_route(
71 YAYMAIL_REST_NAMESPACE,
72 '/templates/get-template-by-name',
73 [
74 [
75 'methods' => \WP_REST_Server::READABLE,
76 'callback' => [ $this, 'exec_get_template_by_name' ],
77 'permission_callback' => [ $this, 'permission_callback' ],
78 ],
79 ]
80 );
81
82 register_rest_route(
83 YAYMAIL_REST_NAMESPACE,
84 '/templates/change-status',
85 [
86 [
87 'methods' => \WP_REST_Server::EDITABLE,
88 'callback' => [ $this, 'exec_change_status' ],
89 'permission_callback' => [ $this, 'permission_callback' ],
90 ],
91 ]
92 );
93
94 register_rest_route(
95 YAYMAIL_REST_NAMESPACE,
96 '/templates/reset',
97 [
98 [
99 'methods' => \WP_REST_Server::EDITABLE,
100 'callback' => [ $this, 'exec_reset_templates' ],
101 'permission_callback' => [ $this, 'permission_callback' ],
102 ],
103 ]
104 );
105
106 register_rest_route(
107 YAYMAIL_REST_NAMESPACE,
108 '/templates/copy-template',
109 [
110 [
111 'methods' => \WP_REST_Server::EDITABLE,
112 'callback' => [ $this, 'exec_copy_template' ],
113 'permission_callback' => [ $this, 'permission_callback' ],
114 ],
115 ]
116 );
117
118 register_rest_route(
119 YAYMAIL_REST_NAMESPACE,
120 '/templates/(?P<template_name>[a-zA-Z0-9_-]+)/all-elements',
121 [
122 [
123 'methods' => \WP_REST_Server::READABLE,
124 'callback' => [ $this, 'exec_get_all_elements_by_template' ],
125 'permission_callback' => [ $this, 'permission_callback' ],
126 'args' => [
127 'template_name' => [
128 'type' => 'string',
129 'required' => true,
130 ],
131 ],
132 ],
133 ]
134 );
135
136 register_rest_route(
137 YAYMAIL_REST_NAMESPACE,
138 '/templates/(?P<template_name>[a-zA-Z0-9_-]+)/all-shortcodes/(?P<order>[a-zA-Z0-9_-]+)',
139 [
140 [
141 'methods' => \WP_REST_Server::READABLE,
142 'callback' => [ $this, 'exec_get_all_shortcodes_by_template' ],
143 'permission_callback' => [ $this, 'permission_callback' ],
144 'args' => [
145 'template_name' => [
146 'type' => 'string',
147 'required' => true,
148 ],
149 'order' => [
150 'type' => 'string',
151 'required' => true,
152 ],
153 ],
154 ],
155 ]
156 );
157
158 register_rest_route(
159 YAYMAIL_REST_NAMESPACE,
160 '/templates/global-header-footer/change-status',
161 [
162 [
163 'methods' => \WP_REST_Server::EDITABLE,
164 'callback' => [ $this, 'exec_change_global_header_footer_status' ],
165 'permission_callback' => [ $this, 'permission_callback' ],
166 ],
167 ]
168 );
169 }
170
171 public function exec_get_all_templates( \WP_REST_Request $request ) {
172 return $this->exec( [ $this, 'get_all_templates' ], $request );
173 }
174
175 public function get_all_templates( \WP_REST_Request $request ) {
176 $templates = $this->model->find_all();
177 return apply_filters( 'yaymail_get_all_templates', $templates );
178 }
179
180 public function exec_get_template_by_id( \WP_REST_Request $request ) {
181 return $this->exec( [ $this, 'get_template_by_id' ], $request );
182 }
183
184 public function get_template_by_id( \WP_REST_Request $request ) {
185 $id = sanitize_text_field( $request->get_param( 'template_id' ) );
186 $template_data = $this->model::find_by_id( $id );
187 return $template_data;
188 }
189
190 public function exec_update_template( \WP_REST_Request $request ) {
191 return $this->exec( [ $this, 'update_template' ], $request );
192 }
193
194 public function update_template( \WP_REST_Request $request ) {
195 $data = json_decode( $request->get_param( 'data' ), true );
196 $id = sanitize_text_field( $data['template_id'] );
197 $elements = $data['template_elements'];
198 // TODO: later
199 // $elements = Helpers::elements_remove_settings_empty( $request->get_param( 'template_elements' ) );
200 $background_color = sanitize_text_field( $data['background_color'] );
201 $text_link_color = sanitize_text_field( $data['text_link_color'] );
202 $content_background_color = sanitize_text_field( $data['content_background_color'] );
203 $content_text_color = sanitize_text_field( $data['content_text_color'] );
204 $title_color = sanitize_text_field( $data['title_color'] );
205 $preheader = sanitize_text_field( $data['preheader'] );
206 $global_header_settings = $data['global_header_settings'] ?? YayMailTemplate::DEFAULT_DATA['global_header_settings'];
207 $global_footer_settings = $data['global_footer_settings'] ?? YayMailTemplate::DEFAULT_DATA['global_footer_settings'];
208 $update_data = [
209 'elements' => $elements,
210 'background_color' => $background_color,
211 'text_link_color' => $text_link_color,
212 'content_background_color' => $content_background_color,
213 'content_text_color' => $content_text_color,
214 'title_color' => $title_color,
215 'global_header_settings' => $global_header_settings,
216 'global_footer_settings' => $global_footer_settings,
217 'preheader' => $preheader,
218 ];
219
220 if ( isset( $data['email_subject'] ) ) {
221 $template_name = get_post_meta( (int) $id, YayMailTemplate::META_KEYS['name'], true );
222 $this->model::save_email_subject( $template_name, $data['email_subject'] );
223 }
224
225 return $this->model::update( $id, $update_data, true );
226 }
227
228 public function exec_delete_template( \WP_REST_Request $request ) {
229 return $this->exec( [ $this, 'delete_template' ], $request );
230 }
231
232 public function delete_template( \WP_REST_Request $request ) {
233 $id = sanitize_text_field( $request->get_param( 'template_id' ) );
234 $this->model::delete( $id );
235 return [ 'success' => true ];
236 }
237
238 public function exec_get_template_by_name( \WP_REST_Request $request ) {
239 return $this->exec( [ $this, 'get_template_by_name' ], $request );
240 }
241
242 public function get_template_by_name( \WP_REST_Request $request ) {
243 $template_name = sanitize_text_field( $request->get_param( 'template_name' ) );
244 $variant = yaymail_sanitize_template_variant( $request->get_param( 'variant' ) );
245 $template_data = $this->model::find_by_name( $template_name, '', $variant );
246 if ( '' !== $variant && empty( $template_data['id'] ) ) {
247 // Variant posts are created on first open, seeded from the default design.
248 new YayMailTemplate( $template_name, '', $variant );
249 $template_data = $this->model::find_by_name( $template_name, '', $variant );
250 }
251
252 if ( null === $template_data ) {
253 $all_emails = yaymail_get_emails();
254 if ( in_array(
255 $template_name,
256 array_map(
257 function ( $email ) {
258 return $email->get_id();
259 },
260 $all_emails
261 )
262 ) ) {
263
264 $template_data = $this->model::insert(
265 [
266 'name' => $template_name,
267 'elements' => yaymail_get_default_elements( $template_name ),
268 ]
269 );
270 }
271 }//end if
272
273 return $template_data;
274 }
275
276 public function exec_change_status( \WP_REST_Request $request ) {
277 return $this->exec( [ $this, 'change_status' ], $request );
278 }
279
280 public function change_status( \WP_REST_Request $request ) {
281 $list_id = is_array( $request->get_param( 'list_id' ) ) ? array_map( 'sanitize_text_field', wp_unslash( $request->get_param( 'list_id' ) ) ) : [];
282 $status = sanitize_text_field( $request->get_param( 'status' ) );
283 foreach ( $list_id as $id ) {
284 $this->model::update(
285 $id,
286 [
287 'status' => $status,
288 ]
289 );
290 }
291 return [ 'success' => true ];
292 }
293
294 public function exec_reset_templates( \WP_REST_Request $request ) {
295 return $this->exec( [ $this, 'reset_templates' ], $request );
296 }
297
298 public function reset_templates( \WP_REST_Request $request ) {
299 $list_id = is_array( $request->get_param( 'list_id' ) ) ? array_map( 'sanitize_text_field', wp_unslash( $request->get_param( 'list_id' ) ) ) : [];
300 $list_template_data = [];
301
302 foreach ( $list_id as $id ) {
303 $template_data = $this->model::find_by_id( $id );
304 $default_elements = yaymail_get_default_elements( $template_data['name'] );
305 $update_data = [
306 'elements' => $default_elements,
307 'background_color' => YayMailTemplate::DEFAULT_DATA['background_color'],
308 'text_link_color' => YayMailTemplate::DEFAULT_DATA['text_link_color'],
309 'content_background_color' => YayMailTemplate::DEFAULT_DATA['content_background_color'],
310 'content_text_color' => YayMailTemplate::DEFAULT_DATA['content_text_color'],
311 'title_color' => YayMailTemplate::DEFAULT_DATA['title_color'],
312 'global_header_settings' => wp_parse_args(
313 [
314 'hidden' => true,
315 ],
316 $template_data['global_header_settings'] ?? YayMailTemplate::DEFAULT_DATA['global_header_settings'],
317 ),
318 'global_footer_settings' => wp_parse_args(
319 [
320 'hidden' => true,
321 ],
322 $template_data['global_footer_settings'] ?? YayMailTemplate::DEFAULT_DATA['global_footer_settings'],
323 ),
324 ];
325 $template_data['elements'] = $update_data['elements'];
326 $template_data['background_color'] = $update_data['background_color'];
327 $template_data['text_link_color'] = $update_data['text_link_color'];
328 $template_data['content_background_color'] = $update_data['content_background_color'];
329 $template_data['content_text_color'] = $update_data['content_text_color'];
330 $template_data['title_color'] = $update_data['title_color'] ?? '#000000';
331 $template_data['global_header_settings'] = $update_data['global_header_settings'];
332 $template_data['global_footer_settings'] = $update_data['global_footer_settings'];
333
334 $list_template_data[] = $template_data;
335 $this->model::update( $id, $update_data, true );
336 }//end foreach
337
338 return [
339 'success' => true,
340 'list_template_data' => $list_template_data,
341 ];
342 }
343
344
345 public function exec_copy_template( \WP_REST_Request $request ) {
346 return $this->exec( [ $this, 'copy_template' ], $request );
347 }
348
349 public function copy_template( \WP_REST_Request $request ) {
350 $template_id = sanitize_text_field( $request->get_param( 'template_id' ) );
351 $from_template = sanitize_text_field( $request->get_param( 'from_template' ) );
352 $copy_template_data = $this->model::find_by_name( $from_template );
353
354 if ( empty( $copy_template_data ) || empty( $copy_template_data['id'] ) ) {
355 return [
356 'success' => false,
357 'message' => 'Template not found',
358 ];
359 }
360
361 $update_data = [
362 'elements' => ! empty( $copy_template_data['elements'] ) ? $copy_template_data['elements'] : yaymail_get_default_elements( $from_template ),
363 'background_color' => $copy_template_data['background_color'] ?? YAYMAIL_COLOR_BACKGROUND_DEFAULT,
364 'content_background_color' => $copy_template_data['content_background_color'] ?? '#ffffff',
365 'content_text_color' => $copy_template_data['content_text_color'] ?? '#000000',
366 'text_link_color' => $copy_template_data['text_link_color'] ?? YAYMAIL_COLOR_WC_DEFAULT,
367 'global_header_settings' => ! empty( $copy_template_data['global_header_settings'] ) ? $copy_template_data['global_header_settings'] : YayMailTemplate::DEFAULT_DATA['global_header_settings'],
368 'global_footer_settings' => ! empty( $copy_template_data['global_footer_settings'] ) ? $copy_template_data['global_footer_settings'] : YayMailTemplate::DEFAULT_DATA['global_footer_settings'],
369 'title_color' => $copy_template_data['title_color'] ?? '#000000',
370 ];
371
372 $this->model::update( $template_id, $update_data, true );
373 return [
374 'success' => true,
375 ];
376 }
377
378 public function exec_get_all_elements_by_template( \WP_REST_Request $request ) {
379 return $this->exec( [ $this, 'get_all_elements' ], $request );
380 }
381
382 public function get_all_elements( \WP_REST_Request $request ) {
383 $template_name = sanitize_text_field( $request->get_param( 'template_name' ) );
384 $elements = $this->model::get_elements_for_template( $template_name );
385 return $elements;
386 }
387
388 public function exec_get_all_shortcodes_by_template( \WP_REST_Request $request ) {
389 return $this->exec( [ $this, 'get_all_shortcodes' ], $request );
390 }
391
392 public function get_all_shortcodes( \WP_REST_Request $request ) {
393 $template_name = sanitize_text_field( $request->get_param( 'template_name' ) );
394 $order_id = sanitize_text_field( $request->get_param( 'order' ) );
395 return $this->model->get_shortcodes_by_template_name_and_order_id( $template_name, $order_id );
396 }
397
398 public function exec_change_global_header_footer_status( \WP_REST_Request $request ) {
399 return $this->exec( [ $this, 'change_global_header_footer_status' ], $request );
400 }
401
402 public function change_global_header_footer_status( \WP_REST_Request $request ) {
403 $status = filter_var( $request->get_param( 'status' ), FILTER_VALIDATE_BOOLEAN );
404 $platform = sanitize_text_field( $request->get_param( 'platform' ) ?? 'yaymail' );
405 $platform_obj = \YayMail\Platform\PlatformRegistry::get( $platform ) ?? \YayMail\Platform\PlatformRegistry::get( 'yaymail' );
406 $key = $platform_obj ? $platform_obj->ghf_option_key( 'enabled' ) : 'global_header_footer_enabled';
407 return SettingModel::update( [ $key => $status ] );
408 }
409 }