Skip to content

Commit d5ef95c

Browse files
authored
Merge pull request #97 from deliciousbrains/improvements-from-wp-offload-media
Port forward new features and improvements from WP Offload Media
2 parents c6168c3 + b6dd6b7 commit d5ef95c

File tree

5 files changed

+978
-88
lines changed

5 files changed

+978
-88
lines changed

.phpcs.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,7 @@
3636

3737
<!-- TODO: Maybe fix -->
3838
<exclude name="WordPress.WP.CronInterval.ChangeDetected"/>
39-
<exclude name="WordPress.WP.AlternativeFunctions.rand_rand"/>
4039
<exclude name="WordPress.DB.DirectDatabaseQuery.DirectQuery"/>
41-
42-
<!-- TODO: Should fix -->
43-
<exclude name="WordPress.DB.PreparedSQL.InterpolatedNotPrepared"/>
44-
<exclude name="PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket"/>
45-
<exclude name="PEAR.Functions.FunctionCallSignature.MultipleArguments"/>
46-
<exclude name="PEAR.Functions.FunctionCallSignature.CloseBracketLine"/>
47-
<exclude name="Generic.Commenting.DocComment.SpacingAfter"/>
4840
</rule>
4941
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
5042
<properties>

README.md

Lines changed: 150 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ __Requires PHP 5.6+__
1010

1111
The recommended way to install this library in your project is by loading it through Composer:
1212

