From f2d4c6135a0b4b909890c8cc3bd090127fb69014 Mon Sep 17 00:00:00 2001 From: Yurii Nalivaiko Date: Tue, 3 Sep 2024 14:49:07 +0200 Subject: [PATCH] Added Action Scheduler --- includes/common/class-action-scheduler.php | 191 +++++++++++++++++++++ includes/common/class-init.php | 13 ++ 2 files changed, 204 insertions(+) create mode 100644 includes/common/class-action-scheduler.php diff --git a/includes/common/class-action-scheduler.php b/includes/common/class-action-scheduler.php new file mode 100644 index 00000000..421a8adf --- /dev/null +++ b/includes/common/class-action-scheduler.php @@ -0,0 +1,191 @@ +prefix . $group; + return as_enqueue_async_action( $hook, $args, $group, $unique, $priority ); + } + + /** + * Schedule an action to run one time at some defined point in the future. + * + * @param integer $timestamp Required. The Unix timestamp representing the date you want the action to run. + * @param string $hook Required. Name of the action hook. + * @param array $args Arguments to pass to callbacks when the hook triggers. Default: array() + * @param string $group The group to assign this job to. Default: ''. + * @param boolean $unique Whether the action should be unique. Default: false. + * @param integer $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255.) + * + * @return int The action’s ID. Zero if there was an error scheduling the action. The error will be sent to error_log. + */ + public function schedule_single_action( $timestamp, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { + $group = $this->prefix . $group; + + return as_schedule_single_action( $timestamp, $hook, $args, $group, $unique, $priority ); + } + + /** + * Schedule an action to run repeatedly with a specified interval in seconds. + * + * @param integer $timestamp Required. The Unix timestamp representing the date you want the action to run. + * @param integer $interval_in_seconds Required. How long to wait between runs. + * @param string $hook Required. Name of the action hook. + * @param array $args Arguments to pass to callbacks when the hook triggers. Default: array(). + * @param string $group The group to assign this job to. Default: ''. + * @param boolean $unique Whether the action should be unique. Default: false. + * @param integer $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255. + * + * @return int The action’s ID. Zero if there was an error scheduling the action. The error will be sent to error_log. + */ + public function schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { + $group = $this->prefix . $group; + + return as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group, $unique, $priority ); + } + + /** + * Schedule an action that recurs on a cron-like schedule. + * + * If execution of a cron-like action is delayed, the next attempt will still be scheduled according to the provided cron expression. + * + * @param integer $timestamp Required. The Unix timestamp representing the date you want the action to run. + * @param string $schedule Required. A cron-like schedule string, see http://en.wikipedia.org/wiki/Cron. + * @param string $hook Required Name of the action hook. + * @param array $args Arguments to pass to callbacks when the hook triggers. Default: array(). + * @param string $group The group to assign this job to. Default: ''. + * @param boolean $unique Whether the action should be unique. Default: false. + * @param integer $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255. + * + * @return int The action’s ID. Zero if there was an error scheduling the action. The error will be sent to error_log. + */ + public function schedule_cron_action( $timestamp, $schedule, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) { + $group = $this->prefix . $group; + + return as_schedule_cron_action( $timestamp, $schedule, $hook, $args, $group, $unique, $priority ); + } + + /** + * Cancel the next occurrence of a scheduled action. + * + * @param string $hook Required. Name of the action hook. + * @param array $args Arguments passed to callbacks when the hook triggers. Default: array(). + * @param string $group The group the job is assigned to. Default: ''. + * + * @return int|null + */ + public function unschedule_action( $hook, $args = array(), $group = '' ) { + $group = $this->prefix . $group; + + return as_unschedule_action( $hook, $args, $group ); + } + + /** + * Cancel all occurrences of a scheduled action. + * + * @param string $hook Required. Name of the action hook. + * @param array $args Arguments passed to callbacks when the hook triggers. Default: array(). + * @param string $group The group the job is assigned to. Default: ''. + * + * @return string|null The scheduled action ID if a scheduled action was found, or null if no matching action found. + */ + public function unschedule_all_actions( $hook, $args = array(), $group = '' ) { + $group = $this->prefix . $group; + + return as_unschedule_all_actions( $hook, $args, $group ); + } + + /** + * Returns the next timestamp for a scheduled action. + * + * @param string $hook Required. Name of the action hook. Default: none. + * @param array $args Arguments passed to callbacks when the hook triggers. Default: array(). + * @param string $group The group the job is assigned to. Default: ''. + * + * @return int|bool The timestamp for the next occurrence of a pending scheduled action, true for an async or in-progress action or false if there is no matching action. + */ + public function next_scheduled_action( $hook, $args = array(), $group = '' ) { + $group = $this->prefix . $group; + + return as_next_scheduled_action( $hook, $args, $group ); + } + + /** + * Check if there is a scheduled action in the queue, but more efficiently than as_next_scheduled_action(). + * It’s recommended to use this function when you need to know whether a specific action is currently scheduled. + * + * @param string $hook Required. Name of the action hook. Default: none. + * @param array $args Arguments passed to callbacks when the hook triggers. Default: array(). + * @param string $group The group the job is assigned to. Default: ''. + * + * @return bool True if a matching action is pending or in-progress, false otherwise. + */ + public function has_scheduled_action( $hook, $args = array(), $group = '' ) { + $group = $this->prefix . $group; + + return as_has_scheduled_action( $hook, $args, $group ); + } + + /** + * @param array $args $args (array) Arguments to search and filter results by. Possible arguments, with their default values: + * 'hook' => '' - the name of the action that will be triggered + * 'args' => NULL - the args array that will be passed with the action + * 'date' => NULL - the scheduled date of the action. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). + * 'date_compare' => '<=’ - operator for testing “date”. accepted values are ‘!=’, ‘>’, ‘>=’, ‘<’, ‘<=’, ‘=’ + * 'modified' => NULL - the date the action was last updated. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). + * 'modified_compare' => '<=' - operator for testing “modified”. accepted values are ‘!=’, ‘>’, ‘>=’, ‘<’, ‘<=’, ‘=’ + * 'group' => '' - the group the action belongs to + * 'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING + * 'claimed' => NULL - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID + * 'per_page' => 5 - Number of results to return + * 'offset' => 0 + * 'orderby' => 'date' - accepted values are ‘hook’, ‘group’, ‘modified’, or ‘date’ + * 'order' => 'ASC' + * @param string $return_format The format in which to return the scheduled actions: 'OBJECT', 'ARRAY_A', or 'ids'. Default: 'OBJECT'. + * + * @return array Array of action rows matching the criteria specified with $args. + */ + public function get_scheduled_actions( $args, $return_format = 'OBJECT' ) { + if ( ! empty( $args['group'] ) ) { + $args['group'] = $this->prefix . $args['group']; + } + return as_get_scheduled_actions( $args, $return_format ); + } + } +} diff --git a/includes/common/class-init.php b/includes/common/class-init.php index 1e9e74cd..a4a61111 100644 --- a/includes/common/class-init.php +++ b/includes/common/class-init.php @@ -25,6 +25,7 @@ if ( ! class_exists( 'um\common\Init' ) ) { $this->secure()->hooks(); $this->site_health(); $this->theme()->hooks(); + $this->action_scheduler(); } /** @@ -86,5 +87,17 @@ if ( ! class_exists( 'um\common\Init' ) ) { } return UM()->classes['um\common\theme']; } + + /** + * @since 2.6.8 + * + * @return Action_Scheduler + */ + public function action_scheduler() { + if ( empty( UM()->classes['um\common\action_scheduler'] ) ) { + UM()->classes['um\common\action_scheduler'] = new Action_Scheduler(); + } + return UM()->classes['um\common\action_scheduler']; + } } }