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
|
<?php
namespace AutomateWoo\Jobs;
use AutomateWoo\ActionScheduler\ActionSchedulerInterface; use AutomateWoo\Cache; use AutomateWoo\Exceptions\InvalidArgument; use AutomateWoo\Jobs\Traits\ItemDeletionDate; use AutomateWoo\Jobs\Traits\ValidateItemAsIntegerId; use AutomateWoo\OptionsStore;
defined( 'ABSPATH' ) || exit;
/** * Job that deletes expired coupons after a specified amount of time. * * @since 5.0.0 * @package AutomateWoo\Jobs */ class DeleteExpiredCoupons extends AbstractRecurringBatchedActionSchedulerJob {
use ItemDeletionDate; use ValidateItemAsIntegerId;
/** * @var OptionsStore $options_store */ protected $options_store;
/** * AbstractBatchedJob constructor. * * @param ActionSchedulerInterface $action_scheduler * @param ActionSchedulerJobMonitor $monitor * @param OptionsStore $options_store */ public function __construct( ActionSchedulerInterface $action_scheduler, ActionSchedulerJobMonitor $monitor, OptionsStore $options_store ) { $this->options_store = $options_store; parent::__construct( $action_scheduler, $monitor ); }
/** * Return the recurring job's interval in seconds. * * @since 6.0.0 * @return int The interval for the action in seconds */ public function get_interval() { return JobService::FOUR_HOURS_INTERVAL; }
/** * Get the name of the job. * * @return string */ public function get_name() { return 'delete_expired_coupons'; }
/** * Get the number of days before expired coupons are deleted. * * @return int */ public function get_deletion_period() { return absint( apply_filters( 'automatewoo/coupons/days_to_keep_expired', 14 ) ); }
/** * Can the job start. * * @return bool Returns true if the job can start. * * @throws InvalidArgument If option value is invalid. */ protected function can_start(): bool { if ( ! $this->options_store->get_clean_expired_coupons_enabled() ) { return false; }
return parent::can_start(); }
/** * Get a new batch of items. * * @param int $batch_number The batch number increments for each new batch in the job cycle. * @param array $args The args for this instance of the job. * * @return int[] */ protected function get_batch( int $batch_number, array $args ) { $deletion_date = $this->get_deletion_date(); if ( ! $deletion_date ) { return []; }
$query_args = [ 'fields' => 'ids', 'post_type' => 'shop_coupon', 'post_status' => 'any', 'posts_per_page' => $this->get_batch_size(), 'orderby' => 'date', 'order' => 'ASC', 'no_found_rows' => true, 'meta_query' => [ [ 'key' => '_is_aw_coupon', 'value' => true, ], [ 'key' => 'date_expires', 'value' => $deletion_date->getTimestamp(), 'compare' => '<', ], ], ];
$query = new \WP_Query( $query_args );
return $query->posts; }
/** * Handle a single item. * * @param int $coupon_id * @param array $args The args for this instance of the job. */ protected function process_item( $coupon_id, array $args ) { wp_delete_post( $coupon_id, true );
// Clear coupon counts. Cache::flush_group( 'coupons' ); } }
|