PluginProbe
YayMail – WooCommerce Email Customizer / 3.2.6
YayMail – WooCommerce Email Customizer v3.2.6
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 3.2.6, at includes/Ajax.php

803 lines 35.2 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_install_plugin', array( $this, 'ajax_install_plugin' ) );
30 add_action( 'wp_ajax_yaymail_save_mail', array( $this, 'saveTemplate' ) );
31 add_action( 'wp_ajax_yaymail_parse_template', array( new Shortcodes(), 'templateParser' ) );
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_review', array( $this, 'reviewYayMail' ) );
35 add_action( 'wp_ajax_yaymail_export_all_template', array( $this, 'exportAllTemplate' ) );
36 add_action( 'wp_ajax_yaymail_import_template', array( $this, 'importAllTemplate' ) );
37 add_action( 'wp_ajax_yaymail_enable_disable_template', array( $this, 'enableDisableTempalte' ) );
38 add_action( 'wp_ajax_yaymail_general_setting', array( $this, 'generalSettings' ) );
39 }
40
41 private function __construct() {}
42
43 public function exportAllTemplate() {
44 try {
45 // 1. check nonce
46 Helper::checkNonce();
47 // 2. download
48 $template_export = CustomPostType::getTemplateExport();
49 $fileName = 'yaymail_all-customize-email-templates_' . gmdate( 'm-d-Y' ) . '.json';
50 header( 'Content-Type: application/json' );
51 header( 'Content-Disposition: attachment; filename="' . $fileName . '";' );
52 $response_object = array(
53 'result' => $template_export,
54 'fileName' => $fileName,
55 'mess' => __( 'Export successfully.', 'yaymail' ),
56 );
57 wp_send_json_success( $response_object );
58 } catch ( \Exception $ex ) {
59 LogHelper::getMessageException( $ex, true );
60 } catch ( \Error $ex ) {
61 LogHelper::getMessageException( $ex, true );
62 }
63
64 }
65
66 public static function getHtmlByElements( $postID, $args = array() ) {
67 $updateElement = new UpdateElement();
68 $yaymail_elements = get_post_meta( $postID, '_yaymail_elements', true );
69 $yaymail_elements = $updateElement->merge_new_props_to_elements( $yaymail_elements );
70 $yaymail_settings = get_option( 'yaymail_settings' );
71 $emailBackgroundColor = get_post_meta( $postID, '_email_backgroundColor_settings', true ) ? get_post_meta( $postID, '_email_backgroundColor_settings', true ) : '#ECECEC';
72 $general_attrs = array( 'tableWidth' => str_replace( 'px', '', $yaymail_settings['container_width'] ) );
73 $yaymail_template = get_post_meta( $postID, '_yaymail_template', true );
74 $html = '<!DOCTYPE html>
75 <html lang="en">
76 <head>
77 <meta charset="UTF-8">
78 <meta http-equiv="X-UA-Compatible" content="IE=edge">
79 <meta name="viewport" content="width=device-width, initial-scale=1"/>
80 <style>
81 h1{ font-family:inherit;text-shadow:unset;text-align:inherit;}
82 h2,h3{ font-family:inherit;color:inherit;text-align:inherit;}
83 </style>
84 </head><body style="background:' . esc_attr( $emailBackgroundColor ) . '">';
85 $html .= '<table
86 border="0"
87 cellpadding="0"
88 cellspacing="0"
89 height="100%"
90 width="100%"
91 class="' . esc_attr( 'yaymail-template-' . $yaymail_template ) . '"
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'] ) ) {
146 if ( ! empty( $element['settingRow']['arrConditionLogic'] ) ) {
147 $conditional_Logic = apply_filters( 'yaymail_addon_for_conditional_logic', false, $args, $element['settingRow'] );
148 if ( $conditional_Logic ) {
149 do_action( 'Yaymail' . $element['type'], $args, $element['settingRow'], $general_attrs, $element['id'], $postID, $isInColumns = false );
150 }
151 } else {
152 if ( 'OneColumn' === $element['type'] || 'TwoColumns' === $element['type'] || 'ThreeColumns' === $element['type'] || 'FourColumns' === $element['type'] ) {
153 for ( $column = 1; $column <= 4; $column++ ) {
154 if ( isset( $element['settingRow'][ 'column' . $column ] ) ) {
155 foreach ( $element['settingRow'][ 'column' . $column ] as $column_key => $column_element ) {
156 if ( isset( $column_element['settingRow']['arrConditionLogic'] ) && ! empty( $column_element['settingRow']['arrConditionLogic'] ) ) {
157 $conditional_Logic = apply_filters( 'yaymail_addon_for_conditional_logic', false, $args, $column_element['settingRow'] );
158 if ( ! $conditional_Logic ) {
159 unset( $element['settingRow'][ 'column' . $column ][ $column_key ] );
160 }
161 }
162 }
163 }
164 }
165 }
166 do_action( 'Yaymail' . $element['type'], $args, $element['settingRow'], $general_attrs, $element['id'], $postID, $isInColumns = false );
167 }
168 } else {
169 do_action( 'Yaymail' . $element['type'], $args, $element['settingRow'], $general_attrs, $element['id'], $postID, $isInColumns = false );
170 }
171 $el_html = ob_get_clean();
172 if ( '' !== $el_html ) {
173 $html .= '<tr><td>';
174 $el_html .= '</tr></td>';
175 }
176 $html .= $el_html;
177 }
178 $html .= '</table></body></html>';
179 return $html;
180 }
181 public function ajax_install_plugin() {
182 try {
183 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
184 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
185 wp_send_json_error( array( 'mess' => __( 'Nonce is invalid', 'yaymail' ) ) );
186 } else {
187 $installed = $this->pluginInstaller( 'yaysmtp' );
188 if ( false === $installed ) {
189 wp_send_json_error( array( 'message' => $installed ) );
190 }
191
192 try {
193 $result = activate_plugin( 'yaysmtp/yay-smtp.php' );
194
195 if ( is_wp_error( $result ) ) {
196 throw new \Exception( $result->get_error_message() );
197 }
198 wp_send_json_success(
199 array(
200 'sendMailSucc' => $result,
201 'mess' => __(
202 'Plugin installation successful.',
203 'yaymail'
204 ),
205 )
206 );
207 } catch ( \Exception $e ) {
208 throw new \Exception( $e->getMessage() );
209 }
210 }
211 } catch ( \Exception $ex ) {
212 LogHelper::getMessageException( $ex, true );
213 } catch ( \Error $ex ) {
214 LogHelper::getMessageException( $ex, true );
215 }
216 }
217 public function pluginInstaller( $slug ) {
218 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
219 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
220 require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
221 require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
222
223 $api = plugins_api(
224 'plugin_information',
225 array(
226 'slug' => $slug,
227 'fields' => array(
228 'short_description' => false,
229 'sections' => false,
230 'requires' => false,
231 'rating' => false,
232 'ratings' => false,
233 'downloaded' => false,
234 'last_updated' => false,
235 'added' => false,
236 'tags' => false,
237 'compatibility' => false,
238 'homepage' => false,
239 'donate_link' => false,
240 ),
241 )
242 );
243 $skin = new \WP_Ajax_Upgrader_Skin();
244 $upgrader = new \Plugin_Upgrader( $skin );
245 try {
246 $result = $upgrader->install( $api->download_link );
247
248 if ( is_wp_error( $result ) ) {
249 throw new \Exception( $result->get_error_message() );
250 }
251
252 return true;
253 } catch ( \Exception $e ) {
254 throw new \Exception( $e->getMessage() );
255 }
256
257 return false;
258 }
259 // html output of mail must to map with html output in single-mail-template.php
260 public function sendTestMail() {
261 try {
262 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
263 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
264 wp_send_json_error( array( 'mess' => __( 'Nonce is invalid', 'yaymail' ) ) );
265 } else {
266 if ( isset( $_POST['order_id'] ) && isset( $_POST['template'] ) && isset( $_POST['email_address'] ) ) {
267 // Helper::checkNonce();
268 $template = sanitize_text_field( $_POST['template'] );
269 $email_address = sanitize_email( $_POST['email_address'] );
270 // check email
271 if ( ! is_email( $email_address ) ) {
272 wp_send_json_error( array( 'mess' => __( 'Invalid email format!', 'yaymail' ) ) );
273 }
274 if ( CustomPostType::postIDByTemplate( $template ) ) {
275 update_user_meta( get_current_user_id(), 'yaymail_default_email_test', $email_address );
276 $customShortcode = new Shortcodes( $template, sanitize_text_field( $_POST['order_id'] ), false );
277 if ( sanitize_text_field( $_POST['order_id'] ) !== 'sampleOrder' ) {
278 $order_id = intval( sanitize_text_field( $_POST['order_id'] ) );
279 $WC_order = new \WC_Order( $order_id );
280 }
281 $postID = CustomPostType::postIDByTemplate( $template );
282
283 if ( in_array( $template, array( 'new_order', 'cancelled_order', 'failed_order' ) ) ) {
284 $customShortcode->setOrderId( $order_id, true );
285 } else {
286 $customShortcode->setOrderId( $order_id, false );
287 }
288
289 $customShortcode->shortCodesOrderDefined();
290 if ( isset( $WC_order ) ) {
291 $html = self::getHtmlByElements( $postID, array( 'order' => $WC_order ) );
292 } else {
293 $html = self::getHtmlByElements( $postID, array( 'order' => 'SampleOrder' ) );
294 }
295
296 $html = html_entity_decode( $html, ENT_QUOTES, 'UTF-8' );
297 $headers = "Content-Type: text/html\r\n";
298 $sendMail = \WC_Emails::instance();
299 $subjectEmail = $this->getSubjectEmail( $sendMail, $template );
300 // $email_admin = get_bloginfo('admin_email');
301 if ( ! empty( $email_address ) ) {
302 $sendMailSucc = $sendMail->send( $email_address, $subjectEmail, $html, $headers, array() );
303 wp_send_json_success(
304 array(
305 'sendMailSucc' => $sendMailSucc,
306 'mess' => __(
307 'Email has been sent.',
308 'yaymail'
309 ),
310 )
311 );
312 }
313 } else {
314 wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) );
315 }
316 }
317 }
318 wp_send_json_error( array( 'mess' => __( 'Error send mail!', 'yaymail' ) ) );
319 } catch ( \Exception $ex ) {
320 LogHelper::getMessageException( $ex, true );
321 } catch ( \Error $ex ) {
322 LogHelper::getMessageException( $ex, true );
323 }
324
325 }
326
327 public function getSubjectEmail( $wc_emails, $template ) {
328 $subject = __( 'Email Test', 'yaymail' );
329 foreach ( $wc_emails->emails as $email => $item ) {
330 if ( $item->id == $template ) {
331 if ( 'customer_invoice' == $template ) {
332 $subject = Helper::getCustomerInvoiceSubject( $wc_emails->emails[ $email ] );
333 if ( ! empty( $subject ) ) {
334 return $subject;
335 }
336 } elseif ( 'new_booking' == $template ) {
337 $subject = Helper::getNewBookingSubject( $wc_emails->emails[ $email ] );
338 if ( ! empty( $subject ) ) {
339 return $subject;
340 }
341 } elseif ( 'customer_payment_retry' == $template ) {
342 $subject = Helper::getNewBookingSubject( $wc_emails->emails[ $email ] );
343 if ( ! empty( $subject ) ) {
344 return $subject;
345 }
346 } elseif ( 'Dokan_Email_Booking_New' == $template ) {
347 $subject = $wc_emails->emails[ $email ]->subject;
348 if ( ! empty( $subject ) ) {
349 return $subject;
350 }
351 } else {
352 if ( ! empty( $wc_emails->emails[ $email ]->subject ) ) {
353 $subject = $wc_emails->emails[ $email ]->subject;
354 if ( ! empty( $subject ) ) {
355 return $subject;
356 }
357 }
358 }
359 }
360 }
361 return $subject;
362 }
363
364 public function sanitize( $var ) {
365 // Prevent XSS
366 $list_elements = Helper::preventXSS( $var['emailContents'] );
367 return wp_kses_post_deep( $list_elements );
368 }
369
370 public function saveTemplate() {
371 try {
372 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
373 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
374 wp_send_json_error( array( 'mess' => __( 'Nonce is invalid', 'yaymail' ) ) );
375 } else {
376 if ( isset( $_POST['template'] ) ) {
377 // Helper::checkNonce();
378 $emailBackgroundColor = isset( $_POST['emailBackgroundColor'] ) ? sanitize_text_field( $_POST['emailBackgroundColor'] ) : 'rgb(236, 236, 236)';
379 $emailTextLinkColor = isset( $_POST['emailTextLinkColor'] ) ? sanitize_text_field( $_POST['emailTextLinkColor'] ) : '#7f54b3';
380 $titleShipping = isset( $_POST['titleShipping'] ) ? sanitize_text_field( $_POST['titleShipping'] ) : 'Shipping Address';
381 $titleBilling = isset( $_POST['titleBilling'] ) ? sanitize_text_field( $_POST['titleBilling'] ) : 'Billing Address';
382 $orderTitle = ( isset( $_POST['orderTitle'] ) && is_array( $_POST['orderTitle'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['orderTitle'] ) ) : array();
383 $orderItemsDownloadTitle = ( isset( $_POST['orderItemsDownloadTitle'] ) && is_array( $_POST['orderItemsDownloadTitle'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['orderItemsDownloadTitle'] ) ) : array();
384 $template = sanitize_text_field( $_POST['template'] );
385 $updateElement = new UpdateElement();
386 $setDefaultLogo = isset( $_POST['setDefaultLogo'] ) ? 'true' == sanitize_text_field( $_POST['setDefaultLogo'] ) ? true : false : false;
387 $setDefaultFooter = isset( $_POST['setDefaultFooter'] ) ? 'true' == sanitize_text_field( $_POST['setDefaultFooter'] ) ? true : false : false;
388 if ( isset( $_POST['emailContents'] ) ) {
389 $emailContents = $this->sanitize( $_POST );
390 $emailContents = $updateElement->merge_new_props_to_elements( $emailContents );
391 } else {
392 $emailContents = array();
393 }
394 foreach ( $emailContents as $key => $value ) {
395 if ( 'TwoColumns' == $value['type'] || 'ThreeColumns' == $value['type'] || 'FourColumns' == $value['type'] ) {
396 if ( ! array_key_exists( 'column1', $emailContents[ $key ]['settingRow'] ) ) {
397 $emailContents[ $key ]['settingRow']['column1'] = array();
398 }
399 if ( ! array_key_exists( 'column2', $emailContents[ $key ]['settingRow'] ) ) {
400 $emailContents[ $key ]['settingRow']['column2'] = array();
401 }
402 if ( ( 'ThreeColumns' == $value['type'] || 'FourColumns' == $value['type'] ) && ! array_key_exists( 'column3', $emailContents[ $key ]['settingRow'] ) ) {
403 $emailContents[ $key ]['settingRow']['column3'] = array();
404 }
405 if ( 'FourColumns' == $value['type'] && ! array_key_exists( 'column4', $emailContents[ $key ]['settingRow'] ) ) {
406 $emailContents[ $key ]['settingRow']['column4'] = array();
407 }
408 }
409 }
410 if ( CustomPostType::postIDByTemplate( $template ) ) {
411 $postID = CustomPostType::postIDByTemplate( $template );
412
413 if ( empty( $orderTitle ) ) {
414 $orderTitle = Helper::OrderItemsTitle();
415 }
416
417 if ( empty( $orderItemsDownloadTitle ) ) {
418 $orderItemsDownloadTitle = Helper::OrderItemsDownloadsTitle();
419 }
420
421 update_post_meta( $postID, '_yaymail_elements', $emailContents );
422 update_post_meta( $postID, '_email_backgroundColor_settings', $emailBackgroundColor );
423 update_post_meta( $postID, '_yaymail_email_textLinkColor_settings', $emailTextLinkColor );
424 update_post_meta( $postID, '_email_title_shipping', $titleShipping );
425 update_post_meta( $postID, '_email_title_billing', $titleBilling );
426 update_post_meta( $postID, '_yaymail_email_order_item_title', $orderTitle );
427 update_post_meta( $postID, '_yaymail_email_order_item_download_title', $orderItemsDownloadTitle );
428 // Change default logo
429 $default_logo = array(
430 'set_default' => (bool) $setDefaultLogo,
431 );
432 if ( 'true' == $setDefaultLogo ) {
433 $posts = CustomPostType::getListPostTemplate();
434 foreach ( $emailContents as $key => $element ) {
435 if ( 'Logo' == $element['type'] ) {
436 $logoDefault = $element['settingRow'];
437 break;
438 }
439 }
440
441 if ( count( $posts ) > 0 && isset( $logoDefault ) ) {
442 foreach ( $posts as $post ) {
443 $yaymail_elements = get_post_meta( $post->ID, '_yaymail_elements', true );
444 foreach ( $yaymail_elements as $key => $element ) {
445 if ( 'Logo' == $element['type'] ) {
446 $yaymail_elements[ $key ]['settingRow'] = wp_parse_args( $logoDefault, $yaymail_elements[ $key ]['settingRow'] );
447 }
448 }
449 update_post_meta( $post->ID, '_yaymail_elements', $yaymail_elements );
450 }
451 }
452 }
453 update_option( 'yaymail_settings_default_logo', $default_logo );
454 // Change default footer
455 $default_footer = array(
456 'set_default' => (bool) $setDefaultFooter,
457 );
458 if ( 'true' == $setDefaultFooter ) {
459 $posts = CustomPostType::getListPostTemplate();
460 foreach ( $emailContents as $key => $element ) {
461 if ( 'ElementText' == $element['type'] && 'Footer' == $element['nameElement'] ) {
462 $footerDefault = $element['settingRow'];
463 break;
464 }
465 }
466
467 if ( count( $posts ) > 0 && isset( $footerDefault ) ) {
468 foreach ( $posts as $post ) {
469 $yaymail_elements = get_post_meta( $post->ID, '_yaymail_elements', true );
470 foreach ( $yaymail_elements as $key => $element ) {
471 if ( 'ElementText' == $element['type'] && 'Footer' == $element['nameElement'] ) {
472 $yaymail_elements[ $key ]['settingRow'] = wp_parse_args( $footerDefault, $yaymail_elements[ $key ]['settingRow'] );
473 }
474 }
475 update_post_meta( $post->ID, '_yaymail_elements', $yaymail_elements );
476 }
477 }
478 }
479 update_option( 'yaymail_settings_default_footer', $default_footer );
480
481 wp_send_json_success(
482 array(
483 'mess' => __( 'Email has been saved.', 'yaymail' ),
484 'orderTitle' => $orderTitle,
485 'orderItemsDownloadTitle' => $orderItemsDownloadTitle,
486 )
487 );
488 } else {
489 wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) );
490 }
491 }
492 wp_send_json_error( array( 'mess' => __( 'Error save data.', 'yaymail' ) ) );
493 }
494 } catch ( \Exception $ex ) {
495 LogHelper::getMessageException( $ex, true );
496 } catch ( \Error $ex ) {
497 LogHelper::getMessageException( $ex, true );
498 }
499
500 }
501 public function copyTemplate() {
502 try {
503 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
504 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
505 wp_send_json_error( array( 'mess' => __( 'Nonce is invalid', 'yaymail' ) ) );
506 } else {
507 if ( isset( $_POST['copy_to'] ) && isset( $_POST['copy_from'] ) ) {
508 Helper::checkNonce();
509 $copyTo = sanitize_text_field( $_POST['copy_to'] );
510 $copyFrom = sanitize_text_field( $_POST['copy_from'] );
511 if ( CustomPostType::postIDByTemplate( $copyFrom ) ) {
512 $postID = CustomPostType::postIDByTemplate( $copyFrom );
513 $emailContentsFrom = get_post_meta( $postID, '_yaymail_elements', true );
514 $emailBackgroundColor = get_post_meta( $postID, '_email_backgroundColor_settings', true ) ? get_post_meta( $postID, '_email_backgroundColor_settings', true ) : 'rgb(236, 236, 236)';
515 $emailTextLinkColor = get_post_meta( $postID, '_yaymail_email_textLinkColor_settings', true ) ? get_post_meta( $postID, '_yaymail_email_textLinkColor_settings', true ) : '#7f54b3';
516 $titleShipping = isset( $_POST['titleShipping'] ) ? sanitize_text_field( $_POST['titleShipping'] ) : 'Shipping Address';
517 $titleBilling = isset( $_POST['titleBilling'] ) ? sanitize_text_field( $_POST['titleBilling'] ) : 'Billing Address';
518 $orderTitle = isset( $_POST['orderTitle'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['orderTitle'] ) ) : array();
519 $orderItemsDownloadTitle = ( isset( $_POST['orderItemsDownloadTitle'] ) && is_array( $_POST['orderItemsDownloadTitle'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['orderItemsDownloadTitle'] ) ) : array();
520 if ( CustomPostType::postIDByTemplate( $copyTo ) ) {
521 $idTo = CustomPostType::postIDByTemplate( $copyTo );
522 update_post_meta( $idTo, '_yaymail_elements', $emailContentsFrom );
523 update_post_meta( $idTo, '_email_backgroundColor_settings', $emailBackgroundColor );
524 update_post_meta( $idTo, '_yaymail_email_textLinkColor_settings', $emailTextLinkColor );
525 update_post_meta( $idTo, '_email_title_shipping', $titleShipping );
526 update_post_meta( $idTo, '_email_title_billing', $titleBilling );
527 update_post_meta( $idTo, '_yaymail_email_order_item_title', $orderTitle );
528 update_post_meta( $idTo, '_yaymail_email_order_item_download_title', $orderItemsDownloadTitle );
529 wp_send_json_success(
530 array(
531 'mess' => __( 'Copied Template successfully.', 'yaymail' ),
532 'data' => $emailContentsFrom,
533 )
534 );
535 } else {
536 wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) );
537 }
538 } else {
539 wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) );
540 }
541 }
542 wp_send_json_error( array( 'mess' => __( 'Error save data.', 'yaymail' ) ) );
543 }
544 } catch ( \Exception $ex ) {
545 LogHelper::getMessageException( $ex, true );
546 } catch ( \Error $ex ) {
547 LogHelper::getMessageException( $ex, true );
548 }
549
550 }
551
552 public function reviewYayMail() {
553 try {
554 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
555 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
556 wp_send_json_error( array( 'mess' => __( 'Nonce is invalid', 'yaymail' ) ) );
557 } else {
558 if ( isset( $_POST['review'] ) ) {
559 $yaymail_review = update_option( 'yaymail_review', true );
560 wp_send_json_success(
561 array(
562 'value' => $yaymail_review,
563 )
564 );
565 }
566 wp_send_json_error( array( 'mess' => __( 'Error Reset Template!', 'yaymail' ) ) );
567 }
568 } catch ( \Exception $ex ) {
569 LogHelper::getMessageException( $ex, true );
570 } catch ( \Error $ex ) {
571 LogHelper::getMessageException( $ex, true );
572 }
573 }
574
575 public function resetTemplate() {
576 try {
577 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
578 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
579 wp_send_json_error( array( 'mess' => __( 'Nonce is invalid', 'yaymail' ) ) );
580 } else {
581 if ( isset( $_POST['template'] ) ) {
582 // Helper::checkNonce();
583 $reset = sanitize_text_field( $_POST['template'] );
584 $templateEmail = \YayMail\Templates\Templates::getInstance();
585 $templates = $templateEmail::getList();
586 $orderItemsTitle = Helper::OrderItemsTitle();
587 $orderItemsDownloadsTitle = Helper::OrderItemsDownloadsTitle();
588 if ( 'all' == $reset ) {
589 foreach ( $templates as $key => $template ) {
590 if ( CustomPostType::postIDByTemplate( $key ) ) {
591 $postID = CustomPostType::postIDByTemplate( $key );
592 update_post_meta( $postID, '_yaymail_elements', json_decode( $template['elements'], true ) );
593 update_post_meta( $postID, '_email_backgroundColor_settings', 'rgb(236, 236, 236)' );
594 update_post_meta( $postID, '_yaymail_email_textLinkColor_settings', '#7f54b3' );
595 update_post_meta( $postID, '_email_title_shipping', __( 'Shipping Address', 'yaymail' ) );
596 update_post_meta( $postID, '_email_title_billing', __( 'Billing Address', 'yaymail' ) );
597 update_post_meta( $postID, '_yaymail_email_order_item_title', $orderItemsTitle );
598 update_post_meta( $postID, '_yaymail_email_order_item_download_title', $orderItemsDownloadsTitle );
599 }
600 }
601
602 if ( get_option( 'yaymail_settings' ) ) {
603 $yaymail_settings = get_option( 'yaymail_settings' );
604 $yaymail_settings['container_width'] = '605px';
605 $yaymail_settings['direction_rtl'] = 'ltr';
606 update_option( 'yaymail_settings', $yaymail_settings );
607 }
608
609 wp_send_json_success( array( 'mess' => __( 'Template reset successfully.', 'yaymail' ) ) );
610 } else {
611 if ( CustomPostType::postIDByTemplate( $reset ) && isset( $templates[ $reset ] ) ) {
612 $postID = CustomPostType::postIDByTemplate( $reset );
613 update_post_meta( $postID, '_yaymail_elements', json_decode( $templates[ $reset ]['elements'], true ) );
614 update_post_meta( $postID, '_email_backgroundColor_settings', 'rgb(236, 236, 236)' );
615 update_post_meta( $postID, '_yaymail_email_textLinkColor_settings', '#7f54b3' );
616 update_post_meta( $postID, '_email_title_shipping', __( 'Shipping Address', 'yaymail' ) );
617 update_post_meta( $postID, '_email_title_billing', __( 'Billing Address', 'yaymail' ) );
618 update_post_meta( $postID, '_yaymail_email_order_item_title', $orderItemsTitle );
619 update_post_meta( $postID, '_yaymail_email_order_item_download_title', $orderItemsDownloadsTitle );
620 wp_send_json_success( array( 'mess' => __( 'Template reset successfully.', 'yaymail' ) ) );
621 } else {
622 wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) );
623 }
624 }
625 }
626 wp_send_json_error( array( 'mess' => __( 'Error Reset Template!', 'yaymail' ) ) );
627 }
628 } catch ( \Exception $ex ) {
629 LogHelper::getMessageException( $ex, true );
630 } catch ( \Error $ex ) {
631 LogHelper::getMessageException( $ex, true );
632 }
633
634 }
635
636 public function importAllTemplate() {
637 try {
638 Helper::checkNonce();
639 if ( isset( $_FILES['file']['type'] ) ) {
640 if ( 'application/json' == $_FILES['file']['type'] ) {
641 if ( ! empty( $_FILES['file']['tmp_name'] ) ) {
642 $fileJson = sanitize_text_field( $_FILES['file']['tmp_name'] );
643 global $wp_filesystem;
644
645 if ( empty( $wp_filesystem ) ) {
646 require_once ABSPATH . '/wp-admin/includes/file.php';
647 WP_Filesystem();
648 }
649 $data = $wp_filesystem->get_contents( $fileJson );
650 $data = json_decode( $data, true );
651 $dataImports = $data['yaymailTemplateExport'];
652
653 $versionOld = $data['yaymail_version'];
654 $versionCurrent = YAYMAIL_VERSION;
655
656 /*
657 check key in settingRow whether or not it exists.
658 note: case when add setting row for element.
659 */
660 if ( $versionOld != $versionCurrent ) {
661 $element = new DefaultElement();
662 $defaultDataElements = $element->defaultDataElement;
663
664 foreach ( $defaultDataElements as $defaultelement ) {
665
666 foreach ( $dataImports as $keyTemplate => $templateImport ) {
667 foreach ( $templateImport['_yaymail_elements'] as $keyElem => $elemImport ) {
668 if ( $defaultelement['type'] == $elemImport['type'] ) {
669 /*
670 @@@ add key default for element
671 */
672 $keyEleDefaus = array();
673 $keyEleDefaus = array_diff_key( $defaultelement, $elemImport );
674 if ( count( $keyEleDefaus ) > 0 ) {
675 $dataImports[ $keyTemplate ]['_yaymail_elements'][ $keyElem ] = array_merge( $elemImport, $keyEleDefaus );
676 }
677
678 /*
679 add key default for setting row
680 note: when add a field in setting row
681 */
682 $propSettings = array();
683 $propSettings = array_diff_key( $defaultelement['settingRow'], $elemImport['settingRow'] );
684 $lenPropSettings = count( $propSettings );
685 if ( $lenPropSettings > 0 ) {
686 $result = array();
687 $result = array_merge( $elemImport['settingRow'], $propSettings );
688 $dataImports[ $keyTemplate ]['_yaymail_elements'][ $keyElem ]['settingRow'] = $result;
689 }
690
691 /*
692 remove Key not needed for setting row
693 note: when deleting a field in setting row
694 */
695 }
696 }
697 }
698 }
699 }
700 $flag = false;
701 if ( count( $dataImports ) > 0 ) {
702 foreach ( $dataImports as $key => $value ) {
703 if ( isset( $value['_yaymail_elements'] ) ) {
704 $template = $value['_yaymail_template'];
705 $template_language = $value['_yaymail_template_language'];
706 if ( CustomPostType::postIDByTemplateLanguage( $template, $template_language ) ) {
707 $pID = CustomPostType::postIDByTemplateLanguage( $template, $template_language );
708 update_post_meta( $pID, '_yaymail_elements', $value['_yaymail_elements'] );
709 } else {
710 $array = array(
711 'mess' => '',
712 'post_date' => current_time( 'Y-m-d H:i:s' ),
713 'post_type' => 'yaymail_template',
714 'post_status' => 'publish',
715 '_yaymail_template' => $template,
716 '_yaymail_elements' => $value['_yaymail_elements'],
717
718 );
719 if ( '' !== $value['_yaymail_template_language'] ) {
720 $array['_yaymail_template_language'] = $value['_yaymail_template_language'];
721 }
722 $insert = CustomPostType::insert( $array );
723 }
724 $flag = true;
725 }
726 }
727 }
728 if ( ! $flag ) {
729 wp_send_json_error( array( 'mess' => __( 'Import Failed.', 'yaymail' ) ) );
730 }
731 wp_send_json_success( array( 'mess' => __( 'Imported successfully.', 'yaymail' ) ) );
732 } else {
733 wp_send_json_error( array( 'mess' => __( 'File not found.', 'yaymail' ) ) );
734 }
735 } else {
736 wp_send_json_error( array( 'mess' => __( 'File not correct format.', 'yaymail' ) ) );
737 }
738 }
739 wp_send_json_error( array( 'mess' => __( 'Please upload 1 file to import.', 'yaymail' ) ) );
740 } catch ( \Exception $ex ) {
741 LogHelper::getMessageException( $ex, true );
742 } catch ( \Error $ex ) {
743 LogHelper::getMessageException( $ex, true );
744 }
745
746 }
747 public function enableDisableTempalte() {
748 try {
749 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
750 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
751 wp_send_json_error( array( 'mess' => __( 'Nonce is invalid', 'yaymail' ) ) );
752 } else {
753 if ( isset( $_POST['settings'] ) ) {
754 // Helper::checkNonce();
755 $settingDefault = CustomPostType::templateEnableDisable();
756 $listTemplates = ! empty( $settingDefault ) ? array_keys( $settingDefault ) : array();
757 $settingCurrent = array_map( 'sanitize_text_field', wp_unslash( $_POST['settings'] ) );
758
759 if ( ! empty( $listTemplates ) ) {
760 foreach ( $settingCurrent as $key => $value ) {
761 if ( in_array( $key, $listTemplates ) ) {
762 update_post_meta( $settingDefault[ $key ]['post_id'], '_yaymail_status', $value );
763 }
764 }
765 }
766 wp_send_json_success( array( 'mess' => __( 'Settings saved.', 'yaymail' ) ) );
767 }
768 wp_send_json_error( array( 'mess' => __( 'Settings Failed!.', 'yaymail' ) ) );
769 }
770 } catch ( \Exception $ex ) {
771 LogHelper::getMessageException( $ex, true );
772 } catch ( \Error $ex ) {
773 LogHelper::getMessageException( $ex, true );
774 }
775
776 }
777
778 public function generalSettings() {
779 try {
780 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
781 if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) {
782 wp_send_json_error( array( 'mess' => __( 'Nonce is invalid', 'yaymail' ) ) );
783 } else {
784 if ( isset( $_POST['settings'] ) ) {
785 $setting = array_map( 'sanitize_text_field', wp_unslash( $_POST['settings'] ) );
786 $yaymail_direction = $setting['direction_rtl'];
787 isset( $yaymail_direction ) ? update_option( 'yaymail_direction', $yaymail_direction ) : update_option( 'yaymail_direction', 'ltr' );
788 $setting['custom_css'] = wp_kses_post( isset( $_POST['settings']['custom_css'] ) ? $_POST['settings']['custom_css'] : '' );
789 update_option( 'yaymail_settings', $setting );
790 wp_send_json_success( array( 'mess' => __( 'Settings saved.', 'yaymail' ) ) );
791 }
792 wp_send_json_error( array( 'mess' => __( 'Settings Failed!.', 'yaymail' ) ) );
793 }
794 } catch ( \Exception $ex ) {
795 LogHelper::getMessageException( $ex, true );
796 } catch ( \Error $ex ) {
797 LogHelper::getMessageException( $ex, true );
798 }
799
800 }
801
802 }
803