Нет описания

class-wc-action-queue.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Action Queue
  4. *
  5. * @version 3.5.0
  6. * @package WooCommerce\Interface
  7. */
  8. if ( ! defined( 'ABSPATH' ) ) {
  9. exit; // Exit if accessed directly.
  10. }
  11. /**
  12. * WC Action Queue
  13. *
  14. * A job queue using WordPress actions.
  15. *
  16. * @version 3.5.0
  17. */
  18. class WC_Action_Queue implements WC_Queue_Interface {
  19. /**
  20. * Enqueue an action to run one time, as soon as possible
  21. *
  22. * @param string $hook The hook to trigger.
  23. * @param array $args Arguments to pass when the hook triggers.
  24. * @param string $group The group to assign this job to.
  25. * @return string The action ID.
  26. */
  27. public function add( $hook, $args = array(), $group = '' ) {
  28. return $this->schedule_single( time(), $hook, $args, $group );
  29. }
  30. /**
  31. * Schedule an action to run once at some time in the future
  32. *
  33. * @param int $timestamp When the job will run.
  34. * @param string $hook The hook to trigger.
  35. * @param array $args Arguments to pass when the hook triggers.
  36. * @param string $group The group to assign this job to.
  37. * @return string The action ID.
  38. */
  39. public function schedule_single( $timestamp, $hook, $args = array(), $group = '' ) {
  40. return as_schedule_single_action( $timestamp, $hook, $args, $group );
  41. }
  42. /**
  43. * Schedule a recurring action
  44. *
  45. * @param int $timestamp When the first instance of the job will run.
  46. * @param int $interval_in_seconds How long to wait between runs.
  47. * @param string $hook The hook to trigger.
  48. * @param array $args Arguments to pass when the hook triggers.
  49. * @param string $group The group to assign this job to.
  50. * @return string The action ID.
  51. */
  52. public function schedule_recurring( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '' ) {
  53. return as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group );
  54. }
  55. /**
  56. * Schedule an action that recurs on a cron-like schedule.
  57. *
  58. * @param int $timestamp The schedule will start on or after this time.
  59. * @param string $cron_schedule A cron-link schedule string.
  60. * @see http://en.wikipedia.org/wiki/Cron
  61. * * * * * * *
  62. * ┬ ┬ ┬ ┬ ┬ ┬
  63. * | | | | | |
  64. * | | | | | + year [optional]
  65. * | | | | +----- day of week (0 - 7) (Sunday=0 or 7)
  66. * | | | +---------- month (1 - 12)
  67. * | | +--------------- day of month (1 - 31)
  68. * | +-------------------- hour (0 - 23)
  69. * +------------------------- min (0 - 59)
  70. * @param string $hook The hook to trigger.
  71. * @param array $args Arguments to pass when the hook triggers.
  72. * @param string $group The group to assign this job to.
  73. * @return string The action ID
  74. */
  75. public function schedule_cron( $timestamp, $cron_schedule, $hook, $args = array(), $group = '' ) {
  76. return as_schedule_cron_action( $timestamp, $cron_schedule, $hook, $args, $group );
  77. }
  78. /**
  79. * Dequeue the next scheduled instance of an action with a matching hook (and optionally matching args and group).
  80. *
  81. * Any recurring actions with a matching hook should also be cancelled, not just the next scheduled action.
  82. *
  83. * While technically only the next instance of a recurring or cron action is unscheduled by this method, that will also
  84. * prevent all future instances of that recurring or cron action from being run. Recurring and cron actions are scheduled
  85. * in a sequence instead of all being scheduled at once. Each successive occurrence of a recurring action is scheduled
  86. * only after the former action is run. As the next instance is never run, because it's unscheduled by this function,
  87. * then the following instance will never be scheduled (or exist), which is effectively the same as being unscheduled
  88. * by this method also.
  89. *
  90. * @param string $hook The hook that the job will trigger.
  91. * @param array $args Args that would have been passed to the job.
  92. * @param string $group The group the job is assigned to (if any).
  93. */
  94. public function cancel( $hook, $args = array(), $group = '' ) {
  95. as_unschedule_action( $hook, $args, $group );
  96. }
  97. /**
  98. * Dequeue all actions with a matching hook (and optionally matching args and group) so no matching actions are ever run.
  99. *
  100. * @param string $hook The hook that the job will trigger.
  101. * @param array $args Args that would have been passed to the job.
  102. * @param string $group The group the job is assigned to (if any).
  103. */
  104. public function cancel_all( $hook, $args = array(), $group = '' ) {
  105. as_unschedule_all_actions( $hook, $args, $group );
  106. }
  107. /**
  108. * Get the date and time for the next scheduled occurence of an action with a given hook
  109. * (an optionally that matches certain args and group), if any.
  110. *
  111. * @param string $hook The hook that the job will trigger.
  112. * @param array $args Filter to a hook with matching args that will be passed to the job when it runs.
  113. * @param string $group Filter to only actions assigned to a specific group.
  114. * @return WC_DateTime|null The date and time for the next occurrence, or null if there is no pending, scheduled action for the given hook.
  115. */
  116. public function get_next( $hook, $args = null, $group = '' ) {
  117. $next_timestamp = as_next_scheduled_action( $hook, $args, $group );
  118. if ( is_numeric( $next_timestamp ) ) {
  119. return new WC_DateTime( "@{$next_timestamp}", new DateTimeZone( 'UTC' ) );
  120. }
  121. return null;
  122. }
  123. /**
  124. * Find scheduled actions
  125. *
  126. * @param array $args Possible arguments, with their default values:
  127. * 'hook' => '' - the name of the action that will be triggered
  128. * 'args' => null - the args array that will be passed with the action
  129. * 'date' => null - the scheduled date of the action. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone.
  130. * 'date_compare' => '<=' - operator for testing "date". accepted values are '!=', '>', '>=', '<', '<=', '='
  131. * 'modified' => null - the date the action was last updated. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone.
  132. * 'modified_compare' => '<=' - operator for testing "modified". accepted values are '!=', '>', '>=', '<', '<=', '='
  133. * 'group' => '' - the group the action belongs to
  134. * 'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING
  135. * 'claimed' => null - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID
  136. * 'per_page' => 5 - Number of results to return
  137. * 'offset' => 0
  138. * 'orderby' => 'date' - accepted values are 'hook', 'group', 'modified', or 'date'
  139. * 'order' => 'ASC'.
  140. *
  141. * @param string $return_format OBJECT, ARRAY_A, or ids.
  142. * @return array
  143. */
  144. public function search( $args = array(), $return_format = OBJECT ) {
  145. return as_get_scheduled_actions( $args, $return_format );
  146. }
  147. }