Creating a New Start Trigger
Triggers are the starting point of any automation. They represent the equivalent of a WordPress do_action which means that:
- they let others know when something happens;
- they provide the data to be used ;
Based on the data objects a trigger provides, filters can be applied and actions can be executed.
Make sure you have Thrive Automator to the latest version and running at least PHP 7.
The first step for creating a new trigger is to create a new class that will extend the abstract class Thrive\Automator\Items\Trigger. After that, either your IDE will autocomplete the methods you will have to implement or you can check the following list:
- abstract public static function get_id(): string - should return a unique identifier that will be used as a key in arrays. To avoid conflicts or overwrites we suggest using a prefix.
- public static function get_wp_hook(): string - should return a string containing an action that at one point will be triggered with do_action. For more information about how WordPress actions work you can have a look here.
- public static function get_hook_params_number(): int - number of parameters the action provides. This function is used when preparing an automation with the WordPress add_action function.
- public static function get_provided_data_objects(): array - an array of Data_Object keys that this trigger provides for Action. Only Actions that require those will be displayed to interact with the trigger. The keys from this array must match the id from Data_Object. The keys also must be in the same order as provided in the do_action hook, so we can match them automatically.
- public static function get_app_id(): string - the id of the app that will group all the triggers that you provide, in the admin UI.
- public static function get_name(): string - the name of the Trigger that will be displayed in the admin dropdown select.
- public static function get_description(): string - a short description of the trigger. This will be displayed with a tooltip in the UI.
- public static function get_image(): string - an URL for the image that will be displayed in the dropdown select near the trigger name.
- public function process_params( $params =  ) - when a WordPress action hook is triggered, it will provide its own parameters as seen here.
This method works as a filter because we receive the raw data and return an array of Data_Objects. By default, we read the array of Data_Object keys the trigger provides and we create an instance for each one we find.
- public function prepare_data( $data =  ) - when a WordPress action hook is triggered, the data stored while setting up the automation in the editor can be filtered/structured for easy access during the execution.
- public function is_top_level() - inside the editor, the triggers are grouped under a category which usually represents the application for which it was developed.
If we require the Trigger to be outside of a category and be a standalone item, this can be set to true.
- public function get_automation_wp_hook() - when setting up listener(s), if the Trigger supports individual Wordpress hooks for each individual automation, this function will retrieve the hook with a dynamic identifier.
- public function get_search_keywords() - inside the editor, in order for the Trigger to be more easily found, extra search tags can be added.
- public function sync_trigger_data() - inside the editor, while setting up the automation, certain Trigger_Field may alter the provided data objects and some more actions may be enabled.
In order to register this trigger so it can appear in the admin area, we should use the thrive_automator_register_trigger function which receives as the only parameter, the class name.