PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.6.3
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.6.3
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 6 months ago purchase-orders 6 months ago zoho-crm 6 months ago zoho-inventory 6 months ago class-api-handler-zoho.php 6 months ago class-auth-zoho.php 6 months ago class-common.php 6 months ago class-plugin.php 9 months ago class-wc-api.php 9 months ago index.php 1 year ago
class-common.php
269 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 if ( ! class_exists( 'CMBIRD_Common_Functions' ) ) {
8 /**
9 * Common functions for CommerceBird plugin integration with Zoho.
10 */
11 class CMBIRD_Common_Functions {
12
13 /**
14 * Constructor.
15 */
16 public function __construct() {
17 add_action( 'woocommerce_thankyou', array( $this, 'cmbird_sync_frontend_order' ) );
18 add_action( 'woocommerce_rest_insert_shop_order_object', array( $this, 'cmbird_on_insert_rest_api' ), 20, 3 );
19 add_filter( 'wcs_renewal_order_created', array( $this, 'cmbird_zi_sync_renewal_order' ), 10, 2 );
20 add_action( 'wp_ajax_zoho_admin_order_sync', array( $this, 'cmbird_zoho_order_sync' ) );
21 }
22
23 /**
24 * Sync order when it's created via the checkout.
25 *
26 * @param int $order_id The order ID.
27 * @return void
28 */
29 public function cmbird_sync_frontend_order( $order_id ) {
30 // return if the order is not coming via thank you page.
31 if ( ! is_wc_endpoint_url( 'order-received' ) ) {
32 return;
33 }
34 // Check if the transient flag is set.
35 if ( get_transient( 'cmbird_thankyou_callback_executed_' . $order_id ) ) {
36 return;
37 }
38 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
39 // First sync the customer to Zoho Inventory if the access token is set.
40 if ( is_string( $zoho_inventory_access_token ) && trim( $zoho_inventory_access_token ) !== '' ) {
41 $zi_order_class = new CMBIRD_Order_Sync_ZI();
42 $zi_order_class->cmbird_zi_sync_customer_checkout( $order_id );
43 // Use WC Action Scheduler to sync the order to Zoho Inventory.
44 $existing_schedule = as_has_scheduled_action( 'sync_zi_order', array( $order_id ) );
45 if ( ! $existing_schedule ) {
46 as_schedule_single_action( time(), 'sync_zi_order', array( $order_id ) );
47 // Set the transient flag to prevent multiple executions.
48 set_transient( 'cmbird_thankyou_callback_executed_' . $order_id, true, 60 );
49 }
50 }
51 $zoho_crm_access_token = get_option( 'cmbird_zoho_crm_access_token' );
52 // If the access token is set, sync the order to Zoho CRM.
53 if ( is_string( $zoho_crm_access_token ) && trim( $zoho_crm_access_token ) !== '' ) {
54 // Use WC Action Scheduler to sync the order to Zoho CRM.
55 $existing_schedule = as_has_scheduled_action( 'sync_zcrm_order', array( $order_id ) );
56 if ( ! $existing_schedule ) {
57 as_schedule_single_action( time(), 'sync_zcrm_order', array( $order_id ) );
58 // Set the transient flag to prevent multiple executions.
59 set_transient( 'cmbird_thankyou_callback_executed_' . $order_id, true, 60 );
60 }
61 }
62 }
63
64 /**
65 * Sync order when its scheduled via the Action Scheduler.
66 *
67 * @return void
68 */
69 public function cmbird_orders_prepare_sync() {
70 $args = func_get_args();
71 $order_id = $args[0] ?? null;
72 if ( get_option( 'cmbird_zoho_inventory_access_token' ) && $order_id ) {
73 try {
74 $zi_order_class = new CMBIRD_Order_Sync_ZI();
75 $zi_order_class->zi_order_sync( $order_id );
76 } catch ( \Throwable $e ) {
77 error_log( 'Error in cmbird_orders_prepare_sync: ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Required for security logging.
78 }
79 }
80 if ( get_option( 'cmbird_zoho_crm_access_token' ) && $order_id ) {
81 try {
82 $zcrm_order_class = new CMBIRD_ZCRM_SalesOrder();
83 $zcrm_order_class->cmbird_zcrm_order_sync( $order_id );
84 } catch ( \Throwable $e ) {
85 error_log( 'Error in cmbird_orders_prepare_sync: ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Required for security logging.
86 }
87 }
88 }
89
90 /**
91 * Sync order when it's created via the WC API.
92 *
93 * @param WC_Data $object Inserted object.
94 * @param WP_REST_Request $request Request object.
95 * @param boolean $is_creating True when creating object, false when updating.
96 */
97 public function cmbird_on_insert_rest_api( $object, $request, $is_creating ) {
98 // $fd = fopen(__DIR__ . '/on_insert_rest_api.txt', 'w+');
99 $request_body = $request->get_body();
100 $request_body_array = json_decode( $request_body, true );
101 $order_status = $request_body_array['status'];
102 $order_id = $object->get_id();
103
104 if ( get_option( 'cmbird_zoho_inventory_access_token' ) ) {
105 $zi_order_class = new CMBIRD_Order_Sync_ZI();
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 if ( in_array( $order_status, array( 'cancelled', 'wc-merged' ) ) ) {
109 $zi_order_class->salesorder_void( $order_id );
110 }
111 } else {
112 $zi_order_class->zi_order_sync( $order_id );
113 }
114 }
115 // same for Zoho CRM.
116 if ( get_option( 'cmbird_zoho_crm_access_token' ) ) {
117 $zcrm_order_class = new CMBIRD_ZCRM_SalesOrder();
118 // 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.
119 if ( count( $request_body_array ) === 2 ) {
120 return;
121 } else {
122 $zcrm_order_class->cmbird_zcrm_order_sync( $order_id );
123 }
124 }
125
126 // fclose($fd);
127 }
128
129 /**
130 * Sync Renewal Order to Zoho once it's created.
131 *
132 * @param WC_Order $renewal_order The renewal order object.
133 * @param WC_Subscription $subscription The subscription object.
134 * @return WC_Order The renewal order object.
135 */
136 public function cmbird_zi_sync_renewal_order( $renewal_order, $subscription ) {
137 $order_id = $renewal_order->get_id();
138
139 // Sync the order to Zoho CRM.
140 if ( get_option( 'cmbird_zoho_crm_access_token' ) ) {
141 $zcrm_order_class = new CMBIRD_ZCRM_SalesOrder();
142 $zcrm_order_class->cmbird_zcrm_order_sync( $order_id );
143 }
144
145 // Sync the order to Zoho Inventory.
146 if ( get_option( 'cmbird_zoho_inventory_access_token' ) ) {
147 $zi_order_class = new CMBIRD_Order_Sync_ZI();
148 $zi_order_class->zi_order_sync( $order_id );
149 }
150
151 return $renewal_order;
152 }
153
154 /**
155 * Sync a WooCommerce order to Zoho CRM and Zoho Inventory.
156 *
157 * Called via AJAX from the Zoho Order Sync admin page.
158 *
159 * @param int $order_id The order ID to sync.
160 *
161 * @return void
162 */
163 public function cmbird_zoho_order_sync( $order_id ) {
164 if ( ! $order_id && isset( $_POST['nonce'], $_POST['arg_order_data'] ) ) {
165 if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'zoho_admin_order_sync' ) ) {
166 wp_send_json_error( 'Nonce verification failed' );
167 }
168 $order_id = sanitize_text_field( wp_unslash( $_POST['arg_order_data'] ) );
169 }
170
171 if ( $order_id <= 0 ) {
172 wp_send_json_error( 'Invalid order ID.' );
173 }
174
175 // Sync the order to Zoho CRM.
176 if ( get_option( 'cmbird_zoho_crm_access_token' ) ) {
177 $zcrm_order_class = new CMBIRD_ZCRM_SalesOrder();
178 $zcrm_order_class->cmbird_zcrm_order_sync( $order_id );
179 }
180
181 // Sync the order to Zoho Inventory.
182 if ( get_option( 'cmbird_zoho_inventory_access_token' ) ) {
183 $zi_order_class = new CMBIRD_Order_Sync_ZI();
184 $zi_order_class->zi_order_sync( $order_id );
185 }
186
187 wp_send_json_success( 'Order synced successfully.' );
188 }
189
190 /**
191 * Function to clear all orphan data.
192 */
193 public function clear_orphan_data() {
194 global $wpdb;
195 // Delete orphaned product variations.
196 $deleted_variations = absint(
197 $wpdb->query(
198 "DELETE products
199 FROM {$wpdb->posts} products
200 LEFT JOIN {$wpdb->posts} wp ON wp.ID = products.post_parent
201 WHERE wp.ID IS NULL AND products.post_type = 'product_variation';"
202 )
203 );
204 // Delete orphaned postmeta.
205 $deleted_postmeta = absint(
206 $wpdb->query(
207 "DELETE pm
208 FROM {$wpdb->postmeta} pm
209 LEFT JOIN {$wpdb->posts} wp ON wp.ID = pm.post_id
210 WHERE wp.ID IS NULL;"
211 )
212 );
213 // Return the number of deleted entries (orphaned variations + orphaned postmeta).
214 return $deleted_variations + $deleted_postmeta;
215 }
216
217 /**
218 * Get the tax class based on the tax percentage.
219 *
220 * @param float $percentage The tax percentage.
221 * @return string|false The tax class if found, or standard if not found.
222 */
223 public function get_tax_class_by_percentage( $percentage ) {
224 // $fd = fopen( __DIR__ . '/get_tax_class_by_percentage.txt', 'a+' );
225
226 global $wpdb;
227 // Determine the number of decimal places in the provided percentage.
228 $decimal_places = strlen( substr( strrchr( $percentage, '.' ), 1 ) );
229
230 // Round the percentage to the determined number of decimal places.
231 $rounded_percentage = round( $percentage, $decimal_places );
232
233 // Try to get from cache first.
234 $cache_key = 'cmbird_tax_rate_' . md5( $rounded_percentage . '_' . $decimal_places );
235 $tax_rates = wp_cache_get( $cache_key, 'commercebird' );
236
237 if ( false === $tax_rates ) {
238 $tax_rates = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates WHERE ROUND(tax_rate, %d) = %f", $decimal_places, $rounded_percentage ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Cached above.
239 wp_cache_set( $cache_key, $tax_rates, 'commercebird', HOUR_IN_SECONDS );
240 }
241
242 // If tax rates are found.
243 if ( $tax_rates ) {
244 // Get the tax class from the first matching tax rate.
245 $tax_class = $tax_rates[0]->tax_rate_class;
246 return $tax_class;
247 } else {
248 // Return null if no tax rates match the provided percentage.
249 return 'standard';
250 }
251 } /**
252 * Send email to admin.
253 *
254 * @param string $subject The subject of the email.
255 * @param string $message The message body of the email.
256 *
257 * @return void
258 */
259 public function send_email( $subject, $message ) {
260 $admin_email = get_option( 'admin_email' );
261 $headers = array( 'Content-Type: text/html; charset=UTF-8' );
262
263 // Send the email.
264 wp_mail( $admin_email, $subject, $message, $headers );
265 }
266 }
267 }
268 $cmbird_common_functions = new CMBIRD_Common_Functions();
269