PluginProbe
MStore API – Create Native Android & iOS Apps On The Cloud / 4.18.0
MStore API – Create Native Android & iOS Apps On The Cloud v4.18.0
4.21.3 4.21.2 4.21.1 trunk 1.1.5 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 2.9.9 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 All 192 releases
mstore-api / mstore-api.php

mstore-api.php in MStore API – Create Native Android & iOS Apps On The Cloud 4.18.0, at mstore-api.php

1,305 lines 53.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: MStore API
4 * Plugin URI: https://github.com/inspireui/mstore-api
5 * Description: The MStore API Plugin which is used for the FluxBuilder and FluxStore Mobile App
6 * Version: 4.18.0
7 * Author: FluxBuilder
8 * Author URI: https://fluxbuilder.com
9 *
10 * Text Domain: MStore-Api
11 */
12
13 defined('ABSPATH') or wp_die('No script kiddies please!');
14
15
16 // use MStoreCheckout\Templates\MDetect;
17
18 include plugin_dir_path(__FILE__) . "templates/class-mobile-detect.php";
19 include plugin_dir_path(__FILE__) . "templates/class-rename-generate.php";
20 include_once plugin_dir_path(__FILE__) . "controllers/flutter-user.php";
21 include_once plugin_dir_path(__FILE__) . "controllers/flutter-home.php";
22 include_once plugin_dir_path(__FILE__) . "controllers/flutter-booking.php";
23 include_once plugin_dir_path(__FILE__) . "controllers/flutter-vendor-admin.php";
24 include_once plugin_dir_path(__FILE__) . "controllers/flutter-woo.php";
25 include_once plugin_dir_path(__FILE__) . "controllers/flutter-delivery.php";
26 include_once plugin_dir_path(__FILE__) . "functions/index.php";
27 include_once plugin_dir_path(__FILE__) . "functions/utils.php";
28 include_once plugin_dir_path(__FILE__) . "controllers/flutter-tera-wallet.php";
29 include_once plugin_dir_path(__FILE__) . "controllers/flutter-paytm.php";
30 include_once plugin_dir_path(__FILE__) . "controllers/flutter-paystack.php";
31 include_once plugin_dir_path(__FILE__) . "controllers/flutter-flutterwave.php";
32 include_once plugin_dir_path(__FILE__) . "controllers/flutter-myfatoorah.php";
33 include_once plugin_dir_path(__FILE__) . "controllers/flutter-midtrans.php";
34 include_once plugin_dir_path(__FILE__) . "controllers/flutter-paid-memberships-pro.php";
35 include_once plugin_dir_path(__FILE__) . "controllers/listing-rest-api/class.api.fields.php";
36 include_once plugin_dir_path(__FILE__) . "controllers/flutter-blog.php";
37 include_once plugin_dir_path(__FILE__) . "controllers/flutter-wholesale.php";
38 include_once plugin_dir_path(__FILE__) . "controllers/flutter-stripe.php";
39 include_once plugin_dir_path(__FILE__) . "controllers/flutter-notification.php";
40 include_once plugin_dir_path(__FILE__) . "controllers/flutter-thawani.php";
41 include_once plugin_dir_path(__FILE__) . "controllers/flutter-expresspay.php";
42 include_once plugin_dir_path(__FILE__) . "controllers/flutter-2c2p.php";
43 include_once plugin_dir_path(__FILE__) . "controllers/flutter-cc-avenue.php";
44 include_once plugin_dir_path(__FILE__) . "controllers/flutter-flow-flow.php";
45 include_once plugin_dir_path(__FILE__) . "controllers/flutter-store-locator.php";
46 include_once plugin_dir_path(__FILE__) . "controllers/flutter-composite-products.php";
47 include_once plugin_dir_path(__FILE__) . "controllers/flutter-b2bking.php";
48 include_once plugin_dir_path(__FILE__) . "controllers/flutter-review.php";
49 include_once plugin_dir_path(__FILE__) . "controllers/helpers/firebase-message-helper.php";
50 include_once plugin_dir_path(__FILE__) . "controllers/flutter-fib.php";
51 include_once plugin_dir_path(__FILE__) . "controllers/helpers/firebase-phone-auth-helper.php";
52 include_once plugin_dir_path(__FILE__) . "controllers/flutter-auction.php";
53 include_once plugin_dir_path(__FILE__) . "controllers/flutter-iyzico.php";
54 include_once plugin_dir_path(__FILE__) . "controllers/flutter-phonepe.php";
55 include_once plugin_dir_path(__FILE__) . "controllers/flutter-points-offline-store.php";
56 include_once plugin_dir_path(__FILE__) . "controllers/flutter-smart-cod.php";
57 include_once plugin_dir_path(__FILE__) . "controllers/flutter-discount-rules.php";
58
59 if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
60 require __DIR__ . '/vendor/autoload.php';
61 }
62
63 class MstoreCheckOut
64 {
65 public $version = '4.18.0';
66
67 public function __construct()
68 {
69 define('MSTORE_CHECKOUT_VERSION', $this->version);
70 define('MSTORE_PLUGIN_FILE', __FILE__);
71
72 /**
73 * Prepare data before checkout by webview
74 */
75 add_action('template_redirect', 'flutter_prepare_checkout');
76
77 add_action( 'pre_get_posts', 'set_author_in_for_vendor_staff' );
78
79 include_once(ABSPATH . 'wp-admin/includes/plugin.php');
80 //include_once(ABSPATH . 'wp-includes/pluggable.php');
81
82 // //migrate old versions to re-verify purchase code automatically
83 // verifyPurchaseCodeAuto();
84
85 add_filter( 'get_avatar_url', array( $this, 'filter_avatar' ), 10, 3 );
86
87 if (is_plugin_active('woocommerce/woocommerce.php') == false) {
88 return 0;
89 }
90 add_action('woocommerce_init', 'woocommerce_mstore_init');
91 function woocommerce_mstore_init()
92 {
93 include_once plugin_dir_path(__FILE__) . "controllers/flutter-order.php";
94 include_once plugin_dir_path(__FILE__) . "controllers/flutter-multi-vendor.php";
95 include_once plugin_dir_path(__FILE__) . "controllers/flutter-vendor.php";
96 include_once plugin_dir_path(__FILE__) . "controllers/helpers/delivery-wcfm-helper.php";
97 include_once plugin_dir_path(__FILE__) . "controllers/helpers/delivery-wcfm-helper.php";
98 include_once plugin_dir_path(__FILE__) . "controllers/helpers/vendor-admin-woo-helper.php";
99 include_once plugin_dir_path(__FILE__) . "controllers/helpers/vendor-admin-wcfm-helper.php";
100 include_once plugin_dir_path(__FILE__) . "controllers/helpers/vendor-admin-dokan-helper.php";
101 include_once plugin_dir_path(__FILE__) . "controllers/flutter-customer.php";
102 include_once plugin_dir_path(__FILE__) . "functions/video-setting-embed.php";
103 }
104
105 add_filter('wp_rest_cache/allowed_endpoints', array($this, 'wprc_add_flutter_api_endpoints'));
106
107 $order = filter_has_var(INPUT_GET, 'code') && strlen(filter_input(INPUT_GET, 'code')) > 0 ? true : false;
108 if ($order) {
109 add_filter('woocommerce_is_checkout', '__return_true');
110 }
111
112 /*
113 add_filter( 'woocommerce_get_item_data', 'display_custom_product_field_data_mstore_api', 10, 2 );
114
115 function display_custom_product_field_data_mstore_api( $cart_data, $cart_item ) {
116
117 if( !empty( $cart_data ) ){
118 $custom_items = $cart_data;
119
120 $code = sanitize_text_field($_GET['code']) ?: get_transient( 'mstore_code' );
121 set_transient( 'mstore_code', $code, 600 );
122
123 global $wpdb;
124 $table_name = $wpdb->prefix . "mstore_checkout";
125 $item = $wpdb->get_row("SELECT * FROM $table_name WHERE code = '$code'");
126 if ($item) {
127 $data = json_decode(urldecode(base64_decode($item->order)), true);
128 $line_items = $data['line_items'];
129 $product_ids = [];
130 foreach($line_items as $line => $item) {
131 $product_ids[$item['product_id']] = $item;
132 }
133
134 if (array_key_exists($cart_item['product_id'], $product_ids)) {
135 if ($varian = $product_ids[$cart_item['product_id']]) {
136 $variations = $varian['meta_data'];
137 foreach($variations as $v => $f) {
138 preg_match('#\((.*?)\)#', $f['key'], $match);
139 $val = $match[1];
140 $custom_items[] = array(
141 'key' => $f['value'],
142 'value' => $val,
143 'display' => $val,
144 );
145 }
146 }
147 }
148 }
149
150 return $custom_items;
151 }
152 return $cart_data;
153 }
154
155
156 add_action( 'woocommerce_before_calculate_totals', 'add_custom_price_mstore_api' );
157
158 function add_custom_price_mstore_api( $cart_object ) {
159 foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
160 $add_price = 0;
161 if ($variations = $cart_item['variation']) {
162 foreach($variations as $v => $f) {
163 preg_match('#\((.*?)\)#', $v, $match);
164 if(is_array($match) && array_key_exists(1,$match)){
165 $val = $match[1];
166 $cents = filter_var($val, FILTER_SANITIZE_NUMBER_INT);
167 if(is_numeric($cents)){
168 $add_price += floatval($cents / 100);
169 }
170 }
171 }
172 }
173 $new_price = $cart_item['data']->get_price() + $add_price;
174 $cart_item['data']->set_price($new_price);
175 }
176 }
177 */
178
179 add_action('wp_print_scripts', array($this, 'handle_received_order_page'));
180
181 //add meta box shipping location in order detail
182 add_action('add_meta_boxes', 'mv_add_meta_boxes');
183 if (!function_exists('mv_add_meta_boxes')) {
184 function mv_add_meta_boxes()
185 {
186 add_meta_box('mv_other_fields', __('Shipping Location', 'woocommerce'), 'mv_add_other_fields_for_packaging', 'shop_order', 'side', 'core');
187 }
188 }
189 // Adding Meta field in the meta container admin shop_order pages
190 if (!function_exists('mv_add_other_fields_for_packaging')) {
191 function mv_add_other_fields_for_packaging()
192 {
193 global $post;
194 $note = $post->post_excerpt;
195 $items = explode("\n", $note);
196 if (strpos($items[0], "URL:") !== false) {
197 $url = str_replace("URL:", "", $items[0]);
198 echo esc_html('<iframe width="600" height="500" src="' . esc_url($url) . '"></iframe>');
199 }
200 }
201 }
202
203 register_activation_hook(__FILE__, array($this, 'create_custom_mstore_table'));
204
205 // Setup Ajax action hook
206 add_action('wp_ajax_mstore_delete_json_file', array($this, 'mstore_delete_json_file'));
207 add_action('wp_ajax_mstore_delete_apple_file', array($this, 'mstore_delete_apple_file'));
208 add_action('wp_ajax_mstore_delete_firebase_file', array($this, 'mstore_delete_firebase_file'));
209 add_action('wp_ajax_mstore_update_limit_product', array($this, 'mstore_update_limit_product'));
210 add_action('wp_ajax_mstore_update_new_order_title', array($this, 'mstore_update_new_order_title'));
211 add_action('wp_ajax_mstore_update_new_order_message', array($this, 'mstore_update_new_order_message'));
212 add_action('wp_ajax_mstore_update_status_order_title', array($this, 'mstore_update_status_order_title'));
213 add_action('wp_ajax_mstore_update_status_order_message', array($this, 'mstore_update_status_order_message'));
214
215 // listen changed order status to notify
216 add_action('woocommerce_order_status_changed', array($this, 'track_order_status_changed'), 9, 4);
217 add_action('woocommerce_checkout_update_order_meta', array($this, 'track_new_order'));
218 add_action('woocommerce_rest_insert_shop_order_object', array($this, 'track_api_new_order'), 10, 4);
219
220 //WCFM - WooCommerce Frontend Manager - Delivery
221 //Handle listen to assign delivery boy on the website
222 add_action( 'wcfmd_after_delivery_boy_assigned', array($this, 'track_delivery_boy_assigned'), 400, 6 );
223
224 $path = get_template_directory() . "/templates";
225 if (!file_exists($path)) {
226 mkdir($path, 0777, true);
227 }
228 if (file_exists($path)) {
229 $templatePath = plugin_dir_path(__FILE__) . "templates/mstore-api-template.php";
230 if (!copy($templatePath, $path . "/mstore-api-template.php")) {
231 return 0;
232 }
233 }
234 }
235
236 public function wprc_add_flutter_api_endpoints($allowed_endpoints){
237 if ( ! isset( $allowed_endpoints[ 'wc/v3' ] ) || ! in_array( 'products', $allowed_endpoints[ 'wc/v3' ] ) ) {
238 $allowed_endpoints[ 'wc/v3' ][] = 'products';
239 $allowed_endpoints[ 'wc/v3' ][] = 'products/categories';
240 $allowed_endpoints[ 'wc/v3' ][] = 'products/attributes';
241 }
242 if ( ! isset( $allowed_endpoints[ 'wc/v2' ] ) || ! in_array( 'products', $allowed_endpoints[ 'wc/v2' ] ) ) {
243 $allowed_endpoints[ 'wc/v2' ][] = 'products';
244 $allowed_endpoints[ 'wc/v2' ][] = 'products/categories';
245 $allowed_endpoints[ 'wc/v2' ][] = 'products/attributes';
246 }
247 return $allowed_endpoints;
248 }
249
250 public function filter_avatar($url, $id_or_email, $args)
251 {
252 $finder = false;
253 $user = false;
254
255 // Early return if id_or_email is empty
256 if (empty($id_or_email)) {
257 return $url;
258 }
259
260 if (is_numeric($id_or_email)) {
261 $user = get_user_by('id', absint($id_or_email));
262 } elseif (is_string($id_or_email) && is_email($id_or_email)) {
263 $user = get_user_by('email', $id_or_email);
264 } elseif ($id_or_email instanceof WP_User) {
265 $user = $id_or_email;
266 } elseif ($id_or_email instanceof WP_Post) {
267 $user = get_user_by('id', (int)$id_or_email->post_author);
268 } elseif ($id_or_email instanceof WP_Comment) {
269 if (!empty($id_or_email->user_id)) {
270 $user = get_user_by('id', (int)$id_or_email->user_id);
271 }
272 }
273
274 if ($user && !is_wp_error($user)) {
275 $avatar = get_user_meta($user->ID, 'user_avatar', true);
276 if (isset($avatar) && $avatar !== "" && !is_bool($avatar) && !empty($avatar[0])) {
277 return $avatar[0];
278 }
279 }
280
281 return $url;
282 }
283
284 function mstore_delete_json_file(){
285 if(checkIsAdmin(get_current_user_id())){
286 $id = sanitize_text_field($_REQUEST['id']);
287 $nonce = sanitize_text_field($_REQUEST['nonce']);
288 FlutterUtils::delete_config_file($id, $nonce);
289 }else{
290 wp_send_json_error('No Permission',401);
291 }
292 }
293
294 function mstore_delete_apple_file(){
295 if(checkIsAdmin(get_current_user_id())){
296 $nonce = sanitize_text_field($_REQUEST['nonce']);
297 FlutterAppleSignInUtils::delete_config_file($nonce);
298 wp_send_json('success',200);
299 }else{
300 wp_send_json_error('No Permission',401);
301 }
302 }
303
304 function mstore_delete_firebase_file(){
305 if(checkIsAdmin(get_current_user_id())){
306 $nonce = sanitize_text_field($_REQUEST['nonce']);
307 FirebaseMessageHelper::delete_config_file($nonce);
308 wp_send_json('success',200);
309 }else{
310 wp_send_json_error('No Permission',401);
311 }
312 }
313
314 function mstore_update_limit_product()
315 {
316 $nonce = sanitize_text_field($_REQUEST['nonce']);
317 if(checkIsAdmin(get_current_user_id()) && wp_verify_nonce($nonce, 'update_limit_product')){
318 $limit = sanitize_text_field($_REQUEST['limit']);
319 if (is_numeric($limit)) {
320 update_option("mstore_limit_product", intval($limit));
321 }
322 }else{
323 wp_send_json_error('No Permission',401);
324 }
325 }
326
327 function mstore_update_new_order_title()
328 {
329 $nonce = sanitize_text_field($_REQUEST['nonce']);
330 if(checkIsAdmin(get_current_user_id()) && wp_verify_nonce($nonce, 'update_new_order_title')){
331 $title = sanitize_text_field($_REQUEST['title']);
332 update_option("mstore_new_order_title", $title);
333 }else{
334 wp_send_json_error('No Permission',401);
335 }
336 }
337
338 function mstore_update_new_order_message()
339 {
340 $nonce = sanitize_text_field($_REQUEST['nonce']);
341 if(checkIsAdmin(get_current_user_id()) && wp_verify_nonce($nonce, 'update_new_order_message')){
342 $message = sanitize_text_field($_REQUEST['message']);
343 update_option("mstore_new_order_message", $message);
344 }else{
345 wp_send_json_error('No Permission',401);
346 }
347 }
348
349 function mstore_update_status_order_title()
350 {
351 $nonce = sanitize_text_field($_REQUEST['nonce']);
352 if(checkIsAdmin(get_current_user_id()) && wp_verify_nonce($nonce, 'update_status_order_title')){
353 $title = sanitize_text_field($_REQUEST['title']);
354 update_option("mstore_status_order_title", $title);
355 }else{
356 wp_send_json_error('No Permission',401);
357 }
358 }
359
360 function mstore_update_status_order_message()
361 {
362 $nonce = sanitize_text_field($_REQUEST['nonce']);
363 if(checkIsAdmin(get_current_user_id()) && wp_verify_nonce($nonce, 'update_status_order_message')){
364 $message = sanitize_text_field($_REQUEST['message']);
365 update_option("mstore_status_order_message", $message);
366 }else{
367 wp_send_json_error('No Permission',401);
368 }
369 }
370
371 // update order via website
372 function track_order_status_changed($id, $previous_status, $next_status)
373 {
374 trackOrderStatusChanged($id, $previous_status, $next_status);
375 }
376
377 // new order via website
378 function track_new_order($order_id)
379 {
380 trackNewOrder($order_id);
381 }
382
383 // New order or update order via API
384 function track_api_new_order($object, $request, $creating)
385 {
386 if ($creating) {
387 trackNewOrder($object->get_id());
388
389 // Update order attributes. Requires WooCommerce 8.5.0 or later.
390 // And make sure you have enabled `Order Attributes` in WooCommerce
391 // Settings > Advanced > Features.
392 // See: https://woocommerce.com/document/order-attribution-tracking/
393 do_action('woocommerce_store_api_checkout_update_order_from_request', $object, $request);
394 } else {
395 $body = $request->get_body_params();
396 if (isset($body['status'])) {
397 sendNotificationForOrderStatusUpdated($object->id, $body['status']);
398 }
399 }
400 }
401
402 function track_delivery_boy_assigned( $order_id, $order_item_id, $wcfm_tracking_data, $product_id, $wcfm_delivery_boy, $wcfm_messages ) {
403 $notification_message = strip_tags($wcfm_messages);
404 $title = "You have new notification";
405 pushNotificationForDeliveryBoy($wcfm_delivery_boy, $title, $notification_message);
406 }
407
408 public function handle_received_order_page()
409 {
410 // default return true for getting checkout library working
411 if (is_order_received_page()) {
412 $detect = new MDetect;
413 if ($detect->isMobile()) {
414 wp_register_style('mstore-order-custom-style', plugins_url('assets/css/mstore-order-style.css', MSTORE_PLUGIN_FILE));
415 wp_enqueue_style('mstore-order-custom-style');
416 }
417 }
418
419 }
420
421 function create_custom_mstore_table()
422 {
423 global $wpdb;
424 // include upgrade-functions for maybe_create_table;
425 if (!function_exists('maybe_create_table')) {
426 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
427 }
428 $charset_collate = $wpdb->get_charset_collate();
429 $table_name = $wpdb->prefix . 'mstore_checkout';
430 $sql = "CREATE TABLE $table_name (
431 id mediumint(9) NOT NULL AUTO_INCREMENT,
432 `code` tinytext NOT NULL,
433 `order` text NOT NULL,
434 PRIMARY KEY (id)
435 ) $charset_collate;";
436 $success = maybe_create_table($table_name, $sql);
437 }
438 }
439
440 $mstoreCheckOut = new MstoreCheckOut();
441
442 // use JO\Module\Templater\Templater;
443 include plugin_dir_path(__FILE__) . "templates/class-templater.php";
444
445 add_action('plugins_loaded', 'load_mstore_templater');
446 function load_mstore_templater()
447 {
448
449 // add our new custom templates
450 $my_templater = new Templater(
451 array(
452 // YOUR_PLUGIN_DIR or plugin_dir_path(__FILE__)
453 'plugin_directory' => plugin_dir_path(__FILE__),
454 // should end with _ > prefix_
455 'plugin_prefix' => 'plugin_prefix_',
456 // templates directory inside your plugin
457 'plugin_template_directory' => 'templates',
458 )
459 );
460 $my_templater->add(
461 array(
462 'page' => array(
463 'mstore-api-template.php' => 'Page Custom Template',
464 ),
465 )
466 )->register();
467 }
468
469 //custom rest api
470 function flutter_users_routes()
471 {
472 $controller = new FlutterUserController();
473 $controller->register_routes();
474 }
475
476 add_action('rest_api_init', 'flutter_users_routes');
477 add_action('rest_api_init', 'mstore_check_payment_routes');
478 function mstore_check_payment_routes()
479 {
480 register_rest_route('order', '/verify', array(
481 'methods' => 'GET',
482 'callback' => 'mstore_check_payment',
483 'permission_callback' => function () {
484 return true;
485 },
486 )
487 );
488 }
489
490 function mstore_check_payment()
491 {
492 return true;
493 }
494
495
496 // Add menu Setting
497 add_action('admin_menu', 'mstore_plugin_setup_menu');
498
499 function mstore_plugin_setup_menu()
500 {
501 add_menu_page('MStore Api', 'MStore Api', 'manage_options', 'mstore-plugin', 'mstore_init');
502 }
503
504 function mstore_init()
505 {
506 load_template(dirname(__FILE__) . '/templates/mstore-api-admin-page.php');
507 }
508
509 add_filter('woocommerce_rest_prepare_product_variation_object', 'custom_woocommerce_rest_prepare_product_variation_object', 20, 3);
510 add_filter('woocommerce_rest_prepare_product_object', 'flutter_custom_change_product_response', 20, 3);
511 add_filter('woocommerce_rest_prepare_product_review', 'custom_product_review', 20, 3);
512 add_filter('woocommerce_rest_prepare_product_cat', 'custom_product_category', 20, 3);
513 add_filter('woocommerce_rest_prepare_shop_order_object', 'flutter_custom_change_order_response', 20, 3);
514 add_filter('woocommerce_rest_prepare_product_attribute', 'flutter_custom_change_product_attribute', 20, 3);
515 add_filter('woocommerce_rest_prepare_product_tag', 'flutter_custom_change_product_taxonomy', 20, 3);
516 add_filter('woocommerce_rest_prepare_product_brand', 'flutter_custom_change_product_taxonomy', 20, 3);
517 add_filter('woocommerce_rest_product_object_query', 'flutter_custom_rest_product_object_query', 10, 2);
518 add_filter('woocommerce_rest_product_tag_query', 'flutter_custom_rest_product_tag_query', 10, 2);
519 add_filter('woocommerce_rest_product_brand_query', 'flutter_custom_rest_product_brand_query', 10, 2);
520 add_filter('rest_product_collection_params', 'flutter_custom_rest_product_collection_params', 10, 1);
521 add_filter('posts_pre_query', 'flutter_custom_posts_pre_query', 10, 2);
522 add_filter('found_posts', 'flutter_custom_found_posts', 20, 2);
523
524 /**
525 * WooCommerce REST API: Random sorting for products.
526 *
527 * rest_{post_type}_collection_params
528 *
529 * @param array $params
530 * @return array
531 */
532 function flutter_custom_rest_product_collection_params($params)
533 {
534 $params['orderby']['enum'][] = 'rand';
535 return $params;
536 }
537
538 function flutter_custom_rest_product_tag_query($args, $request)
539 {
540 return flutter_custom_rest_product_taxomomy_query($args, $request, 'product_tag');
541 }
542
543 function flutter_custom_rest_product_brand_query($args, $request)
544 {
545 return flutter_custom_rest_product_taxomomy_query($args, $request, 'product_brand');
546 }
547
548 function flutter_custom_rest_product_taxomomy_query($args, $request, $taxonomy)
549 {
550 $hide_empty = isset($args['hide_empty']) && $args['hide_empty'] == true;
551
552 // `include` parameter can be an array or comma separated string
553 $include = wp_parse_id_list($args['include']);
554
555 // Get product count for all tags, brands related to the requested params (based on category, brand, etc.)
556 $terms = get_filtered_term_product_counts($request, $taxonomy, [], $hide_empty);
557
558 // Trick: Use the `include` parameter to fix the API problem below
559 // The page = 1 has no visible items (count = 0), the app does not show
560 // anything (include loadmore button). If the page = 2 has visible items,
561 // cannot do anything in app because it do not show load more button
562 foreach ($terms as $key => $term) {
563 if ($hide_empty) {
564 if ($term['term_count'] > 0) {
565 $include[] = $term['term_count_id'];
566 }
567 } else {
568 $include[] = $term['term_count_id'];
569 }
570 }
571
572 $args['include'] = implode(',', wp_parse_id_list($include));
573 return $args;
574 }
575
576 function flutter_custom_rest_product_object_query($args, $request)
577 {
578 $attrs = $request['attributes'];
579
580 if (is_string($attrs) && !empty($attrs)) {
581 $attrs = json_decode($attrs, true);
582 }
583
584 // Attributes filter.
585 if (!empty($attrs)) {
586 foreach ($attrs as $attr_key => $attr_value) {
587 $args['tax_query'][] = [
588 'taxonomy' => $attr_key,
589 'field' => 'term_id',
590 'terms' => explode(',', $attr_value),
591 ];
592 }
593 }
594
595 return $args;
596 }
597
598 function custom_product_category($response, $object, $request)
599 {
600 $id = $response->data['id'];
601 $children = get_term_children($id, 'product_cat');
602
603 if (empty($children)) {
604 $response->data['has_children'] = false;
605 } else {
606 $response->data['has_children'] = true;
607 }
608 return $response;
609 }
610
611 function custom_product_review($response, $object, $request)
612 {
613 if(is_plugin_active('woo-photo-reviews/woo-photo-reviews.php') || is_plugin_active('woocommerce-photo-reviews/woocommerce-photo-reviews.php')){
614 $id = $response->data['id'];
615 $image_post_ids = get_comment_meta( $id, 'reviews-images', true );
616 $image_arr = array();
617 if(!is_string($image_post_ids)){
618 foreach( $image_post_ids as $image_post_id ) {
619 $image_arr[] = wp_get_original_image_url( $image_post_id );
620 }
621 }
622 $response->data['images'] = $image_arr;
623 }
624 return $response;
625 }
626
627 function flutter_custom_change_order_response($response, $object, $request)
628 {
629 return customOrderResponse($response, $object, $request);
630 }
631
632 function flutter_custom_change_product_response($response, $object, $request)
633 {
634 return customProductResponse($response, $object, $request);
635 }
636
637 function flutter_custom_change_product_attribute($response, $item, $request)
638 {
639 $taxonomy = wc_attribute_taxonomy_name($item->attribute_name);
640
641 // Combine all attribute terms into the return result when getting
642 // attributes. Reduce api calls to get sub-attributes from the app
643 $options = get_terms([
644 'taxonomy' => $taxonomy,
645 'hide_empty' => false,
646 ]);
647
648 // Get list count of attribute terms based on attribute.
649 $terms = get_filtered_term_product_counts($request, $taxonomy);
650
651 $is_visible = false;
652 // Show this attribute if any attribute terms have product quantity > 0
653 foreach ($terms as $key => $term) {
654 $count = (int)$term['term_count'];
655 if ($is_visible == false && $count > 0) {
656 $is_visible = true;
657 }
658 // Update term count for options
659 foreach ($options as $option) {
660 if ($option->term_id == $term['term_count_id']) {
661 $option->count = $count;
662 break;
663 }
664 }
665 }
666
667 $response->data['terms'] = $options;
668
669 $response->data['is_visible'] = $is_visible;
670
671 return $response;
672 }
673
674 /// Custom response for product a tag or brand
675 function flutter_custom_change_product_taxonomy($response, $item, $request)
676 {
677 // There is only a maximum of 1 entry because term_ids is an array of a
678 // unique id
679 $terms = get_filtered_term_product_counts($request, $item->taxonomy, $item->term_id);
680
681 if (!empty($terms)) {
682 $term = $terms[0];
683 $count = (int)$term['term_count'];
684 $response->data['count'] = $count;
685 $response->data['is_visible'] = $count > 0;
686 } else {
687 $response->data['count'] = 0;
688 $response->data['is_visible'] = false;
689 }
690
691 return $response;
692 }
693
694 function custom_get_attribute_taxonomy_name( $slug, $product ) {
695 // Format slug so it matches attributes of the product.
696 $slug = wc_attribute_taxonomy_slug( $slug );
697 $attributes = $product->get_attributes();
698 $attribute = false;
699
700 // pa_ attributes.
701 if ( isset( $attributes[ wc_attribute_taxonomy_name( $slug ) ] ) ) {
702 $attribute = $attributes[ wc_attribute_taxonomy_name( $slug ) ];
703 } elseif ( isset( $attributes[ $slug ] ) ) {
704 $attribute = $attributes[ $slug ];
705 }
706
707 if ( ! $attribute ) {
708 return $slug;
709 }
710
711 // Taxonomy attribute name.
712 if ( $attribute->is_taxonomy() ) {
713 $taxonomy = $attribute->get_taxonomy_object();
714 return $taxonomy->attribute_label;
715 }
716
717 // Custom product attribute name.
718 return $attribute->get_name();
719 }
720
721 function custom_woocommerce_rest_prepare_product_variation_object($response, $object, $request)
722 {
723
724 global $woocommerce_wpml;
725
726 //update correct product price with tax setting
727 $response->data['price'] = wc_get_price_to_display( $object );
728 $response->data['regular_price'] = wc_get_price_to_display( $object, array( 'price' => $object->get_regular_price() ) );
729 $response->data['sale_price'] = wc_get_price_to_display( $object, array( 'price' => $object->get_sale_price() ) );
730
731 $is_purchased = false;
732 if (isset($request['user_id'])) {
733 $user_id = $request['user_id'];
734 $user_data = get_userdata($user_id);
735 $user_email = $user_data->user_email;
736 $is_purchased = wc_customer_bought_product($user_email, $user_id, $response->data['id']);
737 }
738 $response->data['is_purchased'] = $is_purchased;
739 if (!empty($woocommerce_wpml->multi_currency) && !empty($woocommerce_wpml->settings['currencies_order'])) {
740
741 $price = $response->data['price'];
742
743 foreach ($woocommerce_wpml->settings['currency_options'] as $key => $currency) {
744 $rate = (float)$currency["rate"];
745 $response->data['multi-currency-prices'][$key]['price'] = $rate == 0 ? $price : sprintf("%.2f", $price * $rate);
746 }
747 }
748
749 /*Update product price for subscription product*/
750 if($object->get_type() == 'subscription_variation'){
751 $meta_data = $response->data['meta_data'];
752 $sign_up_fee = null;
753 foreach ($meta_data as $meta_data_item) {
754 if ($meta_data_item->get_data()["key"] == "_subscription_sign_up_fee") {
755 $sign_up_fee = $meta_data_item->get_data()["value"];
756 }
757 }
758 if($sign_up_fee != null){
759 $response->data['regular_price']= $sign_up_fee;
760 $response->data['price']= $sign_up_fee;
761 }
762 }
763
764 if (class_exists('WooCommerceWholeSalePrices')) {
765 $meta_data = $response->data['meta_data'];
766 foreach ($meta_data as $v) {
767 $key = is_array($v) ? $v['key'] : $v->__get('key');
768 if ((strpos($key, '_wholesale_price') !== false && strpos($key, '_have_wholesale_price') === false) || (strpos($key, '_wholesale_sale_price') !== false && strpos($key, '_have_wholesale_sale_price') === false)) {
769 if($v->__get('value')){
770 $wholesale_price = wc_get_price_to_display( $object, array( 'price' => $v->__get('value') ) );
771 $v->__set("value", number_format($wholesale_price, 2, '.', ''));
772 $v->apply_changes();
773 }
774 }
775 }
776 }
777
778 $variation_product = wc_get_product( $response->data['id'] );
779 if($variation_product) {
780 $_product = wc_get_product( $variation_product->get_parent_id() );
781 $attributes = array();
782
783 foreach ( $variation_product->get_variation_attributes() as $attribute_name => $attribute ) {
784 $name = str_replace( 'attribute_', '', $attribute_name );
785
786 if ( empty( $attribute ) && '0' !== $attribute ) {
787 continue;
788 }
789
790 // Taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`.
791 if ( 0 === strpos( $attribute_name, 'attribute_pa_' ) ) {
792 $option_term = get_term_by( 'slug', $attribute, $name );
793 $attributes[] = array(
794 'id' => wc_attribute_taxonomy_id_by_name( $name ),
795 'name' => custom_get_attribute_taxonomy_name( $name, $_product ),
796 'option' => $option_term && ! is_wp_error( $option_term ) ? $option_term->name : $attribute,
797 );
798 } else {
799 $attributes[] = array(
800 'id' => 0,
801 'name' => custom_get_attribute_taxonomy_name( $name, $_product ),
802 'option' => $attribute,
803 );
804 }
805 }
806 $response->data['attributes'] = $attributes;
807 }
808 return $response;
809 }
810
811 /**
812 * Wordpress REST API: Support Rest API with Relevanssi.
813 *
814 * Attaches to 'the_posts' filter hook, checks to see if there's a place for a
815 * search and runs relevanssi_do_query() if there is.
816 *
817 * https://www.relevanssi.com/user-manual/using-relevanssi-outside-search-pages/
818 *
819 * @param array $posts An array of post objects.
820 * @param WP_Query $query The WP_Query object, default false.
821 */
822 function flutter_custom_posts_pre_query($posts, $query)
823 {
824 if ($query->is_search() && defined('REST_REQUEST') && REST_REQUEST) {
825 if (function_exists('relevanssi_do_query')) {
826 $posts = relevanssi_do_query($query);
827 $query->relevanssi_found_posts = $query->found_posts;
828 return $posts;
829 }
830 }
831 return $posts;
832 }
833
834 /**
835 * Wordpress REST API: Customize `found_posts` in Rest API with Relevanssi.
836 *
837 * @param int $found_posts The number of posts found.
838 * @param WP_Query $query The WP_Query object.
839 */
840 function flutter_custom_found_posts($found_posts, $query)
841 {
842 if ($query->is_search() && defined('REST_REQUEST') && REST_REQUEST) {
843 if (function_exists('relevanssi_do_query')) {
844 return $query->relevanssi_found_posts;
845 }
846 }
847 return $found_posts;
848 }
849
850
851 // Prepare data before checkout by webview
852 function flutter_prepare_checkout()
853 {
854
855 if(empty($_GET) && isset($_SERVER['HTTP_REFERER'])){
856 $url_components = parse_url($_SERVER['HTTP_REFERER']);
857 if (isset($url_components['query'])) {
858 parse_str($url_components['query'], $params);
859 if(!empty($params)){
860 $_GET = $params;
861 }
862 }
863 }
864
865 if (isset($_GET['mobile']) && isset($_GET['code'])) {
866
867 $code = sanitize_text_field($_GET['code']);
868 global $wpdb;
869 $table_name = $wpdb->prefix . "mstore_checkout";
870 $sql = $wpdb->prepare("SELECT * FROM $table_name WHERE code = %s", $code);
871 $item = $wpdb->get_row($sql);
872 if ($item) {
873 $data = json_decode(urldecode(base64_decode($item->order)), true);
874 } else {
875 return var_dump("Can't not get the order");
876 }
877
878 $shipping = isset($data['shipping']) ? $data['shipping'] : NULL;
879 $billing = isset($data['billing']) ? $data['billing'] : $shipping;
880
881 if (isset($data['token'])) {
882 // Validate the cookie token
883 $userId = validateCookieLogin($data['token']);
884 if(!is_wp_error($userId)){
885 if (isset($billing)) {
886 if(isset($billing["first_name"]) && !empty($billing["first_name"])){
887 update_user_meta($userId, 'billing_first_name', $billing["first_name"]);
888 update_user_meta($userId, 'shipping_first_name', $billing["first_name"]);
889 }
890 if(isset($billing["last_name"]) && !empty($billing["last_name"])){
891 update_user_meta($userId, 'billing_last_name', $billing["last_name"]);
892 update_user_meta($userId, 'shipping_last_name', $billing["last_name"]);
893 }
894 if(isset($billing["company"]) && !empty($billing["company"])){
895 update_user_meta($userId, 'billing_company', $billing["company"]);
896 update_user_meta($userId, 'shipping_company', $billing["company"]);
897 }
898 if(isset($billing["address_1"]) && !empty($billing["address_1"])){
899 update_user_meta($userId, 'billing_address_1', $billing["address_1"]);
900 update_user_meta($userId, 'shipping_address_1', $billing["address_1"]);
901 }
902 if(isset($billing["address_2"]) && !empty($billing["address_2"])){
903 update_user_meta($userId, 'billing_address_2', $billing["address_2"]);
904 update_user_meta($userId, 'shipping_address_2', $billing["address_2"]);
905 }
906 if(isset($billing["city"]) && !empty($billing["city"])){
907 update_user_meta($userId, 'billing_city', $billing["city"]);
908 update_user_meta($userId, 'shipping_city', $billing["city"]);
909 }
910 if(isset($billing["state"]) && !empty($billing["state"])){
911 update_user_meta($userId, 'billing_state', $billing["state"]);
912 update_user_meta($userId, 'shipping_state', $billing["state"]);
913 }
914 if(isset($billing["postcode"]) && !empty($billing["postcode"])){
915 update_user_meta($userId, 'billing_postcode', $billing["postcode"]);
916 update_user_meta($userId, 'shipping_postcode', $billing["postcode"]);
917 }
918 if(isset($billing["country"]) && !empty($billing["country"])){
919 update_user_meta($userId, 'billing_country', $billing["country"]);
920 update_user_meta($userId, 'shipping_country', $billing["country"]);
921 }
922 if(isset($billing["email"]) && !empty($billing["email"])){
923 update_user_meta($userId, 'billing_email', $billing["email"]);
924 update_user_meta($userId, 'shipping_email', $billing["email"]);
925 }
926 if(isset($billing["phone"]) && !empty($billing["phone"])){
927 update_user_meta($userId, 'billing_phone', $billing["phone"]);
928 update_user_meta($userId, 'shipping_phone', $billing["phone"]);
929 }
930 } else {
931 $billing = [];
932 $shipping = [];
933
934 $billing["first_name"] = get_user_meta($userId, 'billing_first_name', true);
935 $billing["last_name"] = get_user_meta($userId, 'billing_last_name', true);
936 $billing["company"] = get_user_meta($userId, 'billing_company', true);
937 $billing["address_1"] = get_user_meta($userId, 'billing_address_1', true);
938 $billing["address_2"] = get_user_meta($userId, 'billing_address_2', true);
939 $billing["city"] = get_user_meta($userId, 'billing_city', true);
940 $billing["state"] = get_user_meta($userId, 'billing_state', true);
941 $billing["postcode"] = get_user_meta($userId, 'billing_postcode', true);
942 $billing["country"] = get_user_meta($userId, 'billing_country', true);
943 $billing["email"] = get_user_meta($userId, 'billing_email', true);
944 $billing["phone"] = get_user_meta($userId, 'billing_phone', true);
945
946 $shipping["first_name"] = get_user_meta($userId, 'shipping_first_name', true);
947 $shipping["last_name"] = get_user_meta($userId, 'shipping_last_name', true);
948 $shipping["company"] = get_user_meta($userId, 'shipping_company', true);
949 $shipping["address_1"] = get_user_meta($userId, 'shipping_address_1', true);
950 $shipping["address_2"] = get_user_meta($userId, 'shipping_address_2', true);
951 $shipping["city"] = get_user_meta($userId, 'shipping_city', true);
952 $shipping["state"] = get_user_meta($userId, 'shipping_state', true);
953 $shipping["postcode"] = get_user_meta($userId, 'shipping_postcode', true);
954 $shipping["country"] = get_user_meta($userId, 'shipping_country', true);
955 $shipping["email"] = get_user_meta($userId, 'shipping_email', true);
956 $shipping["phone"] = get_user_meta($userId, 'shipping_phone', true);
957
958 if (isset($billing["first_name"]) && !isset($shipping["first_name"])) {
959 $shipping = $billing;
960 }
961 if (!isset($billing["first_name"]) && isset($shipping["first_name"])) {
962 $billing = $shipping;
963 }
964 }
965
966 // Check user and authentication
967 $user = get_userdata($userId);
968 if ($user && (!is_user_logged_in() || get_current_user_id() !== $userId)) {
969 wp_set_current_user($userId, $user->user_login);
970 wp_set_auth_cookie($userId);
971
972 header("Refresh:0");
973 }
974 cleanupAppointmentCartData($userId);
975 }
976 } else {
977 if (is_user_logged_in()) {
978 wp_logout();
979 wp_set_current_user(0);
980 header("Refresh:0");
981 }
982 }
983
984 if (is_plugin_active('woocommerce/woocommerce.php') == true) {
985 //header("Content-Security-Policy: frame-ancestors 'self' *.yourdomain.com");
986 global $woocommerce;
987 WC()->session->set('refresh_totals', true);
988 WC()->cart->empty_cart();
989
990 $products = $data['line_items'];
991
992 buildCartItemData($products, function($productId, $quantity, $variationId, $attributes, $cart_item_data){
993 global $woocommerce;
994 $woocommerce->cart->add_to_cart($productId, $quantity, $variationId, $attributes, $cart_item_data);
995 });
996
997 if (isset($shipping)) {
998 $woocommerce->customer->set_shipping_first_name($shipping["first_name"]);
999 $woocommerce->customer->set_shipping_last_name($shipping["last_name"]);
1000 $woocommerce->customer->set_shipping_company($shipping["company"]);
1001 $woocommerce->customer->set_shipping_address_1($shipping["address_1"]);
1002 $woocommerce->customer->set_shipping_address_2($shipping["address_2"]);
1003 $woocommerce->customer->set_shipping_city($shipping["city"]);
1004 $woocommerce->customer->set_shipping_state($shipping["state"]);
1005 $woocommerce->customer->set_shipping_postcode($shipping["postcode"]);
1006 $woocommerce->customer->set_shipping_country($shipping["country"]);
1007 }
1008
1009 if (isset($billing)) {
1010 $woocommerce->customer->set_billing_first_name($billing["first_name"]);
1011 $woocommerce->customer->set_billing_last_name($billing["last_name"]);
1012 $woocommerce->customer->set_billing_company($billing["company"]);
1013 $woocommerce->customer->set_billing_address_1($billing["address_1"]);
1014 $woocommerce->customer->set_billing_address_2($billing["address_2"]);
1015 $woocommerce->customer->set_billing_city($billing["city"]);
1016 $woocommerce->customer->set_billing_state($billing["state"]);
1017 $woocommerce->customer->set_billing_postcode($billing["postcode"]);
1018 $woocommerce->customer->set_billing_country($billing["country"]);
1019 $woocommerce->customer->set_billing_email($billing["email"]);
1020 $woocommerce->customer->set_billing_phone($billing["phone"]);
1021 }
1022
1023 if (!empty($data['coupon_lines'])) {
1024 $coupons = $data['coupon_lines'];
1025 foreach ($coupons as $coupon) {
1026 $woocommerce->cart->add_discount($coupon['code']);
1027 }
1028 }
1029
1030 if (!empty($data['shipping_lines'])) {
1031 $shippingLines = $data['shipping_lines'];
1032 $shippingMethods = [];
1033 foreach ($shippingLines as $key => $shippingLine) {
1034 if (is_plugin_active('wc-multivendor-marketplace/wc-multivendor-marketplace.php') && !empty($shippingLine['meta_data'])) {
1035 $seller_meta = array_filter($shippingLine['meta_data'],function($item){
1036 return $item['key'] == 'seller_id';
1037 });
1038 if(!empty($seller_meta)){
1039 $shippingMethods[$seller_meta[0]['value']] = $shippingLine['method_id'];
1040 }else{
1041 $shippingMethods[] = $shippingLine['method_id'];
1042 }
1043 } else{
1044 $shippingMethods[] = $shippingLine['method_id'];
1045 }
1046 }
1047 WC()->session->set('chosen_shipping_methods', $shippingMethods);
1048 }
1049 if (!empty($data['payment_method'])) {
1050 WC()->session->set('chosen_payment_method', $data['payment_method']);
1051 }
1052
1053 if (isset($data['customer_note']) && !empty($data['customer_note'])) {
1054 $_POST["order_comments"] = sanitize_text_field($data['customer_note']);
1055 $checkout_fields = WC()->checkout->__get("checkout_fields");
1056 $checkout_fields["order"] = ["order_comments" => ["type" => "textarea", "class" => [], "label" => "Order notes", "placeholder" => "Notes about your order, e.g. special notes for delivery."]];
1057 WC()->checkout->__set("checkout_fields", $checkout_fields);
1058 }
1059 }
1060 }
1061
1062 if (isset($_GET['cookie'])) {
1063 $cookie = urldecode(base64_decode(sanitize_text_field($_GET['cookie'])));
1064 $userId = validateCookieLogin($cookie);
1065 if (!is_wp_error($userId)) {
1066 $user = get_userdata($userId);
1067 if ($user !== false) {
1068 $cookie_elements = explode('|', $cookie);
1069 if (count($cookie_elements) == 4) {
1070 list($username, $expiration, $token, $hmac) = $cookie_elements;
1071 $_COOKIE[LOGGED_IN_COOKIE] = $cookie;
1072 wp_set_current_user($userId, $user->user_login);
1073 wp_set_auth_cookie($userId, true, '', $token);
1074 }
1075
1076 if (isset($_GET['order_detail']) && isset($_GET['order_id']) && is_plugin_active('dokan-lite/dokan.php')) {
1077 $url = add_query_arg(
1078 [
1079 'order_id' => $_GET['order_id'],
1080 '_wpnonce' => wp_create_nonce('dokan_view_order'),
1081 ],
1082 dokan_get_navigation_url('orders')
1083 );
1084 wp_safe_redirect($url);
1085 die;
1086 }
1087
1088 if (isset($_GET['vendor_admin'])) {
1089 global $wp;
1090 $request = $wp->request;
1091 wp_redirect(esc_url_raw(home_url("/" . $request)));
1092 die;
1093 }
1094 }
1095 }
1096 }
1097 }
1098
1099 function set_author_in_for_vendor_staff( $query ) {
1100 global $post;
1101 if ( class_exists('WeDevs_Dokan') && dokan()->is_pro_exists() && isset( $query->query['post_type'] ) && $query->query['post_type'] === 'global_product_addon' && $post != null ) {
1102 $vendor = dokan_get_vendor_by_product( $post->ID );
1103
1104 // Check if $vendor is valid before proceeding
1105 if (!$vendor) {
1106 return;
1107 }
1108
1109 if ( function_exists( 'dokan_get_vendor_staff' ) ) {
1110 $vendor_staffs = dokan_get_vendor_staff( $vendor->get_id() );
1111 if ( ! in_array( $vendor->get_id(), $vendor_staffs, true ) ) {
1112 return;
1113 }
1114 $query->set( 'author__in', $vendor_staffs );
1115 }
1116 }
1117 }
1118
1119 // Add product image to order
1120 add_filter('woocommerce_rest_prepare_shop_order_object', 'custom_woocommerce_rest_prepare_shop_order_object', 10, 1);
1121 function custom_woocommerce_rest_prepare_shop_order_object($response)
1122 {
1123 if (empty($response->data) || empty($response->data['line_items'])) {
1124 return $response;
1125 }
1126 $api = new WC_REST_Products_Controller();
1127 $req = new WP_REST_Request('GET');
1128 $line_items = [];
1129 foreach ($response->data['line_items'] as $item) {
1130 $product_id = $item['product_id'];
1131 $req->set_query_params(["id" => $product_id]);
1132 $res = $api->get_item($req);
1133 if (is_wp_error($res)) {
1134 $item["product_data"] = null;
1135 } else {
1136 $item["product_data"] = $res->get_data();
1137 }
1138 $line_items[] = $item;
1139
1140 }
1141 $response->data['line_items'] = $line_items;
1142
1143 // Get payment method
1144 $payment_method = $response->data['payment_method'];
1145 $payment_method_title = $response->data['payment_method_title'];
1146 $order_id = $response->data['id'];
1147
1148 // Get default Bank transfer info
1149 if ($payment_method == 'bacs' && class_exists('WC_Gateway_BACS')) {
1150 $bacs = new WC_Gateway_BACS();
1151 $bacs_accounts = apply_filters('woocommerce_bacs_accounts', $bacs->account_details, $order_id);
1152 $response->data['bacs_info'] = $bacs_accounts;
1153 }
1154
1155 // Get other Bank transfer info
1156 if (strpos($payment_method, 'bank_transfer') !== false && is_plugin_active('fr-multi-bank-transfer-payment-gateways-for-woocommerce/fr-multi-bank-transfer-gateways-for-woocommerce.php')) {
1157 require_once ABSPATH . 'wp-content/plugins/fr-multi-bank-transfer-payment-gateways-for-woocommerce/includes/gateways/class-fr-multi-bank-transfer-gateways-for-woocommerce-bank-transfer.php';
1158
1159 $bacs = new Fr_Multi_Bank_Transfer_Gateways_For_Woocommerce_Bank_Transfer([
1160 'id' => $payment_method,
1161 'method_title' => $payment_method_title,
1162 ]);
1163 $bacs_accounts = apply_filters('woocommerce_bacs_accounts', $bacs->account_details, $order_id);
1164 $response->data['bacs_info'] = $bacs_accounts;
1165 }
1166
1167 return $response;
1168 }
1169
1170
1171 function mstore_register_order_refund_requested_order_status()
1172 {
1173 register_post_status('wc-refund-req', array(
1174 'label' => esc_attr__('Refund Requested'),
1175 'public' => true,
1176 'show_in_admin_status_list' => true,
1177 'show_in_admin_all_list' => true,
1178 'exclude_from_search' => false,
1179 'label_count' => _n_noop('Refund requested <span class="count">(%s)</span>', 'Refund requested <span class="count">(%s)</span>')
1180 ));
1181 }
1182
1183 add_action('init', 'mstore_register_order_refund_requested_order_status');
1184
1185
1186 function mstore_add_custom_order_statuses($order_statuses)
1187 {
1188 // Create new status array.
1189 $new_order_statuses = array();
1190 // Loop though statuses.
1191 foreach ($order_statuses as $key => $status) {
1192 // Add status to our new statuses.
1193 $new_order_statuses[$key] = $status;
1194 // Add our custom statuses.
1195 if ('wc-processing' === $key) {
1196 $new_order_statuses['wc-refund-req'] = esc_attr__('Refund Requested');
1197 }
1198 }
1199
1200 return $new_order_statuses;
1201 }
1202
1203 add_filter('wc_order_statuses', 'mstore_add_custom_order_statuses');
1204
1205
1206 function custom_status_bulk_edit($actions)
1207 {
1208 // Add order status changes.
1209 $actions['mark_refund-req'] = __('Change status to refund requested');
1210
1211 return $actions;
1212 }
1213
1214 add_filter('bulk_actions-edit-shop_order', 'custom_status_bulk_edit', 20, 1);
1215
1216 add_action('rest_api_init', 'get_promptpay_qrcode_routes');
1217 function get_promptpay_qrcode_routes()
1218 {
1219 register_rest_route('promptpay', '/detail' . '/(?P<id>[\d]+)', array(
1220 'methods' => 'GET',
1221 'callback' => function($request){
1222 $available_payment_methods = WC()->payment_gateways()->payment_gateways();
1223 $paymentMethod = $available_payment_methods['thai-promptpay-easy'];
1224 $order = wc_get_order($request['id'] );
1225 $thank_msg = $paymentMethod->thank_msg;
1226 $promptpay_id = $paymentMethod->promptpay_id;
1227 $promptpay_type = $paymentMethod->promptpay_type;
1228 $promptpay_name = $paymentMethod->promptpay_name;
1229 $include_price = $paymentMethod->include_price;
1230 $image_url = get_site_url() . "/wp-content/plugins/thai-promptpay-payment-easy-gateway-plugin/images/promptpay_qrcode/promptpay-qr-l.php?type=$promptpay_type&promptpay_id=$promptpay_id";
1231
1232 if($include_price=='yes'){
1233 $price = $order->get_total();
1234 $image_url .= "&price=$price&p=1";
1235 }
1236
1237
1238 return ['thank_msg' => $thank_msg, 'qrcode_url' => $image_url, 'promptpay_id' => $promptpay_id, 'promptpay_name' => $promptpay_name, 'promptpay_type' => $paymentMethod->promptpay_type_name[$promptpay_type]];
1239 },
1240 'permission_callback' => function () {
1241 return true;
1242 },
1243 )
1244 );
1245 }
1246
1247 // Check if Woo Variation Swatches is active before adding the filter
1248 if (!function_exists('add_variation_swatches_attribute_images_to_api')) {
1249 function add_variation_swatches_attribute_images_to_api($response, $product, $request) {
1250 // First check if the plugin is active
1251 if (!function_exists('woo_variation_swatches') || !class_exists('Woo_Variation_Swatches')) {
1252 return $response;
1253 }
1254
1255 $data = $response->get_data();
1256
1257 // Only proceed if product has attributes
1258 if (empty($data['attributes'])) {
1259 return $response;
1260 }
1261
1262 $product_attributes = $product->get_attributes();
1263
1264 foreach ($data['attributes'] as $key => $attribute) {
1265 $attribute_name = $attribute['name'];
1266 $attribute_obj = isset($product_attributes[$attribute_name]) ? $product_attributes[$attribute_name] : null;
1267
1268 if ($attribute_obj && $attribute_obj->is_taxonomy()) {
1269 $terms = wp_get_post_terms($product->get_id(), $attribute_name, ['fields' => 'all']);
1270
1271 foreach ($terms as $term_key => $term) {
1272 // Get image ID from term meta set by Woo Variation Swatches
1273 $image_id = get_term_meta($term->term_id, 'product_attribute_image', true);
1274
1275 if ($image_id) {
1276 $image_size = woo_variation_swatches()->get_option('attribute_image_size', 'variation_swatches_image_size');
1277 $image_src = wp_get_attachment_image_src($image_id, $image_size);
1278
1279 if ($image_src) {
1280 // Add image data to the term in the API response
1281 $data['attributes'][$key]['options'][$term_key] = [
1282 'term_id' => $term->term_id,
1283 'name' => $term->name,
1284 'slug' => $term->slug,
1285 'image' => [
1286 'src' => $image_src[0],
1287 'width' => $image_src[1],
1288 'height' => $image_src[2]
1289 ]
1290 ];
1291 }
1292 }
1293 }
1294 }
1295 }
1296
1297 $response->set_data($data);
1298 return $response;
1299 }
1300 }
1301
1302 // Only add the filter if the function exists (as an extra check)
1303 if (function_exists('add_variation_swatches_attribute_images_to_api')) {
1304 add_filter('woocommerce_rest_prepare_product_object', 'add_variation_swatches_attribute_images_to_api', 10, 3);
1305 }