PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.5.1
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.5.1
3.0.3 3.0.2 3.0.1 trunk 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.3.0 2.3.1 2.3.10 2.3.11 2.3.12 2.3.13 2.3.14 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.7.91 2.7.92 2.7.93 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0
commercebird / includes / classes / class-common.php
commercebird / includes / classes Last commit date
apis 9 months ago purchase-orders 9 months ago zoho-crm 9 months ago zoho-inventory 9 months ago class-api-handler-zoho.php 9 months ago class-auth-zoho.php 9 months ago class-common.php 9 months ago class-plugin.php 9 months ago class-wc-api.php 10 months ago index.php 1 year ago
class-common.php
247 lines
1 <?php
2 /**
3 * Common class function.
4 */
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 if ( ! class_exists( 'CMBIRD_Common_Functions' ) ) {
10 class CMBIRD_Common_Functions {
11
12
13 public function __construct() {
14 add_action( 'woocommerce_thankyou', array( $this, 'cmbird_sync_frontend_order' ) );
15 add_action( 'woocommerce_rest_insert_shop_order_object', array( $this, 'cmbird_on_insert_rest_api' ), 20, 3 );
16 add_filter( 'wcs_renewal_order_created', array( $this, 'cmbird_zi_sync_renewal_order' ), 10, 2 );
17 add_action( 'wp_ajax_zoho_admin_order_sync', array( $this, 'cmbird_zoho_order_sync' ) );
18 }
19
20 /**
21 * Sync order when it's created via the checkout.
22 */
23 public function cmbird_sync_frontend_order( $order_id ) {
24 // return if the order is not coming via thank you page.
25 if ( ! is_wc_endpoint_url( 'order-received' ) ) {
26 return;
27 }
28 // Check if the transient flag is set.
29 if ( get_transient( 'cmbird_thankyou_callback_executed_' . $order_id ) ) {
30 return;
31 }
32 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
33 // First sync the customer to Zoho Inventory if the access token is set.
34 if ( is_string( $zoho_inventory_access_token ) && trim( $zoho_inventory_access_token ) !== '' ) {
35 $zi_order_class = new CMBIRD_Order_Sync_ZI();
36 $zi_order_class->cmbird_zi_sync_customer_checkout( $order_id );
37 // Use WC Action Scheduler to sync the order to Zoho Inventory.
38 $existing_schedule = as_has_scheduled_action( 'sync_zi_order', array( $order_id ) );
39 if ( ! $existing_schedule ) {
40 as_schedule_single_action( time(), 'sync_zi_order', array( $order_id ) );
41 // Set the transient flag to prevent multiple executions.
42 set_transient( 'cmbird_thankyou_callback_executed_' . $order_id, true, 60 );
43 }
44 }
45 $zoho_crm_access_token = get_option( 'cmbird_zoho_crm_access_token' );
46 // If the access token is set, sync the order to Zoho CRM.
47 if ( is_string( $zoho_crm_access_token ) && trim( $zoho_crm_access_token ) !== '' ) {
48 // Use WC Action Scheduler to sync the order to Zoho CRM.
49 $existing_schedule = as_has_scheduled_action( 'sync_zcrm_order', array( $order_id ) );
50 if ( ! $existing_schedule ) {
51 as_schedule_single_action( time(), 'sync_zcrm_order', array( $order_id ) );
52 // Set the transient flag to prevent multiple executions.
53 set_transient( 'cmbird_thankyou_callback_executed_' . $order_id, true, 60 );
54 }
55 }
56 }
57
58 /**
59 * Sync order when its scheduled via the Action Scheduler.
60 *
61 * @param int $order_id Order ID.
62 * @return void
63 */
64 public function cmbird_orders_prepare_sync() {
65 $args = func_get_args();
66 $order_id = $args[0];
67 if ( get_option( 'cmbird_zoho_inventory_access_token' ) && $order_id ) {
68 $zi_order_class = new CMBIRD_Order_Sync_ZI();
69 $zi_order_class->zi_order_sync( $order_id );
70 }
71 if ( get_option( 'cmbird_zoho_crm_access_token' ) && $order_id ) {
72 $zcrm_order_class = new CMBIRD_ZCRM_SalesOrder();
73 // Sync the order to Zoho CRM.
74 $zcrm_order_class->cmbird_zcrm_order_sync( $order_id );
75 }
76 }
77
78 /**
79 * Sync order when it's created via the WC API.
80 *
81 * @param WC_Data $object Inserted object.
82 * @param WP_REST_Request $request Request object.
83 * @param boolean $creating True when creating object, false when updating.
84 */
85 public function cmbird_on_insert_rest_api( $object, $request, $is_creating ) {
86 // $fd = fopen(__DIR__ . '/on_insert_rest_api.txt', 'w+');
87 $request_body = $request->get_body();
88 $request_body_array = json_decode( $request_body, true );
89 $order_status = $request_body_array['status'];
90 $order_id = $object->get_id();
91
92 if ( get_option( 'cmbird_zoho_inventory_access_token' ) ) {
93 $zi_order_class = new CMBIRD_Order_Sync_ZI();
94 // Check how many keys there are in the request body array. If there are only two keys then we don't need to do anything.
95 if ( count( $request_body_array ) === 2 ) {
96 if ( in_array( $order_status, array( 'cancelled', 'wc-merged' ) ) ) {
97 $zi_order_class->salesorder_void( $order_id );
98 }
99 } else {
100 $zi_order_class->zi_order_sync( $order_id );
101 }
102 }
103 // same for Zoho CRM.
104 if ( get_option( 'cmbird_zoho_crm_access_token' ) ) {
105 $zcrm_order_class = new CMBIRD_ZCRM_SalesOrder();
106 // Check how many keys there are in the request body array. If there are only two keys then we don't need to do anything.
107 if ( count( $request_body_array ) === 2 ) {
108 return;
109 } else {
110 $zcrm_order_class->cmbird_zcrm_order_sync( $order_id );
111 }
112 }
113
114 // fclose($fd);
115 }
116
117 /**
118 * Sync Renewal Order to Zoho once it's created.
119 */
120 public function cmbird_zi_sync_renewal_order( $renewal_order, $subscription ) {
121 $order_id = $renewal_order->get_id();
122
123 // Sync the order to Zoho CRM.
124 if ( get_option( 'cmbird_zoho_crm_access_token' ) ) {
125 $zcrm_order_class = new CMBIRD_ZCRM_SalesOrder();
126 $zcrm_order_class->cmbird_zcrm_order_sync( $order_id );
127 }
128
129 // Sync the order to Zoho Inventory.
130 if ( get_option( 'cmbird_zoho_inventory_access_token' ) ) {
131 $zi_order_class = new CMBIRD_Order_Sync_ZI();
132 $zi_order_class->zi_order_sync( $order_id );
133 }
134
135 return $renewal_order;
136 }
137
138 /**
139 * Sync a WooCommerce order to Zoho CRM and Zoho Inventory.
140 *
141 * Called via AJAX from the Zoho Order Sync admin page.
142 *
143 * @param int $order_id The order ID to sync.
144 *
145 * @return void
146 */
147 public function cmbird_zoho_order_sync( $order_id ) {
148 if ( ! $order_id && isset( $_POST['nonce'], $_POST['arg_order_data'] ) ) {
149 if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'zoho_admin_order_sync' ) ) {
150 wp_send_json_error( 'Nonce verification failed' );
151 }
152 $order_id = sanitize_text_field( wp_unslash( $_POST['arg_order_data'] ) );
153 }
154
155 if ( $order_id <= 0 ) {
156 wp_send_json_error( 'Invalid order ID.' );
157 }
158
159 // Sync the order to Zoho CRM.
160 if ( get_option( 'cmbird_zoho_crm_access_token' ) ) {
161 $zcrm_order_class = new CMBIRD_ZCRM_SalesOrder();
162 $zcrm_order_class->cmbird_zcrm_order_sync( $order_id );
163 }
164
165 // Sync the order to Zoho Inventory.
166 if ( get_option( 'cmbird_zoho_inventory_access_token' ) ) {
167 $zi_order_class = new CMBIRD_Order_Sync_ZI();
168 $zi_order_class->zi_order_sync( $order_id );
169 }
170
171 wp_send_json_success( 'Order synced successfully.' );
172 }
173
174 /**
175 * Function to clear all orphan data.
176 */
177 public function clear_orphan_data() {
178 global $wpdb;
179 // Delete orphaned product variations.
180 $deleted_variations = absint(
181 $wpdb->query(
182 "DELETE products
183 FROM {$wpdb->posts} products
184 LEFT JOIN {$wpdb->posts} wp ON wp.ID = products.post_parent
185 WHERE wp.ID IS NULL AND products.post_type = 'product_variation';"
186 )
187 );
188 // Delete orphaned postmeta.
189 $deleted_postmeta = absint(
190 $wpdb->query(
191 "DELETE pm
192 FROM {$wpdb->postmeta} pm
193 LEFT JOIN {$wpdb->posts} wp ON wp.ID = pm.post_id
194 WHERE wp.ID IS NULL;"
195 )
196 );
197 // Return the number of deleted entries (orphaned variations + orphaned postmeta).
198 return $deleted_variations + $deleted_postmeta;
199 }
200
201 /**
202 * Get the tax class based on the tax percentage.
203 *
204 * @param float $percentage The tax percentage.
205 * @return string|false The tax class if found, or standard if not found.
206 */
207 public function get_tax_class_by_percentage( $percentage ) {
208 // $fd = fopen( __DIR__ . '/get_tax_class_by_percentage.txt', 'a+' );
209
210 global $wpdb;
211 // Determine the number of decimal places in the provided percentage.
212 $decimal_places = strlen( substr( strrchr( $percentage, '.' ), 1 ) );
213
214 // Round the percentage to the determined number of decimal places.
215 $rounded_percentage = round( $percentage, $decimal_places );
216 $tax_rates = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates WHERE ROUND(tax_rate, %d) = %f", $decimal_places, $rounded_percentage ) );
217
218 // If tax rates are found.
219 if ( $tax_rates ) {
220 // Get the tax class from the first matching tax rate.
221 $tax_class = $tax_rates[0]->tax_rate_class;
222 return $tax_class;
223 } else {
224 // Return null if no tax rates match the provided percentage.
225 return 'standard';
226 }
227 }
228
229 /**
230 * Send email to admin.
231 *
232 * @param string $subject The subject of the email.
233 * @param string $message The message body of the email.
234 *
235 * @return void
236 */
237 public function send_email( $subject, $message ) {
238 $admin_email = get_option( 'admin_email' );
239 $headers = array( 'Content-Type: text/html; charset=UTF-8' );
240
241 // Send the email.
242 wp_mail( $admin_email, $subject, $message, $headers );
243 }
244 }
245 }
246 $cmbird_common_functions = new CMBIRD_Common_Functions();
247