PluginProbe
YayMail – WooCommerce Email Customizer / 4.3.3
YayMail – WooCommerce Email Customizer v4.3.3
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.3, at src/Ajax.php

827 lines 34.9 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
235 if ( ! current_user_can( 'install_plugins' ) && ! current_user_can( 'activate_plugins' ) ) {
236 return wp_send_json_error( [ 'mess' => __( 'You do not have permission to install plugins', 'yaymail' ) ] );
237 }
238
239 try {
240 $is_installed = $this->process_plugin_installer( 'yaysmtp' );
241
242 if ( false === $is_installed ) {
243 wp_send_json_error( [ 'message' => $is_installed ] );
244 }
245
246 $result = activate_plugin( 'yaysmtp/yay-smtp.php' );
247
248 if ( is_wp_error( $result ) ) {
249 return wp_send_json_error( [ 'mess' => esc_html( $result->get_error_message() ) ] );
250 }
251
252 wp_send_json_success(
253 [
254 'installed' => null === $result,
255 ]
256 );
257
258 } catch ( \Error $error ) {
259 yaymail_get_logger( $error );
260 } catch ( \Exception $exception ) {
261 yaymail_get_logger( $exception );
262 }//end try
263 }
264
265 public function send_test_mail() {
266 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
267 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
268 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
269 }
270 try {
271 $template_name = isset( $_POST['template_name'] ) ? sanitize_text_field( wp_unslash( $_POST['template_name'] ) ) : '';
272 $order_id = isset( $_POST['order_id'] ) ? sanitize_text_field( wp_unslash( $_POST['order_id'] ) ) : 'sample_order';
273 $email = isset( $_POST['email'] ) ? sanitize_text_field( $_POST['email'] ) : '';
274
275 if ( empty( $template_name ) ) {
276 return wp_send_json_error( [ 'mess' => __( 'Can\'t find template', 'yaymail' ) ] );
277 }
278
279 if ( empty( $order_id ) ) {
280 return wp_send_json_error( [ 'mess' => __( 'Can\'t find order', 'yaymail' ) ] );
281 }
282
283 if ( empty( $email ) ) {
284 return wp_send_json_error( [ 'mess' => __( 'Can\'t find email', 'yaymail' ) ] );
285 }
286
287 $template = new YayMailTemplate( $template_name );
288
289 $render_data = [];
290
291 if ( empty( $order_id ) || ( 'sample_order' === $order_id ) ) {
292 $render_data['is_sample'] = true;
293 } else {
294 $render_data['order'] = wc_get_order( $order_id );
295 }
296
297 $render_data['is_customized_preview'] = true;
298 // check if email template on preview and send test mail
299
300 update_option( 'yaymail_default_email_test', $email );
301
302 $html = $template->get_content( $render_data );
303
304 $headers = "Content-Type: text/html\r\n";
305 $class_wc_email = \WC_Emails::instance();
306 $subject = __( 'Email Test', 'yaymail' );
307 $send_mail = $class_wc_email->send( $email, $subject, $html, $headers, [] );
308
309 if ( ! $send_mail ) {
310 return wp_send_json_error( [ 'mess' => __( 'Can\'t send email', 'yaymail' ) ] );
311 }
312
313 wp_send_json_success(
314 [
315 'email' => $email,
316 'send_mail_success' => $send_mail,
317 ]
318 );
319 } catch ( \Error $error ) {
320 yaymail_get_logger( $error );
321 } catch ( \Exception $exception ) {
322 yaymail_get_logger( $exception );
323 }//end try
324 }
325
326 public function preview_mail() {
327 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
328 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
329 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
330 }
331 try {
332 $order_id = isset( $_POST['order_id'] ) ? sanitize_text_field( wp_unslash( $_POST['order_id'] ) ) : 'sample_order';
333 $template_data = isset( $_POST['template_data'] ) ? $this->sanitize( wp_unslash( $_POST['template_data'] ) ) : []; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
334 $unsaved_settings = isset( $_POST['unsaved_settings'] ) ? $this->sanitize( wp_unslash( $_POST['unsaved_settings'] ) ) : []; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
335
336 if ( empty( $template_data ) ) {
337 return wp_send_json_error( [ 'mess' => __( 'Can\'t find template', 'yaymail' ) ] );
338 }
339
340 if ( empty( $order_id ) ) {
341 return wp_send_json_error( [ 'mess' => __( 'Can\'t find order', 'yaymail' ) ] );
342 }
343
344 $template = new YayMailTemplate( $template_data['name'] );
345
346 if ( ! empty( $unsaved_settings ) ) {
347 global $yaymail_unsaved_settings;
348 $yaymail_unsaved_settings = $unsaved_settings;
349 }
350
351 $template->set_background_color( $template_data['background_color'] );
352 $template->set_text_link_color( $template_data['text_link_color'] );
353 $template->set_global_header_settings( $template_data['global_header_settings'] );
354 $template->set_global_footer_settings( $template_data['global_footer_settings'] );
355 $template->set_elements( $template_data['elements'] );
356
357 $render_data = [];
358
359 if ( empty( $order_id ) || ( 'sample_order' === $order_id ) ) {
360 $render_data['is_sample'] = true;
361 } else {
362 $render_data['order'] = wc_get_order( $order_id );
363 }
364
365 $render_data['is_customized_preview'] = true;
366 // check if email template on preview and send test mail
367
368 $html = $template->get_content( $render_data );
369
370 // TODO: render with passing settings
371 $current_email = null;
372 $subject = 'Sample Subject';
373 $emails = wc()->mailer()->emails;
374 foreach ( $emails as $email ) {
375 if ( $email->id === $template_data['name'] ) {
376 $current_email = $email;
377 if ( method_exists( $current_email, 'set_object' ) ) {
378 if ( ! empty( $render_data['order'] ) && is_a( $render_data['order'], '\WC_Order' ) ) {
379 $current_email->set_object( $render_data['order'] );
380 } else {
381 $current_email->set_object( Helpers::get_dummy_order() );
382 }
383 }
384 break;
385 }
386 }
387
388 if ( ! empty( $current_email ) ) {
389 $subject = $current_email->get_subject();
390 }
391 $email_address = wp_get_current_user()->user_email ?? 'sample@example.com';
392
393 wp_send_json_success(
394 [
395 'html' => $html,
396 'subject' => $subject,
397 'email_address' => $email_address,
398 ]
399 );
400 } catch ( \Error $error ) {
401 yaymail_get_logger( $error );
402 } catch ( \Exception $exception ) {
403 yaymail_get_logger( $exception );
404 }//end try
405 }
406
407 public function preview_mail_for_woo() {
408 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
409 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
410 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
411 }
412 try {
413 $template_name = isset( $_POST['template_name'] ) ? sanitize_text_field( wp_unslash( $_POST['template_name'] ) ) : '';
414 $search_order_id = isset( $_POST['search_order_id'] ) ? sanitize_text_field( wp_unslash( $_POST['search_order_id'] ) ) : null;
415 $email_address = isset( $_POST['email_address'] ) ? sanitize_text_field( wp_unslash( $_POST['email_address'] ) ) : '';
416
417 $email_preview_output = PreviewEmail\PreviewEmailWoo::email_preview_output( $search_order_id, $template_name, $email_address );
418
419 $email_preview_output = apply_filters( 'yaymail_preview_email', $email_preview_output, $search_order_id, $template_name, $email_address );
420
421 $send_mail = false;
422
423 if ( ! empty( $email_address ) && ! empty( $email_preview_output['html'] ) ) {
424 $headers = "Content-Type: text/html\r\n";
425 $class_wc_email = \WC_Emails::instance();
426 $subject = __( 'Email Preview', 'yaymail' );
427 $send_mail = $class_wc_email->send( $email_address, $subject, $email_preview_output['html'], $headers, [] );
428 if ( ! $send_mail ) {
429 return wp_send_json_error( [ 'mess' => __( 'Can\'t send email', 'yaymail' ) ] );
430 }
431 }
432
433 wp_send_json_success(
434 [
435 'html' => ! empty( $email_preview_output['html'] ) ? $email_preview_output['html'] : __( 'No email content found', 'yaymail' ),
436 'subject' => ! empty( $email_preview_output['subject'] ) ? $email_preview_output['subject'] : __( 'No subject found', 'yaymail' ),
437 'is_disabled_send_mail' => ! empty( $email_preview_output['is_disabled_send_mail'] ) ? $email_preview_output['is_disabled_send_mail'] : false,
438 'send_mail_success' => $send_mail,
439 ]
440 );
441 } catch ( \Error $error ) {
442 yaymail_get_logger( $error );
443 } catch ( \Exception $exception ) {
444 yaymail_get_logger( $exception );
445 }//end try
446 }
447
448 public function export_templates() {
449 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
450 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
451 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
452 }
453 try {
454 $templates = isset( $_POST['templates'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['templates'] ) ) : [];
455 // TODO: sanitize
456 $default = [
457 'post_type' => 'yaymail_template',
458 'post_status' => [ 'publish', 'pending', 'future' ],
459 'posts_per_page' => '-1',
460 'meta_query' => [
461 [
462 'key' => YayMailTemplate::META_KEYS['name'],
463 'value' => $templates,
464 'compare' => 'IN',
465 ],
466 ],
467 ];
468 $export_data = [];
469 $query = new \WP_Query( $default );
470 if ( $query->have_posts() ) {
471 $posts = $query->get_posts();
472 foreach ( $posts as $post ) {
473 $template_name = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['name'], true );
474 $elements = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['elements'], true );
475 $language = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['language'], true );
476 $text_link_color = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['text_link_color'], true );
477 $background_color = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['background_color'], true );
478 $content_background_color = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['content_background_color'], true );
479 $content_text_color = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['content_text_color'], true );
480 $file_name = "{$template_name}.json";
481 if ( empty( $language ) ) {
482 $export_data[] = [
483 'file_name' => $file_name,
484 'templates_data' => [
485 'template' => $template_name,
486 'elements' => $elements,
487 'language' => '',
488 'text_link_color' => $text_link_color,
489 'background_color' => $background_color,
490 'content_background_color' => $content_background_color,
491 'content_text_color' => $content_text_color,
492 'title_color' => $title_color,
493 ],
494 ];
495 }
496 }//end foreach
497 }//end if
498 wp_reset_postdata();
499 wp_send_json_success(
500 [
501 'message' => __( 'Export successfully', 'yaymail' ),
502 'data' => $export_data,
503 'file_name' => 'yaymail_customizer_templates_' . gmdate( 'm-d-Y' ),
504 ]
505 );
506 } catch ( \Error $error ) {
507 yaymail_get_logger( $error );
508 } catch ( \Exception $exception ) {
509 yaymail_get_logger( $exception );
510 }//end try
511 }
512
513 public function import_templates() {
514 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
515 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
516 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
517 }
518 try {
519 if ( ! empty( $_FILES ) ) {
520 $result = $this->process_import( $_FILES );
521 wp_send_json_success(
522 [
523 'imported_data' => $result,
524 ]
525 );
526 } else {
527 wp_send_json_error( [ 'message' => __( 'Can\'t find import files.', 'yaymail' ) ] );
528 }
529 } catch ( \Error $error ) {
530 yaymail_get_logger( $error );
531 } catch ( \Exception $exception ) {
532 yaymail_get_logger( $exception );
533 }
534 }
535
536 public function process_import( $files ) {
537 global $wp_filesystem;
538 if ( empty( $wp_filesystem ) ) {
539 require_once ABSPATH . '/wp-admin/includes/file.php';
540 WP_Filesystem();
541 }
542 $imported_templates = [];
543 $is_legacy = false;
544 foreach ( $files as $file ) {
545 if ( isset( $file['type'] ) ) {
546 if ( 'application/json' === $file['type'] ) {
547 if ( ! empty( $file['tmp_name'] ) ) {
548 $file_tmp_name = sanitize_text_field( $file['tmp_name'] );
549 $file_content = $wp_filesystem->get_contents( $file_tmp_name );
550 $file_content = json_decode( $file_content, true );
551 if ( ! isset( $file_content['template'] ) ) {
552 if ( isset( $file_content['yaymailTemplateExport'] ) ) {
553 $is_legacy = true;
554 $imported_templates = array_merge( $imported_templates, $this->process_import_legacy( $file_content ) );
555 } else {
556 continue;
557 }
558 } else {
559 $update_result = $this->processing_import_update_data( $file_content );
560 if ( ! empty( $update_result ) ) {
561 $imported_templates[] = $update_result;
562 }
563 }
564 }//end if
565 }//end if
566 }//end if
567 }//end foreach
568 if ( $is_legacy ) {
569 MainMigration::get_instance()->migrate( true );
570 }
571 return $imported_templates;
572 }
573
574 public function process_import_legacy( $file_content ) {
575 $updated_templates = [];
576 // Import templates
577 foreach ( $file_content['yaymailTemplateExport'] as $template ) {
578 $updated_result = $this->processing_import_update_data( $template, true );
579 if ( ! empty( $updated_result ) ) {
580 $updated_templates[] = $updated_result;
581 }
582 }//end foreach
583 // Import settings
584 $import_settings = isset( $file_content['yaymail_settings'] ) ? $file_content['yaymail_settings'] : [];
585 if ( ! empty( $import_settings ) ) {
586 update_option( 'yaymail_settings', $import_settings );
587 }
588 return $updated_templates;
589 }
590
591 public function processing_import_update_data( $data, $is_legacy = false ) {
592 $template_name = $data['template'] ?? null;
593 $elements = $data['elements'] ?? null;
594 $text_link_color = $data['text_link_color'] ?? null;
595 $background_color = $data['background_color'] ?? null;
596 $title_color = $data['title_color'] ?? null;
597
598 if ( empty( $template_name ) ) {
599 return null;
600 }
601
602 $template = new YayMailTemplate( $template_name );
603 if ( ! $template->is_exists() ) {
604 return null;
605 }
606
607 $template->set_elements( $elements );
608 $template->set_text_link_color( $text_link_color );
609 $template->set_background_color( $background_color );
610 $template->set_title_color( $title_color );
611 $template->set_content_background_color( $content_background_color );
612 $template->set_content_text_color( $content_text_color );
613 if ( $is_legacy ) {
614 $template->set_status( 'inactive' );
615 }
616
617 $template->save();
618
619 wp_reset_postdata();
620
621 return [
622 'template_name' => $template_name,
623 ];
624 }
625
626 /**
627 * Process a custom hook request and generate HTML content.
628 *
629 * This function handles a custom hook request, generates HTML content based on the provided data and attributes.
630 * It is designed to be used as an AJAX callback.
631 *
632 * @example $_POST['data'] =
633 * [
634 * 'template_data' => YayMail\YayMailTemplate,
635 * 'order_id' => 'sample_order',
636 * 'attributes' => [
637 * [
638 * 'name' => 'hook',
639 * 'value' => 'your_hook'
640 * ],
641 * [
642 * 'name' => 'background_color',
643 * 'value' => '#ffffff'
644 * ]
645 * ]
646 * ]
647 *
648 * @return void This function sends a JSON response with HTML content or error messages.
649 */
650 public function get_custom_hook_html() {
651 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
652 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
653 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
654 }
655 try {
656 $attributes = isset( $_POST['data']['attributes'] ) ? $_POST['data']['attributes'] : []; // phpcs:ignore
657 if ( empty( $attributes ) ) {
658 return wp_send_json_error( [ 'mess' => __( 'Attributes empty', 'yaymail' ) ] );
659 }
660
661 /**
662 * Build data for shortcode
663 */
664 $template_model = \YayMail\Models\TemplateModel::get_instance();
665 $data = [];
666 if ( ! empty( $_POST['data']['template_data'] ) ) {
667 $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'] ) ) );
668
669 $data['template']->set_props( sanitize_text_field( wp_unslash( $_POST['data']['template_data'] ) ) );
670 }
671
672 $hook_shortcodes = \YayMail\Shortcodes\HookShortcodes::get_instance();
673 $html = $hook_shortcodes->yaymail_handle_custom_hook_shortcode( $data, $attributes );
674 wp_send_json_success(
675 [
676 'html' => $html,
677 ]
678 );
679 } catch ( \Error $error ) {
680 yaymail_get_logger( $error );
681 } catch ( \Exception $exception ) {
682 yaymail_get_logger( $exception );
683 }//end try
684 }
685
686 /**
687 * Get all needed data when load YayMail template to customizer.
688 */
689 public function get_template_data_onload() {
690 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
691 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
692 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
693 }
694 try {
695 $setting_model = SettingModel::get_instance();
696 $settings_data = $setting_model->find_all();
697
698 $template_name = isset( $_POST['data']['template_name'] ) ? sanitize_text_field( $_POST['data']['template_name'] ) : 'new_order';
699 $order_id = isset( $_POST['data']['order_id'] ) ? sanitize_text_field( $_POST['data']['order_id'] ) : 'sample_order';
700
701 $template_model = TemplateModel::get_instance();
702
703 $shortcodes_data = $template_model->get_shortcodes_by_template_name_and_order_id( $template_name, $order_id );
704
705 $templates_data = apply_filters( 'yaymail_get_all_templates', $template_model->find_all() );
706
707 $selected_template_data = $template_model->find_by_name( $template_name );
708
709 $elements_data = TemplateModel::get_elements_for_template( $template_name );
710
711 $revision_model = RevisionModel::get_instance();
712 $revisions_data = $revision_model->get_by_template( $template_name );
713
714 wp_send_json_success(
715 [
716 'settings_data' => $settings_data,
717 'templates_data' => $templates_data,
718 'selected_template_data' => $selected_template_data,
719 'elements_data' => $elements_data,
720 'revisions_data' => $revisions_data,
721 'shortcodes_data' => $shortcodes_data,
722 ]
723 );
724 } catch ( \Error $error ) {
725 yaymail_get_logger( $error );
726 wp_send_json_error( [ 'mess' => $error->getMessage() ] );
727 } catch ( \Exception $exception ) {
728 yaymail_get_logger( $exception );
729 wp_send_json_error( [ 'mess' => $exception->getMessage() ] );
730 } catch ( \Throwable $throwable ) {
731 yaymail_get_logger( $throwable );
732 wp_send_json_error( [ 'mess' => $throwable->getMessage() ] );
733 }//end try
734 }
735
736 public function yaymail_review() {
737 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
738 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
739 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
740 }
741 try {
742
743 $yaymail_review = update_option( 'yaymail_review', true );
744
745 wp_send_json_success(
746 [
747 'reviewed' => $yaymail_review,
748 ]
749 );
750
751 } catch ( \Error $error ) {
752 yaymail_get_logger( $error );
753 } catch ( \Exception $exception ) {
754 yaymail_get_logger( $exception );
755 }
756 }
757
758 public function change_ghf_tour() {
759 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
760 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
761 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
762 }
763
764 try {
765 $next_move = isset( $_POST['next_move'] ) ? sanitize_text_field( wp_unslash( $_POST['next_move'] ) ) : 'initial';
766 $ghf_tour = update_option( 'yaymail_ghf_tour', $next_move );
767
768 wp_send_json_success(
769 [
770 'ghf_tour' => $ghf_tour,
771 ]
772 );
773 } catch ( \Error $error ) {
774 yaymail_get_logger( $error );
775 } catch ( \Exception $exception ) {
776 yaymail_get_logger( $exception );
777 }
778 }
779
780 public function dismiss_multi_select_notice() {
781 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
782 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
783 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
784 }
785
786 try {
787 update_option( 'yaymail_show_multi_select_notice', 'no' );
788
789 wp_send_json_success(
790 [
791 'show_multi_select_notice' => 'no',
792 ]
793 );
794 } catch ( \Error $error ) {
795 yaymail_get_logger( $error );
796 } catch ( \Exception $exception ) {
797 yaymail_get_logger( $exception );
798 }
799 }
800
801 public function dismiss_new_element_notification() {
802 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
803 if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) {
804 return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] );
805 }
806
807 $elements = isset( $_POST['elements'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['elements'] ) ) : [];
808
809 $viewed_new_elements = get_option( 'yaymail_viewed_new_elements', [] );
810 $viewed_new_elements = array_unique( array_merge( $viewed_new_elements, $elements ) );
811
812 try {
813 update_option( 'yaymail_viewed_new_elements', $viewed_new_elements );
814
815 wp_send_json_success(
816 [
817 'viewed_new_elements' => $viewed_new_elements,
818 ]
819 );
820 } catch ( \Error $error ) {
821 wp_send_json_error( [ 'mess' => $error->getMessage() ] );
822 } catch ( \Exception $exception ) {
823 yaymail_get_logger( $exception );
824 }
825 }
826 }
827