/var/www/html_uk/wp-content/plugins/automatewoo/includes/ActionScheduler/ActionScheduler.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php

namespace AutomateWoo\ActionScheduler;

defined'ABSPATH' ) || exit;

/**
 * ActionScheduler service class.
 *
 * Acts as a wrapper for ActionScheduler's public functions.
 *
 * @since 5.1.0
 */
class ActionScheduler implements ActionSchedulerInterface {

    
/**
     * @var AsyncActionRunner
     */
    
protected $async_runner;

    
/**
     * List of async actions to enqueue on shutdown.
     *
     * @var array[]
     */
    
protected $shutdown_async_actions = [];

    
/**
     * ActionScheduler constructor.
     *
     * @param AsyncActionRunner $async_runner
     */
    
public function __constructAsyncActionRunner $async_runner ) {
        
$this->async_runner $async_runner;

        
add_action'shutdown', [ $this'enqueue_shutdown_actions' ], );
    }

    
/**
     * Schedule an action to run once at some time in the future
     *
     * @param int    $timestamp When the job will run.
     * @param string $hook      The hook to trigger.
     * @param array  $args      Arguments to pass when the hook triggers.
     * @param string $group     The group to assign this job to.
     *
     * @return string The action ID.
     */
    
public function schedule_single$timestamp$hook$args = [], $group 'automatewoo' ) {
        return 
as_schedule_single_action$timestamp$hook$args$group );
    }

    
/**
     * Schedule an action to run now i.e. in the next available batch.
     *
     * This differs from async actions by having a scheduled time rather than being set for '0000-00-00 00:00:00'.
     * We could use an async action instead but they can't be viewed easily in the admin area
     * because the table is sorted by schedule date.
     *
     * @since 5.2.0
     *
     * @param string $hook     The hook to trigger.
     * @param array  $args     Arguments to pass when the hook triggers.
     * @param string $group    The group to assign this job to.
     * @param int    $priority The priority of the scheduled action.
     *
     * @return string The action ID.
     */
    
public function schedule_immediatestring $hook$args = [], $group 'automatewoo'$priority 10 ) {
        return 
as_schedule_single_actiongmdate'U' ) - 1$hook$args$groupfalse$priority );
    }

    
/**
     * Schedule a recurring action
     *
     * @since 5.8.1
     *
     * @param int    $timestamp When the first instance of the job will run.
     * @param int    $interval_in_seconds How long to wait between runs.
     * @param string $hook The hook to trigger.
     * @param array  $args Arguments to pass when the hook triggers.
     * @param string $group The group to assign this job to.
     * @param bool   $unique Whether the action should be unique.
     *
     * @return int The action ID.
     */
    
public function schedule_recurring_action$timestamp$interval_in_seconds$hook$args = array(), $group 'automatewoo'$unique true ) {
        return 
as_schedule_recurring_action$timestamp$interval_in_seconds$hook$args$group$unique );
    }

    
/**
     * Enqueue an action to run one time, as soon as possible
     *
     * @param string $hook  The hook to trigger.
     * @param array  $args  Arguments to pass when the hook triggers.
     * @param string $group The group to assign this job to. Defaults to 'automatewoo'.
     *
     * @return int The action ID.
     */
    
public function enqueue_async_action$hook$args = [], $group 'automatewoo' ) {
        
$this->async_runner->attach_shutdown_hook();
        return 
$this->schedule_immediate$hook$args$group);
    }

    
/**
     * Enqueue an action to run one time, as soon as possible, BUT the action is not created until 'shutdown' or
     * when this request is finished.
     *
     * This is useful to avoid cases where Action Scheduler is already running in the background and runs an action
     * before the current request is finished.
     *
     * @since 5.5.5
     *
     * @param string $hook  The hook to trigger.
     * @param array  $args  Arguments to pass when the hook triggers.
     * @param string $group The group to assign this job to. Defaults to 'automatewoo'.
     */
    
public function enqueue_async_action_on_shutdown$hook$args = [], $group 'automatewoo' ) {
        
$this->shutdown_async_actions[] = [ $hook$args$group ];
    }

    
/**
     * Check if there is an existing action in the queue with a given hook, args and group combination.
     *
     * An action in the queue could be pending, in-progress or async. If the is pending for a time in
     * future, its scheduled date will be returned as a timestamp. If it is currently being run, or an
     * async action sitting in the queue waiting to be processed, in which case boolean true will be
     * returned. Or there may be no async, in-progress or pending action for this hook, in which case,
     * boolean false will be the return value.
     *
     * @param string $hook
     * @param array  $args
     * @param string $group The group to check for jobs. Defaults to 'automatewoo'.
     *
     * @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 null$group 'automatewoo' ) {
        return 
as_next_scheduled_action$hook$args$group );
    }

    
/**
     * Search for scheduled actions.
     *
     * @param array  $args          See as_get_scheduled_actions() for possible arguments.
     * @param string $return_format OBJECT, ARRAY_A, or ids.
     * @param string $group         The group to search for jobs. Defaults to 'automatewoo'.
     *
     * @return array
     */
    
public function search$args = [], $return_format OBJECT$group 'automatewoo' ) {
        
$args['group'] = $group;

        return 
as_get_scheduled_actions$args$return_format );
    }

    
/**
     * Cancel the next scheduled instance of an action with a matching hook (and optionally matching args and group).
     *
     * Any recurring actions with a matching hook should also be cancelled, not just the next scheduled action.
     *
     * @param string $hook  The hook that the job will trigger.
     * @param array  $args  Args that would have been passed to the job.
     * @param string $group The group the job is assigned to. Defaults to 'automatewoo'.
     *
     * @return string|null The scheduled action ID if a scheduled action was found, or null if no matching action found.
     */
    
public function cancelstring $hook$args = [], $group 'automatewoo' ) {
        return 
as_unschedule_action$hook$args$group );
    }

    
/**
     * Enqueue shutdown actions if there are any.
     *
     * @since 5.5.5
     */
    
public function enqueue_shutdown_actions() {
        foreach ( 
$this->shutdown_async_actions as $action ) {
            
$this->enqueue_async_action( ...$action );
        }
    }
}