PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.4.6
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.4.6
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 / data-sync.php
commercebird Last commit date
admin 9 months ago includes 9 months ago languages 1 year ago vendor 9 months ago LICENSE 1 year ago commercebird.php 9 months ago composer.json 9 months ago data-sync.php 9 months ago fix-yoda.php 9 months ago index.php 1 year ago readme.txt 9 months ago
data-sync.php
325 lines
1 <?php
2
3 /**
4 * File for ZOHO inventory plugin initialization.
5 *
6 * @category Zoho_Integration
7 * @package commercebird
8 * @author commercebird
9 * @license GNU General Public License v3.0
10 * @link https://commercebird.com
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Zoho Api Error Email
19 *
20 * @param [string] $subject - Email subject.
21 * @param [string] $message - Message.
22 *
23 * @return void
24 */
25 function cmbird_error_log_api_email( $subject, $message ) {
26 // $domain = get_site_url();
27
28 $to = get_bloginfo( 'admin_email' );
29
30 $headers = 'From: ' . $to . "\r\n";
31 $headers .= "MIME-Version: 1.0\r\n";
32 $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
33
34 $messages = '<html><body>';
35 $messages .= '<p>' . $message . '</p>';
36 $messages .= '<p>' . $to . '</p>';
37 $messages .= '</body></html>';
38
39 wp_mail( $to, $subject, $messages, $headers );
40 }
41
42 /**
43 * Function to be called at variable item sync from zoho to woo ajax call.
44 */
45
46 add_action( 'wp_ajax_zoho_ajax_call_variable_item_from_zoho', 'cmbird_ajax_call_variable_item_from_zoho' );
47 function cmbird_ajax_call_variable_item_from_zoho() {
48 // Clear Orphan data.
49 $zi_common_class = new CMBIRD_Common_Functions();
50 $zi_common_class->clear_orphan_data();
51
52 // check if a category is selected
53 $selected_category = isset( $_GET['category'] ) ? sanitize_text_field( $_GET['category'] ) : null;
54
55 if ( $selected_category ) {
56 $categories = array( $selected_category ); // Only sync the selected category
57 } else {
58 // get category to filter by category
59 $opt_category = get_option( 'cmbird_zoho_item_category' );
60 if ( $opt_category ) {
61 // convert serialized string to array
62 $categories = maybe_unserialize( $opt_category );
63 if ( ! is_array( $categories ) ) {
64 $categories = array();
65 }
66 } else {
67 $categories = array();
68 }
69 }
70
71 // Slice the category array to start from the last synced category index
72 if ( ! empty( $categories ) ) {
73 foreach ( $categories as $category_index => $category_id ) {
74 $data = array(
75 'page' => 1,
76 'category' => $category_id,
77 );
78 $existing_schedule = as_has_scheduled_action( 'import_group_items_cron', $data );
79 // Schedule the action if it doesn't exist.
80 if ( ! $existing_schedule ) {
81 as_schedule_single_action( time(), 'import_group_items_cron', $data );
82 }
83 }
84 }
85
86 wp_send_json_success( array( 'message' => 'Items are being imported in background. You can visit other tabs :).' ) );
87 wp_die();
88 }
89
90
91 // Attach the function to the cron event
92 add_action( 'zi_execute_import_sync', 'cmbird_ajax_call_item_from_zoho_func' );
93
94 /**
95 * Function to be called at simple item sync from zoho to woo ajax call.
96 */
97
98 add_action( 'wp_ajax_zoho_ajax_call_item_from_zoho', 'cmbird_ajax_call_item_from_zoho_func' );
99 function cmbird_ajax_call_item_from_zoho_func() {
100 // Clear Orphan data.
101 $zi_common_class = new CMBIRD_Common_Functions();
102 $zi_common_class->clear_orphan_data();
103
104 // Check if a category is selected
105 $selected_category = isset( $_GET['category'] ) ? sanitize_text_field( $_GET['category'] ) : null;
106
107 if ( $selected_category ) {
108 $categories = array( $selected_category ); // Only sync the selected category
109 } else {
110 $zoho_item_category = get_option( 'cmbird_zoho_item_category' );
111 if ( $zoho_item_category ) {
112 // convert serialized string to array
113 $categories = maybe_unserialize( $zoho_item_category );
114 if ( ! is_array( $categories ) ) {
115 $categories = array();
116 }
117 } else {
118 $categories = array();
119 }
120 }
121
122 if ( empty( $categories ) ) {
123 wp_send_json_error( array( 'message' => __( 'Please select at least one category from cron tab', 'commercebird' ) ) );
124 } else {
125 foreach ( $categories as $index => $category_id ) {
126 $data = array(
127 'page' => 1,
128 'category' => $category_id,
129 );
130 $existing_schedule = as_has_scheduled_action( 'import_simple_items_cron', $data );
131 if ( ! $existing_schedule ) {
132 as_schedule_single_action( time(), 'import_simple_items_cron', $data );
133 }
134 }
135 }
136 wp_send_json_success( array( 'message' => __( 'Items are being imported in background. You can visit other tabs :).', 'commercebird' ) ) );
137 wp_die();
138 }
139
140 /**
141 * Zoho Inventory Function sync items from WooCommerce to Zoho in Background
142 */
143 add_action( 'wp_ajax_zoho_ajax_call_item', 'cmbird_ajax_call_item' );
144 function cmbird_ajax_call_item() {
145 // $fd = fopen(__DIR__ . '/cmbird_ajax_call_item.txt', 'a+');
146
147 global $wpdb;
148
149 $meta_key = 'zi_item_id';
150 $post_ids = $wpdb->get_col(
151 $wpdb->prepare(
152 "SELECT p.ID
153 FROM {$wpdb->prefix}posts AS p
154 WHERE p.post_type = 'product'
155 AND p.post_status = 'publish'
156 AND NOT EXISTS (
157 SELECT 1
158 FROM {$wpdb->prefix}postmeta AS pm
159 WHERE p.ID = pm.post_id
160 AND pm.meta_key = %s
161 )",
162 $meta_key
163 )
164 );
165 // Create an array to hold the product IDs
166 $product_ids = array();
167 // fwrite($fd, PHP_EOL . '------------- $post_ids : ' . print_r($post_ids, true));
168 // Adding all items in the queue
169 foreach ( $post_ids as $post_id ) {
170 // Add post ID to the array
171 $product_ids[] = $post_id;
172 // Check if the array contains 10 product IDs (batch size)
173 if ( count( $product_ids ) === 10 ) {
174 // Schedule the action with a delay increasing exponentially for each batch
175 as_schedule_single_action( time(), 'sync_zi_product_cron', array( $product_ids ) );
176 // Clear the array for the next batch of product IDs
177 $product_ids = array();
178 }
179 }
180
181 // Handle any remaining product IDs if the count is less than 10 in the last batch
182 if ( count( $product_ids ) > 0 ) {
183 // Pass the remaining product IDs to the scheduler function
184 as_schedule_single_action( time(), 'sync_zi_product_cron', array( $product_ids ) );
185 }
186
187 // fclose($fd);
188 // Send Success Response and Terminate AJAX Call
189 wp_send_json_success();
190 wp_die();
191 }
192
193 /**
194 * Sync contacts from zoho to woocommerce.
195 *
196 * @return void
197 */
198 add_action( 'zoho_contact_sync', 'cmbird_zoho_contacts_import' );
199 add_action( 'wp_ajax_import_zoho_contacts', 'cmbird_zoho_contacts_import' );
200 function cmbird_zoho_contacts_import( $page = '' ) {
201 if ( empty( $page ) ) {
202 $page = 1;
203 }
204 $data_arr = (object) array();
205 $data_arr->page = $page;
206 $existing_schedule = as_has_scheduled_action( 'sync_zi_import_contacts', array( $data_arr ) );
207
208 // Wrap this via Action Scheduler per page
209 if ( ! $existing_schedule ) {
210 // Schedule the cron job
211 as_schedule_single_action( time(), 'sync_zi_import_contacts', array( $data_arr ) );
212 }
213
214 // send success response to admin and terminate AJAX call.
215 wp_send_json(
216 array(
217 'success' => true,
218 'message' => 'Syncing in background. You can visit other tabs :).',
219 )
220 );
221 wp_die();
222 }
223
224 /**
225 * Sync composite item from zoho to woocommerce.
226 *
227 * @return void
228 */
229 add_action( 'wp_ajax_zoho_ajax_call_composite_item_from_zoho', 'cmbird_sync_composite_item_from_zoho' );
230 function cmbird_sync_composite_item_from_zoho() {
231
232 // Clear Orphan data.
233 $zi_common_class = new CMBIRD_Common_Functions();
234 $zi_common_class->clear_orphan_data();
235
236 // check if a category is selected
237 $selected_category = isset( $_GET['category'] ) ? sanitize_text_field( $_GET['category'] ) : null;
238 if ( $selected_category ) {
239 $categories = array( $selected_category ); // Only sync the selected category
240 } else {
241 // get category to filter by category
242 $opt_category = get_option( 'cmbird_zoho_item_category' );
243 if ( $opt_category ) {
244 // convert serialized string to array
245 $categories = maybe_unserialize( $opt_category );
246 if ( ! is_array( $categories ) ) {
247 $categories = array();
248 }
249 } else {
250 $categories = array();
251 }
252 }
253
254 $item_add_resp = array();
255 foreach ( $categories as $category_id ) {
256 $product_class = new CMBIRD_Products_ZI();
257 $response = $product_class->recursively_sync_composite_item_from_zoho( 1, $category_id );
258 $item_add_resp = array_merge( $item_add_resp, $response );
259 }
260 cmbird_send_log_message_to_admin( $item_add_resp, 'Log Message for manual sync', 'Composite item sync from zoho' );
261 wp_send_json_success( $item_add_resp );
262 wp_die();
263 }
264
265 /**
266 * Send log message to admin
267 *
268 * @return void
269 */
270 function cmbird_send_log_message_to_admin( $sync_logs, $subject, $message ) {
271 $table_root = "<h3>$message</h3>";
272 $table_root .= '<table><thead><tr><th>Action</th><th> Log message</th></tr></thead><tbody>';
273
274 foreach ( $sync_logs as $logs ) {
275 $table_root .= "<tr><td>$logs->resp_id</td><td>$logs->message</td></tr>";
276 }
277 $table_root .= '</tbody></table>';
278 cmbird_error_log_api_email( $subject, $table_root );
279 }
280
281 // ZohoInventory api call hook to sync composite products from WooCommerce to Zoho.
282 add_action( 'wp_ajax_zoho_ajax_call_composite_item', 'cmbird_sync_composite_item_to_zoho' );
283 function cmbird_sync_composite_item_to_zoho() {
284
285 $irgs = array(
286 'post_type' => array( 'product' ),
287 'posts_per_page' => '-1',
288 'post_status' => 'publish',
289 'tax_query' => array(
290 array(
291 'taxonomy' => 'product_type',
292 'field' => 'slug',
293 'terms' => 'bundle',
294 ),
295 ),
296 );
297
298 $my_query = new WP_Query( $irgs );
299 $bundled_product = $my_query->posts;
300 if ( count( $bundled_product ) > 0 ) {
301 foreach ( $bundled_product as $prod ) {
302 $bundle_childs = WC_PB_DB::query_bundled_items(
303 array(
304 'return' => 'id=>product_id', // 'objects'
305 'bundle_id' => array( $prod->ID ),
306 )
307 );
308 if ( count( $bundle_childs ) > 0 ) {
309 foreach ( $bundle_childs as $child_item_id ) {
310 $zoho_item_id = get_post_meta( $child_item_id, 'zi_item_id', true );
311 if ( empty( $zoho_item_id ) ) {
312 break;
313 }
314 }
315 }
316 }
317 }
318
319 $response = array();
320 $response['message'] = 'Sync Started';
321 $response['code'] = 200;
322 wp_send_json_success( $response );
323 wp_die();
324 }
325