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

namespace AutomateWoo\Jobs;

use 
AutomateWoo\Exceptions\InvalidArgument;
use 
AutomateWoo\Exceptions\InvalidClass;
use 
AutomateWoo\Traits\ArrayValidator;

defined'ABSPATH' ) || exit;

/**
 * JobService class.
 *
 * @version 5.1.0
 */
class JobService {

    use 
ArrayValidator;

    
// Job intervals for using in the recurrent Jobs
    
const ONE_MINUTE_INTERVAL     60;
    const 
TWO_MINUTE_INTERVAL     self::ONE_MINUTE_INTERVAL 2;
    const 
FIVE_MINUTE_INTERVAL    self::ONE_MINUTE_INTERVAL 5;
    const 
FIFTEEN_MINUTE_INTERVAL self::ONE_MINUTE_INTERVAL 15;
    const 
THIRTY_MINUTE_INTERVAL  self::ONE_MINUTE_INTERVAL 30;
    const 
ONE_HOUR_INTERVAL       self::ONE_MINUTE_INTERVAL 60;
    const 
FOUR_HOURS_INTERVAL     self::ONE_HOUR_INTERVAL 4;
    const 
ONE_DAY_INTERVAL        self::ONE_HOUR_INTERVAL 24;
    const 
TWO_DAY_INTERVAL        self::ONE_DAY_INTERVAL 2;
    const 
WEEKLY_INTERVAL         self::ONE_DAY_INTERVAL 7;

    
/**
     * @var JobRegistryInterface
     */
    
protected $registry;

    
/**
     * JobService constructor.
     *
     * @param JobRegistryInterface $registry
     */
    
public function __constructJobRegistryInterface $registry ) {
        
$this->registry $registry;
    }

    
/**
     * Initialize all jobs.
     *
     * @throws InvalidClass|InvalidArgument When there is an error loading jobs.
     */
    
public function init_jobs() {
        foreach ( 
$this->registry->list() as $job ) {
            
$job->init();

            if ( 
$job instanceof StartOnHookInterface ) {
                
$this->check_is_using_deprecated_cron_workers$job );
                
add_action$job->get_start_hook(), [ $job'start' ], 10);
            }

            if ( 
$job instanceof RecurringJobInterface ) {
                
// ActionScheduler loads its tables on "init" action
                
add_action'admin_init', [ $job'schedule_recurring' ], 10);

                
// Cancel the recurring action on deactivation
                
register_deactivation_hookAUTOMATEWOO_FILE, [ $job'cancel_recurring' ] );
            }
        }
    }

    
/**
     * Get all the jobs
     *
     * @since 6.0.0
     * @return JobInterface[] All the Jobs
     *
     * @throws InvalidClass|InvalidArgument When there is an error loading jobs.
     */
    
public function get_jobs(): array {
        return 
$this->registry->list();
    }

    
/**
     * Get a job by name.
     *
     * @param string $name The job name.
     *
     * @return JobInterface
     *
     * @throws JobException If the job is not found.
     * @throws InvalidClass|InvalidArgument When there is an invalid job class.
     */
    
public function get_jobstring $name ): JobInterface {
        return 
$this->registry->get$name );
    }

    
/**
     * Check if the start hook is relying on legacy Cron workers. If so, it shows a deprecation notice.
     *
     * @since 6.0.0
     * @param StartOnHookInterface $job The job to check.
     */
    
public function check_is_using_deprecated_cron_workers$job ) {

        
$legacy_cron_workers array_map(
            function ( 
$worker ) {
                return 
'automatewoo_' $worker '_worker';
            },
            [
                
'events',
                
'one_minute',
                
'two_minute',
                
'five_minute',
                
'fifteen_minute',
                
'thirty_minute',
                
'hourly',
                
'four_hourly',
                
'daily',
                
'two_days',
                
'weekly',
            ]
        );

        if ( 
in_array$job->get_start_hook(), $legacy_cron_workerstrue ) ) {
            
wc_deprecated_hook$job->get_start_hook(), '6.0.0''Action Scheduler Recurrent Jobs''See \AutomateWoo\Jobs\RecurringJobInterface' );
        }
    }
}