开发者 |
Michael Uno
miunosoft |
---|---|
更新时间 | 2021年2月24日 14:25 |
捐献地址: | 去捐款 |
PHP版本: | 3.7 及以上 |
WordPress版本: | 5.6.2 |
版权: | GPLv2 or later |
版权网址: | 版权信息 |
task-scheduler.php
and other files compressed in the zip folder to the /wp-content/plugins/
directory.,Plugins
menu in WordPress.Task
via Dashboard -> Task Scheduler -> Add New Taskfunctions.php
of the activated theme.
1. Decide your action slug which also serves as a WordPress filter hook.
Say, you pick my_custom_action
as an action name.
2. Use the add_filter()
WordPress core function to hook into the action.
`
/*
* Called when the Task Scheduler plugin gets loaded.
/
function doMyCustomAction( $isExitCode, $oRoutine ) {
/
* Do you stuff here.
*/
TaskScheduler_Debug::log( $oRoutine->getMeta() );
return 1;}
/
* Set the 'my_custom_action' custom action slug in the Select Action screen
* via Dashboard -> Task Scheduler -> Add New Task.
/
add_filter( 'my_custom_action', 'doMyCustomAction', 10, 2 );
Please note that we use
add_filter()not
add_action()in order to return an exit code.
Return
1if the task completes and
0when there is a problem. You can pass any value except
null`.
3. Go to Dashboard -> Task Scheduler -> Add New Task. Proceed with the wizard and when you get the Select Action screen after setting up the occurrence, type my_custom_action*, the one you defined in the above step.
The action slug set in the field will be triggered at the scheduled time.
It will be easier for you to modify an existent code. You can download the zip file and install it on your site.
Create a Custom Action Module
If you want your action to be listed in the Select Action screen, you need to create an action module.
To create an action module, you need to define a class by extending a base class that Task Scheduler prepares for you.
1. Define your custom action module class by extending the TaskScheduler_Action_Base
class.
`
class TaskScheduler_SampleActionModule extends TaskScheduler_Action_Base {
/*
* The user constructor.
*
* This method is automatically called at the end of the class constructor.
/
public function construct() {}
/*
* Returns the readable label of this action.
*
* This will be called when displaying the action in an pull-down select option, task listing table, or notification email message.
/
public function getLabel( $sLabel ) {
return __( 'Sample Action Module', 'task-scheduler-sample-action-module' );
}
/*
* Returns the description of the module.
/
public function getDescription( $sDescription ) {
return __( 'This is a sample action module.', 'task-scheduler-sample-action-module' );
}
/*
* Defines the behaviour of the task action.
*
/
public function doAction( $isExitCode, $oRoutine ) {
/*
* Write your own code here! Delete the below log method.
*
* Good luck!
/
TaskScheduler_Debug::log( $oRoutine->getMeta() );
// Exit code.
return 1;
}
}
In the
doAction()method of the above class, define the behaviour of your action what it does. The second parameter receives a routine object. The object has a public method named
getMeta()which returns the associated arguments.
**2.** Use the
task_scheduler_action_after_loading_pluginaction hook to register your action module.
To register your action module, just instantiate the class you defined.
function loadTaskSchedulerSampleActionModule() {
// Register a custom action module.
include( dirname( FILE ) . '/module/TaskScheduler_SampleActionModule.php' );
new TaskScheduler_SampleActionModule;
}
add_action( 'task_scheduler_action_after_loading_plugin', 'loadTaskSchedulerSampleActionModule' );
**3.** Go to **Dashboard** -> **Task Scheduler** -> **Add New Task**. Proceed the wizard and when you get the **Select Action** screen, choose your action.
You can set your custom arguments in the **Argument (optional)** field if necessary.
The set values will be stored in the argument element of the array returned by the
getMeta()public method of the routine object.
It will be easier for you to modify an existent module. Get an example action module which comes as a plugin from [this page](https://github.com/michaeluno/task-scheduler-sample-action-module). Download and activate it on your test site. Then modify the code, especially the
doAction()` method which defines the behavior of the action.
Create Threads
When your routine is too heavy and gets hung often, you can create threads that performs sub-routines of the main routine.
1. Define your thread class the TaskScheduler_Action_Base
class.
`
class TaskScheduler_SampleActionModule_Thread extends TaskScheduler_Action_Base {
/*
* Returns the readable label of this action.
*
* This will be called when displaying the action in an pull-down select option, task listing table, or notification email message.
/
public function getLabel( $sLabel ) {
return __( 'Run a PHP Script', 'task-scheduler' );
}
/*
* Defines the behavior of the task action.
/
public function doAction( $isExitCode, $oThread ) {
// Do your stuff
$_aThreadArguments = $oThread->getMeta();
TaskScheduler_Debug::log( $_aThreadArguments );
return 1;
}
}
**2.** Instantiate the thread class.
In the
construct()method of your action module class introduced above that calls threads, instantiate the thread class by passing a custom action name. Here we pass
task_scheduler_my_threadas an example.
class TaskScheduler_SampleActionModule extends TaskScheduler_Action_Base {
public function construct() {
new TaskScheduler_SampleActionModule_Thread( 'task_scheduler_my_thread' );
}
...
}
**3.** Create a thread.
In the
doAction()method of your action module class, create a thread with the
createThread()method. The parameters are:
createThread( $sThreadActionHookName, $oRoutine, array $aThreadOptions, array $aSystemTaxonomyTerms=array(), $bAllowDuplicate )1.
$sThreadActionHookName- (string, required) the slug that serves as an action hook name
2.
$oRoutine- (object, required) the routine object that is passed to the second parameter of
doAction()method. 3. `$aThreadOptions`- (array, required) an associative array holding arguments to pass to the thread. 4.`$aSystemTaxonomyTerms`- (array, optional) an array holding taxonomy terms for the system the plugin provides. Default:`array()
.
5. $bAllowDuplicate
- (boolean, optional) whether to allow threads to be created with same arguments. Default: false
.
Make sure the return value is null
so that the routine will not close. Here we assume the $_aData
variable holds lots of items so it must be processed separately by threads.
`
class TaskScheduler_SampleActionModule extends TaskScheduler_Action_Base {
...
public function doAction( $isExitCode, $oRoutine ) {
// Assuming this is big.
$_aData = array(
array( 'a', 'b', 'c' ),
array( 'd', 'e', 'f', 'g' ),
array( 'h', 'i' ),
);
foreach( $_aData as $_aDatum ) {
$_aArguments = array(
'datum' => $_aDatum,
'foo' => 'bar',
);
$this->createThread( 'task_scheduler_my_thread', $oRoutine, $_aArguments );
}
// Do not close this routine by returning 'null'. When all the threads are done, this routine will be automatically closed.
return null;
}
...
}
**4.** Process Passed Data from a Routine to a Thread.
In the thread class, retrieve the passed data.
class TaskScheduler_SampleActionModule_Thread extends TaskScheduler_Action_Base {
...
/*
* Defines the behavior of the task action.
/
public function doAction( $isExitCode, $oThread ) {
// Do your stuff
$_aArguments = $oThread->getMeta();
$_sFoo = $_aArguments[ 'foo' ]; // is 'bar'
$_aDatum = $_aArguments[ 'datum' ]; // is either array( 'a', 'b', 'c' ), array( 'd', 'e', 'f', 'g' ), or array( 'h', 'i' )
TaskScheduler_Debug::log( $_aArguments );
return 1;
}
}
The entire code will look like this.
Action Module Class:
class TaskScheduler_SampleActionModule extends TaskScheduler_Action_Base {
/*
* The user constructor.
*
* This method is automatically called at the end of the class constructor.
/
public function construct() {
new TaskScheduler_SampleActionModule_Thread( 'task_scheduler_my_thread' );
}
/*
* Returns the readable label of this action.
*
* This will be called when displaying the action in an pull-down select option, task listing table, or notification email message.
/
public function getLabel( $sLabel ) {
return __( 'Sample Action Module', 'task-scheduler-sample-action-module' );
}
/*
* Returns the description of the module.
/
public function getDescription( $sDescription ) {
return __( 'This is a sample action module.', 'task-scheduler-sample-action-module' );
}
public function doAction( $isExitCode, $oRoutine ) {
// Assuming this is big.
$_aData = array(
array( 'a', 'b', 'c' ),
array( 'd', 'e', 'f', 'g' ),
array( 'h', 'i' ),
);
foreach( $_aData as $_aDatum ) {
$_aArguments = array(
'datum' => $_aDatum,
'foo' => 'bar',
);
$this->createThread( 'task_scheduler_my_thread', $oRoutine, $_aArguments );
}
// Do not close this routine by returning 'null'. When all the threads are done, this routine will be automatically closed.
return null;
}
}
Thread Class:
class TaskScheduler_SampleActionModule_Thread extends TaskScheduler_Action_Base {
/*
* Returns the readable label of this action.
*
* This will be called when displaying the action in an pull-down select option, task listing table, or notification email message.
/
public function getLabel( $sLabel ) {
return __( 'Run a PHP Script', 'task-scheduler' );
}
/*
* Defines the behavior of the task action.
/
public function doAction( $isExitCode, $oThread ) {
// Do your stuff
$_aArguments = $oThread->getMeta();
$_sFoo = $_aArguments[ 'foo' ]; // is `bar'
$_aDatum = $_aArguments[ 'datum' ]; // is either array( 'a', 'b', 'c' ), array( 'd', 'e', 'f', 'g' ), or array( 'h', 'i' )
TaskScheduler_Debug::log( $_aArguments );
return 1;
}
}
Don't forget to instantiate the action module class.
new TaskScheduler_SampleActionModule;`
Terminologies
Exit Code
occurrence type.Negate
option for the Exit Code
occurrence type allows the user choose whether the action gets triggered when the routine returns none of the set exit codes.Run
action link from normally triggering an action from forcing it.Specific Time
occurrence type that the d/m/Y
date format caused time miscalculations.### 1.4.7- 2018/10/17Daily
occurrence type that spawned routines multiple times on some servers.### 1.4.5 - 2017/06/11Daily
occurrence type that did not set the correct time for cases of 7 days ahead.### 1.4.4 - 2017/03/11Elapsed Time
option for the Delete Posts
action module.Notice: Undefined property: stdClass::$delete_posts class-wp-posts-list-table.php on line 403
in the Log
page.Delete Posts
action module.Declaration of TaskScheduler_Utility::uniteArrays() should be compatible with...
.### 1.4.0 - 2016/09/21Run PHP Script
action module.Cannot redeclare class TaskScheduler_Routine_Base
in WordPress 4.6.1.### 1.3.3 - 2016/09/01Send Email
action module.Daily
occurrence type.### 1.1.0 - 2015/08/02Delete Posts
action module that the taxonomy and post status options did not take effect.daily
occurrence type.autocomplete
admin page framework custom field to search users that can be used by modules.### 1.0.0b10 - 2014/08/18Ready
from Inactive
.Check Action Now
button in the task listing table page.Number of Posts to Process per Routine
option to the Delete Posts
action module.Delete Posts
action module to load threads smoothly for sites disabling the server heartbeat.Update
submit button, some time indications, and the switch option of Enabled
or Disabled
.Delete Posts
action module not to insert a taxonomy query argument when the taxonomy slug is not selected.Change
button did not appear when the Debug
action is selected.