This repository has been archived by the owner on Mar 20, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 47a9534
Showing
5 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
class WP_Example_Request extends WP_Async_Request { | ||
|
||
use WP_Example_Logger; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $action = 'example_request'; | ||
|
||
/** | ||
* Handle | ||
* | ||
* Override this method to perform any actions required | ||
* during the async request. | ||
*/ | ||
protected function handle() { | ||
$message = $this->get_message( $_POST['name'] ); | ||
|
||
$this->really_long_running_task(); | ||
$this->log( $message ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
class WP_Example_Process extends WP_Background_Process { | ||
|
||
use WP_Example_Logger; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $action = 'example_process'; | ||
|
||
/** | ||
* Task | ||
* | ||
* Override this method to perform any actions required on each | ||
* queue item. Return the modified item for further processing | ||
* in the next pass through. Or, return false to remove the | ||
* item from the queue. | ||
* | ||
* @param mixed $item Queue item to iterate over | ||
* | ||
* @return mixed | ||
*/ | ||
protected function task( $item ) { | ||
$message = $this->get_message( $item ); | ||
|
||
$this->really_long_running_task(); | ||
$this->log( $message ); | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Complete | ||
* | ||
* Override if applicable, but ensure that the below actions are | ||
* performed, or, call parent::complete(). | ||
*/ | ||
protected function complete() { | ||
parent::complete(); | ||
|
||
// Show notice to user or perform some other arbitrary task... | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
trait WP_Example_Logger { | ||
|
||
/** | ||
* Really long running process | ||
* | ||
* @return int | ||
*/ | ||
public function really_long_running_task() { | ||
return sleep( 5 ); | ||
} | ||
|
||
/** | ||
* Log | ||
* | ||
* @param string $message | ||
*/ | ||
public function log( $message ) { | ||
error_log( $message ); | ||
} | ||
|
||
/** | ||
* Get lorem | ||
* | ||
* @param string $name | ||
* | ||
* @return string | ||
*/ | ||
protected function get_message( $name ) { | ||
$response = wp_remote_get( esc_url_raw( 'http://loripsum.net/api/1/short/plaintext' ) ); | ||
$body = trim( wp_remote_retrieve_body( $response ) ); | ||
|
||
if ( empty( $body ) ) { | ||
$body = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quippe: habes enim a rhetoribus; Eaedem res maneant alio modo.'; | ||
} | ||
|
||
return $name . ': ' . $body; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?php | ||
/* | ||
Plugin Name: Example Background Processing | ||
Plugin URI: https://github.com/A5hleyRich/wp-background-processing | ||
Description: Background processing in WordPress. | ||
Author: Ashley Rich | ||
Version: 0.1 | ||
Author URI: https://deliciousbrains.com/ | ||
Text Domain: example-plugin | ||
Domain Path: /languages/ | ||
*/ | ||
|
||
class Example_Background_Processing { | ||
|
||
/** | ||
* @var WP_Example_Request | ||
*/ | ||
protected $process_single; | ||
|
||
/** | ||
* @var WP_Example_Process | ||
*/ | ||
protected $process_all; | ||
|
||
/** | ||
* Example_Background_Processing constructor. | ||
*/ | ||
public function __construct() { | ||
add_action( 'plugins_loaded', array( $this, 'init' ) ); | ||
add_action( 'admin_bar_menu', array( $this, 'admin_bar' ), 100 ); | ||
add_action( 'init', array( $this, 'process_handler' ) ); | ||
} | ||
|
||
/** | ||
* Init | ||
*/ | ||
public function init() { | ||
require_once plugin_dir_path( __FILE__ ) . 'class-logger.php'; | ||
require_once plugin_dir_path( __FILE__ ) . 'async-requests/class-example-request.php'; | ||
require_once plugin_dir_path( __FILE__ ) . 'background-processes/class-example-process.php'; | ||
|
||
$this->process_single = new WP_Example_Request(); | ||
$this->process_all = new WP_Example_Process(); | ||
} | ||
|
||
/** | ||
* Admin bar | ||
* | ||
* @param WP_Admin_Bar $wp_admin_bar | ||
*/ | ||
public function admin_bar( $wp_admin_bar ) { | ||
if ( ! current_user_can( 'manage_options' ) ) { | ||
return; | ||
} | ||
|
||
$wp_admin_bar->add_menu( array( | ||
'id' => 'example-plugin', | ||
'title' => __( 'Process', 'example-plugin' ), | ||
'href' => '#', | ||
) ); | ||
|
||
$wp_admin_bar->add_menu( array( | ||
'parent' => 'example-plugin', | ||
'id' => 'example-plugin-single', | ||
'title' => __( 'Single User', 'example-plugin' ), | ||
'href' => wp_nonce_url( admin_url( '?process=single'), 'process' ), | ||
) ); | ||
|
||
$wp_admin_bar->add_menu( array( | ||
'parent' => 'example-plugin', | ||
'id' => 'example-plugin-all', | ||
'title' => __( 'All Users', 'example-plugin' ), | ||
'href' => wp_nonce_url( admin_url( '?process=all'), 'process' ), | ||
) ); | ||
} | ||
|
||
/** | ||
* Process handler | ||
*/ | ||
public function process_handler() { | ||
if ( ! isset( $_GET['process'] ) || ! isset( $_GET['_wpnonce'] ) ) { | ||
return; | ||
} | ||
|
||
if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'process') ) { | ||
return; | ||
} | ||
|
||
if ( 'single' === $_GET['process'] ) { | ||
$this->handle_single(); | ||
} | ||
|
||
if ( 'all' === $_GET['process'] ) { | ||
$this->handle_all(); | ||
} | ||
} | ||
|
||
/** | ||
* Handle single | ||
*/ | ||
protected function handle_single() { | ||
$names = $this->get_names(); | ||
$rand = array_rand( $names, 1 ); | ||
$name = $names[ $rand ]; | ||
|
||
$this->process_single->data( array( 'name' => $name ) )->dispatch(); | ||
} | ||
|
||
/** | ||
* Handle all | ||
*/ | ||
protected function handle_all() { | ||
$names = $this->get_names(); | ||
|
||
foreach ( $names as $name ) { | ||
$this->process_all->push_to_queue( $name ); | ||
} | ||
|
||
$this->process_all->save()->dispatch(); | ||
} | ||
|
||
/** | ||
* Get names | ||
* | ||
* @return array | ||
*/ | ||
protected function get_names() { | ||
return array( | ||
'Grant Buel', | ||
'Bryon Pennywell', | ||
'Jarred Mccuiston', | ||
'Reynaldo Azcona', | ||
'Jarrett Pelc', | ||
'Blake Terrill', | ||
'Romeo Tiernan', | ||
'Marion Buckle', | ||
'Theodore Barley', | ||
'Carmine Hopple', | ||
'Suzi Rodrique', | ||
'Fran Velez', | ||
'Sherly Bolten', | ||
'Rossana Ohalloran', | ||
'Sonya Water', | ||
'Marget Bejarano', | ||
'Leslee Mans', | ||
'Fernanda Eldred', | ||
'Terina Calvo', | ||
'Dawn Partridge', | ||
); | ||
} | ||
|
||
} | ||
|
||
new Example_Background_Processing(); |