| @@ -26,13 +26,13 @@ | ||
| 26 | 26 | // $domain = get_site_url(); |
| 27 | 27 | |
| 28 | 28 | $to = get_bloginfo( 'admin_email' ); |
| 29 | 29 | |
| 30 | - $headers = 'From: ' . $to . "\r\n"; | |
| 30 | + $headers = 'From: ' . $to . "\r\n"; | |
| 31 | 31 | $headers .= "MIME-Version: 1.0\r\n"; |
| 32 | 32 | $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; |
| 33 | 33 | |
| 34 | - $messages = '<html><body>'; | |
| 34 | + $messages = '<html><body>'; | |
| 35 | 35 | $messages .= '<p>' . $message . '</p>'; |
| 36 | 36 | $messages .= '<p>' . $to . '</p>'; |
| 37 | 37 | $messages .= '</body></html>'; |
| 38 | 38 | |
| @@ -48,34 +48,32 @@ | ||
| 48 | 48 | // Clear Orphan data. |
| 49 | 49 | $zi_common_class = new CMBIRD_Common_Functions(); |
| 50 | 50 | $zi_common_class->clear_orphan_data(); |
| 51 | 51 | |
| 52 | - // get category to filter by category | |
| 53 | - $opt_category = get_option( 'cmbird_zoho_item_category' ); | |
| 52 | + // check if a category is selected. | |
| 53 | + $selected_category = isset( $_GET['category'] ) ? sanitize_text_field( $_GET['category'] ) : null; | |
| 54 | 54 | |
| 55 | - if ( $opt_category ) { | |
| 56 | - $opt_category = maybe_unserialize( $opt_category ); | |
| 55 | + if ( $selected_category ) { | |
| 56 | + $categories = array( $selected_category ); // Only sync the selected category. | |
| 57 | 57 | } else { |
| 58 | - $opt_category = array(); | |
| 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 | + } | |
| 59 | 69 | } |
| 60 | 70 | |
| 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, | |
| 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, | |
| 78 | 76 | 'category' => $category_id, |
| 79 | 77 | ); |
| 80 | 78 | $existing_schedule = as_has_scheduled_action( 'import_group_items_cron', $data ); |
| 81 | 79 | // Schedule the action if it doesn't exist. |
| @@ -81,20 +79,9 @@ | ||
| 81 | 79 | // Schedule the action if it doesn't exist. |
| 82 | 80 | if ( ! $existing_schedule ) { |
| 83 | 81 | as_schedule_single_action( time(), 'import_group_items_cron', $data ); |
| 84 | 82 | } |
| 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 | 83 | } |
| 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 | 84 | } |
| 98 | 85 | |
| 99 | 86 | wp_send_json_success( array( 'message' => 'Items are being imported in background. You can visit other tabs :).' ) ); |
| 100 | 87 | wp_die(); |
| @@ -100,9 +87,9 @@ | ||
| 100 | 87 | wp_die(); |
| 101 | 88 | } |
| 102 | 89 | |
| 103 | 90 | |
| 104 | -// Attach the function to the cron event | |
| 91 | +// Attach the function to the cron event. | |
| 105 | 92 | add_action( 'zi_execute_import_sync', 'cmbird_ajax_call_item_from_zoho_func' ); |
| 106 | 93 | |
| 107 | 94 | /** |
| 108 | 95 | * Function to be called at simple item sync from zoho to woo ajax call. |
| @@ -113,17 +100,24 @@ | ||
| 113 | 100 | // Clear Orphan data. |
| 114 | 101 | $zi_common_class = new CMBIRD_Common_Functions(); |
| 115 | 102 | $zi_common_class->clear_orphan_data(); |
| 116 | 103 | |
| 117 | - $zoho_item_category = get_option( 'cmbird_zoho_item_category' ); | |
| 118 | - $last_synced_category_index = get_option( 'cmbird_last_synced_category_index', 0 ); | |
| 104 | + // Check if a category is selected. | |
| 105 | + $selected_category = isset( $_GET['category'] ) ? sanitize_text_field( $_GET['category'] ) : null; | |
| 119 | 106 | |
| 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 ); | |
| 107 | + if ( $selected_category ) { | |
| 108 | + $categories = array( $selected_category ); // Only sync the selected category. | |
| 124 | 109 | } else { |
| 125 | - $categories = array(); | |
| 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 | + } | |
| 126 | 120 | } |
| 127 | 121 | |
| 128 | 122 | if ( empty( $categories ) ) { |
| 129 | 123 | wp_send_json_error( array( 'message' => __( 'Please select at least one category from cron tab', 'commercebird' ) ) ); |
| @@ -128,14 +122,10 @@ | ||
| 128 | 122 | if ( empty( $categories ) ) { |
| 129 | 123 | wp_send_json_error( array( 'message' => __( 'Please select at least one category from cron tab', 'commercebird' ) ) ); |
| 130 | 124 | } else { |
| 131 | 125 | 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, | |
| 126 | + $data = array( | |
| 127 | + 'page' => 1, | |
| 138 | 128 | 'category' => $category_id, |
| 139 | 129 | ); |
| 140 | 130 | $existing_schedule = as_has_scheduled_action( 'import_simple_items_cron', $data ); |
| 141 | 131 | if ( ! $existing_schedule ) { |
| @@ -140,17 +130,9 @@ | ||
| 140 | 130 | $existing_schedule = as_has_scheduled_action( 'import_simple_items_cron', $data ); |
| 141 | 131 | if ( ! $existing_schedule ) { |
| 142 | 132 | as_schedule_single_action( time(), 'import_simple_items_cron', $data ); |
| 143 | 133 | } |
| 144 | - update_option( 'cmbird_last_synced_category_index', $last_synced_category_index + $index + 1 ); | |
| 145 | 134 | } |
| 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 | 135 | } |
| 154 | 136 | wp_send_json_success( array( 'message' => __( 'Items are being imported in background. You can visit other tabs :).', 'commercebird' ) ) ); |
| 155 | 137 | wp_die(); |
| 156 | 138 | } |
| @@ -179,32 +161,32 @@ | ||
| 179 | 161 | )", |
| 180 | 162 | $meta_key |
| 181 | 163 | ) |
| 182 | 164 | ); |
| 183 | - // Create an array to hold the product IDs | |
| 165 | + // Create an array to hold the product IDs. | |
| 184 | 166 | $product_ids = array(); |
| 185 | 167 | // fwrite($fd, PHP_EOL . '------------- $post_ids : ' . print_r($post_ids, true)); |
| 186 | - // Adding all items in the queue | |
| 168 | + // Adding all items in the queue. | |
| 187 | 169 | foreach ( $post_ids as $post_id ) { |
| 188 | - // Add post ID to the array | |
| 170 | + // Add post ID to the array. | |
| 189 | 171 | $product_ids[] = $post_id; |
| 190 | - // Check if the array contains 10 product IDs (batch size) | |
| 172 | + // Check if the array contains 10 product IDs (batch size). | |
| 191 | 173 | if ( count( $product_ids ) === 10 ) { |
| 192 | - // Schedule the action with a delay increasing exponentially for each batch | |
| 174 | + // Schedule the action with a delay increasing exponentially for each batch. | |
| 193 | 175 | as_schedule_single_action( time(), 'sync_zi_product_cron', array( $product_ids ) ); |
| 194 | - // Clear the array for the next batch of product IDs | |
| 176 | + // Clear the array for the next batch of product IDs. | |
| 195 | 177 | $product_ids = array(); |
| 196 | 178 | } |
| 197 | 179 | } |
| 198 | 180 | |
| 199 | - // Handle any remaining product IDs if the count is less than 10 in the last batch | |
| 181 | + // Handle any remaining product IDs if the count is less than 10 in the last batch. | |
| 200 | 182 | if ( count( $product_ids ) > 0 ) { |
| 201 | - // Pass the remaining product IDs to the scheduler function | |
| 183 | + // Pass the remaining product IDs to the scheduler function. | |
| 202 | 184 | as_schedule_single_action( time(), 'sync_zi_product_cron', array( $product_ids ) ); |
| 203 | 185 | } |
| 204 | 186 | |
| 205 | 187 | // fclose($fd); |
| 206 | - // Send Success Response and Terminate AJAX Call | |
| 188 | + // Send Success Response and Terminate AJAX Call. | |
| 207 | 189 | wp_send_json_success(); |
| 208 | 190 | wp_die(); |
| 209 | 191 | } |
| 210 | 192 | |
| @@ -209,8 +191,9 @@ | ||
| 209 | 191 | } |
| 210 | 192 | |
| 211 | 193 | /** |
| 212 | 194 | * Sync contacts from zoho to woocommerce. |
| 195 | + * | |
| 213 | 196 | * @return void |
| 214 | 197 | */ |
| 215 | 198 | add_action( 'zoho_contact_sync', 'cmbird_zoho_contacts_import' ); |
| 216 | 199 | add_action( 'wp_ajax_import_zoho_contacts', 'cmbird_zoho_contacts_import' ); |
| @@ -217,15 +200,15 @@ | ||
| 217 | 200 | function cmbird_zoho_contacts_import( $page = '' ) { |
| 218 | 201 | if ( empty( $page ) ) { |
| 219 | 202 | $page = 1; |
| 220 | 203 | } |
| 221 | - $data_arr = (object) array(); | |
| 222 | - $data_arr->page = $page; | |
| 204 | + $data_arr = (object) array(); | |
| 205 | + $data_arr->page = $page; | |
| 223 | 206 | $existing_schedule = as_has_scheduled_action( 'sync_zi_import_contacts', array( $data_arr ) ); |
| 224 | 207 | |
| 225 | - // Wrap this via Action Scheduler per page | |
| 208 | + // Wrap this via Action Scheduler per page. | |
| 226 | 209 | if ( ! $existing_schedule ) { |
| 227 | - // Schedule the cron job | |
| 210 | + // Schedule the cron job. | |
| 228 | 211 | as_schedule_single_action( time(), 'sync_zi_import_contacts', array( $data_arr ) ); |
| 229 | 212 | } |
| 230 | 213 | |
| 231 | 214 | // send success response to admin and terminate AJAX call. |
| @@ -245,19 +228,34 @@ | ||
| 245 | 228 | */ |
| 246 | 229 | add_action( 'wp_ajax_zoho_ajax_call_composite_item_from_zoho', 'cmbird_sync_composite_item_from_zoho' ); |
| 247 | 230 | function cmbird_sync_composite_item_from_zoho() { |
| 248 | 231 | |
| 249 | - $opt_category = get_option( 'cmbird_zoho_item_category' ); | |
| 250 | - if ( $opt_category ) { | |
| 251 | - $opt_category = maybe_unserialize( $opt_category ); | |
| 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. | |
| 252 | 240 | } else { |
| 253 | - $opt_category = array(); | |
| 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 | + } | |
| 254 | 252 | } |
| 255 | 253 | |
| 256 | 254 | $item_add_resp = array(); |
| 257 | - foreach ( $opt_category as $category_id ) { | |
| 255 | + foreach ( $categories as $category_id ) { | |
| 258 | 256 | $product_class = new CMBIRD_Products_ZI(); |
| 259 | - $response = $product_class->recursively_sync_composite_item_from_zoho( 1, $category_id, 'sync' ); | |
| 257 | + $response = $product_class->recursively_sync_composite_item_from_zoho( 1, $category_id ); | |
| 260 | 258 | $item_add_resp = array_merge( $item_add_resp, $response ); |
| 261 | 259 | } |
| 262 | 260 | cmbird_send_log_message_to_admin( $item_add_resp, 'Log Message for manual sync', 'Composite item sync from zoho' ); |
| 263 | 261 | wp_send_json_success( $item_add_resp ); |
| @@ -269,9 +267,9 @@ | ||
| 269 | 267 | * |
| 270 | 268 | * @return void |
| 271 | 269 | */ |
| 272 | 270 | function cmbird_send_log_message_to_admin( $sync_logs, $subject, $message ) { |
| 273 | - $table_root = "<h3>$message</h3>"; | |
| 271 | + $table_root = "<h3>$message</h3>"; | |
| 274 | 272 | $table_root .= '<table><thead><tr><th>Action</th><th> Log message</th></tr></thead><tbody>'; |
| 275 | 273 | |
| 276 | 274 | foreach ( $sync_logs as $logs ) { |
| 277 | 275 | $table_root .= "<tr><td>$logs->resp_id</td><td>$logs->message</td></tr>"; |
| @@ -284,27 +282,27 @@ | ||
| 284 | 282 | add_action( 'wp_ajax_zoho_ajax_call_composite_item', 'cmbird_sync_composite_item_to_zoho' ); |
| 285 | 283 | function cmbird_sync_composite_item_to_zoho() { |
| 286 | 284 | |
| 287 | 285 | $irgs = array( |
| 288 | - 'post_type' => array( 'product' ), | |
| 286 | + 'post_type' => array( 'product' ), | |
| 289 | 287 | 'posts_per_page' => '-1', |
| 290 | - 'post_status' => 'publish', | |
| 291 | - 'tax_query' => array( | |
| 288 | + 'post_status' => 'publish', | |
| 289 | + 'tax_query' => array( | |
| 292 | 290 | array( |
| 293 | 291 | 'taxonomy' => 'product_type', |
| 294 | - 'field' => 'slug', | |
| 295 | - 'terms' => 'bundle', | |
| 292 | + 'field' => 'slug', | |
| 293 | + 'terms' => 'bundle', | |
| 296 | 294 | ), |
| 297 | 295 | ), |
| 298 | 296 | ); |
| 299 | 297 | |
| 300 | - $my_query = new WP_Query( $irgs ); | |
| 298 | + $my_query = new WP_Query( $irgs ); | |
| 301 | 299 | $bundled_product = $my_query->posts; |
| 302 | 300 | if ( count( $bundled_product ) > 0 ) { |
| 303 | 301 | foreach ( $bundled_product as $prod ) { |
| 304 | 302 | $bundle_childs = WC_PB_DB::query_bundled_items( |
| 305 | 303 | array( |
| 306 | - 'return' => 'id=>product_id', // 'objects' | |
| 304 | + 'return' => 'id=>product_id', // 'objects'. | |
| 307 | 305 | 'bundle_id' => array( $prod->ID ), |
| 308 | 306 | ) |
| 309 | 307 | ); |
| 310 | 308 | if ( count( $bundle_childs ) > 0 ) { |
| @@ -317,10 +315,10 @@ | ||
| 317 | 315 | } |
| 318 | 316 | } |
| 319 | 317 | } |
| 320 | 318 | |
| 321 | - $response = array(); | |
| 319 | + $response = array(); | |
| 322 | 320 | $response['message'] = 'Sync Started'; |
| 323 | - $response['code'] = 200; | |
| 321 | + $response['code'] = 200; | |
| 324 | 322 | wp_send_json_success( $response ); |
| 325 | 323 | wp_die(); |
| 326 | 324 | } |