ActionMigrator.php
4 years ago
ActionScheduler_DBStoreMigrator.php
4 years ago
BatchFetcher.php
4 years ago
Config.php
4 years ago
Controller.php
4 years ago
DryRun_ActionMigrator.php
4 years ago
DryRun_LogMigrator.php
4 years ago
LogMigrator.php
4 years ago
Runner.php
4 years ago
Scheduler.php
4 years ago
BatchFetcher.php
86 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Action_Scheduler\Migration; |
| 5 | |
| 6 | |
| 7 | use ActionScheduler_Store as Store; |
| 8 | |
| 9 | /** |
| 10 | * Class BatchFetcher |
| 11 | * |
| 12 | * @package Action_Scheduler\Migration |
| 13 | * |
| 14 | * @since 3.0.0 |
| 15 | * |
| 16 | * @codeCoverageIgnore |
| 17 | */ |
| 18 | class BatchFetcher { |
| 19 | /** var ActionScheduler_Store */ |
| 20 | private $store; |
| 21 | |
| 22 | /** |
| 23 | * BatchFetcher constructor. |
| 24 | * |
| 25 | * @param ActionScheduler_Store $source_store Source store object. |
| 26 | */ |
| 27 | public function __construct( Store $source_store ) { |
| 28 | $this->store = $source_store; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Retrieve a list of actions. |
| 33 | * |
| 34 | * @param int $count The number of actions to retrieve |
| 35 | * |
| 36 | * @return int[] A list of action IDs |
| 37 | */ |
| 38 | public function fetch( $count = 10 ) { |
| 39 | foreach ( $this->get_query_strategies( $count ) as $query ) { |
| 40 | $action_ids = $this->store->query_actions( $query ); |
| 41 | if ( ! empty( $action_ids ) ) { |
| 42 | return $action_ids; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | return []; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Generate a list of prioritized of action search parameters. |
| 51 | * |
| 52 | * @param int $count Number of actions to find. |
| 53 | * |
| 54 | * @return array |
| 55 | */ |
| 56 | private function get_query_strategies( $count ) { |
| 57 | $now = as_get_datetime_object(); |
| 58 | $args = [ |
| 59 | 'date' => $now, |
| 60 | 'per_page' => $count, |
| 61 | 'offset' => 0, |
| 62 | 'orderby' => 'date', |
| 63 | 'order' => 'ASC', |
| 64 | ]; |
| 65 | |
| 66 | $priorities = [ |
| 67 | Store::STATUS_PENDING, |
| 68 | Store::STATUS_FAILED, |
| 69 | Store::STATUS_CANCELED, |
| 70 | Store::STATUS_COMPLETE, |
| 71 | Store::STATUS_RUNNING, |
| 72 | '', // any other unanticipated status |
| 73 | ]; |
| 74 | |
| 75 | foreach ( $priorities as $status ) { |
| 76 | yield wp_parse_args( [ |
| 77 | 'status' => $status, |
| 78 | 'date_compare' => '<=', |
| 79 | ], $args ); |
| 80 | yield wp_parse_args( [ |
| 81 | 'status' => $status, |
| 82 | 'date_compare' => '>=', |
| 83 | ], $args ); |
| 84 | } |
| 85 | } |
| 86 | } |