-
Notifications
You must be signed in to change notification settings - Fork 385
/
CronBasedBackgroundTask.php
197 lines (169 loc) · 5.22 KB
/
CronBasedBackgroundTask.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* Abstract class CronBasedBackgroundTask.
*
* @package AmpProject\AmpWP
*/
namespace AmpProject\AmpWP\BackgroundTask;
use AmpProject\AmpWP\Icon;
use AmpProject\AmpWP\Infrastructure\Conditional;
use AmpProject\AmpWP\Infrastructure\Deactivateable;
use AmpProject\AmpWP\Infrastructure\Registerable;
use AmpProject\AmpWP\Infrastructure\Service;
/**
* Abstract base class for using cron to execute a background task.
*
* @package AmpProject\AmpWP
* @since 2.0
* @internal
*/
abstract class CronBasedBackgroundTask implements Service, Registerable, Conditional, Deactivateable {
const DEFAULT_INTERVAL_HOURLY = 'hourly';
const DEFAULT_INTERVAL_TWICE_DAILY = 'twicedaily';
const DEFAULT_INTERVAL_DAILY = 'daily';
/**
* Name of the plugin as WordPress is expecting it.
*
* This should usually have the form "amp/amp.php".
*
* @var string
*/
private $plugin_file;
/**
* Check whether the conditional object is currently needed.
*
* @return bool Whether the conditional object is needed.
*/
public static function is_needed() {
return is_admin() || wp_doing_cron();
}
/**
* Register the service with the system.
*
* @return void
*/
public function register() {
add_action( 'admin_init', [ $this, 'schedule_event' ] );
add_action( $this->get_event_name(), [ $this, 'process' ] );
$this->plugin_file = plugin_basename( dirname( dirname( __DIR__ ) ) . '/amp.php' );
add_action( "network_admin_plugin_action_links_{$this->plugin_file}", [ $this, 'add_warning_sign_to_network_deactivate_action' ], 10, 1 );
add_action( 'plugin_row_meta', [ $this, 'add_warning_to_plugin_meta' ], 10, 2 );
}
/**
* Get warning icon markup.
*
* @return string Warning icon markup.
*/
private function get_warning_icon() {
return sprintf( '<span style="vertical-align: middle">%s</span>', Icon::warning()->to_html() );
}
/**
* Schedule the event.
*
* This does nothing if the event is already scheduled.
*
* @return void
*/
public function schedule_event() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$event_name = $this->get_event_name();
$timestamp = wp_next_scheduled( $event_name );
if ( $timestamp ) {
return;
}
wp_schedule_event( time(), $this->get_interval(), $event_name );
}
/**
* Run deactivation logic.
*
* This should be hooked up to the WordPress deactivation hook.
*
* @todo Needs refactoring if used for more than one cron-based task, to avoid iterating over sites multiple times.
*
* @param bool $network_wide Whether the deactivation was done network-wide.
* @return void
*/
public function deactivate( $network_wide ) {
if ( $network_wide && is_multisite() && ! wp_is_large_network( 'sites' ) ) {
foreach ( get_sites(
[
'fields' => 'ids',
'number' => 0,
]
) as $blog_id ) {
switch_to_blog( $blog_id );
wp_clear_scheduled_hook( $this->get_event_name() );
restore_current_blog();
}
} else {
wp_clear_scheduled_hook( $this->get_event_name() );
}
}
/**
* Add a warning sign to the network deactivate action on the network plugins screen.
*
* @param string[] $actions An array of plugin action links. By default this can include 'activate',
* 'deactivate', and 'delete'.
* @return string[]
*/
public function add_warning_sign_to_network_deactivate_action( $actions ) {
if ( ! wp_is_large_network() ) {
return $actions;
}
if ( ! array_key_exists( 'deactivate', $actions ) ) {
return $actions;
}
wp_enqueue_style( 'amp-icons' );
$warning_icon = $this->get_warning_icon();
if ( false === strpos( $actions['deactivate'], $warning_icon ) ) {
$actions['deactivate'] = preg_replace( '#(?=</a>)#i', ' ' . $warning_icon, $actions['deactivate'] );
}
return $actions;
}
/**
* Add a warning to the plugin meta row on the network plugins screen.
*
* @param string[] $plugin_meta An array of the plugin's metadata, including the version, author, author URI, and
* plugin URI.
* @param string $plugin_file Path to the plugin file relative to the plugins directory.
* @return string[]
*/
public function add_warning_to_plugin_meta( $plugin_meta, $plugin_file ) {
if ( ! is_multisite() || ! wp_is_large_network() ) {
return $plugin_meta;
}
if ( $plugin_file !== $this->plugin_file ) {
return $plugin_meta;
}
wp_enqueue_style( 'amp-icons' );
$warning = $this->get_warning_icon() . ' ' . esc_html__( 'Large site detected. Deactivation will leave orphaned scheduled events behind.', 'amp' ) . ' ' . $this->get_warning_icon();
if ( ! in_array( $warning, $plugin_meta, true ) ) {
$plugin_meta[] = $warning;
}
return $plugin_meta;
}
/**
* Get the interval to use for the event.
*
* @return string An existing interval name. Valid values are 'hourly', 'twicedaily' or 'daily'.
*/
abstract protected function get_interval();
/**
* Get the event name.
*
* This is the "slug" of the event, not the display name.
*
* Note: the event name should be prefixed to prevent naming collisions.
*
* @return string Name of the event.
*/
abstract protected function get_event_name();
/**
* Process a single cron tick.
*
* @return void
*/
abstract public function process();
}