PluginProbe
YayMail – WooCommerce Email Customizer / 2.1.5
YayMail – WooCommerce Email Customizer v2.1.5
4.4.6 4.4.5 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 All 57 releases
yaymail / includes / Ajax.php

Ajax.php in YayMail – WooCommerce Email Customizer 2.1.5, at includes/Ajax.php

657 lines 29.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\Helper\Helper;
6 use YayMail\Helper\LogHelper;
7 use YayMail\MailBuilder\Shortcodes;
8 use YayMail\Page\Source\CustomPostType;
9 use YayMail\Page\Source\DefaultElement;
10 use YayMail\Page\Source\UpdateElement;
11 use YayMail\Templates\Templates;
12
13 defined( 'ABSPATH' ) || exit;
14
15 class Ajax {
16
17 protected static $instance = null;
18
19 public static function getInstance() {
20 if ( null == self::$instance ) {
21 self::$instance = new self();
22 self::$instance->doHooks();
23 }
24
25 return self::$instance;
26 }
27 private function doHooks() {
28 add_action( 'wp_ajax_yaymail_send_mail', array( $this, 'sendTestMail' ) );
29 add_action( 'wp_ajax_yaymail_parse_template', array( new Shortcodes(), 'templateParser' ) );
30
31 add_action( 'wp_ajax_yaymail_save_mail', array( $this, 'saveTemplate' ) );
32 add_action( 'wp_ajax_yaymail_copy_mail', array( $this, 'copyTemplate' ) );
33 add_action( 'wp_ajax_yaymail_reset_template', array( $this, 'resetTemplate' ) );
34 add_action( 'wp_ajax_yaymail_export_all_template', array( $this, 'exportAllTemplate' ) );
35 add_action( 'wp_ajax_yaymail_import_template', array( $this, 'importAllTemplate' ) );
36 add_action( 'wp_ajax_yaymail_enable_disable_template', array( $this, 'enableDisableTempalte' ) );
37 add_action( 'wp_ajax_yaymail_general_setting', array( $this, 'generalSettings' ) );
38 }
39
40 private function __construct() {}
41 // public function showMailError($wp_error)
42 // {
43 // wp_send_json_error(array('mess' => $wp_error));
44 // }
45 public function exportAllTemplate() {
46 try {
47 // 1. check nonce
48 Helper::checkNonce();
49 // 2. download
50 $template_export = CustomPostType::getTemplateExport();
51 $fileName = 'yaymail_all-customize-email-templates_' . gmdate( 'm-d-Y' ) . '.json';
52 header( 'Content-Type: application/json' );
53 header( 'Content-Disposition: attachment; filename="' . $fileName . '";' );
54 $response_object = array(
55 'result' => $template_export,
56 'fileName' => $fileName,
57 'mess' => __( 'Export successfully.', 'yaymail' ),
58 );
59 wp_send_json_success( $response_object );
60 } catch ( \Exception $ex ) {
61 LogHelper::getMessageException( $ex, true );
62 } catch ( \Error $ex ) {
63 LogHelper::getMessageException( $ex, true );
64 }
65
66 }
67
68 public static function getHtmlByElements( $postID, $args = array() ) {
69 $updateElement = new UpdateElement();
70 $yaymail_elements = get_post_meta( $postID, '_yaymail_elements', true );
71 $yaymail_elements = $updateElement->merge_new_props_to_elements( $yaymail_elements );
72 $yaymail_settings = get_option( 'yaymail_settings' );
73 $emailBackgroundColor = get_post_meta( $postID, '_email_backgroundColor_settings', true ) ? get_post_meta( $postID, '_email_backgroundColor_settings', true ) : '#ECECEC';
74 $general_attrs = array( 'tableWidth' => str_replace( 'px', '', $yaymail_settings['container_width'] ) );
75 $html = '<!DOCTYPE html>
76 <html lang="en">
77 <head>
78 <meta charset="UTF-8">
79 <meta http-equiv="X-UA-Compatible" content="IE=edge">
80 <meta name="viewport" content="width=device-width, initial-scale=1"/>
81 <style>
82 h1{ font-family:inherit;text-shadow:unset;text-align:inherit;}
83 h2,h3{ font-family:inherit;color:inherit;text-align:inherit;}
84 </style>
85 </head><body style="background:' . esc_attr( $emailBackgroundColor ) . '">';
86 $html .= '<table
87 border="0"
88 cellpadding="0"
89 cellspacing="0"
90 height="100%"
91 width="100%"
92 >';
93 foreach ( $yaymail_elements as $key => $element ) {
94 // add shortcode params
95 $reg_pattern = '/\[([a-z0-9A-Z_]+)\]/';
96 if ( isset( $element['settingRow']['content'] ) ) {
97 $content = $element['settingRow']['content'];
98 $contentTitle = isset( $element['settingRow']['contentTitle'] ) ? $element['settingRow']['contentTitle'] : '';
99
100 // Add $atts for content if has shortcode
101 preg_match_all( $reg_pattern, $content, $result );
102 if ( ! empty( $result[0] ) ) {
103 foreach ( $result[0] as $key => $shortcode ) {
104 $textcolor = isset( $element['settingRow']['textColor'] ) ? ' textcolor=' . $element['settingRow']['textColor'] : '';
105 $bordercolor = isset( $element['settingRow']['borderColor'] ) ? ' bordercolor=' . $element['settingRow']['borderColor'] : '';
106 $titlecolor = isset( $element['settingRow']['titleColor'] ) ? ' titlecolor=' . $element['settingRow']['titleColor'] : '';
107 $fontfamily = isset( $element['settingRow']['family'] ) ? ' fontfamily=' . str_replace( ' ', '', str_replace( array( '\'', '"' ), '', $element['settingRow']['family'] ) ) : '';
108 $newshortcode = substr( $shortcode, 0, -1 );
109 $newshortcode .= $textcolor . $bordercolor . $titlecolor . $fontfamily . ']';
110 $content = str_replace( $shortcode, $newshortcode, $content );
111 }
112 $element['settingRow']['content'] = $content;
113 }
114 // Add $atts for contentTitle if has shortcode
115 if ( $contentTitle ) {
116 preg_match_all( $reg_pattern, $contentTitle, $result );
117 if ( ! empty( $result[0] ) ) {
118 foreach ( $result[0] as $key => $shortcode ) {
119 $textcolor = isset( $element['settingRow']['textColor'] ) ? ' textcolor=' . $element['settingRow']['textColor'] : '';
120 $bordercolor = isset( $element['settingRow']['borderColor'] ) ? ' bordercolor=' . $element['settingRow']['borderColor'] : '';
121 $titlecolor = isset( $element['settingRow']['titleColor'] ) ? ' titlecolor=' . $element['settingRow']['titleColor'] : '';
122 $fontfamily = isset( $element['settingRow']['family'] ) ? ' fontfamily=' . str_replace( ' ', '', str_replace( array( '\'', '"' ), '', $element['settingRow']['family'] ) ) : '';
123 $newshortcode = substr( $shortcode, 0, -1 );
124 $newshortcode .= $textcolor . $bordercolor . $titlecolor . $fontfamily . ']';
125 $contentTitle = str_replace( $shortcode, $newshortcode, $contentTitle );
126 }
127 $element['settingRow']['contentTitle'] = $contentTitle;
128 }
129 }
130
131 // Add $atts for content of shipment tracking if has shortcode
132 if ( '[yaymail_order_meta:_wc_shipment_tracking_items]' === $content ) {
133 $shortcode = $content;
134 $textcolor = isset( $element['settingRow']['textColor'] ) ? ' textcolor=' . $element['settingRow']['textColor'] : '';
135 $bordercolor = isset( $element['settingRow']['borderColor'] ) ? ' bordercolor=' . $element['settingRow']['borderColor'] : '';
136 $titlecolor = isset( $element['settingRow']['titleColor'] ) ? ' titlecolor=' . $element['settingRow']['titleColor'] : '';
137 $fontfamily = isset( $element['settingRow']['family'] ) ? ' fontfamily=' . str_replace( ' ', '', str_replace( array( '\'', '"' ), '', $element['settingRow']['family'] ) ) : '';
138 $newshortcode = substr( $shortcode, 0, -1 );
139 $newshortcode .= $textcolor . $bordercolor . $titlecolor . $fontfamily . ']';
140 $content = str_replace( $shortcode, $newshortcode, $content );
141 $element['settingRow']['content'] = $content;
142 }
143 }
144 ob_start();
145 if ( isset( $element['settingRow']['arrConditionLogic'] ) && ! empty( $element['settingRow']['arrConditionLogic'] ) ) {
146 $conditional_Logic = apply_filters( 'yaymail_addon_for_conditional_logic', false, $args, $element['settingRow'] );
147 if ( $conditional_Logic ) {
148 do_action( 'Yaymail' . $element['type'], $args, $element['settingRow'], $general_attrs, $element['id'], $postID, $isInColumns = false );
149 }
150 } else {
151 do_action( 'Yaymail' . $element['type'], $args, $element['settingRow'], $general_attrs, $element['id'], $postID, $isInColumns = false );
152 }
153 $el_html = ob_get_clean();
154 if ( '' !== $el_html ) {
155 $html .= '<tr><td>';
156 $el_html .= '</tr></td>';
157 }
158 $html .= $el_html;
159 }
160 $html .= '</table></body></html>';
161 return $html;
162 }
163
164 // html output of mail must to map with html output in single-mail-template.php
165 public function sendTestMail() {
166 try {
167 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
168 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
169 wp_send_json_error( array( 'mess' => 'Nonce is invalid' ) );
170 } else {
171 if ( isset( $_POST['order_id'] ) && isset( $_POST['template'] ) && isset( $_POST['email_address'] ) ) {
172 // Helper::checkNonce();
173 $template = sanitize_text_field( $_POST['template'] );
174 $email_address = sanitize_email( $_POST['email_address'] );
175 // check email
176 if ( ! is_email( $email_address ) ) {
177 wp_send_json_error( array( 'mess' => __( 'Invalid email format!', 'yaymail' ) ) );
178 }
179 if ( CustomPostType::postIDByTemplate( $template ) ) {
180 update_user_meta( get_current_user_id(), 'yaymail_default_email_test', $email_address );
181 $customShortcode = new Shortcodes( $template, sanitize_text_field( $_POST['order_id'] ) );
182 if ( sanitize_text_field( $_POST['order_id'] ) !== 'sampleOrder' ) {
183 $order_id = intval( sanitize_text_field( $_POST['order_id'] ) );
184 $WC_order = new \WC_Order( $order_id );
185 }
186 $postID = CustomPostType::postIDByTemplate( $template );
187 $order_tn = 'mail-sent';
188 $customShortcode->setOrderId( $order_id, true );
189 $customShortcode->shortCodesOrderDefined();
190 if ( isset( $WC_order ) ) {
191 $html = self::getHtmlByElements( $postID, array( 'order' => $WC_order ) );
192 } else {
193 $html = self::getHtmlByElements( $postID, array( 'order' => 'SampleOrder' ) );
194 }
195
196 $html = html_entity_decode( $html, ENT_QUOTES, 'UTF-8' );
197 $headers = "Content-Type: text/html\r\n";
198 $sendMail = \WC_Emails::instance();
199 $subjectEmail = $this->getSubjectEmail( $sendMail, $template );
200 // $email_admin = get_bloginfo('admin_email');
201 if ( ! empty( $email_address ) ) {
202 $sendMailSucc = $sendMail->send( $email_address, $subjectEmail, $html, $headers, array() );
203 wp_send_json_success(
204 array(
205 'sendMailSucc' => $sendMailSucc,
206 'mess' => __(
207 'Email has been sent.',
208 'yaymail'
209 ),
210 )
211 );
212 }
213 } else {
214 wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) );
215 }
216 }
217 }
218 wp_send_json_error( array( 'mess' => __( 'Error send mail!', 'yaymail' ) ) );
219 } catch ( \Exception $ex ) {
220 LogHelper::getMessageException( $ex, true );
221 } catch ( \Error $ex ) {
222 LogHelper::getMessageException( $ex, true );
223 }
224
225 }
226
227 public function getSubjectEmail( $wc_emails, $template ) {
228 foreach ( $wc_emails->emails as $email => $item ) {
229 if ( $item->id == $template ) {
230 if ( 'customer_invoice' == $template ) {
231 $subject = Helper::getCustomerInvoiceSubject( $wc_emails->emails[ $email ] );
232 return $subject;
233 } if ( 'new_booking' == $template ) {
234 $subject = Helper::getNewBookingSubject( $wc_emails->emails[ $email ] );
235 return $subject;
236 } else {
237 $subject = $wc_emails->emails[ $email ]->get_subject();
238 return $subject;
239 }
240 }
241 }
242 return null;
243 }
244
245 public function saveTemplate() {
246 try {
247 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
248 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
249 wp_send_json_error( array( 'mess' => 'Nonce is invalid' ) );
250 } else {
251 if ( isset( $_POST['template'] ) ) {
252 // Helper::checkNonce();
253 $emailBackgroundColor = isset( $_POST['emailBackgroundColor'] ) ? sanitize_text_field( $_POST['emailBackgroundColor'] ) : 'rgb(236, 236, 236)';
254 $emailTextLinkColor = isset( $_POST['emailTextLinkColor'] ) ? sanitize_text_field( $_POST['emailTextLinkColor'] ) : '#96588a';
255 $titleShipping = isset( $_POST['titleShipping'] ) ? sanitize_text_field( $_POST['titleShipping'] ) : 'Shipping Address';
256 $titleBilling = isset( $_POST['titleBilling'] ) ? sanitize_text_field( $_POST['titleBilling'] ) : 'Billing Address';
257 $orderTitle = isset( $_POST['orderTitle'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['orderTitle'] ) ) : array();
258 $template = sanitize_text_field( $_POST['template'] );
259 $updateElement = new UpdateElement();
260 $setDefaultLogo = isset( $_POST['setDefaultLogo'] ) ? 'true' == sanitize_text_field( $_POST['setDefaultLogo'] ) ? true : false : false;
261 $setDefaultFooter = isset( $_POST['setDefaultFooter'] ) ? 'true' == sanitize_text_field( $_POST['setDefaultFooter'] ) ? true : false : false;
262 if ( isset( $_POST['emailContents'] ) ) {
263 $emailContents = filter_var_array( $_POST['emailContents'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
264 $emailContents = $updateElement->merge_new_props_to_elements( $emailContents );
265 } else {
266 $emailContents = array();
267 }
268 foreach ( $emailContents as $key => $value ) {
269 if ( 'TwoColumns' == $value['type'] || 'ThreeColumns' == $value['type'] || 'FourColumns' == $value['type'] ) {
270 if ( ! array_key_exists( 'column1', $emailContents[ $key ]['settingRow'] ) ) {
271 $emailContents[ $key ]['settingRow']['column1'] = array();
272 }
273 if ( ! array_key_exists( 'column2', $emailContents[ $key ]['settingRow'] ) ) {
274 $emailContents[ $key ]['settingRow']['column2'] = array();
275 }
276 if ( ( 'ThreeColumns' == $value['type'] || 'FourColumns' == $value['type'] ) && ! array_key_exists( 'column3', $emailContents[ $key ]['settingRow'] ) ) {
277 $emailContents[ $key ]['settingRow']['column3'] = array();
278 }
279 if ( 'FourColumns' == $value['type'] && ! array_key_exists( 'column4', $emailContents[ $key ]['settingRow'] ) ) {
280 $emailContents[ $key ]['settingRow']['column4'] = array();
281 }
282 }
283 }
284 if ( CustomPostType::postIDByTemplate( $template ) ) {
285 $postID = CustomPostType::postIDByTemplate( $template );
286 // write Log use to check error
287 // LogHelper::writeLogContent($htmlContent, 'html');
288 // LogHelper::writeLogContent($emailContents, 'text');
289
290 update_post_meta( $postID, '_yaymail_elements', $emailContents );
291 update_post_meta( $postID, '_email_backgroundColor_settings', $emailBackgroundColor );
292 update_post_meta( $postID, '_yaymail_email_textLinkColor_settings', $emailTextLinkColor );
293 update_post_meta( $postID, '_email_title_shipping', $titleShipping );
294 update_post_meta( $postID, '_email_title_billing', $titleBilling );
295 update_post_meta( $postID, '_yaymail_email_order_item_title', $orderTitle );
296 // Change default logo
297 $default_logo = array(
298 'set_default' => (bool) $setDefaultLogo,
299 );
300 if ( 'true' == $setDefaultLogo ) {
301 $posts = CustomPostType::getListPostTemplate();
302 foreach ( $emailContents as $key => $element ) {
303 if ( 'Logo' == $element['type'] ) {
304 $logoDefault = $element['settingRow'];
305 break;
306 }
307 }
308
309 if ( count( $posts ) > 0 && isset( $logoDefault ) ) {
310 foreach ( $posts as $post ) {
311 $yaymail_elements = get_post_meta( $post->ID, '_yaymail_elements', true );
312 foreach ( $yaymail_elements as $key => $element ) {
313 if ( 'Logo' == $element['type'] ) {
314 $yaymail_elements[ $key ]['settingRow'] = wp_parse_args( $logoDefault, $yaymail_elements[ $key ]['settingRow'] );
315 }
316 }
317 update_post_meta( $post->ID, '_yaymail_elements', $yaymail_elements );
318 }
319 }
320 }
321 update_option( 'yaymail_settings_default_logo', $default_logo );
322 // Change default footer
323 $default_footer = array(
324 'set_default' => (bool) $setDefaultFooter,
325 );
326 if ( 'true' == $setDefaultFooter ) {
327 $posts = CustomPostType::getListPostTemplate();
328 foreach ( $emailContents as $key => $element ) {
329 if ( 'ElementText' == $element['type'] && 'Footer' == $element['nameElement'] ) {
330 $footerDefault = $element['settingRow'];
331 break;
332 }
333 }
334
335 if ( count( $posts ) > 0 && isset( $footerDefault ) ) {
336 foreach ( $posts as $post ) {
337 $yaymail_elements = get_post_meta( $post->ID, '_yaymail_elements', true );
338 foreach ( $yaymail_elements as $key => $element ) {
339 if ( 'ElementText' == $element['type'] && 'Footer' == $element['nameElement'] ) {
340 $yaymail_elements[ $key ]['settingRow'] = wp_parse_args( $footerDefault, $yaymail_elements[ $key ]['settingRow'] );
341 }
342 }
343 update_post_meta( $post->ID, '_yaymail_elements', $yaymail_elements );
344 }
345 }
346 }
347 update_option( 'yaymail_settings_default_footer', $default_footer );
348
349 wp_send_json_success( array( 'mess' => __( 'Email has been saved.', 'yaymail' ) ) );
350 } else {
351 wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) );
352 }
353 }
354 wp_send_json_error( array( 'mess' => __( 'Error save data.', 'yaymail' ) ) );
355 }
356 } catch ( \Exception $ex ) {
357 LogHelper::getMessageException( $ex, true );
358 } catch ( \Error $ex ) {
359 LogHelper::getMessageException( $ex, true );
360 }
361
362 }
363 public function copyTemplate() {
364 try {
365 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
366 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
367 wp_send_json_error( array( 'mess' => 'Nonce is invalid' ) );
368 } else {
369 if ( isset( $_POST['copy_to'] ) && isset( $_POST['copy_from'] ) ) {
370 Helper::checkNonce();
371 $copyTo = sanitize_text_field( $_POST['copy_to'] );
372 $copyFrom = sanitize_text_field( $_POST['copy_from'] );
373 if ( CustomPostType::postIDByTemplate( $copyFrom ) ) {
374 $postID = CustomPostType::postIDByTemplate( $copyFrom );
375 $emailContentsFrom = get_post_meta( $postID, '_yaymail_elements', true );
376 $emailBackgroundColor = get_post_meta( $postID, '_email_backgroundColor_settings', true ) ? get_post_meta( $postID, '_email_backgroundColor_settings', true ) : 'rgb(236, 236, 236)';
377 $emailTextLinkColor = get_post_meta( $postID, '_yaymail_email_textLinkColor_settings', true ) ? get_post_meta( $postID, '_yaymail_email_textLinkColor_settings', true ) : '#96588a';
378 $titleShipping = isset( $_POST['titleShipping'] ) ? sanitize_text_field( $_POST['titleShipping'] ) : 'Shipping Address';
379 $titleBilling = isset( $_POST['titleBilling'] ) ? sanitize_text_field( $_POST['titleBilling'] ) : 'Billing Address';
380 $orderTitle = isset( $_POST['orderTitle'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['orderTitle'] ) ) : array();
381 if ( CustomPostType::postIDByTemplate( $copyTo ) ) {
382 $idTo = CustomPostType::postIDByTemplate( $copyTo );
383 update_post_meta( $idTo, '_yaymail_elements', $emailContentsFrom );
384 update_post_meta( $idTo, '_email_backgroundColor_settings', $emailBackgroundColor );
385 update_post_meta( $idTo, '_yaymail_email_textLinkColor_settings', $emailTextLinkColor );
386 update_post_meta( $idTo, '_email_title_shipping', $titleShipping );
387 update_post_meta( $idTo, '_email_title_billing', $titleBilling );
388 update_post_meta( $idTo, '_yaymail_email_order_item_title', $orderTitle );
389 wp_send_json_success(
390 array(
391 'mess' => __( 'Copied Template successfully.', 'yaymail' ),
392 'data' => $emailContentsFrom,
393 )
394 );
395 } else {
396 wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) );
397 }
398 } else {
399 wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) );
400 }
401 }
402 wp_send_json_error( array( 'mess' => __( 'Error save data.', 'yaymail' ) ) );
403 }
404 } catch ( \Exception $ex ) {
405 LogHelper::getMessageException( $ex, true );
406 } catch ( \Error $ex ) {
407 LogHelper::getMessageException( $ex, true );
408 }
409
410 }
411
412 public function resetTemplate() {
413 try {
414 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
415 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
416 wp_send_json_error( array( 'mess' => 'Nonce is invalid' ) );
417 } else {
418 if ( isset( $_POST['template'] ) ) {
419 // Helper::checkNonce();
420 $reset = sanitize_text_field( $_POST['template'] );
421 $templateEmail = \YayMail\Templates\Templates::getInstance();
422 $templates = $templateEmail::getList();
423 $orderTitle = array(
424 'order_title' => '',
425 'product_title' => 'Product',
426 'quantity_title' => 'Quantity',
427 'price_title' => 'Price',
428 'subtoltal_title' => 'Subtotal:',
429 'payment_method_title' => 'Payment method:',
430 'total_title' => 'Total:',
431 'subscript_id' => 'ID',
432 'subscript_start_date' => 'Start date',
433 'subscript_end_date' => 'End date',
434 'subscript_recurring_total' => 'Recurring total',
435 'subscript_subscription' => 'Subscription',
436 'subscript_price' => 'Price',
437 'subscript_last_order_date' => 'Last Order Date',
438 'subscript_end_of_prepaid_term' => 'End of Prepaid Term',
439 'subscript_date_suspended' => 'Date Suspended',
440 );
441 if ( 'all' == $reset ) {
442 foreach ( $templates as $key => $template ) {
443 if ( CustomPostType::postIDByTemplate( $key ) ) {
444 $postID = CustomPostType::postIDByTemplate( $key );
445 update_post_meta( $postID, '_yaymail_elements', json_decode( $template['elements'], true ) );
446 update_post_meta( $postID, '_email_backgroundColor_settings', 'rgb(236, 236, 236)' );
447 update_post_meta( $postID, '_yaymail_email_textLinkColor_settings', '#96588a' );
448 update_post_meta( $postID, '_email_title_shipping', __( 'Shipping Address', 'yaymail' ) );
449 update_post_meta( $postID, '_email_title_billing', __( 'Billing Address', 'yaymail' ) );
450 update_post_meta( $postID, '_yaymail_email_order_item_title', $orderTitle );
451 }
452 }
453
454 if ( get_option( 'yaymail_settings' ) ) {
455 $yaymail_settings = get_option( 'yaymail_settings' );
456 $yaymail_settings['container_width'] = '605px';
457 $yaymail_settings['direction_rtl'] = 'ltr';
458 update_option( 'yaymail_settings', $yaymail_settings );
459 }
460
461 wp_send_json_success( array( 'mess' => __( 'Template reset successfully.', 'yaymail' ) ) );
462 } else {
463 if ( CustomPostType::postIDByTemplate( $reset ) && isset( $templates[ $reset ] ) ) {
464 $postID = CustomPostType::postIDByTemplate( $reset );
465 update_post_meta( $postID, '_yaymail_elements', json_decode( $templates[ $reset ]['elements'], true ) );
466 update_post_meta( $postID, '_email_backgroundColor_settings', 'rgb(236, 236, 236)' );
467 update_post_meta( $postID, '_yaymail_email_textLinkColor_settings', '#96588a' );
468 update_post_meta( $postID, '_email_title_shipping', __( 'Shipping Address', 'yaymail' ) );
469 update_post_meta( $postID, '_email_title_billing', __( 'Billing Address', 'yaymail' ) );
470 update_post_meta( $postID, '_yaymail_email_order_item_title', $orderTitle );
471 wp_send_json_success( array( 'mess' => __( 'Template reset successfully.', 'yaymail' ) ) );
472 } else {
473 wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) );
474 }
475 }
476 }
477 wp_send_json_error( array( 'mess' => __( 'Error Reset Template!', 'yaymail' ) ) );
478 }
479 } catch ( \Exception $ex ) {
480 LogHelper::getMessageException( $ex, true );
481 } catch ( \Error $ex ) {
482 LogHelper::getMessageException( $ex, true );
483 }
484
485 }
486
487 public function importAllTemplate() {
488 try {
489 Helper::checkNonce();
490 if ( isset( $_FILES['file']['type'] ) ) {
491 if ( 'application/json' == $_FILES['file']['type'] ) {
492 if ( ! empty( $_FILES['file']['tmp_name'] ) ) {
493 $fileJson = sanitize_text_field( $_FILES['file']['tmp_name'] );
494 $data = file_get_contents( $fileJson );
495 $data = json_decode( $data, true );
496 $dataImports = $data['yaymailTemplateExport'];
497
498 $versionOld = $data['yaymail_version'];
499 $versionCurrent = YAYMAIL_VERSION;
500
501 /*
502 check key in settingRow whether or not it exists.
503 note: case when add setting row for element.
504 */
505 if ( $versionOld != $versionCurrent ) {
506 $element = new DefaultElement();
507 $defaultDataElements = $element->defaultDataElement;
508
509 foreach ( $defaultDataElements as $defaultelement ) {
510
511 foreach ( $dataImports as $keyTemplate => $templateImport ) {
512 foreach ( $templateImport['_yaymail_elements'] as $keyElem => $elemImport ) {
513 if ( $defaultelement['type'] == $elemImport['type'] ) {
514 /*
515 @@@ add key default for element
516 */
517 $keyEleDefaus = array();
518 $keyEleDefaus = array_diff_key( $defaultelement, $elemImport );
519 if ( count( $keyEleDefaus ) > 0 ) {
520 $dataImports[ $keyTemplate ]['_yaymail_elements'][ $keyElem ] = array_merge( $elemImport, $keyEleDefaus );
521 }
522
523 /*
524 add key default for setting row
525 note: when add a field in setting row
526 */
527 $propSettings = array();
528 $propSettings = array_diff_key( $defaultelement['settingRow'], $elemImport['settingRow'] );
529 $lenPropSettings = count( $propSettings );
530 if ( $lenPropSettings > 0 ) {
531 $result = array();
532 $result = array_merge( $elemImport['settingRow'], $propSettings );
533 $dataImports[ $keyTemplate ]['_yaymail_elements'][ $keyElem ]['settingRow'] = $result;
534 }
535
536 /*
537 remove Key not needed for setting row
538 note: when deleting a field in setting row
539 */
540 // $propSetNotNeeds = array();
541 // $propSetNotNeeds = array_diff_key($elemImport['settingRow'], $defaultelement['settingRow']);
542 // $lenpropSetNotNeeds = count($propSetNotNeeds);
543 // if ($lenpropSetNotNeeds > 0) {
544 // foreach ($propSetNotNeeds as $keyNotNeed => $propSetNotNeed) {
545 // unset($dataImports[$keyTemplate]['_yaymail_elements'][$keyElem]['settingRow'][$keyNotNeed]);
546 // }
547 // }
548
549 }
550 }
551 }
552 }
553 }
554 $flag = false;
555 if ( count( $dataImports ) > 0 ) {
556 foreach ( $dataImports as $key => $value ) {
557 if ( isset( $value['_yaymail_elements'] ) ) {
558 $template = $value['_yaymail_template'];
559 $template_language = $value['_yaymail_template_language'];
560 if ( CustomPostType::postIDByTemplateLanguage( $template, $template_language ) ) {
561 $pID = CustomPostType::postIDByTemplateLanguage( $template, $template_language );
562 update_post_meta( $pID, '_yaymail_elements', $value['_yaymail_elements'] );
563 } else {
564 $array = array(
565 'mess' => '',
566 'post_date' => current_time( 'Y-m-d H:i:s' ),
567 'post_type' => 'yaymail_template',
568 'post_status' => 'publish',
569 '_yaymail_template' => $template,
570 '_yaymail_elements' => $value['_yaymail_elements'],
571
572 );
573 if ( '' !== $value['_yaymail_template_language'] ){
574 $array['_yaymail_template_language'] = $value['_yaymail_template_language'];
575 }
576 $insert = CustomPostType::insert( $array );
577 }
578 $flag = true;
579 }
580 }
581 }
582 if ( ! $flag ) {
583 wp_send_json_error( array( 'mess' => __( 'Import Failed.', 'yaymail' ) ) );
584 }
585 wp_send_json_success( array( 'mess' => __( 'Imported successfully.', 'yaymail' ) ) );
586 } else {
587 wp_send_json_error( array( 'mess' => __( 'File not found.', 'yaymail' ) ) );
588 }
589 } else {
590 wp_send_json_error( array( 'mess' => __( 'File not correct format.', 'yaymail' ) ) );
591 }
592 }
593 wp_send_json_error( array( 'mess' => __( 'Please upload 1 file to import.', 'yaymail' ) ) );
594 } catch ( \Exception $ex ) {
595 LogHelper::getMessageException( $ex, true );
596 } catch ( \Error $ex ) {
597 LogHelper::getMessageException( $ex, true );
598 }
599
600 }
601 public function enableDisableTempalte() {
602 try {
603 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
604 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
605 wp_send_json_error( array( 'mess' => 'Nonce is invalid' ) );
606 } else {
607 if ( isset( $_POST['settings'] ) ) {
608 // Helper::checkNonce();
609 $settingDefault = CustomPostType::templateEnableDisable();
610 $listTemplates = ! empty( $settingDefault ) ? array_keys( $settingDefault ) : array();
611 $settingCurrent = array_map( 'sanitize_text_field', wp_unslash( $_POST['settings'] ) );
612
613 if ( ! empty( $listTemplates ) ) {
614 foreach ( $settingCurrent as $key => $value ) {
615 if ( in_array( $key, $listTemplates ) ) {
616 update_post_meta( $settingDefault[ $key ]['post_id'], '_yaymail_status', $value );
617 }
618 }
619 }
620 wp_send_json_success( array( 'mess' => __( 'Settings saved.', 'yaymail' ) ) );
621 }
622 wp_send_json_error( array( 'mess' => __( 'Settings Failed!.', 'yaymail' ) ) );
623 }
624 } catch ( \Exception $ex ) {
625 LogHelper::getMessageException( $ex, true );
626 } catch ( \Error $ex ) {
627 LogHelper::getMessageException( $ex, true );
628 }
629
630 }
631
632 public function generalSettings() {
633 try {
634 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
635 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
636 wp_send_json_error( array( 'mess' => 'Nonce is invalid' ) );
637 } else {
638 if ( isset( $_POST['settings'] ) ) {
639 $setting = array_map( 'sanitize_text_field', wp_unslash( $_POST['settings'] ) );
640 $yaymail_direction = $setting['direction_rtl'];
641 isset( $yaymail_direction ) ? update_option( 'yaymail_direction', $yaymail_direction ) : update_option( 'yaymail_direction', 'ltr' );
642 // Helper::checkNonce();
643 update_option( 'yaymail_settings', $setting );
644 wp_send_json_success( array( 'mess' => __( 'Settings saved.', 'yaymail' ) ) );
645 }
646 wp_send_json_error( array( 'mess' => __( 'Settings Failed!.', 'yaymail' ) ) );
647 }
648 } catch ( \Exception $ex ) {
649 LogHelper::getMessageException( $ex, true );
650 } catch ( \Error $ex ) {
651 LogHelper::getMessageException( $ex, true );
652 }
653
654 }
655
656 }
657