Action postpone is very useful in some scenarios. It allows you to test some conditions in an earlier action and execute the Trigger in a later one.
Postponed Trigger
Letβs take a look at an example:
classCustomTriggerextendsBracketSpace\Notification\Abstracts\Trigger {/** * Constructor */publicfunction__construct() {parent::__construct('myplugin/custom_trigger',__('Custom Trigger title','textdomain'));// We are registering our ealier action with 2 arguments.$this->add_action('earlier_action',10,2); }/** * Assigns action callback args to object * Return `false` if you want to abort the trigger execution * * @returnmixed void or false if no notifications should be sent */publicfunctionaction( $param, $another_param ) {$this->this = $param;$this->that = $another_param;// Postpone this action to a later hook.$this->postpone_action('later_action',10,1); }}
As you can see we can set any properties in ealier action and execute the trigger later.
Postponed action callback
What if you need another callback argument, passed to the later action? You can use the second method:
/** * Postponed action callback * * Return `false` if you want to abort the trigger execution * * @returnmixed void or false if no notifications should be sent */publicfunctionpostponed_action( $later_action_param ) {// we have access to the new callback args here.$this->from_later_action = $later_action_param;// you can also abort in the later action by returning false.if ( $this->from_later_action ===false ) {returnfalse; }}
Troubleshooting
Postponed trigger is executed twice
Sometimes an action is done recursively. In this case you have to bail in the postponed_action callback if the action has been already done:
publicfunctionpostponed_action() {// Fix for the action being called recursively.if ( did_action('postponed_action_hook_name') ) {returnfalse; }}