commercebird
Last commit date
admin
1 year ago
includes
1 year ago
languages
1 year ago
vendor
1 year ago
.rsyncignore
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
328 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 | // get category to filter by category |
| 53 | $opt_category = get_option( 'cmbird_zoho_item_category' ); |
| 54 | |
| 55 | if ( $opt_category ) { |
| 56 | $opt_category = maybe_unserialize( $opt_category ); |
| 57 | } else { |
| 58 | $opt_category = array(); |
| 59 | } |
| 60 | |
| 61 | // Retrieve the last synced category index from the previous run |
| 62 | $last_synced_category_index = get_option( 'cmbird_last_synced_category_index_groupitems', 0 ); |
| 63 | |
| 64 | // Slice the category array to start from the last synced category index |
| 65 | $opt_category = array_slice( $opt_category, $last_synced_category_index ); |
| 66 | $category_index = 0; |
| 67 | if ( ! empty( $opt_category ) ) { |
| 68 | foreach ( $opt_category as $category_index => $category_id ) { |
| 69 | // get last backed up page number for particular category Id. |
| 70 | // And start syncing from the last synced page. |
| 71 | // If no page number available, it will start from zero. |
| 72 | $last_synced_page = get_option( 'cmbird_group_item_sync_page_cat_id_' . $category_id ); |
| 73 | if ( ! intval( $last_synced_page ) ) { |
| 74 | $last_synced_page = 1; |
| 75 | } |
| 76 | $data = array( |
| 77 | 'page' => $last_synced_page, |
| 78 | 'category' => $category_id, |
| 79 | ); |
| 80 | $existing_schedule = as_has_scheduled_action( 'import_group_items_cron', $data ); |
| 81 | // Schedule the action if it doesn't exist. |
| 82 | if ( ! $existing_schedule ) { |
| 83 | as_schedule_single_action( time(), 'import_group_items_cron', $data ); |
| 84 | } |
| 85 | |
| 86 | // Update the last synced category index in the options |
| 87 | update_option( 'cmbird_last_synced_category_index_groupitems', $last_synced_category_index + $category_index + 1 ); |
| 88 | } |
| 89 | // Check if all categories have been imported or passed to the loop |
| 90 | $total_categories = count( $opt_category ); |
| 91 | $processed_categories = $last_synced_category_index + $category_index + 1; |
| 92 | |
| 93 | if ( $processed_categories >= $total_categories ) { |
| 94 | // Reset the last synced category index |
| 95 | update_option( 'cmbird_last_synced_category_index_groupitems', 0 ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | wp_send_json_success( array( 'message' => 'Items are being imported in background. You can visit other tabs :).' ) ); |
| 100 | wp_die(); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | // Attach the function to the cron event |
| 105 | add_action( 'zi_execute_import_sync', 'cmbird_ajax_call_item_from_zoho_func' ); |
| 106 | |
| 107 | /** |
| 108 | * Function to be called at simple item sync from zoho to woo ajax call. |
| 109 | */ |
| 110 | |
| 111 | add_action( 'wp_ajax_zoho_ajax_call_item_from_zoho', 'cmbird_ajax_call_item_from_zoho_func' ); |
| 112 | function cmbird_ajax_call_item_from_zoho_func() { |
| 113 | // Clear Orphan data. |
| 114 | $zi_common_class = new CMBIRD_Common_Functions(); |
| 115 | $zi_common_class->clear_orphan_data(); |
| 116 | |
| 117 | $zoho_item_category = get_option( 'cmbird_zoho_item_category' ); |
| 118 | $last_synced_category_index = get_option( 'cmbird_last_synced_category_index', 0 ); |
| 119 | |
| 120 | if ( $zoho_item_category ) { |
| 121 | // convert serialized string to array |
| 122 | $categories = maybe_unserialize( $zoho_item_category ); |
| 123 | $categories = array_slice( $categories, $last_synced_category_index ); |
| 124 | } else { |
| 125 | $categories = array(); |
| 126 | } |
| 127 | |
| 128 | if ( empty( $categories ) ) { |
| 129 | wp_send_json_error( array( 'message' => __( 'Please select at least one category from cron tab', 'commercebird' ) ) ); |
| 130 | } else { |
| 131 | foreach ( $categories as $index => $category_id ) { |
| 132 | $last_synced_page = get_option( 'cmbird_simple_item_sync_page_cat_id_' . $category_id ); |
| 133 | if ( ! intval( $last_synced_page ) ) { |
| 134 | $last_synced_page = 1; |
| 135 | } |
| 136 | $data = array( |
| 137 | 'page' => $last_synced_page, |
| 138 | 'category' => $category_id, |
| 139 | ); |
| 140 | $existing_schedule = as_has_scheduled_action( 'import_simple_items_cron', $data ); |
| 141 | if ( ! $existing_schedule ) { |
| 142 | as_schedule_single_action( time(), 'import_simple_items_cron', $data ); |
| 143 | } |
| 144 | update_option( 'cmbird_last_synced_category_index', $last_synced_category_index + $index + 1 ); |
| 145 | } |
| 146 | |
| 147 | $total_categories = count( $categories ); |
| 148 | $processed_categories = $last_synced_category_index + $index + 1; |
| 149 | |
| 150 | if ( $processed_categories >= $total_categories ) { |
| 151 | update_option( 'cmbird_last_synced_category_index', 0 ); |
| 152 | } |
| 153 | } |
| 154 | wp_send_json_success( array( 'message' => __( 'Items are being imported in background. You can visit other tabs :).', 'commercebird' ) ) ); |
| 155 | wp_die(); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Zoho Inventory Function sync items from WooCommerce to Zoho in Background |
| 160 | */ |
| 161 | add_action( 'wp_ajax_zoho_ajax_call_item', 'cmbird_ajax_call_item' ); |
| 162 | function cmbird_ajax_call_item() { |
| 163 | // $fd = fopen(__DIR__ . '/cmbird_ajax_call_item.txt', 'a+'); |
| 164 | |
| 165 | global $wpdb; |
| 166 | |
| 167 | $meta_key = 'zi_item_id'; |
| 168 | $post_ids = $wpdb->get_col( |
| 169 | $wpdb->prepare( |
| 170 | "SELECT p.ID |
| 171 | FROM {$wpdb->prefix}posts AS p |
| 172 | WHERE p.post_type = 'product' |
| 173 | AND p.post_status = 'publish' |
| 174 | AND NOT EXISTS ( |
| 175 | SELECT 1 |
| 176 | FROM {$wpdb->prefix}postmeta AS pm |
| 177 | WHERE p.ID = pm.post_id |
| 178 | AND pm.meta_key = %s |
| 179 | )", |
| 180 | $meta_key |
| 181 | ) |
| 182 | ); |
| 183 | // Create an array to hold the product IDs |
| 184 | $product_ids = array(); |
| 185 | // fwrite($fd, PHP_EOL . '------------- $post_ids : ' . print_r($post_ids, true)); |
| 186 | // Adding all items in the queue |
| 187 | foreach ( $post_ids as $post_id ) { |
| 188 | // Add post ID to the array |
| 189 | $product_ids[] = $post_id; |
| 190 | // Check if the array contains 10 product IDs (batch size) |
| 191 | if ( count( $product_ids ) === 10 ) { |
| 192 | // Schedule the action with a delay increasing exponentially for each batch |
| 193 | as_schedule_single_action( time(), 'sync_zi_product_cron', array( $product_ids ) ); |
| 194 | // Clear the array for the next batch of product IDs |
| 195 | $product_ids = array(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // Handle any remaining product IDs if the count is less than 10 in the last batch |
| 200 | if ( count( $product_ids ) > 0 ) { |
| 201 | // Pass the remaining product IDs to the scheduler function |
| 202 | as_schedule_single_action( time(), 'sync_zi_product_cron', array( $product_ids ) ); |
| 203 | } |
| 204 | |
| 205 | // fclose($fd); |
| 206 | // Send Success Response and Terminate AJAX Call |
| 207 | wp_send_json_success(); |
| 208 | wp_die(); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Sync contacts from zoho to woocommerce. |
| 213 | * @return void |
| 214 | */ |
| 215 | add_action( 'zoho_contact_sync', 'cmbird_zoho_contacts_import' ); |
| 216 | add_action( 'wp_ajax_import_zoho_contacts', 'cmbird_zoho_contacts_import' ); |
| 217 | function cmbird_zoho_contacts_import( $page = '' ) { |
| 218 | if ( empty( $page ) ) { |
| 219 | $page = 1; |
| 220 | } |
| 221 | $data_arr = (object) array(); |
| 222 | $data_arr->page = $page; |
| 223 | $existing_schedule = as_has_scheduled_action( 'sync_zi_import_contacts', array( $data_arr ) ); |
| 224 | |
| 225 | // Wrap this via Action Scheduler per page |
| 226 | if ( ! $existing_schedule ) { |
| 227 | // Schedule the cron job |
| 228 | as_schedule_single_action( time(), 'sync_zi_import_contacts', array( $data_arr ) ); |
| 229 | } |
| 230 | |
| 231 | // send success response to admin and terminate AJAX call. |
| 232 | wp_send_json( |
| 233 | array( |
| 234 | 'success' => true, |
| 235 | 'message' => 'Syncing in background. You can visit other tabs :).', |
| 236 | ) |
| 237 | ); |
| 238 | wp_die(); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Sync composite item from zoho to woocommerce. |
| 243 | * |
| 244 | * @return void |
| 245 | */ |
| 246 | add_action( 'wp_ajax_zoho_ajax_call_composite_item_from_zoho', 'cmbird_sync_composite_item_from_zoho' ); |
| 247 | function cmbird_sync_composite_item_from_zoho() { |
| 248 | |
| 249 | $opt_category = get_option( 'cmbird_zoho_item_category' ); |
| 250 | if ( $opt_category ) { |
| 251 | $opt_category = maybe_unserialize( $opt_category ); |
| 252 | } else { |
| 253 | $opt_category = array(); |
| 254 | } |
| 255 | |
| 256 | $item_add_resp = array(); |
| 257 | foreach ( $opt_category as $category_id ) { |
| 258 | $product_class = new CMBIRD_Products_ZI(); |
| 259 | $response = $product_class->recursively_sync_composite_item_from_zoho( 1, $category_id, 'sync' ); |
| 260 | $item_add_resp = array_merge( $item_add_resp, $response ); |
| 261 | } |
| 262 | cmbird_send_log_message_to_admin( $item_add_resp, 'Log Message for manual sync', 'Composite item sync from zoho' ); |
| 263 | wp_send_json_success( $item_add_resp ); |
| 264 | wp_die(); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Send log message to admin |
| 269 | * |
| 270 | * @return void |
| 271 | */ |
| 272 | function cmbird_send_log_message_to_admin( $sync_logs, $subject, $message ) { |
| 273 | $table_root = "<h3>$message</h3>"; |
| 274 | $table_root .= '<table><thead><tr><th>Action</th><th> Log message</th></tr></thead><tbody>'; |
| 275 | |
| 276 | foreach ( $sync_logs as $logs ) { |
| 277 | $table_root .= "<tr><td>$logs->resp_id</td><td>$logs->message</td></tr>"; |
| 278 | } |
| 279 | $table_root .= '</tbody></table>'; |
| 280 | cmbird_error_log_api_email( $subject, $table_root ); |
| 281 | } |
| 282 | |
| 283 | // ZohoInventory api call hook to sync composite products from WooCommerce to Zoho. |
| 284 | add_action( 'wp_ajax_zoho_ajax_call_composite_item', 'cmbird_sync_composite_item_to_zoho' ); |
| 285 | function cmbird_sync_composite_item_to_zoho() { |
| 286 | |
| 287 | $irgs = array( |
| 288 | 'post_type' => array( 'product' ), |
| 289 | 'posts_per_page' => '-1', |
| 290 | 'post_status' => 'publish', |
| 291 | 'tax_query' => array( |
| 292 | array( |
| 293 | 'taxonomy' => 'product_type', |
| 294 | 'field' => 'slug', |
| 295 | 'terms' => 'bundle', |
| 296 | ), |
| 297 | ), |
| 298 | ); |
| 299 | |
| 300 | $my_query = new WP_Query( $irgs ); |
| 301 | $bundled_product = $my_query->posts; |
| 302 | if ( count( $bundled_product ) > 0 ) { |
| 303 | foreach ( $bundled_product as $prod ) { |
| 304 | $bundle_childs = WC_PB_DB::query_bundled_items( |
| 305 | array( |
| 306 | 'return' => 'id=>product_id', // 'objects' |
| 307 | 'bundle_id' => array( $prod->ID ), |
| 308 | ) |
| 309 | ); |
| 310 | if ( count( $bundle_childs ) > 0 ) { |
| 311 | foreach ( $bundle_childs as $child_item_id ) { |
| 312 | $zoho_item_id = get_post_meta( $child_item_id, 'zi_item_id', true ); |
| 313 | if ( empty( $zoho_item_id ) ) { |
| 314 | break; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | $response = array(); |
| 322 | $response['message'] = 'Sync Started'; |
| 323 | $response['code'] = 200; |
| 324 | echo wp_json_encode( $response ); |
| 325 | |
| 326 | exit(); |
| 327 | } |
| 328 |