ProductsCleanupJob.php
78 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Sync\Jobs\Cleanup; |
| 4 | |
| 5 | use SureCart\Background\Job; |
| 6 | |
| 7 | /** |
| 8 | * This process fetches and queues all products for syncing. |
| 9 | */ |
| 10 | class ProductsCleanupJob extends Job { |
| 11 | /** |
| 12 | * The prefix for the action. |
| 13 | * |
| 14 | * @var string |
| 15 | */ |
| 16 | protected $prefix = 'surecart'; |
| 17 | |
| 18 | /** |
| 19 | * The action. |
| 20 | * |
| 21 | * @var string |
| 22 | */ |
| 23 | protected $action = 'cleanup_products'; |
| 24 | |
| 25 | /** |
| 26 | * Perform task with queued item. |
| 27 | * |
| 28 | * Override this method to perform any actions required on each |
| 29 | * queue item. Return the modified item for further processing |
| 30 | * in the next pass through. Or, return false to remove the |
| 31 | * item from the queue. |
| 32 | * |
| 33 | * @param mixed $args Queue item to iterate over. |
| 34 | * |
| 35 | * @return mixed |
| 36 | */ |
| 37 | protected function task( $args ) { |
| 38 | $query = new \WP_Query( |
| 39 | [ |
| 40 | 'post_type' => 'sc_product', |
| 41 | 'post_status' => [ 'publish', 'pending', 'draft', 'future', 'private', 'inherit', 'trash', 'auto-draft', 'sc_archived' ], |
| 42 | 'posts_per_page' => absint( $args['batch_size'] ?? 25 ), |
| 43 | 'paged' => absint( $args['page'] ?? 1 ), |
| 44 | 'suppress_filters' => true, // important to bypass store filter. |
| 45 | 'fields' => 'ids', |
| 46 | 'tax_query' => [ |
| 47 | [ |
| 48 | 'taxonomy' => 'sc_account', |
| 49 | 'field' => 'name', |
| 50 | 'terms' => [ \SureCart::account()->id ], |
| 51 | 'operator' => 'NOT IN', |
| 52 | ], |
| 53 | ], |
| 54 | ] |
| 55 | ); |
| 56 | |
| 57 | if ( is_wp_error( $query ) ) { |
| 58 | error_log( $query->get_error_message() ); |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | // add each item to the queue. |
| 63 | foreach ( $query->posts as $product_id ) { |
| 64 | $this->task->queue( $product_id ); |
| 65 | } |
| 66 | |
| 67 | if ( $query->max_num_pages > $query->query_vars['paged'] ) { |
| 68 | return [ |
| 69 | 'page' => $query->query_vars['paged'] + 1, |
| 70 | 'batch_size' => $args['batch_size'] ?? 25, |
| 71 | ]; |
| 72 | } |
| 73 | |
| 74 | // nothing more to process. |
| 75 | return false; |
| 76 | } |
| 77 | } |
| 78 |