| @@ -10,13 +10,14 @@ | ||
| 10 | 10 | * |
| 11 | 11 | * @param string $hook The hook to trigger. |
| 12 | 12 | * @param array $args Arguments to pass when the hook triggers. |
| 13 | 13 | * @param string $group The group to assign this job to. |
| 14 | - * @param bool $unique Whether the action should be unique. | |
| 14 | + * @param bool $unique Whether the action should be unique. It will not be scheduled if another pending or running action has the same hook and group parameters. | |
| 15 | + * @param int $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255. | |
| 15 | 16 | * |
| 16 | - * @return int The action ID. | |
| 17 | + * @return int The action ID. Zero if there was an error scheduling the action. | |
| 17 | 18 | */ |
| 18 | -function as_enqueue_async_action( $hook, $args = array(), $group = '', $unique = false ) { | |
| 19 | +function as_enqueue_async_action( $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { | |
| 19 | 20 | if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) { |
| 20 | 21 | return 0; |
| 21 | 22 | } |
| 22 | 23 | |
| @@ -32,15 +33,26 @@ | ||
| 32 | 33 | * @param int|null $pre_option The value to return instead of the option value. |
| 33 | 34 | * @param string $hook Action hook. |
| 34 | 35 | * @param array $args Action arguments. |
| 35 | 36 | * @param string $group Action group. |
| 37 | + * @param int $priority Action priority. | |
| 38 | + * @param bool $unique Unique action. | |
| 36 | 39 | */ |
| 37 | - $pre = apply_filters( 'pre_as_enqueue_async_action', null, $hook, $args, $group ); | |
| 40 | + $pre = apply_filters( 'pre_as_enqueue_async_action', null, $hook, $args, $group, $priority, $unique ); | |
| 38 | 41 | if ( null !== $pre ) { |
| 39 | 42 | return is_int( $pre ) ? $pre : 0; |
| 40 | 43 | } |
| 41 | 44 | |
| 42 | - return ActionScheduler::factory()->async_unique( $hook, $args, $group, $unique ); | |
| 45 | + return ActionScheduler::factory()->create( | |
| 46 | + array( | |
| 47 | + 'type' => 'async', | |
| 48 | + 'hook' => $hook, | |
| 49 | + 'arguments' => $args, | |
| 50 | + 'group' => $group, | |
| 51 | + 'unique' => $unique, | |
| 52 | + 'priority' => $priority, | |
| 53 | + ) | |
| 54 | + ); | |
| 43 | 55 | } |
| 44 | 56 | |
| 45 | 57 | /** |
| 46 | 58 | * Schedule an action to run one time |
| @@ -48,13 +60,14 @@ | ||
| 48 | 60 | * @param int $timestamp When the job will run. |
| 49 | 61 | * @param string $hook The hook to trigger. |
| 50 | 62 | * @param array $args Arguments to pass when the hook triggers. |
| 51 | 63 | * @param string $group The group to assign this job to. |
| 52 | - * @param bool $unique Whether the action should be unique. | |
| 64 | + * @param bool $unique Whether the action should be unique. It will not be scheduled if another pending or running action has the same hook and group parameters. | |
| 65 | + * @param int $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255. | |
| 53 | 66 | * |
| 54 | - * @return int The action ID. | |
| 67 | + * @return int The action ID. Zero if there was an error scheduling the action. | |
| 55 | 68 | */ |
| 56 | -function as_schedule_single_action( $timestamp, $hook, $args = array(), $group = '', $unique = false ) { | |
| 69 | +function as_schedule_single_action( $timestamp, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { | |
| 57 | 70 | if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) { |
| 58 | 71 | return 0; |
| 59 | 72 | } |
| 60 | 73 | |
| @@ -71,15 +84,27 @@ | ||
| 71 | 84 | * @param int $timestamp When the action will run. |
| 72 | 85 | * @param string $hook Action hook. |
| 73 | 86 | * @param array $args Action arguments. |
| 74 | 87 | * @param string $group Action group. |
| 88 | + * @param int $priorities Action priority. | |
| 89 | + * @param bool $unique Unique action. | |
| 75 | 90 | */ |
| 76 | - $pre = apply_filters( 'pre_as_schedule_single_action', null, $timestamp, $hook, $args, $group ); | |
| 91 | + $pre = apply_filters( 'pre_as_schedule_single_action', null, $timestamp, $hook, $args, $group, $priority, $unique ); | |
| 77 | 92 | if ( null !== $pre ) { |
| 78 | 93 | return is_int( $pre ) ? $pre : 0; |
| 79 | 94 | } |
| 80 | 95 | |
| 81 | - return ActionScheduler::factory()->single_unique( $hook, $args, $timestamp, $group, $unique ); | |
| 96 | + return ActionScheduler::factory()->create( | |
| 97 | + array( | |
| 98 | + 'type' => 'single', | |
| 99 | + 'hook' => $hook, | |
| 100 | + 'arguments' => $args, | |
| 101 | + 'when' => $timestamp, | |
| 102 | + 'group' => $group, | |
| 103 | + 'unique' => $unique, | |
| 104 | + 'priority' => $priority, | |
| 105 | + ) | |
| 106 | + ); | |
| 82 | 107 | } |
| 83 | 108 | |
| 84 | 109 | /** |
| 85 | 110 | * Schedule a recurring action |
| @@ -88,17 +113,38 @@ | ||
| 88 | 113 | * @param int $interval_in_seconds How long to wait between runs. |
| 89 | 114 | * @param string $hook The hook to trigger. |
| 90 | 115 | * @param array $args Arguments to pass when the hook triggers. |
| 91 | 116 | * @param string $group The group to assign this job to. |
| 92 | - * @param bool $unique Whether the action should be unique. | |
| 117 | + * @param bool $unique Whether the action should be unique. It will not be scheduled if another pending or running action has the same hook and group parameters. | |
| 118 | + * @param int $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255. | |
| 93 | 119 | * |
| 94 | - * @return int The action ID. | |
| 120 | + * @return int The action ID. Zero if there was an error scheduling the action. | |
| 95 | 121 | */ |
| 96 | -function as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '', $unique = false ) { | |
| 122 | +function as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { | |
| 97 | 123 | if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) { |
| 98 | 124 | return 0; |
| 99 | 125 | } |
| 100 | 126 | |
| 127 | + $interval = (int) $interval_in_seconds; | |
| 128 | + | |
| 129 | + // We expect an integer and allow it to be passed using float and string types, but otherwise | |
| 130 | + // should reject unexpected values. | |
| 131 | + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison | |
| 132 | + if ( ! is_numeric( $interval_in_seconds ) || $interval_in_seconds != $interval ) { | |
| 133 | + _doing_it_wrong( | |
| 134 | + __METHOD__, | |
| 135 | + sprintf( | |
| 136 | + /* translators: 1: provided value 2: provided type. */ | |
| 137 | + esc_html__( 'An integer was expected but "%1$s" (%2$s) was received.', 'action-scheduler' ), | |
| 138 | + esc_html( $interval_in_seconds ), | |
| 139 | + esc_html( gettype( $interval_in_seconds ) ) | |
| 140 | + ), | |
| 141 | + '3.6.0' | |
| 142 | + ); | |
| 143 | + | |
| 144 | + return 0; | |
| 145 | + } | |
| 146 | + | |
| 101 | 147 | /** |
| 102 | 148 | * Provides an opportunity to short-circuit the default process for enqueuing recurring |
| 103 | 149 | * actions. |
| 104 | 150 | * |
| @@ -112,15 +158,28 @@ | ||
| 112 | 158 | * @param int $interval_in_seconds How long to wait between runs. |
| 113 | 159 | * @param string $hook Action hook. |
| 114 | 160 | * @param array $args Action arguments. |
| 115 | 161 | * @param string $group Action group. |
| 162 | + * @param int $priority Action priority. | |
| 163 | + * @param bool $unique Unique action. | |
| 116 | 164 | */ |
| 117 | - $pre = apply_filters( 'pre_as_schedule_recurring_action', null, $timestamp, $interval_in_seconds, $hook, $args, $group ); | |
| 165 | + $pre = apply_filters( 'pre_as_schedule_recurring_action', null, $timestamp, $interval_in_seconds, $hook, $args, $group, $priority, $unique ); | |
| 118 | 166 | if ( null !== $pre ) { |
| 119 | 167 | return is_int( $pre ) ? $pre : 0; |
| 120 | 168 | } |
| 121 | 169 | |
| 122 | - return ActionScheduler::factory()->recurring_unique( $hook, $args, $timestamp, $interval_in_seconds, $group, $unique ); | |
| 170 | + return ActionScheduler::factory()->create( | |
| 171 | + array( | |
| 172 | + 'type' => 'recurring', | |
| 173 | + 'hook' => $hook, | |
| 174 | + 'arguments' => $args, | |
| 175 | + 'when' => $timestamp, | |
| 176 | + 'pattern' => $interval_in_seconds, | |
| 177 | + 'group' => $group, | |
| 178 | + 'unique' => $unique, | |
| 179 | + 'priority' => $priority, | |
| 180 | + ) | |
| 181 | + ); | |
| 123 | 182 | } |
| 124 | 183 | |
| 125 | 184 | /** |
| 126 | 185 | * Schedule an action that recurs on a cron-like schedule. |
| @@ -141,13 +200,14 @@ | ||
| 141 | 200 | * +------------------------- min (0 - 59) |
| 142 | 201 | * @param string $hook The hook to trigger. |
| 143 | 202 | * @param array $args Arguments to pass when the hook triggers. |
| 144 | 203 | * @param string $group The group to assign this job to. |
| 145 | - * @param bool $unique Whether the action should be unique. | |
| 204 | + * @param bool $unique Whether the action should be unique. It will not be scheduled if another pending or running action has the same hook and group parameters. | |
| 205 | + * @param int $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255. | |
| 146 | 206 | * |
| 147 | - * @return int The action ID. | |
| 207 | + * @return int The action ID. Zero if there was an error scheduling the action. | |
| 148 | 208 | */ |
| 149 | -function as_schedule_cron_action( $timestamp, $schedule, $hook, $args = array(), $group = '', $unique = false ) { | |
| 209 | +function as_schedule_cron_action( $timestamp, $schedule, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { | |
| 150 | 210 | if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) { |
| 151 | 211 | return 0; |
| 152 | 212 | } |
| 153 | 213 | |
| @@ -165,15 +225,28 @@ | ||
| 165 | 225 | * @param string $schedule Cron-like schedule string. |
| 166 | 226 | * @param string $hook Action hook. |
| 167 | 227 | * @param array $args Action arguments. |
| 168 | 228 | * @param string $group Action group. |
| 229 | + * @param int $priority Action priority. | |
| 230 | + * @param bool $unique Unique action. | |
| 169 | 231 | */ |
| 170 | - $pre = apply_filters( 'pre_as_schedule_cron_action', null, $timestamp, $schedule, $hook, $args, $group ); | |
| 232 | + $pre = apply_filters( 'pre_as_schedule_cron_action', null, $timestamp, $schedule, $hook, $args, $group, $priority, $unique ); | |
| 171 | 233 | if ( null !== $pre ) { |
| 172 | 234 | return is_int( $pre ) ? $pre : 0; |
| 173 | 235 | } |
| 174 | 236 | |
| 175 | - return ActionScheduler::factory()->cron_unique( $hook, $args, $timestamp, $schedule, $group, $unique ); | |
| 237 | + return ActionScheduler::factory()->create( | |
| 238 | + array( | |
| 239 | + 'type' => 'cron', | |
| 240 | + 'hook' => $hook, | |
| 241 | + 'arguments' => $args, | |
| 242 | + 'when' => $timestamp, | |
| 243 | + 'pattern' => $schedule, | |
| 244 | + 'group' => $group, | |
| 245 | + 'unique' => $unique, | |
| 246 | + 'priority' => $priority, | |
| 247 | + ) | |
| 248 | + ); | |
| 176 | 249 | } |
| 177 | 250 | |
| 178 | 251 | /** |
| 179 | 252 | * Cancel the next occurrence of a scheduled action. |
| @@ -214,11 +287,12 @@ | ||
| 214 | 287 | } catch ( Exception $exception ) { |
| 215 | 288 | ActionScheduler::logger()->log( |
| 216 | 289 | $action_id, |
| 217 | 290 | sprintf( |
| 218 | - /* translators: %s is the name of the hook to be cancelled. */ | |
| 219 | - __( 'Caught exception while cancelling action: %s', 'action-scheduler' ), | |
| 220 | - esc_attr( $hook ) | |
| 291 | + /* translators: %1$s is the name of the hook to be cancelled, %2$s is the exception message. */ | |
| 292 | + __( 'Caught exception while cancelling action "%1$s": %2$s', 'action-scheduler' ), | |
| 293 | + $hook, | |
| 294 | + $exception->getMessage() | |
| 221 | 295 | ) |
| 222 | 296 | ); |
| 223 | 297 | |
| 224 | 298 | $action_id = null; |
| @@ -385,9 +459,9 @@ | ||
| 385 | 459 | foreach ( $ids as $action_id ) { |
| 386 | 460 | $actions[ $action_id ] = $store->fetch_action( $action_id ); |
| 387 | 461 | } |
| 388 | 462 | |
| 389 | - if ( ARRAY_A == $return_format ) { | |
| 463 | + if ( ARRAY_A === $return_format ) { | |
| 390 | 464 | foreach ( $actions as $action_id => $action_object ) { |
| 391 | 465 | $actions[ $action_id ] = get_object_vars( $action_object ); |
| 392 | 466 | } |
| 393 | 467 | } |
| @@ -420,5 +494,20 @@ | ||
| 420 | 494 | } else { |
| 421 | 495 | $date = new ActionScheduler_DateTime( null === $date_string ? 'now' : $date_string, new DateTimeZone( $timezone ) ); |
| 422 | 496 | } |
| 423 | 497 | return $date; |
| 498 | +} | |
| 499 | + | |
| 500 | +/** | |
| 501 | + * Check if a specific feature is supported by the current version of Action Scheduler. | |
| 502 | + * | |
| 503 | + * @since 3.9.3 | |
| 504 | + * | |
| 505 | + * @param string $feature The feature to check support for. | |
| 506 | + * | |
| 507 | + * @return bool True if the feature is supported, false otherwise. | |
| 508 | + */ | |
| 509 | +function as_supports( string $feature ): bool { | |
| 510 | + $supported_features = array( 'ensure_recurring_actions_hook' ); | |
| 511 | + | |
| 512 | + return in_array( $feature, $supported_features, true ); | |
| 424 | 513 | } |