Custom Automation Trigger

Automation is magic and FluentCRM is the magician. But anyone can be part of this. Following some steps, anyone can be part of this magic. In this article, we will show you, how you are able to add a custom trigger in automation.

Trigger

 A funnel trigger or automation trigger will let you start an automation funnel based on your user’s behavior. Triggers are essential for initiating email marketing automation. There are a lot of triggers that can start or initiate automation in FluentCRM such as Primary Automation TriggersEcommerce TriggersMembership TriggersLMS Triggers, and many more. Using trigger you can track various activities in your WordPress ecosystem. Isn’t cool. Let’s dive into deep to learn, how to create a custom trigger in FluentCRM.

To make an automation trigger, we need to use two filters and one action hook.

TypeHookDescription
Filterfluentcrm_funnel_triggersThis is a filter hook and this hook will add your custom trigger to the automation trigger list.
Filterfluentcrm_funnel_editor_details_{trigger_name}This is a filter hook and this hook will generate your custom trigger setting block. For the following trigger source code, the {trigger_name} is my_plugin_registration.
Actionfluentcrm_funnel_start_{trigger_name}This is an action hook and this action will be called automatically depending on the trigger setting. You can do further using this action hook.

To add a custom trigger in automation, I am going to tell you to step by step. This example will be using composer, you can do without composer too.

Step 1

First of all, I created a class named CustomTrigger which will contain the full source code of this automation trigger.

<?php

namespace Custom\Triggers;

class CustomTrigger
{
    public function __construct()
    {
        $this->triggerName = 'my_plugin_registration';
        $this->priority = 20;
        $this->actionArgNum = 2;
        add_filter('fluentcrm_funnel_triggers', array($this, 'addTrigger'), $this->priority, 1);
        add_filter('fluentcrm_funnel_editor_details_'.$this->triggerName, array($this, 'prepareEditorDetails'), 10, 1);
        add_action('fluentcrm_funnel_start_' . $this->triggerName, array($this, 'handle'), 10, 2);
    }

    public function getTrigger()
    {
        return [
            'category'    => __('My-plugin'),
            'label'       => __('My-plugin'),
            'description' => __('This Funnel will be initiated when a My-plugin registration completed'),
            'icon'        => 'fc-icon-wp_new_user_signup',
        ];
    }

    public function addTrigger($triggers)
    {
        $triggers[$this->triggerName] = $this->getTrigger();
        return $triggers;
    }

    public function getSettingsFields($funnel)
    {
        return [
            'title'     => __('My-plugin Registration'),
            'sub_title' => __('This Funnel will be initiated when a My-plugin registration completed'),
            'fields'    => [
                'message' => [
                    'type'        => 'input-text',
                    'label'       => __('Message Title'),
                    'placeholder' => __('Type Message Title')
                ],
                'description' => [
                    'type' => 'html',
                    'label' => 'Description',
                    'info' => '<b>'.__('This message will add to user but need to set title').'</b>'
                ]
            ]
        ];
    }

    public function prepareEditorDetails($funnel)
    {
        $funnel->settings = wp_parse_args($funnel->settings, $this->getFunnelSettingsDefaults());
        $funnel->settingsFields = $this->getSettingsFields($funnel);
        $funnel->conditions = wp_parse_args($funnel->conditions, $this->getFunnelConditionDefaults($funnel));
        $funnel->conditionFields = $this->getConditionFields($funnel);
        return $funnel;
    }

    public function getFunnelSettingsDefaults()
    {
        return [
            'subscription_status' => 'subscribed'
        ];
    }

    public function getConditionFields($funnel)
    {
        return [
            'update_type'  => [
                'type'    => 'radio',
                'label'   => __('If Contact Already Exist?', 'fluent-crm'),
                'help'    => __('Please specify what will happen if the subscriber already exist in the database', 'fluent-crm'),
                'options' => $this->getUpdateOptions()
            ],
            'user_roles'   => [
                'type'        => 'multi-select',
                'is_multiple' => true,
                'label'       => __('Targeted User Roles', 'fluent-crm'),
                'help'        => __('Select which roles registration will run this automation Funnel', 'fluent-crm'),
                'placeholder' => __('Select Roles', 'fluent-crm'),
                'options'     => $this->getUserRoles(),
                'inline_help' => __('Leave blank to run for all user roles', 'fluent-crm')
            ],
            'run_multiple'       => [
                'type'        => 'yes_no_check',
                'label'       => '',
                'check_label' => __('Restart the Automation Multiple times for a contact for this event. (Only enable if you want to restart automation for the same contact)'),
                'inline_help' => __('If you enable, then it will restart the automation for a contact if the contact already in the automation. Otherwise, It will just skip if already exist')
            ]
        ];
    }

    public function getFunnelConditionDefaults($funnel)
    {
        return [
            'update_type'  => 'update', // skip_all_actions, skip_update_if_exist
            'user_roles'   => $this->getUserRoles(),
            'run_multiple'       => 'yes'
        ];
    }

    public function getUpdateOptions()
    {
        return [
            [
                'id'    => 'update',
                'title' => __('Update if Exist', 'fluent-crm')
            ],
            [
                'id'    => 'skip_all_if_exist',
                'title' => __('Skip this automation if contact already exist', 'fluent-crm')
            ]
        ];
    }

    public function getUserRoles($keyed = false)
    {
        if (!function_exists('get_editable_roles')) {
            require_once(ABSPATH . '/wp-admin/includes/user.php');
        }

        $roles = \get_editable_roles();
        $formattedRoles = [];
        foreach ($roles as $roleKey => $role) {

            if ($keyed) {
                $formattedRoles[$roleKey] = $role['name'];
            } else {
                $formattedRoles[] = [
                    'id'    => $roleKey,
                    'title' => $role['name']
                ];
            }

        }
        return $formattedRoles;
    }

    public function handle($funnel, $originalArgs)
    {
        error_log(print_r([$funnel, $originalArgs], 1));
    }
}

In the above example, you see, there is a method called prepareEditorDetails. This method returns an object and the object contains several fields. This object is the structure of this trigger’s setting page design. Let’s see the preview of this trigger’s setting block and discuss it.

screenshot 2022 09 07 at 9.31.23 am

In the above source code, the Target Products block is generated by getSettingsFields. You see there is a fields property in this method return array. There is so many field type in FluentCRM, you can find those here.

In the above example, you see, there is a method called handle. This method is called when this trigger is triggered. With this method you can do, what you want.

Step 2

Using the following code, You can able to add this custom trigger code in FluentCRM.

add_action('plugins_loaded', function () {
   if (defined('FLUENTCAMPAIGN_DIR_FILE')) {
      new \Custom\Triggers\CustomTrigger();
   }
});

Leave a Reply

Your email address will not be published.