| @@ -19,6 +19,36 @@ | ||
| 19 | 19 | } |
| 20 | 20 | } |
| 21 | 21 | }die; |
| 22 | 22 | } |
| 23 | +add_action('wp_ajax_xyz_ihs_sync_usage', function() { | |
| 24 | + global $wpdb; | |
| 23 | 25 | |
| 26 | + $offset = isset($_POST['offset']) ? intval($_POST['offset']) : 0; | |
| 27 | + $batch_size = 1; // adjust as needed | |
| 28 | + $table_name = $wpdb->prefix . 'xyz_ihs_usage'; | |
| 29 | + // Get posts starting from $offset | |
| 30 | + $posts = $wpdb->get_results($wpdb->prepare( | |
| 31 | + "SELECT ID, post_content, post_type FROM {$wpdb->posts} | |
| 32 | + WHERE post_status = 'publish' | |
| 33 | + ORDER BY ID ASC | |
| 34 | + LIMIT %d, %d", | |
| 35 | + $offset, $batch_size | |
| 36 | + )); | |
| 37 | + if (empty($posts)) { | |
| 38 | + // Finished | |
| 39 | + update_option('xyz_ihs_sync_needed', 0); // reset | |
| 40 | + wp_send_json_success(['status' => 'complete']); | |
| 41 | + } | |
| 42 | + foreach ($posts as $post) { | |
| 43 | + // Run your usage update logic | |
| 44 | + xyz_ihs_update_usage_for_post($post->ID, $post->post_content, $post->post_type); | |
| 45 | + } | |
| 46 | + $new_offset = $offset + count($posts); | |
| 47 | + // Store the new offset | |
| 48 | + update_option('xyz_ihs_sync_needed', $new_offset); | |
| 49 | + wp_send_json_success([ | |
| 50 | + 'status' => 'processing', | |
| 51 | + 'new_offset' => $new_offset | |
| 52 | + ]); | |
| 53 | +}); | |
| 24 | 54 | ?> |