Fluentcrm funnel action

From Lonsdale201/wp-agent-skills · View interactive version

Fluentcrm funnel action is an AI agent skill from the GitHub repository Lonsdale201/wp-agent-skills. Build a custom FluentCRM funnel action — a per-contact sequence step run when an automation reaches it — by extending BaseAction. Covers the three abstract methods (getBlock, getBlockFields, handle), the per-step handle($subscriber, $sequence, $funnelSubscriberId, $funnelMetric) signature, and the s

How to use this skill

Skill file

FluentCRM: register a custom funnel action

For developers building a companion plugin that needs to add a step inside FluentCRM's automation funnel — "Enroll user to LMS course", "Issue Woo coupon", "Send to webhook", "Update post status". Each action is a block the admin drags into the funnel sequence; when a contact reaches the block, the handler runs once per (contact, sequence) tuple. Extends FluentCrm\App\Services\Funnel\BaseAction. Verified end-to-end against FluentCRM 3.1.8 source.

API stability note

BaseAction, the fluentcrm_funnel_blocks / fluentcrm_funnel_block_fields filter pair, the per-action fluentcrm_funnel_sequence_handle_{name} action, and the (subscriber, sequence, funnelSubscriberId, funnelMetric) handler signature have been stable since 2.7. The implicit sequence-progress pattern (FunnelProcessor sets last_sequence_status = 'complete' before dispatching the handler) has been in place since 1.2 of the funnel processor.

Misconception this skill corrects

"I'll mark the sequence as 'completed' from my handler when the work succeeds."

Two bugs in one sentence.

Bug A: the canonical string depends on the column. FunnelProcessor::processSequence() writes FunnelSubscriber.last_sequence_status = 'complete' through FunnelHelper::changeFunnelSubSequenceStatus(). The full automation run status uses FunnelSubscriber.status = 'completed', and FunnelMetric.status defaults to 'completed' in the migration/model. Writing 'completed' to last_sequence_status is wrong; writing 'complete' to FunnelMetric.status is also wrong for the current reporting model.

Bug B: you don't need to mark anything as complete on success. FunnelProcessor::processSequence() calls FunnelHelper::changeFunnelSubSequenceStatus($funnelSubscriberId, $sequence->id, 'complete') before firing do_action('fluentcrm_funnel_sequence_handle_' . $sequence->action_name, ...). By the time your handler runs, the sequence is already marked 'complete'. Your job is to override only when you want a different outcome:

Other AI-prone misconceptions:

When to use this skill

Trigger when ANY of the following is true:

Step 1 — Register on the right hook (same rule as triggers)

<?php
namespace MyPlugin\Modules\Actions;

use HelloWP\MyPlugin\Support\Dependency;
use HelloWP\MyPlugin\Settings\SettingsRepository;

final class ActionManager
{
    public function __construct()
    {
        // Same lifecycle rule as triggers — instantiate before FluentCRM's
        // init:1/2 funnel item and listener passes. FluentCampaign Pro
        // registers its funnel items on init:1.
        add_action('fluentcrm_loaded', [$this, 'registerActions'], 5);
    }

    public function registerActions(): void
    {
        if (Dependency::isMyServiceActive() && SettingsRepository::isEnabled('actions', 'do_thing')) {
            new DoThingAction();
        }
    }
}

new DoThingAction() invokes the parent constructor, which calls register() (BaseAction.php:14-22) — the two filter additions and the per-action listener register here.

Step 2 — Extend BaseAction

<?php
namespace MyPlugin\Modules\Actions;

use FluentCrm\App\Services\Funnel\BaseAction;
use FluentCrm\App\Services\Funnel\FunnelHelper;
use FluentCrm\Framework\Support\Arr;

final class DoThingAction extends BaseAction
{
    public function __construct()
    {
        $this->actionName = 'my_plugin_do_thing';
        $this->priority   = 20;
        parent::__construct();
    }

    public function getBlock()
    {
        return [
            'category'    => __('My Service', 'my-plugin'),
            'title'       => __('Do The Thing', 'my-plugin'),
            'description' => __('Calls My Service for the contact.', 'my-plugin'),
            'icon'        => 'fc-icon-trigger',
            // CRITICAL — 'settings' is the seed for new block instances. The
            // editor's Vue components bind directly to settings.<field_key>;
            // omit this and dragging the block in throws
            // `TypeError: Cannot read properties of undefined (reading
            // '<first_field>')` in start.js, leaving the panel empty.
            // Keys MUST match getBlockFields()['fields'] keys exactly.
            'settings'    => [
                'thing_id'        => '',
                'send_welcome'    => 'yes',
                'skip_for_public' => 'no',
            ],
        ];
    }

