PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.0
YayMail – WooCommerce Email Customizer v4.4.0
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 / TemplateController.php

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

394 lines 16.6 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 $global_header_settings = $data['global_header_settings'] ?? YayMailTemplate::DEFAULT_DATA['global_header_settings'];
206 $global_footer_settings = $data['global_footer_settings'] ?? YayMailTemplate::DEFAULT_DATA['global_footer_settings'];
207 $update_data = [
208 'elements' => $elements,
209 'background_color' => $background_color,
210 'text_link_color' => $text_link_color,
211 'content_background_color' => $content_background_color,
212 'content_text_color' => $content_text_color,
213 'title_color' => $title_color,
214 'global_header_settings' => $global_header_settings,
215 'global_footer_settings' => $global_footer_settings,
216 ];
217 $updated_data = $this->model::update( $id, $update_data, true );
218 return $updated_data;
219 }
220
221 public function exec_delete_template( \WP_REST_Request $request ) {
222 return $this->exec( [ $this, 'delete_template' ], $request );
223 }
224
225 public function delete_template( \WP_REST_Request $request ) {
226 $id = sanitize_text_field( $request->get_param( 'template_id' ) );
227 $this->model::delete( $id );
228 return [ 'success' => true ];
229 }
230
231 public function exec_get_template_by_name( \WP_REST_Request $request ) {
232 return $this->exec( [ $this, 'get_template_by_name' ], $request );
233 }
234
235 public function get_template_by_name( \WP_REST_Request $request ) {
236 $template_name = sanitize_text_field( $request->get_param( 'template_name' ) );
237 $template_data = $this->model::find_by_name( $template_name );
238
239 if ( null === $template_data ) {
240 $all_emails = yaymail_get_emails();
241 if ( in_array(
242 $template_name,
243 array_map(
244 function ( $email ) {
245 return $email->get_id();
246 },
247 $all_emails
248 )
249 ) ) {
250
251 $template_data = $this->model::insert(
252 [
253 'name' => $template_name,
254 'elements' => yaymail_get_default_elements( $template_name ),
255 ]
256 );
257 }
258 }//end if
259
260 return $template_data;
261 }
262
263 public function exec_change_status( \WP_REST_Request $request ) {
264 return $this->exec( [ $this, 'change_status' ], $request );
265 }
266
267 public function change_status( \WP_REST_Request $request ) {
268 $list_id = is_array( $request->get_param( 'list_id' ) ) ? array_map( 'sanitize_text_field', wp_unslash( $request->get_param( 'list_id' ) ) ) : [];
269 $status = sanitize_text_field( $request->get_param( 'status' ) );
270 foreach ( $list_id as $id ) {
271 $this->model::update(
272 $id,
273 [
274 'status' => $status,
275 ]
276 );
277 }
278 return [ 'success' => true ];
279 }
280
281 public function exec_reset_templates( \WP_REST_Request $request ) {
282 return $this->exec( [ $this, 'reset_templates' ], $request );
283 }
284
285 public function reset_templates( \WP_REST_Request $request ) {
286 $list_id = is_array( $request->get_param( 'list_id' ) ) ? array_map( 'sanitize_text_field', wp_unslash( $request->get_param( 'list_id' ) ) ) : [];
287 $list_template_data = [];
288
289 foreach ( $list_id as $id ) {
290 $template_data = $this->model::find_by_id( $id );
291 $default_elements = yaymail_get_default_elements( $template_data['name'] );
292 $update_data = [
293 'elements' => $default_elements,
294 'background_color' => YayMailTemplate::DEFAULT_DATA['background_color'],
295 'text_link_color' => YayMailTemplate::DEFAULT_DATA['text_link_color'],
296 'content_background_color' => YayMailTemplate::DEFAULT_DATA['content_background_color'],
297 'content_text_color' => YayMailTemplate::DEFAULT_DATA['content_text_color'],
298 'title_color' => YayMailTemplate::DEFAULT_DATA['title_color'],
299 'global_header_settings' => wp_parse_args(
300 [
301 'hidden' => true,
302 ],
303 $template_data['global_header_settings'] ?? YayMailTemplate::DEFAULT_DATA['global_header_settings'],
304 ),
305 'global_footer_settings' => wp_parse_args(
306 [
307 'hidden' => true,
308 ],
309 $template_data['global_footer_settings'] ?? YayMailTemplate::DEFAULT_DATA['global_footer_settings'],
310 ),
311 ];
312 $template_data['elements'] = $update_data['elements'];
313 $template_data['background_color'] = $update_data['background_color'];
314 $template_data['text_link_color'] = $update_data['text_link_color'];
315 $template_data['content_background_color'] = $update_data['content_background_color'];
316 $template_data['content_text_color'] = $update_data['content_text_color'];
317 $template_data['title_color'] = $update_data['title_color'] ?? '#000000';
318 $template_data['global_header_settings'] = $update_data['global_header_settings'];
319 $template_data['global_footer_settings'] = $update_data['global_footer_settings'];
320
321 $list_template_data[] = $template_data;
322 $this->model::update( $id, $update_data, true );
323 }//end foreach
324
325 return [
326 'success' => true,
327 'list_template_data' => $list_template_data,
328 ];
329 }
330
331
332 public function exec_copy_template( \WP_REST_Request $request ) {
333 return $this->exec( [ $this, 'copy_template' ], $request );
334 }
335
336 public function copy_template( \WP_REST_Request $request ) {
337 $template_id = sanitize_text_field( $request->get_param( 'template_id' ) );
338 $from_template = sanitize_text_field( $request->get_param( 'from_template' ) );
339 $copy_template_data = $this->model::find_by_name( $from_template );
340
341 if ( empty( $copy_template_data ) || empty( $copy_template_data['id'] ) ) {
342 return [
343 'success' => false,
344 'message' => 'Template not found',
345 ];
346 }
347
348 $update_data = [
349 'elements' => ! empty( $copy_template_data['elements'] ) ? $copy_template_data['elements'] : yaymail_get_default_elements( $from_template ),
350 'background_color' => $copy_template_data['background_color'] ?? YAYMAIL_COLOR_BACKGROUND_DEFAULT,
351 'content_background_color' => $copy_template_data['content_background_color'] ?? '#ffffff',
352 'content_text_color' => $copy_template_data['content_text_color'] ?? '#000000',
353 'text_link_color' => $copy_template_data['text_link_color'] ?? YAYMAIL_COLOR_WC_DEFAULT,
354 'global_header_settings' => ! empty( $copy_template_data['global_header_settings'] ) ? $copy_template_data['global_header_settings'] : YayMailTemplate::DEFAULT_DATA['global_header_settings'],
355 'global_footer_settings' => ! empty( $copy_template_data['global_footer_settings'] ) ? $copy_template_data['global_footer_settings'] : YayMailTemplate::DEFAULT_DATA['global_footer_settings'],
356 'title_color' => $copy_template_data['title_color'] ?? '#000000',
357 ];
358
359 $this->model::update( $template_id, $update_data, true );
360 return [
361 'success' => true,
362 ];
363 }
364
365 public function exec_get_all_elements_by_template( \WP_REST_Request $request ) {
366 return $this->exec( [ $this, 'get_all_elements' ], $request );
367 }
368
369 public function get_all_elements( \WP_REST_Request $request ) {
370 $template_name = sanitize_text_field( $request->get_param( 'template_name' ) );
371 $elements = $this->model::get_elements_for_template( $template_name );
372 return $elements;
373 }
374
375 public function exec_get_all_shortcodes_by_template( \WP_REST_Request $request ) {
376 return $this->exec( [ $this, 'get_all_shortcodes' ], $request );
377 }
378
379 public function get_all_shortcodes( \WP_REST_Request $request ) {
380 $template_name = sanitize_text_field( $request->get_param( 'template_name' ) );
381 $order_id = sanitize_text_field( $request->get_param( 'order' ) );
382 return $this->model->get_shortcodes_by_template_name_and_order_id( $template_name, $order_id );
383 }
384
385 public function exec_change_global_header_footer_status( \WP_REST_Request $request ) {
386 return $this->exec( [ $this, 'change_global_header_footer_status' ], $request );
387 }
388
389 public function change_global_header_footer_status( \WP_REST_Request $request ) {
390 $status = sanitize_text_field( $request->get_param( 'status' ) );
391 return SettingModel::update( [ 'global_header_footer_enabled' => $status ] );
392 }
393 }
394