Wallogit.com
2017 © Pedro Peláez
Run asynchronous tasks for long-running operations in WordPress
Create one time asynchronous tasks for WordPress, (*1)
Forked from The WP Asynchronous Tasks plugin for TechCrunch.com - https://github.com/techcrunch/wp-async-task, (*2)
Make sure to install as a composer dependnecy in plugin/site/app/whatever and include your autoloader., (*3)
Read my article about using original version, (*4)
Basically:, (*5)
shelob9\async\task
$action
prepare_data()
prepare_data
callback to call with async taskshelob9\async\system
helob9\async\task class as first parameterdo_action. Should have priority in first index and number of callback params in second argument. Default is [20,1].Consider:, (*6)
shelob9\async\task::run_action()
use shelob9\async\system;
$task = new example();
new system( $task, false, [ 20, 2 ] );
class example extends shelob9\async\task {
/**
* Name of action
*
* @var string
*/
protected $action = 'save_post';
/**
* Prepare async task -- runs when the original hook is fired
*
* @param array $data Dta passed to save_post hook
*
* @return array
*/
protected function prepare_data( $data ) {
$post_id = $data[0];
return array( 'post_id' => $post_id );
}
/**
* Runs the async hook in next session
*/
protected function run_action() {
$post_id = $_POST['post_id'];
$post = get_post( $post_id );
do_action( "wp_async_$this->action", $post->ID, $post );
}
/**
* Process async task
*
* @param $id
* @param $post
*/
public function callback( $id, $post ){
//do something here
}
}
use shelob9\async\system;
$task = new example();
new system( $task, 'hi_roy', [ 20, 2 ] );
class example extends shelob9\async\task {
/**
* Name of action
*
* @var string
*/
protected $action = 'save_post';
/**
* Prepare async task -- runs when the original hook is fired
*
* @param array $data Dta passed to save_post hook
*
* @return array
*/
protected function prepare_data( $data ) {
$post_id = $data[0];
return array( 'post_id' => $post_id );
}
/**
* Runs the async hook in next session
*/
protected function run_action() {
$post_id = $_POST['post_id'];
$post = get_post( $post_id );
do_action( "wp_async_$this->action", $post->ID, $post );
}
}
function hi_roy( $id, $post ){
//do something here
}
Copyright Josh Pollock 2016 -- Based heavily on TechCrunch WP Asynchronous Tasks plugin for TechCrunch.com - https://github.com/techcrunch/wp-async-task - copyright 2014 TechCrunch. Much thanks, very open source, (*7)
This library is licensed under the MIT license. See LICENSE.md for more details., (*8)