PluginProbe
YayMail – WooCommerce Email Customizer / 3.4
YayMail – WooCommerce Email Customizer v3.4
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 3.4.2 3.4.3 All 55 releases
yaymail / includes / Ajax.php

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

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