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