|
10 | 10 | */
|
11 | 11 | class WCS_Action_Scheduler extends WCS_Scheduler {
|
12 | 12 |
|
| 13 | + /** |
| 14 | + * The action scheduler group to use for scheduled subscription events. |
| 15 | + */ |
| 16 | + const ACTION_GROUP = 'wc_subscription_scheduled_event'; |
| 17 | + |
| 18 | + /** |
| 19 | + * The priority of the subscription-related scheduled action. |
| 20 | + */ |
| 21 | + const ACTION_PRIORITY = 1; |
| 22 | + |
13 | 23 | /**
|
14 | 24 | * An internal cache of action hooks and corresponding date types.
|
15 | 25 | *
|
@@ -55,7 +65,7 @@ public function update_date( $subscription, $date_type, $datetime ) {
|
55 | 65 |
|
56 | 66 | // Only schedule it if it's valid. It's active, it's a payment retry or it's pending cancelled and the end date being updated.
|
57 | 67 | if ( 'payment_retry' === $date_type || $subscription->has_status( 'active' ) || ( $subscription->has_status( 'pending-cancel' ) && 'end' === $date_type ) ) {
|
58 |
| - as_schedule_single_action( $timestamp, $action_hook, $action_args ); |
| 68 | + $this->schedule_action( $timestamp, $action_hook, $action_args ); |
59 | 69 | }
|
60 | 70 | }
|
61 | 71 | }
|
@@ -110,7 +120,7 @@ public function update_status( $subscription, $new_status, $old_status ) {
|
110 | 120 | }
|
111 | 121 |
|
112 | 122 | if ( 0 != $event_time && $event_time > current_time( 'timestamp', true ) && $next_scheduled !== $event_time ) {
|
113 |
| - as_schedule_single_action( $event_time, $action_hook, $action_args ); |
| 123 | + $this->schedule_action( $event_time, $action_hook, $action_args ); |
114 | 124 | }
|
115 | 125 | }
|
116 | 126 |
|
@@ -139,7 +149,7 @@ public function update_status( $subscription, $new_status, $old_status ) {
|
139 | 149 |
|
140 | 150 | // The end date was set in WC_Subscriptions::update_dates() to the appropriate value, so we can schedule our action for that time
|
141 | 151 | if ( $end_time > current_time( 'timestamp', true ) && $next_scheduled !== $end_time ) {
|
142 |
| - as_schedule_single_action( $end_time, 'woocommerce_scheduled_subscription_end_of_prepaid_term', $action_args ); |
| 152 | + $this->schedule_action( $end_time, 'woocommerce_scheduled_subscription_end_of_prepaid_term', $action_args ); |
143 | 153 | }
|
144 | 154 | break;
|
145 | 155 | case 'on-hold':
|
@@ -226,4 +236,36 @@ protected function get_action_args( $date_type, $subscription ) {
|
226 | 236 | protected function unschedule_actions( $action_hook, $action_args ) {
|
227 | 237 | as_unschedule_all_actions( $action_hook, $action_args );
|
228 | 238 | }
|
| 239 | + |
| 240 | + /** |
| 241 | + * Gets the priority of the subscription-related scheduled action. |
| 242 | + * |
| 243 | + * @return int The priority of the subscription-related scheduled action. |
| 244 | + */ |
| 245 | + public function get_action_priority( $action_hook ) { |
| 246 | + return apply_filters( 'woocommerce_subscriptions_scheduled_action_priority', self::ACTION_PRIORITY, $action_hook ); |
| 247 | + } |
| 248 | + |
| 249 | + /** |
| 250 | + * Schedule an subscription-related action with the Action Scheduler. |
| 251 | + * |
| 252 | + * Subscription events are scheduled with a priority of 1 (see self::ACTION_PRIORITY) and the |
| 253 | + * group 'wc_subscription_scheduled_event' (see self::ACTION_GROUP). |
| 254 | + * |
| 255 | + * @param int $timestamp Unix timestamp of when the action should run. |
| 256 | + * @param string $action_hook Name of event used as the hook for the scheduled action. |
| 257 | + * @param array $action_args Array of name => value pairs stored against the scheduled action. |
| 258 | + * |
| 259 | + * @return int The action ID. |
| 260 | + */ |
| 261 | + protected function schedule_action( $timestamp, $action_hook, $action_args ) { |
| 262 | + $as_version = ActionScheduler_Versions::instance()->latest_version(); |
| 263 | + |
| 264 | + // On older versions of Action Scheduler, we cannot specify a priority. |
| 265 | + if ( version_compare( $as_version, '3.6.0', '<' ) ) { |
| 266 | + return as_schedule_single_action( $timestamp, $action_hook, $action_args, self::ACTION_GROUP ); |
| 267 | + } |
| 268 | + |
| 269 | + return as_schedule_single_action( $timestamp, $action_hook, $action_args, self::ACTION_GROUP, false, $this->get_action_priority( $action_hook ) ); |
| 270 | + } |
229 | 271 | }
|
0 commit comments