Нема описа

BatchFetcher.php 1.6KB

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