    public function getBlockFields()
    {
        return [
            'title'     => __('Do The Thing', 'my-plugin'),
            'sub_title' => __('Calls My Service for the contact.', 'my-plugin'),
            'fields'    => [
                'thing_id' => [
                    'type'        => 'rest_selector',
                    'option_key'  => 'my_plugin_things',  // pairs with fluentcrm_ajax_options_my_plugin_things filter
                    'is_multiple' => false,
                    'clearable'   => true,
                    'label'       => __('Select Thing', 'my-plugin'),
                    'placeholder' => __('Select Thing', 'my-plugin'),
                ],
                'skip_for_public' => [
                    'type'        => 'yes_no_check',
                    'check_label' => __('Skip if contact has no WP user account.', 'my-plugin'),
                ],
                'send_welcome' => [
                    'type'        => 'yes_no_check',
                    'check_label' => __('Send default WP welcome email if a new user is created.', 'my-plugin'),
                    'dependency'  => [
                        'depends_on' => 'skip_for_public',
                        'operator'   => '=',
                        'value'      => 'no',
                    ],
                ],
            ],
        ];
    }

    public function handle($subscriber, $sequence, $funnelSubscriberId, $funnelMetric)
    {
        $settings = $sequence->settings;
        $thingId  = (int) Arr::get($settings, 'thing_id');
        $userId   = $subscriber->getWpUserId();

        // SKIP path #1 — config invalid.
        if ($thingId <= 0) {
            $funnelMetric->notes  = __('Skipped: no thing selected.', 'my-plugin');
            $funnelMetric->status = 'skipped';
            $funnelMetric->save();
            FunnelHelper::changeFunnelSubSequenceStatus($funnelSubscriberId, $sequence->id, 'skipped');
            return false;
        }

        // SKIP path #2 — guest contact + admin asked to skip guests.
        if (!$userId && Arr::get($settings, 'skip_for_public') === 'yes') {
            $funnelMetric->notes  = __('Skipped: contact is not a WP user.', 'my-plugin');
            $funnelMetric->status = 'skipped';
            $funnelMetric->save();
            FunnelHelper::changeFunnelSubSequenceStatus($funnelSubscriberId, $sequence->id, 'skipped');
            return false;
        }

        // Real work goes here.
        $result = my_service_do_thing_for_user($userId, $thingId);

        if (is_wp_error($result)) {
            $funnelMetric->notes  = $result->get_error_message();
            $funnelMetric->status = 'skipped';
            $funnelMetric->save();
            FunnelHelper::changeFunnelSubSequenceStatus($funnelSubscriberId, $sequence->id, 'skipped');
            return false;
        }

        // SUCCESS path — DO NOT call changeFunnelSubSequenceStatus(..., 'complete').
        // FunnelProcessor::processSequence already wrote 'complete'
        // before dispatching us. Just leave a useful note and return.
        $funnelMetric->notes = __('Thing done successfully.', 'my-plugin');
        $funnelMetric->save();

        return true;
    }
}

Step 3 — Status vocabulary

The two records that track action state and the strings each accepts:

| Record | Method to write | Canonical values |

|--|--|--|

| Sequence <-> Subscriber progress (FunnelSubscriber.last_sequence_status) | FunnelHelper::changeFunnelSubSequenceStatus($funnelSubscriberId, $sequenceId, $status) | 'pending', 'complete', 'skipped' |

| Funnel metric row (per-step audit log) | $funnelMetric->status = '...'; $funnelMetric->save(); | 'completed' success/default, 'skipped', 'failed' |

| Whole automation run (FunnelSubscriber.status) | Processor/controller only; do not set from normal action handlers | 'draft', 'pending', 'active', 'waiting', 'completed', 'cancelled', 'skipped' |

Notes:

Step 4 — How the registration plumbs through

Follow the chain from the editor click to your handler:

  1. Admin opens the funnel editor → editor REST request → server filters via fluentcrm_funnel_blocks (your pushBlock adds your block to the picker, BaseAction.php:24-35) and fluentcrm_funnel_block_fields (your pushBlockFields adds your editor form schema, BaseAction.php:37-43).
  2. Admin drags your block into the sequence + saves the funnel → the sequence row stores action_name = 'my_plugin_do_thing' plus the settings JSON.
  3. At runtime, when a contact reaches your sequence step, FunnelProcessor::processSequence() runs:
  1. Your handler does the work and overrides status only on skip/failure.

Critical rules

Common mistakes

Cross-references

What this skill does NOT cover

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.