skip to Main Content

Comparison between 2.x and 3.x Automator code

Custom Triggers & Actions
1. How to create your own integration with Automator
2. How to create your own trigger with Automator
3. Comparison between 2.x and 3.x Automator code
4. How to create a simple trigger with Automator
5. How to create a simple Automator action

There are a lot of changes that happened with the release of Automator 3.0 and along with it, the support to better integrate third-party integrations in Automator’s ecosystem.

A developer who has previously worked with Automator in creating a trigger will know how complex and untidy it was to navigate.

Here’s a review of 2.x trigger declaration code:

	public static $integration = 'UOA';

	/**
	 * @var string
	 */
	private $trigger_code;
	/**
	 * @var string
	 */
	private $trigger_meta;

	public function __construct() {
		$this->trigger_code = 'UOARECIPESNOTCOMPLETED';
		$this->trigger_meta = 'UOARECIPE';
		$this->define_trigger();
	}

	/**
	 * Define and register the trigger by pushing it into the Automator object
	 */
	public function define_trigger() {
        global $uncanny_automator;

		$trigger = array(
			'author'              => $uncanny_automator->get_author_name( $this->trigger_code ),
			'support_link'        => $uncanny_automator->get_author_support_link( $this->trigger_code ),
			'integration'         => self::$integration,
			'code'                => $this->trigger_code,
			/* translators: Logged-in trigger - Uncanny Automator */
			'sentence'            => sprintf( esc_attr__( '{{A recipe:%1$s}} is not completed', 'uncanny-automator' ), $this->trigger_meta ),
			/* translators: Logged-in trigger - Uncanny Automator */
			'select_option_name'  => esc_attr__( '{{A recipe}} is not completed', 'uncanny-automator' ),
			'action'              => 'automator_recipe_completed_with_errors',
			'priority'            => 99,
			'accepted_args'       => 4,
			'validation_function' => array( $this, 'on_completion' ),
			'options'             => [
				$uncanny_automator->helpers->recipe->uncanny_automator->options->get_recipes(),
			],
		);

		Automator()->register->trigger( $trigger );
	}

View this on Github

This same trigger declaration in 3.0+ looks like this:

    use Recipe\Triggers;

	public function __construct() {
		$this->setup_trigger();
	}

	protected function setup_trigger() {
		$this->set_integration( 'UOA' );
		$this->set_trigger_code( 'UOARECIPESNOTCOMPLETED' );
		$this->set_trigger_meta( 'UOARECIPE' );
		/* Translators: Some information for translators */
		$this->set_sentence( sprintf( esc_attr__( '{{A recipe:%1$s}} is not completed', 'uncanny-automator' ), $this->get_trigger_meta() ) );
		/* Translators: Some information for translators */
		$this->set_readable_sentence( esc_attr__( '{{A recipe}} is not completed', 'uncanny-automator' ) );

		$this->add_action( 'automator_recipe_completed_with_errors', 90, 4 );
        $this->set_options( array(
			Automator()->helpers->recipe->uncanny_automator->options->get_recipes(),
		) );
		$this->register_trigger();
	}

View this on Github

The first thing to notice here, that all of the array keys to register a trigger in < 3.0 are a callable function in 3.0+,i.e., integration became $this->set_integration(). We’ve made a lot of similar changes. An overview of the available functions are summarized in the image below:

Back To Top