| @@ -7,8 +7,11 @@ | ||
| 7 | 7 | use YayMail\Models\TemplateModel; |
| 8 | 8 | use YayMail\Models\RevisionModel; |
| 9 | 9 | use YayMail\Migrations\MainMigration; |
| 10 | 10 | use YayMail\Utils\Helpers; |
| 11 | +use YayMail\Utils\Logger; | |
| 12 | +use YayMail\Models\PatternTemplateModel; | |
| 13 | +use YayMail\Utils\TemplateHelpers; | |
| 11 | 14 | |
| 12 | 15 | /** |
| 13 | 16 | * I18n Logic |
| 14 | 17 | * |
| @@ -35,10 +38,59 @@ | ||
| 35 | 38 | add_action( 'wp_ajax_yaymail_dismiss_multi_select_notice', [ $this, 'dismiss_multi_select_notice' ] ); |
| 36 | 39 | add_action( 'wp_ajax_yaymail_export_state', [ $this, 'export_state' ] ); |
| 37 | 40 | add_action( 'wp_ajax_yaymail_import_state', [ $this, 'import_state' ] ); |
| 38 | 41 | add_action( 'wp_ajax_yaymail_dismiss_new_element_notification', [ $this, 'dismiss_new_element_notification' ] ); |
| 42 | + add_action( 'wp_ajax_yaymail_log_client_error', [ $this, 'log_client_error' ] ); | |
| 39 | 43 | } |
| 40 | 44 | |
| 45 | + /** | |
| 46 | + * Records a runtime error reported by the admin SPA (see apps/customizer/src/utils/client-error-reporter.ts). | |
| 47 | + * | |
| 48 | + * Goes to the WooCommerce log (WooCommerce > Status > Logs, source "yaymail-client") so support can ask the | |
| 49 | + * customer to copy it from wp-admin; falls back to the yaymail-logs file when WooCommerce is absent. | |
| 50 | + */ | |
| 51 | + public function log_client_error() { | |
| 52 | + $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; | |
| 53 | + if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) || ! current_user_can( 'manage_woocommerce' ) ) { | |
| 54 | + return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ], 403 ); | |
| 55 | + } | |
| 56 | + | |
| 57 | + // A render loop in one browser must not be able to fill the log. | |
| 58 | + $rate_key = 'yaymail_client_error_rate_' . get_current_user_id(); | |
| 59 | + $count = (int) get_transient( $rate_key ); | |
| 60 | + if ( $count >= 20 ) { | |
| 61 | + return wp_send_json_success( [ 'logged' => false ] ); | |
| 62 | + } | |
| 63 | + set_transient( $rate_key, $count + 1, MINUTE_IN_SECONDS ); | |
| 64 | + | |
| 65 | + $text = function ( $key, $max = 8000 ) { | |
| 66 | + $value = isset( $_POST[ $key ] ) ? sanitize_textarea_field( wp_unslash( $_POST[ $key ] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 67 | + return mb_substr( $value, 0, $max ); | |
| 68 | + }; | |
| 69 | + | |
| 70 | + $lines = [ | |
| 71 | + 'CLIENT ERROR [' . $text( 'source', 20 ) . ']: ' . $text( 'message', 1000 ), | |
| 72 | + 'Version: ' . $text( 'version', 20 ) . ' | Platform: ' . $text( 'platform', 40 ) . ' | Template: ' . $text( 'template', 200 ), | |
| 73 | + 'URL: ' . $text( 'url', 2000 ), | |
| 74 | + 'User agent: ' . $text( 'user_agent', 500 ), | |
| 75 | + ]; | |
| 76 | + foreach ( [ 'hint' => 'Hint', 'stack' => 'Stack', 'component_stack' => 'Component stack' ] as $key => $label ) { | |
| 77 | + $value = $text( $key ); | |
| 78 | + if ( '' !== $value ) { | |
| 79 | + $lines[] = $label . ':' . PHP_EOL . $value; | |
| 80 | + } | |
| 81 | + } | |
| 82 | + $message = implode( PHP_EOL, $lines ); | |
| 83 | + | |
| 84 | + if ( function_exists( 'wc_get_logger' ) ) { | |
| 85 | + wc_get_logger()->error( $message, [ 'source' => 'yaymail-client' ] ); | |
| 86 | + } else { | |
| 87 | + ( new Logger() )->log( $message ); | |
| 88 | + } | |
| 89 | + | |
| 90 | + wp_send_json_success( [ 'logged' => true ] ); | |
| 91 | + } | |
| 92 | + | |
| 41 | 93 | public function import_state() { |
| 42 | 94 | $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 43 | 95 | if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) { |
| 44 | 96 | return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] ); |
| @@ -60,9 +112,9 @@ | ||
| 60 | 112 | |
| 61 | 113 | $state_data = null; |
| 62 | 114 | for ( $i = 0; $i < $zip->numFiles; $i++ ) { |
| 63 | 115 | $filename = $zip->getNameIndex( $i ); |
| 64 | - // Matches both "yaymail_backup.json" (YayMail) and "email_builder_backup.json" | |
| 116 | + // Matches both "yaymail_backup.json" (YayMail) and "yaymagic_email_builder_backup.json" | |
| 65 | 117 | // (Email Builder) -- the zip always holds exactly this one entry, named per |
| 66 | 118 | // getBrandName() in backup/index.tsx. |
| 67 | 119 | if ( 1 === preg_match( '/_backup\.json$/', (string) $filename ) ) { |
| 68 | 120 | $state_data = $zip->getFromIndex( $i ); |
| @@ -246,9 +298,9 @@ | ||
| 246 | 298 | * requests don't reliably expose the calling screen via get_current_screen(). |
| 247 | 299 | */ |
| 248 | 300 | private function export_file_name_prefix() { |
| 249 | 301 | $platform = isset( $_POST['platform'] ) ? sanitize_text_field( wp_unslash( $_POST['platform'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 250 | - return 'email-builder' === $platform ? 'email_builder' : 'yaymail'; | |
| 302 | + return 'yaymagic-email-builder' === $platform ? 'yaymagic_email_builder' : 'yaymail'; | |
| 251 | 303 | } |
| 252 | 304 | |
| 253 | 305 | public function process_plugin_installer( $slug ) { |
| 254 | 306 | require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| @@ -356,9 +408,10 @@ | ||
| 356 | 408 | if ( empty( $email ) ) { |
| 357 | 409 | return wp_send_json_error( [ 'mess' => __( 'Can\'t find email', 'yaymail' ) ] ); |
| 358 | 410 | } |
| 359 | 411 | |
| 360 | - $template = new YayMailTemplate( $template_name ); | |
| 412 | + $variant = yaymail_sanitize_template_variant( isset( $_POST['variant'] ) ? sanitize_text_field( wp_unslash( $_POST['variant'] ) ) : '' ); | |
| 413 | + $template = new YayMailTemplate( $template_name, '', $variant ); | |
| 361 | 414 | |
| 362 | 415 | $render_data = []; |
| 363 | 416 | |
| 364 | 417 | if ( empty( $order_id ) || ( 'sample_order' === $order_id ) ) { |
| @@ -503,12 +556,13 @@ | ||
| 503 | 556 | try { |
| 504 | 557 | $template_name = isset( $_POST['template_name'] ) ? sanitize_text_field( wp_unslash( $_POST['template_name'] ) ) : ''; |
| 505 | 558 | $search_order_id = isset( $_POST['search_order_id'] ) ? sanitize_text_field( wp_unslash( $_POST['search_order_id'] ) ) : null; |
| 506 | 559 | $email_address = isset( $_POST['email_address'] ) ? sanitize_text_field( wp_unslash( $_POST['email_address'] ) ) : ''; |
| 560 | + $variant = yaymail_sanitize_template_variant( isset( $_POST['variant'] ) ? sanitize_text_field( wp_unslash( $_POST['variant'] ) ) : '' ); | |
| 507 | 561 | |
| 508 | - $email_preview_output = PreviewEmail\PreviewEmailWoo::email_preview_output( $search_order_id, $template_name, $email_address ); | |
| 562 | + $email_preview_output = PreviewEmail\PreviewEmailWoo::email_preview_output( $search_order_id, $template_name, $email_address, $variant ); | |
| 509 | 563 | |
| 510 | - $email_preview_output = apply_filters( 'yaymail_preview_email', $email_preview_output, $search_order_id, $template_name, $email_address ); | |
| 564 | + $email_preview_output = apply_filters( 'yaymail_preview_email', $email_preview_output, $search_order_id, $template_name, $email_address, $variant ); | |
| 511 | 565 | |
| 512 | 566 | $send_mail = false; |
| 513 | 567 | |
| 514 | 568 | if ( ! empty( $email_address ) && ! empty( $email_preview_output['html'] ) ) { |
| @@ -555,8 +609,13 @@ | ||
| 555 | 609 | 'key' => YayMailTemplate::META_KEYS['name'], |
| 556 | 610 | 'value' => $templates, |
| 557 | 611 | 'compare' => 'IN', |
| 558 | 612 | ], |
| 613 | + // Export the default design only; import keys on name and would collapse variants onto it. | |
| 614 | + [ | |
| 615 | + 'key' => YayMailTemplate::META_KEYS['variant'], | |
| 616 | + 'compare' => 'NOT EXISTS', | |
| 617 | + ], | |
| 559 | 618 | ], |
| 560 | 619 | ]; |
| 561 | 620 | $export_data = []; |
| 562 | 621 | $query = new \WP_Query( $default ); |
| @@ -802,8 +861,9 @@ | ||
| 802 | 861 | $settings_data = $setting_model->find_all(); |
| 803 | 862 | |
| 804 | 863 | $template_name = isset( $_POST['data']['template_name'] ) ? sanitize_text_field( wp_unslash( $_POST['data']['template_name'] ) ) : 'new_order'; |
| 805 | 864 | $order_id = isset( $_POST['data']['order_id'] ) ? sanitize_text_field( wp_unslash( $_POST['data']['order_id'] ) ) : 'sample_order'; |
| 865 | + $variant = yaymail_sanitize_template_variant( isset( $_POST['data']['variant'] ) ? sanitize_text_field( wp_unslash( $_POST['data']['variant'] ) ) : '' ); | |
| 806 | 866 | |
| 807 | 867 | $template_model = TemplateModel::get_instance(); |
| 808 | 868 | |
| 809 | 869 | $shortcodes_data = $template_model->get_shortcodes_by_template_name_and_order_id( $template_name, $order_id ); |
| @@ -809,14 +869,14 @@ | ||
| 809 | 869 | $shortcodes_data = $template_model->get_shortcodes_by_template_name_and_order_id( $template_name, $order_id ); |
| 810 | 870 | |
| 811 | 871 | $templates_data = apply_filters( 'yaymail_get_all_templates', $template_model->find_all() ); |
| 812 | 872 | |
| 813 | - $selected_template_data = $template_model->find_by_name( $template_name ); | |
| 873 | + $selected_template_data = $template_model->find_by_name( $template_name, '', $variant ); | |
| 814 | 874 | |
| 815 | 875 | $elements_data = TemplateModel::get_elements_for_template( $template_name ); |
| 816 | 876 | |
| 817 | 877 | $revision_model = RevisionModel::get_instance(); |
| 818 | - $revisions_data = $revision_model->get_by_template( $template_name ); | |
| 878 | + $revisions_data = $revision_model->get_by_template( $template_name, $variant ); | |
| 819 | 879 | |
| 820 | 880 | wp_send_json_success( |
| 821 | 881 | [ |
| 822 | 882 | 'settings_data' => $settings_data, |
| @@ -935,5 +995,5 @@ | ||
| 935 | 995 | yaymail_get_logger( $exception ); |
| 936 | 996 | wp_send_json_error( [ 'mess' => $exception->getMessage() ] ); |
| 937 | 997 | } |
| 938 | 998 | } |
| 939 | -} | |
| 999 | +} | |