Postponing an action

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.

Let’s take a look at an example:

class CustomTrigger extends underDEV\Notification\Abstracts\Trigger {

	/**
	 * Constructor
	 */
	public function __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
	 *
	 * @return mixed void or false if no notifications should be sent
	 */
	public function action() {

		$this->this = $this->callback_args[0];
		$this->that = $this->callback_args[1];

		// 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
 *
 * @return mixed void or false if no notifications should be sent
 */
public function postponed_action() {

	// we have access to the new callback args here.
	$this->from_later_action = $this->callback_args[0];

	// you can also abort in the later action by returning false.
	if ( $this->from_later_action === false ) {
		return false;
	}

}

 

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:

public function postponed_action() {

	// fix for the action being called recursively.
	if ( did_action( 'postponed_action_hook_name' ) ) {
		return false;
	}

}