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

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