13-
```
13+
```shell
1414
composer require deliciousbrains/wp-background-processing
1515
```
1616

@@ -27,6 +27,11 @@ Extend the `WP_Async_Request` class:
2727
```php
2828
class WP_Example_Request extends WP_Async_Request {
2929

30+
/**
31+
* @var string
32+
*/
33+
protected $prefix = 'my_plugin';
34+
3035
/**
3136
* @var string
3237
*/
@@ -45,45 +50,62 @@ class WP_Example_Request extends WP_Async_Request {
4550
}
4651
```
4752

48-
##### `protected $action`
53+
#### `protected $prefix`
54+
55+
Should be set to a unique prefix associated with your plugin, theme, or site's custom function prefix.
56+
57+
#### `protected $action`
4958

5059
Should be set to a unique name.
5160

52-
##### `protected function handle()`
61+
#### `protected function handle()`
5362

5463
Should contain any logic to perform during the non-blocking request. The data passed to the request will be accessible via `$_POST`.
5564

56-
##### Dispatching Requests
65+
#### Dispatching Requests
5766

5867
Instantiate your request:
5968

60-
`$this->example_request = new WP_Example_Request();`
69+
```php
70+
$this->example_request = new WP_Example_Request();
71+
```
6172

6273
Add data to the request if required:
6374

64-
`$this->example_request->data( array( 'value1' => $value1, 'value2' => $value2 ) );`
75+
```php
76+
$this->example_request->data( array( 'value1' => $value1, 'value2' => $value2 ) );
77+
```
6578

6679
Fire off the request:
6780

68-
`$this->example_request->dispatch();`
81+
```php
82+
$this->example_request->dispatch();
83+
```
6984

7085
Chaining is also supported:
7186

72-
`$this->example_request->data( array( 'data' => $data ) )->dispatch();`
87+
```php
88+
$this->example_request->data( array( 'data' => $data ) )->dispatch();
89+
```
7390

7491
### Background Process
7592

76-
Background processes work in a similar fashion to async requests, but they allow you to queue tasks. Items pushed onto the queue will be processed in the background once the queue has been dispatched. Queues will also scale based on available server resources, so higher end servers will process more items per batch. Once a batch has completed, the next batch will start instantly.
93+
Background processes work in a similar fashion to async requests, but they allow you to queue tasks. Items pushed onto the queue will be processed in the background once the queue has been saved and dispatched. Queues will also scale based on available server resources, so higher end servers will process more items per batch. Once a batch has completed, the next batch will start instantly.
7794

7895
Health checks run by default every 5 minutes to ensure the queue is running when queued items exist. If the queue has failed it will be restarted.
7996

80-
Queues work on a first in first out basis, which allows additional items to be pushed to the queue even if it’s already processing.
97+
Queues work on a first in first out basis, which allows additional items to be pushed to the queue even if it’s already processing. Saving a new batch of queued items and dispatching while another background processing instance is already running will result in the dispatch shortcutting out and the existing instance eventually picking up the new items and processing them when it is their turn.
8198

8299
Extend the `WP_Background_Process` class:
83100

84101
```php
85102
class WP_Example_Process extends WP_Background_Process {
86103

104+
/**
105+
* @var string
106+
*/
107+
protected $prefix = 'my_plugin';
108+
87109
/**
88110
* @var string
89111
*/
@@ -122,23 +144,29 @@ class WP_Example_Process extends WP_Background_Process {
122144
}
123145
```
124146

125-
##### `protected $action`
147+
#### `protected $prefix`
148+
149+
Should be set to a unique prefix associated with your plugin, theme, or site's custom function prefix.
150+
151+
#### `protected $action`
126152

127153
Should be set to a unique name.
128154

129-
##### `protected function task( $item )`
155+
#### `protected function task( $item )`
130156

131157
Should contain any logic to perform on the queued item. Return `false` to remove the item from the queue or return `$item` to push it back onto the queue for further processing. If the item has been modified and is pushed back onto the queue the current state will be saved before the batch is exited.
132158

133-
##### `protected function complete()`
159+
#### `protected function complete()`
134160

135161
Optionally contain any logic to perform once the queue has completed.
136162

137-
##### Dispatching Processes
163+
#### Dispatching Processes
138164

139165
Instantiate your process:
140166

141-
`$this->example_process = new WP_Example_Process();`
167+
```php
168+
$this->example_process = new WP_Example_Process();
169+
```
142170

143171
**Note:** You must instantiate your process unconditionally. All requests should do this, even if nothing is pushed to the queue.
144172

@@ -152,7 +180,113 @@ foreach ( $items as $item ) {
152180

153181
Save and dispatch the queue:
154182

155-
`$this->example_process->save()->dispatch();`
183+
```php
184+
$this->example_process->save()->dispatch();
185+
```
186+
187+
#### Background Process Status
188+
189+
A background process can be queued, processing, paused, cancelled, or none of the above (not started or has completed).
190+
191+
##### Queued
192+
193+
To check whether a background process has queued items use `is_queued()`.
194+
195+
```php
196+
if ( $this->example_process->is_queued() ) {
197+
// Do something because background process has queued items, e.g. add notice in admin UI.
198+
}
199+
```
200+
201+
##### Processing
202+
203+
To check whether a background process is currently handling a queue of items use `is_processing()`.
204+
205+
```php
206+
if ( $this->example_process->is_processing() ) {
207+
// Do something because background process is running, e.g. add notice in admin UI.
208+
}
209+
```
210+
211+
##### Paused
212+
213+
You can pause a background process with `pause()`.
214+
215+
```php
216+
$this->example_process->pause();
217+
```
218+
219+
The currently processing batch will continue until it either completes or reaches the time or memory limit. At that point it'll unlock the process and either complete the batch if the queue is empty, or perform a dispatch that will result in the handler removing the healthcheck cron and firing a "paused" action.
220+
221+
To check whether a background process is currently paused use `is_paused()`.
222+
223+
```php
224+
if ( $this->example_process->is_paused() ) {
225+
// Do something because background process is paused, e.g. add notice in admin UI.
226+
}
227+
```
228+
229+
You can perform an action in response to background processing being paused by handling the "paused" action for the background process's identifier ($prefix + $action).
230+
231+
```php
232+
add_action( 'my_plugin_example_process_paused', function() {
233+
// Do something because background process is paused, e.g. add notice in admin UI.
234+
});
235+
```
236+
237+
You can resume a background process with `resume()`.
238+
239+
```php
240+
$this->example_process->resume();
241+
```
242+
243+
You can perform an action in response to background processing being resumed by handling the "resumed" action for the background process's identifier ($prefix + $action).
244+
245+
```php
246+
add_action( 'my_plugin_example_process_resumed', function() {
247+
// Do something because background process is resumed, e.g. add notice in admin UI.
248+
});
249+
```
250+
251+
##### Cancelled
252+
253+
You can cancel a background process with `cancel()`.
254+
255+
```php
256+
$this->example_process->cancel();
257+
```
258+
259+
The currently processing batch will continue until it either completes or reaches the time or memory limit. At that point it'll unlock the process and either complete the batch if the queue is empty, or perform a dispatch that will result in the handler removing the healthcheck cron, deleting all batches of queued items and firing a "cancelled" action.
260+
261+
To check whether a background process is currently cancelled use `is_cancelled()`.
262+
263+
```php
264+
if ( $this->example_process->is_cancelled() ) {
265+
// Do something because background process is cancelled, e.g. add notice in admin UI.
266+
}
267+
```
268+
269+
You can perform an action in response to background processing being cancelled by handling the "cancelled" action for the background process's identifier ($prefix + $action).
270+
271+
```php
272+
add_action( 'my_plugin_example_process_cancelled', function() {
273+
// Do something because background process is paused, e.g. add notice in admin UI.
274+
});
275+
```
276+
277+
The "cancelled" action fires once the queue has been cleared down and cancelled status removed. After which `is_cancelled()` will no longer be true as the background process is now dormant.
278+
279+
##### Active
280+
281+
To check whether a background process has queued items, is processing, is paused, or is cancelling, use `is_active()`.
282+
283+
```php
284+
if ( $this->example_process->is_active() ) {
285+
// Do something because background process is active, e.g. add notice in admin UI.
286+
}
287+
```
288+
289+
If a background process is not active, then it either has not had anything queued yet and not started, or has finished processing all queued items.
156290

157291
### BasicAuth
158292

classes/wp-async-request.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ protected function get_post_args() {
142142
'timeout' => 0.01,
143143
'blocking' => false,
144144
'body' => $this->data,
145-
'cookies' => $_COOKIE,
146-
'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
145+
'cookies' => $_COOKIE, // Passing cookies ensures request is performed as initiating user.
146+
'sslverify' => apply_filters( 'https_local_ssl_verify', false ), // Local requests, fine to pass false.
147147
);
148148

149149
/**
@@ -158,6 +158,8 @@ protected function get_post_args() {
158158
* Maybe handle a dispatched request.
159159
*
160160
* Check for correct nonce and pass to handler.
161+
*
162+
* @return void|mixed
161163
*/
162164
public function maybe_handle() {
163165
// Don't lock up other requests while processing.
@@ -167,7 +169,27 @@ public function maybe_handle() {
167169

168170
$this->handle();
169171

170-
wp_die();
172+
return $this->maybe_wp_die();
173+
}
174+
175+
/**
176+
* Should the process exit with wp_die?
177+
*
178+
* @param mixed $return What to return if filter says don't die, default is null.
179+
*
180+
* @return void|mixed
181+
*/
182+
protected function maybe_wp_die( $return = null ) {
183+
/**
184+
* Should wp_die be used?
185+
*
186+
* @return bool
187+
*/
188+
if ( apply_filters( $this->identifier . '_wp_die', true ) ) {
189+
wp_die();
190+
}
191+
192+
return $return;
171193
}
172194

173195
/**

0 commit comments

Comments
 (0)