PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.3.6
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.3.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 1 year ago includes 1 year ago languages 1 year ago vendor 1 year ago LICENSE 1 year ago changelog.txt 1 year ago commercebird.php 1 year ago composer.json 1 year ago data-sync.php 1 year ago index.php 1 year ago readme.txt 1 year 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 = [ $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
87 wp_send_json_success( array( 'message' => 'Items are being imported in background. You can visit other tabs :).' ) );
88 wp_die();
89 }
90
91
92 // Attach the function to the cron event
93 add_action( 'zi_execute_import_sync', 'cmbird_ajax_call_item_from_zoho_func' );
94
95 /**
96 * Function to be called at simple item sync from zoho to woo ajax call.
97 */
98
99 add_action( 'wp_ajax_zoho_ajax_call_item_from_zoho', 'cmbird_ajax_call_item_from_zoho_func' );
100 function cmbird_ajax_call_item_from_zoho_func() {
101 // Clear Orphan data.
102 $zi_common_class = new CMBIRD_Common_Functions();
103 $zi_common_class->clear_orphan_data();
104
105 // Check if a category is selected
106 $selected_category = isset( $_GET['category'] ) ? sanitize_text_field( $_GET['category'] ) : null;
107
108 if ( $selected_category ) {
109 $categories = [ $selected_category ]; // Only sync the selected category
110 } else {
111 $zoho_item_category = get_option( 'cmbird_zoho_item_category' );
112 if ( $zoho_item_category ) {
113 // convert serialized string to array
114 $categories = maybe_unserialize( $zoho_item_category );
115 if ( ! is_array( $categories ) ) {
116 $categories = array();
117 }
118 } else {
119 $categories = array();
120 }
121 }
122
123 if ( empty( $categories ) ) {
124 wp_send_json_error( array( 'message' => __( 'Please select at least one category from cron tab', 'commercebird' ) ) );
125 } else {
126 foreach ( $categories as $index => $category_id ) {
127 $data = array(
128 'page' => 1,
129 'category' => $category_id,
130 );
131 $existing_schedule = as_has_scheduled_action( 'import_simple_items_cron', $data );
132 if ( ! $existing_schedule ) {
133 as_schedule_single_action( time(), 'import_simple_items_cron', $data );
134 }
135 }
136 }
137 wp_send_json_success( array( 'message' => __( 'Items are being imported in background. You can visit other tabs :).', 'commercebird' ) ) );
138 wp_die();
139 }
140
141 /**
142 * Zoho Inventory Function sync items from WooCommerce to Zoho in Background
143 */
144 add_action( 'wp_ajax_zoho_ajax_call_item', 'cmbird_ajax_call_item' );
145 function cmbird_ajax_call_item() {
146 // $fd = fopen(__DIR__ . '/cmbird_ajax_call_item.txt', 'a+');
147
148 global $wpdb;
149
150 $meta_key = 'zi_item_id';
151 $post_ids = $wpdb->get_col(
152 $wpdb->prepare(
153 "SELECT p.ID
154 FROM {$wpdb->prefix}posts AS p
155 WHERE p.post_type = 'product'
156 AND p.post_status = 'publish'
157 AND NOT EXISTS (
158 SELECT 1
159 FROM {$wpdb->prefix}postmeta AS pm
160 WHERE p.ID = pm.post_id
161 AND pm.meta_key = %s
162 )",
163 $meta_key
164 )
165 );
166 // Create an array to hold the product IDs
167 $product_ids = array();
168 // fwrite($fd, PHP_EOL . '------------- $post_ids : ' . print_r($post_ids, true));
169 // Adding all items in the queue
170 foreach ( $post_ids as $post_id ) {
171 // Add post ID to the array
172 $product_ids[] = $post_id;
173 // Check if the array contains 10 product IDs (batch size)
174 if ( count( $product_ids ) === 10 ) {
175 // Schedule the action with a delay increasing exponentially for each batch
176 as_schedule_single_action( time(), 'sync_zi_product_cron', array( $product_ids ) );
177 // Clear the array for the next batch of product IDs
178 $product_ids = array();
179 }
180 }
181
182 // Handle any remaining product IDs if the count is less than 10 in the last batch
183 if ( count( $product_ids ) > 0 ) {
184 // Pass the remaining product IDs to the scheduler function
185 as_schedule_single_action( time(), 'sync_zi_product_cron', array( $product_ids ) );
186 }
187
188 // fclose($fd);
189 // Send Success Response and Terminate AJAX Call
190 wp_send_json_success();
191 wp_die();
192 }
193
194 /**
195 * Sync contacts from zoho to woocommerce.
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 = [ $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