PluginProbe
YayMail – WooCommerce Email Customizer / 2.1.4
YayMail – WooCommerce Email Customizer v2.1.4
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.4, at includes/Ajax.php

656 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 } if ( 'new_booking' == $template ) {
233 $subject = Helper::getNewBookingSubject( $wc_emails->emails[ $email ] );
234 } else {
235 $subject = $wc_emails->emails[ $email ]->get_subject();
236 }
237
238 return $subject;
239 }
240 }
241 return null;
242 }
243
244 public function saveTemplate() {
245 try {
246 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
247 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
248 wp_send_json_error( array( 'mess' => 'Nonce is invalid' ) );
249 } else {
250 if ( isset( $_POST['template'] ) ) {
251 // Helper::checkNonce();
252 $emailBackgroundColor = isset( $_POST['emailBackgroundColor'] ) ? sanitize_text_field( $_POST['emailBackgroundColor'] ) : 'rgb(236, 236, 236)';
253 $emailTextLinkColor = isset( $_POST['emailTextLinkColor'] ) ? sanitize_text_field( $_POST['emailTextLinkColor'] ) : '#96588a';
254 $titleShipping = isset( $_POST['titleShipping'] ) ? sanitize_text_field( $_POST['titleShipping'] ) : 'Shipping Address';
255 $titleBilling = isset( $_POST['titleBilling'] ) ? sanitize_text_field( $_POST['titleBilling'] ) : 'Billing Address';
256 $orderTitle = isset( $_POST['orderTitle'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['orderTitle'] ) ) : array();
257 $template = sanitize_text_field( $_POST['template'] );
258 $updateElement = new UpdateElement();
259 $setDefaultLogo = isset( $_POST['setDefaultLogo'] ) ? 'true' == sanitize_text_field( $_POST['setDefaultLogo'] ) ? true : false : false;
260 $setDefaultFooter = isset( $_POST['setDefaultFooter'] ) ? 'true' == sanitize_text_field( $_POST['setDefaultFooter'] ) ? true : false : false;
261 if ( isset( $_POST['emailContents'] ) ) {
262 $emailContents = filter_var_array( $_POST['emailContents'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
263 $emailContents = $updateElement->merge_new_props_to_elements( $emailContents );
264 } else {
265 $emailContents = array();
266 }
267 foreach ( $emailContents as $key => $value ) {
268 if ( 'TwoColumns' == $value['type'] || 'ThreeColumns' == $value['type'] || 'FourColumns' == $value['type'] ) {
269 if ( ! array_key_exists( 'column1', $emailContents[ $key ]['settingRow'] ) ) {
270 $emailContents[ $key ]['settingRow']['column1'] = array();
271 }
272 if ( ! array_key_exists( 'column2', $emailContents[ $key ]['settingRow'] ) ) {
273 $emailContents[ $key ]['settingRow']['column2'] = array();
274 }
275 if ( ( 'ThreeColumns' == $value['type'] || 'FourColumns' == $value['type'] ) && ! array_key_exists( 'column3', $emailContents[ $key ]['settingRow'] ) ) {
276 $emailContents[ $key ]['settingRow']['column3'] = array();
277 }
278 if ( 'FourColumns' == $value['type'] && ! array_key_exists( 'column4', $emailContents[ $key ]['settingRow'] ) ) {
279 $emailContents[ $key ]['settingRow']['column4'] = array();
280 }
281 }
282 }
283 if ( CustomPostType::postIDByTemplate( $template ) ) {
284 $postID = CustomPostType::postIDByTemplate( $template );
285 // write Log use to check error
286 // LogHelper::writeLogContent($htmlContent, 'html');
287 // LogHelper::writeLogContent($emailContents, 'text');
288
289 update_post_meta( $postID, '_yaymail_elements', $emailContents );
290 update_post_meta( $postID, '_email_backgroundColor_settings', $emailBackgroundColor );
291 update_post_meta( $postID, '_yaymail_email_textLinkColor_settings', $emailTextLinkColor );
292 update_post_meta( $postID, '_email_title_shipping', $titleShipping );
293 update_post_meta( $postID, '_email_title_billing', $titleBilling );
294 update_post_meta( $postID, '_yaymail_email_order_item_title', $orderTitle );
295 // Change default logo
296 $default_logo = array(
297 'set_default' => (bool) $setDefaultLogo,
298 );
299 if ( 'true' == $setDefaultLogo ) {
300 $posts = CustomPostType::getListPostTemplate();
301 foreach ( $emailContents as $key => $element ) {
302 if ( 'Logo' == $element['type'] ) {
303 $logoDefault = $element['settingRow'];
304 break;
305 }
306 }
307
308 if ( count( $posts ) > 0 && isset( $logoDefault ) ) {
309 foreach ( $posts as $post ) {
310 $yaymail_elements = get_post_meta( $post->ID, '_yaymail_elements', true );
311 foreach ( $yaymail_elements as $key => $element ) {
312 if ( 'Logo' == $element['type'] ) {
313 $yaymail_elements[ $key ]['settingRow'] = wp_parse_args( $logoDefault, $yaymail_elements[ $key ]['settingRow'] );
314 }
315 }
316 update_post_meta( $post->ID, '_yaymail_elements', $yaymail_elements );
317 }
318 }
319 }
320 update_option( 'yaymail_settings_default_logo', $default_logo );
321 // Change default footer
322 $default_footer = array(
323 'set_default' => (bool) $setDefaultFooter,
324 );
325 if ( 'true' == $setDefaultFooter ) {
326 $posts = CustomPostType::getListPostTemplate();
327 foreach ( $emailContents as $key => $element ) {
328 if ( 'ElementText' == $element['type'] && 'Footer' == $element['nameElement'] ) {
329 $footerDefault = $element['settingRow'];
330 break;
331 }
332 }
333
334 if ( count( $posts ) > 0 && isset( $footerDefault ) ) {
335 foreach ( $posts as $post ) {
336 $yaymail_elements = get_post_meta( $post->ID, '_yaymail_elements', true );
337 foreach ( $yaymail_elements as $key => $element ) {
338 if ( 'ElementText' == $element['type'] && 'Footer' == $element['nameElement'] ) {
339 $yaymail_elements[ $key ]['settingRow'] = wp_parse_args( $footerDefault, $yaymail_elements[ $key ]['settingRow'] );
340 }
341 }
342 update_post_meta( $post->ID, '_yaymail_elements', $yaymail_elements );
343 }
344 }
345 }
346 update_option( 'yaymail_settings_default_footer', $default_footer );
347
348 wp_send_json_success( array( 'mess' => __( 'Email has been saved.', 'yaymail' ) ) );
349 } else {
350 wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) );
351 }
352 }
353 wp_send_json_error( array( 'mess' => __( 'Error save data.', 'yaymail' ) ) );
354 }
355 } catch ( \Exception $ex ) {
356 LogHelper::getMessageException( $ex, true );
357 } catch ( \Error $ex ) {
358 LogHelper::getMessageException( $ex, true );
359 }
360
361 }
362 public function copyTemplate() {
363 try {
364 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
365 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
366 wp_send_json_error( array( 'mess' => 'Nonce is invalid' ) );
367 } else {
368 if ( isset( $_POST['copy_to'] ) && isset( $_POST['copy_from'] ) ) {
369 Helper::checkNonce();
370 $copyTo = sanitize_text_field( $_POST['copy_to'] );
371 $copyFrom = sanitize_text_field( $_POST['copy_from'] );
372 if ( CustomPostType::postIDByTemplate( $copyFrom ) ) {
373 $postID = CustomPostType::postIDByTemplate( $copyFrom );
374 $emailContentsFrom = get_post_meta( $postID, '_yaymail_elements', true );
375 $emailBackgroundColor = get_post_meta( $postID, '_email_backgroundColor_settings', true ) ? get_post_meta( $postID, '_email_backgroundColor_settings', true ) : 'rgb(236, 236, 236)';
376 $emailTextLinkColor = get_post_meta( $postID, '_yaymail_email_textLinkColor_settings', true ) ? get_post_meta( $postID, '_yaymail_email_textLinkColor_settings', true ) : '#96588a';
377 $titleShipping = isset( $_POST['titleShipping'] ) ? sanitize_text_field( $_POST['titleShipping'] ) : 'Shipping Address';
378 $titleBilling = isset( $_POST['titleBilling'] ) ? sanitize_text_field( $_POST['titleBilling'] ) : 'Billing Address';
379 $orderTitle = isset( $_POST['orderTitle'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['orderTitle'] ) ) : array();
380 if ( CustomPostType::postIDByTemplate( $copyTo ) ) {
381 $idTo = CustomPostType::postIDByTemplate( $copyTo );
382 update_post_meta( $idTo, '_yaymail_elements', $emailContentsFrom );
383 update_post_meta( $idTo, '_email_backgroundColor_settings', $emailBackgroundColor );
384 update_post_meta( $idTo, '_yaymail_email_textLinkColor_settings', $emailTextLinkColor );
385 update_post_meta( $idTo, '_email_title_shipping', $titleShipping );
386 update_post_meta( $idTo, '_email_title_billing', $titleBilling );
387 update_post_meta( $idTo, '_yaymail_email_order_item_title', $orderTitle );
388 wp_send_json_success(
389 array(
390 'mess' => __( 'Copied Template successfully.', 'yaymail' ),
391 'data' => $emailContentsFrom,
392 )
393 );
394 } else {
395 wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) );
396 }
397 } else {
398 wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) );
399 }
400 }
401 wp_send_json_error( array( 'mess' => __( 'Error save data.', 'yaymail' ) ) );
402 }
403 } catch ( \Exception $ex ) {
404 LogHelper::getMessageException( $ex, true );
405 } catch ( \Error $ex ) {
406 LogHelper::getMessageException( $ex, true );
407 }
408
409 }
410
411 public function resetTemplate() {
412 try {
413 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
414 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
415 wp_send_json_error( array( 'mess' => 'Nonce is invalid' ) );
416 } else {
417 if ( isset( $_POST['template'] ) ) {
418 // Helper::checkNonce();
419 $reset = sanitize_text_field( $_POST['template'] );
420 $templateEmail = \YayMail\Templates\Templates::getInstance();
421 $templates = $templateEmail::getList();
422 $orderTitle = array(
423 'order_title' => '',
424 'product_title' => 'Product',
425 'quantity_title' => 'Quantity',
426 'price_title' => 'Price',
427 'subtoltal_title' => 'Subtotal:',
428 'payment_method_title' => 'Payment method:',
429 'total_title' => 'Total:',
430 'subscript_id' => 'ID',
431 'subscript_start_date' => 'Start date',
432 'subscript_end_date' => 'End date',
433 'subscript_recurring_total' => 'Recurring total',
434 'subscript_subscription' => 'Subscription',
435 'subscript_price' => 'Price',
436 'subscript_last_order_date' => 'Last Order Date',
437 'subscript_end_of_prepaid_term' => 'End of Prepaid Term',
438 'subscript_date_suspended' => 'Date Suspended',
439 );
440 if ( 'all' == $reset ) {
441 foreach ( $templates as $key => $template ) {
442 if ( CustomPostType::postIDByTemplate( $key ) ) {
443 $postID = CustomPostType::postIDByTemplate( $key );
444 update_post_meta( $postID, '_yaymail_elements', json_decode( $template['elements'], true ) );
445 update_post_meta( $postID, '_email_backgroundColor_settings', 'rgb(236, 236, 236)' );
446 update_post_meta( $postID, '_yaymail_email_textLinkColor_settings', '#96588a' );
447 update_post_meta( $postID, '_email_title_shipping', __( 'Shipping Address', 'yaymail' ) );
448 update_post_meta( $postID, '_email_title_billing', __( 'Billing Address', 'yaymail' ) );
449 update_post_meta( $postID, '_yaymail_email_order_item_title', $orderTitle );
450 }
451 }
452
453 if ( get_option( 'yaymail_settings' ) ) {
454 $yaymail_settings = get_option( 'yaymail_settings' );
455 $yaymail_settings['container_width'] = '605px';
456 $yaymail_settings['direction_rtl'] = 'ltr';
457 update_option( 'yaymail_settings', $yaymail_settings );
458 }
459
460 wp_send_json_success( array( 'mess' => __( 'Template reset successfully.', 'yaymail' ) ) );
461 } else {
462 if ( CustomPostType::postIDByTemplate( $reset ) && isset( $templates[ $reset ] ) ) {
463 $postID = CustomPostType::postIDByTemplate( $reset );
464 update_post_meta( $postID, '_yaymail_elements', json_decode( $templates[ $reset ]['elements'], true ) );
465 update_post_meta( $postID, '_email_backgroundColor_settings', 'rgb(236, 236, 236)' );
466 update_post_meta( $postID, '_yaymail_email_textLinkColor_settings', '#96588a' );
467 update_post_meta( $postID, '_email_title_shipping', __( 'Shipping Address', 'yaymail' ) );
468 update_post_meta( $postID, '_email_title_billing', __( 'Billing Address', 'yaymail' ) );
469 update_post_meta( $postID, '_yaymail_email_order_item_title', $orderTitle );
470 wp_send_json_success( array( 'mess' => __( 'Template reset successfully.', 'yaymail' ) ) );
471 } else {
472 wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) );
473 }
474 }
475 }
476 wp_send_json_error( array( 'mess' => __( 'Error Reset Template!', 'yaymail' ) ) );
477 }
478 } catch ( \Exception $ex ) {
479 LogHelper::getMessageException( $ex, true );
480 } catch ( \Error $ex ) {
481 LogHelper::getMessageException( $ex, true );
482 }
483
484 }
485
486 public function importAllTemplate() {
487 try {
488 Helper::checkNonce();
489 if ( isset( $_FILES['file']['type'] ) ) {
490 if ( 'application/json' == $_FILES['file']['type'] ) {
491 if ( ! empty( $_FILES['file']['tmp_name'] ) ) {
492 $fileJson = sanitize_text_field( $_FILES['file']['tmp_name'] );
493 $data = file_get_contents( $fileJson );
494 $data = json_decode( $data, true );
495 $dataImports = $data['yaymailTemplateExport'];
496
497 $versionOld = $data['yaymail_version'];
498 $versionCurrent = YAYMAIL_VERSION;
499
500 /*
501 check key in settingRow whether or not it exists.
502 note: case when add setting row for element.
503 */
504 if ( $versionOld != $versionCurrent ) {
505 $element = new DefaultElement();
506 $defaultDataElements = $element->defaultDataElement;
507
508 foreach ( $defaultDataElements as $defaultelement ) {
509
510 foreach ( $dataImports as $keyTemplate => $templateImport ) {
511 foreach ( $templateImport['_yaymail_elements'] as $keyElem => $elemImport ) {
512 if ( $defaultelement['type'] == $elemImport['type'] ) {
513 /*
514 @@@ add key default for element
515 */
516 $keyEleDefaus = array();
517 $keyEleDefaus = array_diff_key( $defaultelement, $elemImport );
518 if ( count( $keyEleDefaus ) > 0 ) {
519 $dataImports[ $keyTemplate ]['_yaymail_elements'][ $keyElem ] = array_merge( $elemImport, $keyEleDefaus );
520 }
521
522 /*
523 add key default for setting row
524 note: when add a field in setting row
525 */
526 $propSettings = array();
527 $propSettings = array_diff_key( $defaultelement['settingRow'], $elemImport['settingRow'] );
528 $lenPropSettings = count( $propSettings );
529 if ( $lenPropSettings > 0 ) {
530 $result = array();
531 $result = array_merge( $elemImport['settingRow'], $propSettings );
532 $dataImports[ $keyTemplate ]['_yaymail_elements'][ $keyElem ]['settingRow'] = $result;
533 }
534
535 /*
536 remove Key not needed for setting row
537 note: when deleting a field in setting row
538 */
539 // $propSetNotNeeds = array();
540 // $propSetNotNeeds = array_diff_key($elemImport['settingRow'], $defaultelement['settingRow']);
541 // $lenpropSetNotNeeds = count($propSetNotNeeds);
542 // if ($lenpropSetNotNeeds > 0) {
543 // foreach ($propSetNotNeeds as $keyNotNeed => $propSetNotNeed) {
544 // unset($dataImports[$keyTemplate]['_yaymail_elements'][$keyElem]['settingRow'][$keyNotNeed]);
545 // }
546 // }
547
548 }
549 }
550 }
551 }
552 }
553 $flag = false;
554 if ( count( $dataImports ) > 0 ) {
555 foreach ( $dataImports as $key => $value ) {
556 if ( isset( $value['_yaymail_elements'] ) ) {
557 $template = $value['_yaymail_template'];
558 $template_language = $value['_yaymail_template_language'];
559 if ( CustomPostType::postIDByTemplateLanguage( $template, $template_language ) ) {
560 $pID = CustomPostType::postIDByTemplateLanguage( $template, $template_language );
561 update_post_meta( $pID, '_yaymail_elements', $value['_yaymail_elements'] );
562 } else {
563 $array = array(
564 'mess' => '',
565 'post_date' => current_time( 'Y-m-d H:i:s' ),
566 'post_type' => 'yaymail_template',
567 'post_status' => 'publish',
568 '_yaymail_template' => $template,
569 '_yaymail_elements' => $value['_yaymail_elements'],
570
571 );
572 if ( '' !== $value['_yaymail_template_language'] ){
573 $array['_yaymail_template_language'] = $value['_yaymail_template_language'];
574 }
575 $insert = CustomPostType::insert( $array );
576 }
577 $flag = true;
578 }
579 }
580 }
581 if ( ! $flag ) {
582 wp_send_json_error( array( 'mess' => __( 'Import Failed.', 'yaymail' ) ) );
583 }
584 wp_send_json_success( array( 'mess' => __( 'Imported successfully.', 'yaymail' ) ) );
585 } else {
586 wp_send_json_error( array( 'mess' => __( 'File not found.', 'yaymail' ) ) );
587 }
588 } else {
589 wp_send_json_error( array( 'mess' => __( 'File not correct format.', 'yaymail' ) ) );
590 }
591 }
592 wp_send_json_error( array( 'mess' => __( 'Please upload 1 file to import.', 'yaymail' ) ) );
593 } catch ( \Exception $ex ) {
594 LogHelper::getMessageException( $ex, true );
595 } catch ( \Error $ex ) {
596 LogHelper::getMessageException( $ex, true );
597 }
598
599 }
600 public function enableDisableTempalte() {
601 try {
602 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
603 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
604 wp_send_json_error( array( 'mess' => 'Nonce is invalid' ) );
605 } else {
606 if ( isset( $_POST['settings'] ) ) {
607 // Helper::checkNonce();
608 $settingDefault = CustomPostType::templateEnableDisable();
609 $listTemplates = ! empty( $settingDefault ) ? array_keys( $settingDefault ) : array();
610 $settingCurrent = array_map( 'sanitize_text_field', wp_unslash( $_POST['settings'] ) );
611
612 if ( ! empty( $listTemplates ) ) {
613 foreach ( $settingCurrent as $key => $value ) {
614 if ( in_array( $key, $listTemplates ) ) {
615 update_post_meta( $settingDefault[ $key ]['post_id'], '_yaymail_status', $value );
616 }
617 }
618 }
619 wp_send_json_success( array( 'mess' => __( 'Settings saved.', 'yaymail' ) ) );
620 }
621 wp_send_json_error( array( 'mess' => __( 'Settings Failed!.', 'yaymail' ) ) );
622 }
623 } catch ( \Exception $ex ) {
624 LogHelper::getMessageException( $ex, true );
625 } catch ( \Error $ex ) {
626 LogHelper::getMessageException( $ex, true );
627 }
628
629 }
630
631 public function generalSettings() {
632 try {
633 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
634 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
635 wp_send_json_error( array( 'mess' => 'Nonce is invalid' ) );
636 } else {
637 if ( isset( $_POST['settings'] ) ) {
638 $setting = array_map( 'sanitize_text_field', wp_unslash( $_POST['settings'] ) );
639 $yaymail_direction = $setting['direction_rtl'];
640 isset( $yaymail_direction ) ? update_option( 'yaymail_direction', $yaymail_direction ) : update_option( 'yaymail_direction', 'ltr' );
641 // Helper::checkNonce();
642 update_option( 'yaymail_settings', $setting );
643 wp_send_json_success( array( 'mess' => __( 'Settings saved.', 'yaymail' ) ) );
644 }
645 wp_send_json_error( array( 'mess' => __( 'Settings Failed!.', 'yaymail' ) ) );
646 }
647 } catch ( \Exception $ex ) {
648 LogHelper::getMessageException( $ex, true );
649 } catch ( \Error $ex ) {
650 LogHelper::getMessageException( $ex, true );
651 }
652
653 }
654
655 }
656