PluginProbe
YayMail – WooCommerce Email Customizer / 4.3.4
YayMail – WooCommerce Email Customizer v4.3.4
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.4, at src/Ajax.php

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