PluginProbe
YayMail – WooCommerce Email Customizer / 4.3.2
YayMail – WooCommerce Email Customizer v4.3.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 / Ajax.php

Ajax.php in YayMail – WooCommerce Email Customizer 4.3.2, at src/Ajax.php

822 lines 34.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;
4
5 use YayMail\Utils\SingletonTrait;
6 use YayMail\Models\SettingModel;
7 use YayMail\Models\TemplateModel;
8 use YayMail\Models\RevisionModel;
9 use YayMail\Migrations\MainMigration;
10 use YayMail\Utils\Helpers;
11 use YayMail\Migrations\AbstractMigration;
12
13 /**
14 * I18n Logic
15 *
16 * @method static Ajax get_instance()
17 */
18 class Ajax {
19 use SingletonTrait;
20
21 protected function __construct() {
22 $this->init_hooks();
23 }
24
25 protected function init_hooks() {
26 add_action( 'wp_ajax_yaymail_preview_mail', [ $this, 'preview_mail' ] );
27 add_action( 'wp_ajax_yaymail_preview_mail_for_woo', [ $this, 'preview_mail_for_woo' ] );
28 add_action( 'wp_ajax_yaymail_send_test_mail', [ $this, 'send_test_mail' ] );
29 add_action( 'wp_ajax_yaymail_install_yaysmtp', [ $this, 'install_yaysmtp' ] );
30 add_action( 'wp_ajax_yaymail_get_custom_hook_html', [ $this, 'get_custom_hook_html' ] );
31 add_action( 'wp_ajax_yaymail_get_template_data_onload', [ $this, 'get_template_data_onload' ] );
32 add_action( 'wp_ajax_yaymail_export_templates', [ $this, 'export_templates' ] );
33 add_action( 'wp_ajax_yaymail_import_templates', [ $this, 'import_templates' ] );
34 add_action( 'wp_ajax_yaymail_review', [ $this, 'yaymail_review' ] );
35 add_action( 'wp_ajax_yaymail_change_ghf_tour', [ $this, 'change_ghf_tour' ] );
36 add_action( 'wp_ajax_yaymail_dismiss_multi_select_notice', [ $this, 'dismiss_multi_select_notice' ] );
37 add_action( 'wp_ajax_yaymail_export_state', [ $this, 'export_state' ] );
38 add_action( 'wp_ajax_yaymail_import_state', [ $this, 'import_state' ] );
39 add_action( 'wp_ajax_yaymail_dismiss_new_element_notification', [ $this, 'dismiss_new_element_notification' ] );
40 }
41
42 public function import_state() {
43 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
44 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
45 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
46 }
47 try {
48 $import_file = isset( $_FILES['import_file'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_FILES['import_file'] ) ) : null;
49 if ( ! $import_file ) {
50 return wp_send_json_error( [ 'mess' => __( 'Can\'t find import file', 'yaymail' ) ] );
51 }
52
53 if ( $import_file['type'] !== 'application/zip' ) {
54 return wp_send_json_error( [ 'mess' => __( 'Invalid file type. Please upload a ZIP file.', 'yaymail' ) ] );
55 }
56
57 $zip = new \ZipArchive();
58 if ( $zip->open( $import_file['tmp_name'] ) !== true ) {
59 return wp_send_json_error( [ 'mess' => __( 'Cannot open ZIP file.', 'yaymail' ) ] );
60 }
61
62 $state_data = null;
63 for ( $i = 0; $i < $zip->numFiles; $i++ ) {
64 $filename = $zip->getNameIndex( $i );
65 if ( $filename === 'yaymail_backup.json' ) {
66 $state_data = $zip->getFromIndex( $i );
67 break;
68 }
69 }
70
71 $zip->close();
72
73 if ( ! $state_data ) {
74 return wp_send_json_error( [ 'mess' => __( 'Cannot find yaymail_backup.json in the ZIP file.', 'yaymail' ) ] );
75 }
76
77 $imported_data = json_decode( $state_data );
78 if ( json_last_error() !== JSON_ERROR_NONE ) {
79 return wp_send_json_error( [ 'mess' => __( 'Invalid JSON data in the state file.', 'yaymail' ) ] );
80 }
81
82 if ( empty( $imported_data->posts ) || empty( $imported_data->postmeta ) || empty( $imported_data->options ) ) {
83 return wp_send_json_error( [ 'mess' => __( 'Invalid state file structure.', 'yaymail' ) ] );
84 }
85
86 $migration_model = \YayMail\Models\MigrationModel::get_instance();
87
88 $source_version = $imported_data->version;
89
90 $backup_data = [
91 'posts' => $imported_data->posts,
92 'postmeta' => $imported_data->postmeta,
93 'options' => $imported_data->options,
94 'created_date' => $imported_data->created_date ?? current_datetime()->format( 'Y-m-d H:i:s' ),
95 'name' => '_yaymail_import_backup_' . $source_version,
96 'version' => $source_version,
97 ];
98
99 $migration_model->reset( $backup_data );
100
101 wp_send_json_success(
102 [
103 'message' => __( 'Import state successfully', 'yaymail' ),
104 ]
105 );
106 } catch ( \Error $error ) {
107 yaymail_get_logger( $error );
108 wp_send_json_error( [ 'mess' => __( 'Import failed: ', 'yaymail' ) . $error->getMessage() ] );
109 } catch ( \Exception $exception ) {
110 yaymail_get_logger( $exception );
111 wp_send_json_error( [ 'mess' => __( 'Import failed: ', 'yaymail' ) . $exception->getMessage() ] );
112 }//end try
113 }
114
115 public function export_state() {
116 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
117 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
118 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
119 }
120
121 try {
122 global $wpdb;
123
124 /**
125 * Backup posts and postmeta
126 */
127 $query_posts = "
128 SELECT *
129 FROM {$wpdb->posts}
130 WHERE post_type = 'yaymail_template'
131 ";
132 $yaymail_template_posts = $wpdb->get_results( $query_posts );// phpcs:ignore
133
134 $query_postmeta = "
135 SELECT *
136 FROM {$wpdb->postmeta}
137 WHERE meta_key LIKE '%yaymail%'
138 ";
139 $yaymail_template_postmeta = $wpdb->get_results( $query_postmeta );// phpcs:ignore
140
141 $backup_data = [
142 'posts' => $yaymail_template_posts,
143 'postmeta' => $yaymail_template_postmeta,
144 ];
145 /** ****************************** */
146
147 /**
148 * Backup options
149 */
150 $query_options = "
151 SELECT *
152 FROM {$wpdb->options}
153 WHERE option_name LIKE '%yaymail%'
154 ";
155 $yaymail_options = $wpdb->get_results( $query_options ); // phpcs:ignore
156 $backup_data['options'] = $yaymail_options;
157
158 $backup_data['created_date'] = current_datetime()->format( 'Y-m-d H:i:s' );
159 $backup_data['version'] = get_option( 'yaymail_version_backup' );
160
161 $backup_data = apply_filters( 'yaymail_backup_state_data', $backup_data );
162
163 wp_send_json_success(
164 [
165 'message' => 'success',
166 'data' => $backup_data,
167 'file_name' => 'yaymail_export_backup_' . gmdate( 'm-d-Y' ),
168 ]
169 );
170
171 } catch ( \Error $error ) {
172 yaymail_get_logger( $error );
173 } catch ( \Exception $exception ) {
174 yaymail_get_logger( $exception );
175 }//end try
176 }
177
178 public function sanitize( $array ) {
179
180 return wp_kses_post_deep( $array );
181 }
182
183 public function process_plugin_installer( $slug ) {
184 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
185 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
186 require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
187 require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
188
189 $api = plugins_api(
190 'plugin_information',
191 [
192 'slug' => $slug,
193 'fields' => [
194 'short_description' => false,
195 'sections' => false,
196 'requires' => false,
197 'rating' => false,
198 'ratings' => false,
199 'downloaded' => false,
200 'last_updated' => false,
201 'added' => false,
202 'tags' => false,
203 'compatibility' => false,
204 'homepage' => false,
205 'donate_link' => false,
206 ],
207 ]
208 );
209
210 $skin = new \WP_Ajax_Upgrader_Skin();
211
212 $plugin_upgrader = new \Plugin_Upgrader( $skin );
213
214 try {
215 $result = $plugin_upgrader->install( $api->download_link );
216
217 if ( is_wp_error( $result ) ) {
218 yaymail_get_logger( $result );
219 }
220
221 return true;
222 } catch ( \Exception $exception ) {
223 yaymail_get_logger( $exception );
224 }
225
226 return false;
227 }
228
229 public function install_yaysmtp() {
230 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
231 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
232 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
233 }
234 try {
235 $is_installed = $this->process_plugin_installer( 'yaysmtp' );
236
237 if ( false === $is_installed ) {
238 wp_send_json_error( [ 'message' => $is_installed ] );
239 }
240
241 $result = activate_plugin( 'yaysmtp/yay-smtp.php' );
242
243 if ( is_wp_error( $result ) ) {
244 return wp_send_json_error( [ 'mess' => esc_html( $result->get_error_message() ) ] );
245 }
246
247 wp_send_json_success(
248 [
249 'installed' => null === $result,
250 ]
251 );
252
253 } catch ( \Error $error ) {
254 yaymail_get_logger( $error );
255 } catch ( \Exception $exception ) {
256 yaymail_get_logger( $exception );
257 }//end try
258 }
259
260 public function send_test_mail() {
261 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
262 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
263 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
264 }
265 try {
266 $template_name = isset( $_POST['template_name'] ) ? sanitize_text_field( wp_unslash( $_POST['template_name'] ) ) : '';
267 $order_id = isset( $_POST['order_id'] ) ? sanitize_text_field( wp_unslash( $_POST['order_id'] ) ) : 'sample_order';
268 $email = isset( $_POST['email'] ) ? sanitize_text_field( $_POST['email'] ) : '';
269
270 if ( empty( $template_name ) ) {
271 return wp_send_json_error( [ 'mess' => __( 'Can\'t find template', 'yaymail' ) ] );
272 }
273
274 if ( empty( $order_id ) ) {
275 return wp_send_json_error( [ 'mess' => __( 'Can\'t find order', 'yaymail' ) ] );
276 }
277
278 if ( empty( $email ) ) {
279 return wp_send_json_error( [ 'mess' => __( 'Can\'t find email', 'yaymail' ) ] );
280 }
281
282 $template = new YayMailTemplate( $template_name );
283
284 $render_data = [];
285
286 if ( empty( $order_id ) || ( 'sample_order' === $order_id ) ) {
287 $render_data['is_sample'] = true;
288 } else {
289 $render_data['order'] = wc_get_order( $order_id );
290 }
291
292 $render_data['is_customized_preview'] = true;
293 // check if email template on preview and send test mail
294
295 update_option( 'yaymail_default_email_test', $email );
296
297 $html = $template->get_content( $render_data );
298
299 $headers = "Content-Type: text/html\r\n";
300 $class_wc_email = \WC_Emails::instance();
301 $subject = __( 'Email Test', 'yaymail' );
302 $send_mail = $class_wc_email->send( $email, $subject, $html, $headers, [] );
303
304 if ( ! $send_mail ) {
305 return wp_send_json_error( [ 'mess' => __( 'Can\'t send email', 'yaymail' ) ] );
306 }
307
308 wp_send_json_success(
309 [
310 'email' => $email,
311 'send_mail_success' => $send_mail,
312 ]
313 );
314 } catch ( \Error $error ) {
315 yaymail_get_logger( $error );
316 } catch ( \Exception $exception ) {
317 yaymail_get_logger( $exception );
318 }//end try
319 }
320
321 public function preview_mail() {
322 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
323 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
324 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
325 }
326 try {
327 $order_id = isset( $_POST['order_id'] ) ? sanitize_text_field( wp_unslash( $_POST['order_id'] ) ) : 'sample_order';
328 $template_data = isset( $_POST['template_data'] ) ? $this->sanitize( wp_unslash( $_POST['template_data'] ) ) : []; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
329 $unsaved_settings = isset( $_POST['unsaved_settings'] ) ? $this->sanitize( wp_unslash( $_POST['unsaved_settings'] ) ) : []; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
330
331 if ( empty( $template_data ) ) {
332 return wp_send_json_error( [ 'mess' => __( 'Can\'t find template', 'yaymail' ) ] );
333 }
334
335 if ( empty( $order_id ) ) {
336 return wp_send_json_error( [ 'mess' => __( 'Can\'t find order', 'yaymail' ) ] );
337 }
338
339 $template = new YayMailTemplate( $template_data['name'] );
340
341 if ( ! empty( $unsaved_settings ) ) {
342 global $yaymail_unsaved_settings;
343 $yaymail_unsaved_settings = $unsaved_settings;
344 }
345
346 $template->set_background_color( $template_data['background_color'] );
347 $template->set_text_link_color( $template_data['text_link_color'] );
348 $template->set_global_header_settings( $template_data['global_header_settings'] );
349 $template->set_global_footer_settings( $template_data['global_footer_settings'] );
350 $template->set_elements( $template_data['elements'] );
351
352 $render_data = [];
353
354 if ( empty( $order_id ) || ( 'sample_order' === $order_id ) ) {
355 $render_data['is_sample'] = true;
356 } else {
357 $render_data['order'] = wc_get_order( $order_id );
358 }
359
360 $render_data['is_customized_preview'] = true;
361 // check if email template on preview and send test mail
362
363 $html = $template->get_content( $render_data );
364
365 // TODO: render with passing settings
366 $current_email = null;
367 $subject = 'Sample Subject';
368 $emails = wc()->mailer()->emails;
369 foreach ( $emails as $email ) {
370 if ( $email->id === $template_data['name'] ) {
371 $current_email = $email;
372 if ( method_exists( $current_email, 'set_object' ) ) {
373 if ( ! empty( $render_data['order'] ) && is_a( $render_data['order'], '\WC_Order' ) ) {
374 $current_email->set_object( $render_data['order'] );
375 } else {
376 $current_email->set_object( Helpers::get_dummy_order() );
377 }
378 }
379 break;
380 }
381 }
382
383 if ( ! empty( $current_email ) ) {
384 $subject = $current_email->get_subject();
385 }
386 $email_address = wp_get_current_user()->user_email ?? 'sample@example.com';
387
388 wp_send_json_success(
389 [
390 'html' => $html,
391 'subject' => $subject,
392 'email_address' => $email_address,
393 ]
394 );
395 } catch ( \Error $error ) {
396 yaymail_get_logger( $error );
397 } catch ( \Exception $exception ) {
398 yaymail_get_logger( $exception );
399 }//end try
400 }
401
402 public function preview_mail_for_woo() {
403 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
404 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
405 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
406 }
407 try {
408 $template_name = isset( $_POST['template_name'] ) ? sanitize_text_field( wp_unslash( $_POST['template_name'] ) ) : '';
409 $search_order_id = isset( $_POST['search_order_id'] ) ? sanitize_text_field( wp_unslash( $_POST['search_order_id'] ) ) : null;
410 $email_address = isset( $_POST['email_address'] ) ? sanitize_text_field( wp_unslash( $_POST['email_address'] ) ) : '';
411
412 $email_preview_output = PreviewEmail\PreviewEmailWoo::email_preview_output( $search_order_id, $template_name, $email_address );
413
414 $email_preview_output = apply_filters( 'yaymail_preview_email', $email_preview_output, $search_order_id, $template_name, $email_address );
415
416 $send_mail = false;
417
418 if ( ! empty( $email_address ) && ! empty( $email_preview_output['html'] ) ) {
419 $headers = "Content-Type: text/html\r\n";
420 $class_wc_email = \WC_Emails::instance();
421 $subject = __( 'Email Preview', 'yaymail' );
422 $send_mail = $class_wc_email->send( $email_address, $subject, $email_preview_output['html'], $headers, [] );
423 if ( ! $send_mail ) {
424 return wp_send_json_error( [ 'mess' => __( 'Can\'t send email', 'yaymail' ) ] );
425 }
426 }
427
428 wp_send_json_success(
429 [
430 'html' => ! empty( $email_preview_output['html'] ) ? $email_preview_output['html'] : __( 'No email content found', 'yaymail' ),
431 'subject' => ! empty( $email_preview_output['subject'] ) ? $email_preview_output['subject'] : __( 'No subject found', 'yaymail' ),
432 'is_disabled_send_mail' => ! empty( $email_preview_output['is_disabled_send_mail'] ) ? $email_preview_output['is_disabled_send_mail'] : false,
433 'send_mail_success' => $send_mail,
434 ]
435 );
436 } catch ( \Error $error ) {
437 yaymail_get_logger( $error );
438 } catch ( \Exception $exception ) {
439 yaymail_get_logger( $exception );
440 }//end try
441 }
442
443 public function export_templates() {
444 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
445 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
446 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
447 }
448 try {
449 $templates = isset( $_POST['templates'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['templates'] ) ) : [];
450 // TODO: sanitize
451 $default = [
452 'post_type' => 'yaymail_template',
453 'post_status' => [ 'publish', 'pending', 'future' ],
454 'posts_per_page' => '-1',
455 'meta_query' => [
456 [
457 'key' => YayMailTemplate::META_KEYS['name'],
458 'value' => $templates,
459 'compare' => 'IN',
460 ],
461 ],
462 ];
463 $export_data = [];
464 $query = new \WP_Query( $default );
465 if ( $query->have_posts() ) {
466 $posts = $query->get_posts();
467 foreach ( $posts as $post ) {
468 $template_name = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['name'], true );
469 $elements = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['elements'], true );
470 $language = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['language'], true );
471 $text_link_color = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['text_link_color'], true );
472 $background_color = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['background_color'], true );
473 $content_background_color = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['content_background_color'], true );
474 $content_text_color = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['content_text_color'], true );
475 $file_name = "{$template_name}.json";
476 if ( empty( $language ) ) {
477 $export_data[] = [
478 'file_name' => $file_name,
479 'templates_data' => [
480 'template' => $template_name,
481 'elements' => $elements,
482 'language' => '',
483 'text_link_color' => $text_link_color,
484 'background_color' => $background_color,
485 'content_background_color' => $content_background_color,
486 'content_text_color' => $content_text_color,
487 'title_color' => $title_color,
488 ],
489 ];
490 }
491 }//end foreach
492 }//end if
493 wp_reset_postdata();
494 wp_send_json_success(
495 [
496 'message' => __( 'Export successfully', 'yaymail' ),
497 'data' => $export_data,
498 'file_name' => 'yaymail_customizer_templates_' . gmdate( 'm-d-Y' ),
499 ]
500 );
501 } catch ( \Error $error ) {
502 yaymail_get_logger( $error );
503 } catch ( \Exception $exception ) {
504 yaymail_get_logger( $exception );
505 }//end try
506 }
507
508 public function import_templates() {
509 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
510 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
511 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
512 }
513 try {
514 if ( ! empty( $_FILES ) ) {
515 $result = $this->process_import( $_FILES );
516 wp_send_json_success(
517 [
518 'imported_data' => $result,
519 ]
520 );
521 } else {
522 wp_send_json_error( [ 'message' => __( 'Can\'t find import files.', 'yaymail' ) ] );
523 }
524 } catch ( \Error $error ) {
525 yaymail_get_logger( $error );
526 } catch ( \Exception $exception ) {
527 yaymail_get_logger( $exception );
528 }
529 }
530
531 public function process_import( $files ) {
532 global $wp_filesystem;
533 if ( empty( $wp_filesystem ) ) {
534 require_once ABSPATH . '/wp-admin/includes/file.php';
535 WP_Filesystem();
536 }
537 $imported_templates = [];
538 $is_legacy = false;
539 foreach ( $files as $file ) {
540 if ( isset( $file['type'] ) ) {
541 if ( 'application/json' === $file['type'] ) {
542 if ( ! empty( $file['tmp_name'] ) ) {
543 $file_tmp_name = sanitize_text_field( $file['tmp_name'] );
544 $file_content = $wp_filesystem->get_contents( $file_tmp_name );
545 $file_content = json_decode( $file_content, true );
546 if ( ! isset( $file_content['template'] ) ) {
547 if ( isset( $file_content['yaymailTemplateExport'] ) ) {
548 $is_legacy = true;
549 $imported_templates = array_merge( $imported_templates, $this->process_import_legacy( $file_content ) );
550 } else {
551 continue;
552 }
553 } else {
554 $update_result = $this->processing_import_update_data( $file_content );
555 if ( ! empty( $update_result ) ) {
556 $imported_templates[] = $update_result;
557 }
558 }
559 }//end if
560 }//end if
561 }//end if
562 }//end foreach
563 if ( $is_legacy ) {
564 MainMigration::get_instance()->migrate( true );
565 }
566 return $imported_templates;
567 }
568
569 public function process_import_legacy( $file_content ) {
570 $updated_templates = [];
571 // Import templates
572 foreach ( $file_content['yaymailTemplateExport'] as $template ) {
573 $updated_result = $this->processing_import_update_data( $template, true );
574 if ( ! empty( $updated_result ) ) {
575 $updated_templates[] = $updated_result;
576 }
577 }//end foreach
578 // Import settings
579 $import_settings = isset( $file_content['yaymail_settings'] ) ? $file_content['yaymail_settings'] : [];
580 if ( ! empty( $import_settings ) ) {
581 update_option( 'yaymail_settings', $import_settings );
582 }
583 return $updated_templates;
584 }
585
586 public function processing_import_update_data( $data, $is_legacy = false ) {
587 $template_name = $data['template'] ?? null;
588 $elements = $data['elements'] ?? null;
589 $text_link_color = $data['text_link_color'] ?? null;
590 $background_color = $data['background_color'] ?? null;
591 $title_color = $data['title_color'] ?? null;
592
593 if ( empty( $template_name ) ) {
594 return null;
595 }
596
597 $template = new YayMailTemplate( $template_name );
598 if ( ! $template->is_exists() ) {
599 return null;
600 }
601
602 $template->set_elements( $elements );
603 $template->set_text_link_color( $text_link_color );
604 $template->set_background_color( $background_color );
605 $template->set_title_color( $title_color );
606 $template->set_content_background_color( $content_background_color );
607 $template->set_content_text_color( $content_text_color );
608 if ( $is_legacy ) {
609 $template->set_status( 'inactive' );
610 }
611
612 $template->save();
613
614 wp_reset_postdata();
615
616 return [
617 'template_name' => $template_name,
618 ];
619 }
620
621 /**
622 * Process a custom hook request and generate HTML content.
623 *
624 * This function handles a custom hook request, generates HTML content based on the provided data and attributes.
625 * It is designed to be used as an AJAX callback.
626 *
627 * @example $_POST['data'] =
628 * [
629 * 'template_data' => YayMail\YayMailTemplate,
630 * 'order_id' => 'sample_order',
631 * 'attributes' => [
632 * [
633 * 'name' => 'hook',
634 * 'value' => 'your_hook'
635 * ],
636 * [
637 * 'name' => 'background_color',
638 * 'value' => '#ffffff'
639 * ]
640 * ]
641 * ]
642 *
643 * @return void This function sends a JSON response with HTML content or error messages.
644 */
645 public function get_custom_hook_html() {
646 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
647 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
648 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
649 }
650 try {
651 $attributes = isset( $_POST['data']['attributes'] ) ? $_POST['data']['attributes'] : []; // phpcs:ignore
652 if ( empty( $attributes ) ) {
653 return wp_send_json_error( [ 'mess' => __( 'Attributes empty', 'yaymail' ) ] );
654 }
655
656 /**
657 * Build data for shortcode
658 */
659 $template_model = \YayMail\Models\TemplateModel::get_instance();
660 $data = [];
661 if ( ! empty( $_POST['data']['template_data'] ) ) {
662 $data = \YayMail\Models\TemplateModel::get_shortcode_executor_data( sanitize_text_field( wp_unslash( $_POST['data']['template_data'] ) ), sanitize_text_field( wp_unslash( $_POST['data']['order_id'] ) ) );
663
664 $data['template']->set_props( sanitize_text_field( wp_unslash( $_POST['data']['template_data'] ) ) );
665 }
666
667 $hook_shortcodes = \YayMail\Shortcodes\HookShortcodes::get_instance();
668 $html = $hook_shortcodes->yaymail_handle_custom_hook_shortcode( $data, $attributes );
669 wp_send_json_success(
670 [
671 'html' => $html,
672 ]
673 );
674 } catch ( \Error $error ) {
675 yaymail_get_logger( $error );
676 } catch ( \Exception $exception ) {
677 yaymail_get_logger( $exception );
678 }//end try
679 }
680
681 /**
682 * Get all needed data when load YayMail template to customizer.
683 */
684 public function get_template_data_onload() {
685 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
686 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
687 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
688 }
689 try {
690 $setting_model = SettingModel::get_instance();
691 $settings_data = $setting_model->find_all();
692
693 $template_name = isset( $_POST['data']['template_name'] ) ? sanitize_text_field( $_POST['data']['template_name'] ) : 'new_order';
694 $order_id = isset( $_POST['data']['order_id'] ) ? sanitize_text_field( $_POST['data']['order_id'] ) : 'sample_order';
695
696 $template_model = TemplateModel::get_instance();
697
698 $shortcodes_data = $template_model->get_shortcodes_by_template_name_and_order_id( $template_name, $order_id );
699
700 $templates_data = apply_filters( 'yaymail_get_all_templates', $template_model->find_all() );
701
702 $selected_template_data = $template_model->find_by_name( $template_name );
703
704 $elements_data = TemplateModel::get_elements_for_template( $template_name );
705
706 $revision_model = RevisionModel::get_instance();
707 $revisions_data = $revision_model->get_by_template( $template_name );
708
709 wp_send_json_success(
710 [
711 'settings_data' => $settings_data,
712 'templates_data' => $templates_data,
713 'selected_template_data' => $selected_template_data,
714 'elements_data' => $elements_data,
715 'revisions_data' => $revisions_data,
716 'shortcodes_data' => $shortcodes_data,
717 ]
718 );
719 } catch ( \Error $error ) {
720 yaymail_get_logger( $error );
721 wp_send_json_error( [ 'mess' => $error->getMessage() ] );
722 } catch ( \Exception $exception ) {
723 yaymail_get_logger( $exception );
724 wp_send_json_error( [ 'mess' => $exception->getMessage() ] );
725 } catch ( \Throwable $throwable ) {
726 yaymail_get_logger( $throwable );
727 wp_send_json_error( [ 'mess' => $throwable->getMessage() ] );
728 }//end try
729 }
730
731 public function yaymail_review() {
732 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
733 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
734 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
735 }
736 try {
737
738 $yaymail_review = update_option( 'yaymail_review', true );
739
740 wp_send_json_success(
741 [
742 'reviewed' => $yaymail_review,
743 ]
744 );
745
746 } catch ( \Error $error ) {
747 yaymail_get_logger( $error );
748 } catch ( \Exception $exception ) {
749 yaymail_get_logger( $exception );
750 }
751 }
752
753 public function change_ghf_tour() {
754 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
755 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
756 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
757 }
758
759 try {
760 $next_move = isset( $_POST['next_move'] ) ? sanitize_text_field( wp_unslash( $_POST['next_move'] ) ) : 'initial';
761 $ghf_tour = update_option( 'yaymail_ghf_tour', $next_move );
762
763 wp_send_json_success(
764 [
765 'ghf_tour' => $ghf_tour,
766 ]
767 );
768 } catch ( \Error $error ) {
769 yaymail_get_logger( $error );
770 } catch ( \Exception $exception ) {
771 yaymail_get_logger( $exception );
772 }
773 }
774
775 public function dismiss_multi_select_notice() {
776 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
777 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
778 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
779 }
780
781 try {
782 update_option( 'yaymail_show_multi_select_notice', 'no' );
783
784 wp_send_json_success(
785 [
786 'show_multi_select_notice' => 'no',
787 ]
788 );
789 } catch ( \Error $error ) {
790 yaymail_get_logger( $error );
791 } catch ( \Exception $exception ) {
792 yaymail_get_logger( $exception );
793 }
794 }
795
796 public function dismiss_new_element_notification() {
797 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
798 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
799 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
800 }
801
802 $elements = isset( $_POST['elements'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['elements'] ) ) : [];
803
804 $viewed_new_elements = get_option( 'yaymail_viewed_new_elements', [] );
805 $viewed_new_elements = array_unique( array_merge( $viewed_new_elements, $elements ) );
806
807 try {
808 update_option( 'yaymail_viewed_new_elements', $viewed_new_elements );
809
810 wp_send_json_success(
811 [
812 'viewed_new_elements' => $viewed_new_elements,
813 ]
814 );
815 } catch ( \Error $error ) {
816 wp_send_json_error( [ 'mess' => $error->getMessage() ] );
817 } catch ( \Exception $exception ) {
818 yaymail_get_logger( $exception );
819 }
820 }
821 }
822