/var/www/html_uk/wp-content/plugins/automatewoo/includes/ActionScheduler/AsyncActionRunner.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
<?php

namespace AutomateWoo\ActionScheduler;

use 
ActionScheduler_Lock;

defined'ABSPATH' ) || exit;

/**
 * Class AsyncActionRunner
 *
 * @since 5.2.0
 */
class AsyncActionRunner {

    
/**
     * Whether the shutdown hook has been attached.
     *
     * @var bool
     */
    
protected $has_attached_shutdown_hook false;

    
/**
     * @var QueueRunnerAsyncRequest
     */
    
protected $async_request;

    
/**
     * @var ActionScheduler_Lock
     */
    
protected $locker;

    
/**
     * AsyncActionRunner constructor.
     *
     * @param AW_AsyncRequest_QueueRunner $async_request
     * @param ActionScheduler_Lock        $locker
     */
    
public function __constructAW_AsyncRequest_QueueRunner $async_requestActionScheduler_Lock $locker ) {
        
$this->async_request $async_request;
        
$this->locker        $locker;
    }

    
/**
     * Attach async runner shutdown hook before ActionScheduler shutdown hook.
     *
     * The shutdown hook should only be attached if an async event has been created in the current request.
     * The hook is only attached if it hasn't already been attached.
     *
     * @see ActionScheduler_QueueRunner::hook_dispatch_async_request
     */
    
public function attach_shutdown_hook() {
        if ( 
$this->has_attached_shutdown_hook ) {
            return;
        }

        
$this->has_attached_shutdown_hook true;
        
add_action'shutdown', [ $this'maybe_dispatch_async_request' ], );
    }

    
/**
     * Dispatches an async queue runner if the following conditions are met:
     *
     * - Not running in the admin context (ActionScheduler will dispatch a queue runner in that instance)
     * - The filter `automatewoo_disable_async_runner` doesn't return true
     * - The async runner is not currently locked
     */
    
public function maybe_dispatch_async_request() {
        if ( 
is_admin() ) {
            
// ActionScheduler will dispatch an async runner request on it's own.
            
return;
        }

        if ( 
apply_filters'automatewoo_disable_async_runner'false ) ) {
            return;
        }

        if ( 
$this->locker->is_locked'async-request-runner' ) ) {
            
// An async runner request has already occurred in the last 60 seconds.
            
return;
        }

        
$this->locker->set'async-request-runner' );
        
$this->async_request->maybe_dispatch();
    }
}