classes
1 year ago
deprecated
1 year ago
lib
1 year ago
action-scheduler.php
1 year ago
functions.php
1 year ago
index.php
3 years ago
functions.php
233 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | function as_enqueue_async_action( $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { |
| 4 | if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) { |
| 5 | return 0; |
| 6 | } |
| 7 | $pre = apply_filters( 'pre_as_enqueue_async_action', null, $hook, $args, $group, $priority, $unique ); |
| 8 | if ( null !== $pre ) { |
| 9 | return is_int( $pre ) ? $pre : 0; |
| 10 | } |
| 11 | return ActionScheduler::factory()->create( |
| 12 | array( |
| 13 | 'type' => 'async', |
| 14 | 'hook' => $hook, |
| 15 | 'arguments' => $args, |
| 16 | 'group' => $group, |
| 17 | 'unique' => $unique, |
| 18 | 'priority' => $priority, |
| 19 | ) |
| 20 | ); |
| 21 | } |
| 22 | function as_schedule_single_action( $timestamp, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { |
| 23 | if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) { |
| 24 | return 0; |
| 25 | } |
| 26 | $pre = apply_filters( 'pre_as_schedule_single_action', null, $timestamp, $hook, $args, $group, $priority ); |
| 27 | if ( null !== $pre ) { |
| 28 | return is_int( $pre ) ? $pre : 0; |
| 29 | } |
| 30 | return ActionScheduler::factory()->create( |
| 31 | array( |
| 32 | 'type' => 'single', |
| 33 | 'hook' => $hook, |
| 34 | 'arguments' => $args, |
| 35 | 'when' => $timestamp, |
| 36 | 'group' => $group, |
| 37 | 'unique' => $unique, |
| 38 | 'priority' => $priority, |
| 39 | ) |
| 40 | ); |
| 41 | } |
| 42 | function as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { |
| 43 | if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) { |
| 44 | return 0; |
| 45 | } |
| 46 | $interval = (int) $interval_in_seconds; |
| 47 | // We expect an integer and allow it to be passed using float and string types, but otherwise |
| 48 | // should reject unexpected values. |
| 49 | // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
| 50 | if ( ! is_numeric( $interval_in_seconds ) || $interval_in_seconds != $interval ) { |
| 51 | _doing_it_wrong( |
| 52 | __METHOD__, |
| 53 | sprintf( |
| 54 | esc_html__( 'An integer was expected but "%1$s" (%2$s) was received.', 'action-scheduler' ), |
| 55 | esc_html( $interval_in_seconds ), |
| 56 | esc_html( gettype( $interval_in_seconds ) ) |
| 57 | ), |
| 58 | '3.6.0' |
| 59 | ); |
| 60 | return 0; |
| 61 | } |
| 62 | $pre = apply_filters( 'pre_as_schedule_recurring_action', null, $timestamp, $interval_in_seconds, $hook, $args, $group, $priority ); |
| 63 | if ( null !== $pre ) { |
| 64 | return is_int( $pre ) ? $pre : 0; |
| 65 | } |
| 66 | return ActionScheduler::factory()->create( |
| 67 | array( |
| 68 | 'type' => 'recurring', |
| 69 | 'hook' => $hook, |
| 70 | 'arguments' => $args, |
| 71 | 'when' => $timestamp, |
| 72 | 'pattern' => $interval_in_seconds, |
| 73 | 'group' => $group, |
| 74 | 'unique' => $unique, |
| 75 | 'priority' => $priority, |
| 76 | ) |
| 77 | ); |
| 78 | } |
| 79 | function as_schedule_cron_action( $timestamp, $schedule, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { |
| 80 | if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) { |
| 81 | return 0; |
| 82 | } |
| 83 | $pre = apply_filters( 'pre_as_schedule_cron_action', null, $timestamp, $schedule, $hook, $args, $group, $priority ); |
| 84 | if ( null !== $pre ) { |
| 85 | return is_int( $pre ) ? $pre : 0; |
| 86 | } |
| 87 | return ActionScheduler::factory()->create( |
| 88 | array( |
| 89 | 'type' => 'cron', |
| 90 | 'hook' => $hook, |
| 91 | 'arguments' => $args, |
| 92 | 'when' => $timestamp, |
| 93 | 'pattern' => $schedule, |
| 94 | 'group' => $group, |
| 95 | 'unique' => $unique, |
| 96 | 'priority' => $priority, |
| 97 | ) |
| 98 | ); |
| 99 | } |
| 100 | function as_unschedule_action( $hook, $args = array(), $group = '' ) { |
| 101 | if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) { |
| 102 | return 0; |
| 103 | } |
| 104 | $params = array( |
| 105 | 'hook' => $hook, |
| 106 | 'status' => ActionScheduler_Store::STATUS_PENDING, |
| 107 | 'orderby' => 'date', |
| 108 | 'order' => 'ASC', |
| 109 | 'group' => $group, |
| 110 | ); |
| 111 | if ( is_array( $args ) ) { |
| 112 | $params['args'] = $args; |
| 113 | } |
| 114 | $action_id = ActionScheduler::store()->query_action( $params ); |
| 115 | if ( $action_id ) { |
| 116 | try { |
| 117 | ActionScheduler::store()->cancel_action( $action_id ); |
| 118 | } catch ( Exception $exception ) { |
| 119 | ActionScheduler::logger()->log( |
| 120 | $action_id, |
| 121 | sprintf( |
| 122 | __( 'Caught exception while cancelling action "%1$s": %2$s', 'action-scheduler' ), |
| 123 | $hook, |
| 124 | $exception->getMessage() |
| 125 | ) |
| 126 | ); |
| 127 | $action_id = null; |
| 128 | } |
| 129 | } |
| 130 | return $action_id; |
| 131 | } |
| 132 | function as_unschedule_all_actions( $hook, $args = array(), $group = '' ) { |
| 133 | if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) { |
| 134 | return; |
| 135 | } |
| 136 | if ( empty( $args ) ) { |
| 137 | if ( ! empty( $hook ) && empty( $group ) ) { |
| 138 | ActionScheduler_Store::instance()->cancel_actions_by_hook( $hook ); |
| 139 | return; |
| 140 | } |
| 141 | if ( ! empty( $group ) && empty( $hook ) ) { |
| 142 | ActionScheduler_Store::instance()->cancel_actions_by_group( $group ); |
| 143 | return; |
| 144 | } |
| 145 | } |
| 146 | do { |
| 147 | $unscheduled_action = as_unschedule_action( $hook, $args, $group ); |
| 148 | } while ( ! empty( $unscheduled_action ) ); |
| 149 | } |
| 150 | function as_next_scheduled_action( $hook, $args = null, $group = '' ) { |
| 151 | if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) { |
| 152 | return false; |
| 153 | } |
| 154 | $params = array( |
| 155 | 'hook' => $hook, |
| 156 | 'orderby' => 'date', |
| 157 | 'order' => 'ASC', |
| 158 | 'group' => $group, |
| 159 | ); |
| 160 | if ( is_array( $args ) ) { |
| 161 | $params['args'] = $args; |
| 162 | } |
| 163 | $params['status'] = ActionScheduler_Store::STATUS_RUNNING; |
| 164 | $action_id = ActionScheduler::store()->query_action( $params ); |
| 165 | if ( $action_id ) { |
| 166 | return true; |
| 167 | } |
| 168 | $params['status'] = ActionScheduler_Store::STATUS_PENDING; |
| 169 | $action_id = ActionScheduler::store()->query_action( $params ); |
| 170 | if ( null === $action_id ) { |
| 171 | return false; |
| 172 | } |
| 173 | $action = ActionScheduler::store()->fetch_action( $action_id ); |
| 174 | $scheduled_date = $action->get_schedule()->get_date(); |
| 175 | if ( $scheduled_date ) { |
| 176 | return (int) $scheduled_date->format( 'U' ); |
| 177 | } elseif ( null === $scheduled_date ) { // pending async action with NullSchedule. |
| 178 | return true; |
| 179 | } |
| 180 | return false; |
| 181 | } |
| 182 | function as_has_scheduled_action( $hook, $args = null, $group = '' ) { |
| 183 | if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) { |
| 184 | return false; |
| 185 | } |
| 186 | $query_args = array( |
| 187 | 'hook' => $hook, |
| 188 | 'status' => array( ActionScheduler_Store::STATUS_RUNNING, ActionScheduler_Store::STATUS_PENDING ), |
| 189 | 'group' => $group, |
| 190 | 'orderby' => 'none', |
| 191 | ); |
| 192 | if ( null !== $args ) { |
| 193 | $query_args['args'] = $args; |
| 194 | } |
| 195 | $action_id = ActionScheduler::store()->query_action( $query_args ); |
| 196 | return null !== $action_id; |
| 197 | } |
| 198 | function as_get_scheduled_actions( $args = array(), $return_format = OBJECT ) { |
| 199 | if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) { |
| 200 | return array(); |
| 201 | } |
| 202 | $store = ActionScheduler::store(); |
| 203 | foreach ( array( 'date', 'modified' ) as $key ) { |
| 204 | if ( isset( $args[ $key ] ) ) { |
| 205 | $args[ $key ] = as_get_datetime_object( $args[ $key ] ); |
| 206 | } |
| 207 | } |
| 208 | $ids = $store->query_actions( $args ); |
| 209 | if ( 'ids' === $return_format || 'int' === $return_format ) { |
| 210 | return $ids; |
| 211 | } |
| 212 | $actions = array(); |
| 213 | foreach ( $ids as $action_id ) { |
| 214 | $actions[ $action_id ] = $store->fetch_action( $action_id ); |
| 215 | } |
| 216 | if ( ARRAY_A === $return_format ) { |
| 217 | foreach ( $actions as $action_id => $action_object ) { |
| 218 | $actions[ $action_id ] = get_object_vars( $action_object ); |
| 219 | } |
| 220 | } |
| 221 | return $actions; |
| 222 | } |
| 223 | function as_get_datetime_object( $date_string = null, $timezone = 'UTC' ) { |
| 224 | if ( is_object( $date_string ) && $date_string instanceof DateTime ) { |
| 225 | $date = new ActionScheduler_DateTime( $date_string->format( 'Y-m-d H:i:s' ), new DateTimeZone( $timezone ) ); |
| 226 | } elseif ( is_numeric( $date_string ) ) { |
| 227 | $date = new ActionScheduler_DateTime( '@' . $date_string, new DateTimeZone( $timezone ) ); |
| 228 | } else { |
| 229 | $date = new ActionScheduler_DateTime( null === $date_string ? 'now' : $date_string, new DateTimeZone( $timezone ) ); |
| 230 | } |
| 231 | return $date; |
| 232 | } |
| 233 |