CollectionCleanupTask.php
1 year ago
ProductCleanupTask.php
1 year ago
ProductSyncTask.php
1 year ago
Task.php
1 year ago
Task.php
109 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Sync\Tasks; |
| 4 | |
| 5 | /** |
| 6 | * Abstract task class. |
| 7 | */ |
| 8 | abstract class Task { |
| 9 | /** |
| 10 | * The action name. |
| 11 | * |
| 12 | * @var string |
| 13 | */ |
| 14 | protected $action_name; |
| 15 | |
| 16 | /** |
| 17 | * Show a notice. |
| 18 | * |
| 19 | * @var boolean |
| 20 | */ |
| 21 | protected $show_notice = false; |
| 22 | |
| 23 | /** |
| 24 | * Bootstrap any actions. |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public function bootstrap() { |
| 29 | add_action( $this->action_name, [ $this, 'handle' ], 10, 2 ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Whether to show a notice. |
| 34 | * |
| 35 | * @param boolean $show_notice Whether to show a notice. |
| 36 | * |
| 37 | * @return self |
| 38 | */ |
| 39 | public function withNotice( $show_notice = true ) { |
| 40 | $this->show_notice = $show_notice; |
| 41 | return $this; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Queue the sync for a later time. |
| 46 | * |
| 47 | * @param string $collection_term_id The term id. |
| 48 | * |
| 49 | * @return \SureCart\Queue\Async |
| 50 | */ |
| 51 | public function queue( $id ) { |
| 52 | return \SureCart::queue()->async( |
| 53 | $this->action_name, |
| 54 | [ |
| 55 | 'id' => $id, |
| 56 | 'show_notice' => $this->show_notice, |
| 57 | ], |
| 58 | $this->action_name . '-' . $id, // unique id for the term. |
| 59 | true // force unique. This will replace any existing jobs. |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Check if the task is scheduled. |
| 65 | * |
| 66 | * @param string $id The id. |
| 67 | * |
| 68 | * @return bool |
| 69 | */ |
| 70 | public function isScheduled( $id ) { |
| 71 | return \SureCart::queue()->isScheduled( |
| 72 | $this->action_name, |
| 73 | [ |
| 74 | 'id' => $id, |
| 75 | 'show_notice' => $this->show_notice, |
| 76 | ], |
| 77 | $this->action_name . '-' . $id, // unique id for the term. |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Check are any actions scheduled. |
| 83 | * |
| 84 | * @return bool |
| 85 | */ |
| 86 | public function showNotice() { |
| 87 | return \SureCart::queue()->showNotice( $this->action_name ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Cancel the task. |
| 92 | * |
| 93 | * @param string $id The id. |
| 94 | * |
| 95 | * @return \SureCart\Queue\Async |
| 96 | */ |
| 97 | public function cancel( $id ) { |
| 98 | return \SureCart::queue()->cancel( |
| 99 | $this->action_name, |
| 100 | [ |
| 101 | 'id' => $id, |
| 102 | 'show_notice' => $this->show_notice, |
| 103 | ], |
| 104 | $this->action_name . '-' . $id, // unique id for the term. |
| 105 | true // force unique. This will replace any existing jobs. |
| 106 | ); |
| 107 | } |
| 108 | } |
| 109 |