Initial commit
This commit is contained in:
+271
@@ -0,0 +1,271 @@
|
||||
<?php
|
||||
|
||||
namespace FluentCrm\App\Services\Funnel\Triggers;
|
||||
|
||||
use FluentCrm\App\Services\Funnel\BaseTrigger;
|
||||
use FluentCrm\App\Services\Funnel\FunnelHelper;
|
||||
use FluentCrm\App\Services\Funnel\FunnelProcessor;
|
||||
use FluentCrm\Framework\Support\Arr;
|
||||
use FluentForm\App\Modules\Form\FormFieldsParser;
|
||||
use FluentForm\App\Services\FormBuilder\ShortCodeParser;
|
||||
|
||||
class FluentFormSubmissionTrigger extends BaseTrigger
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
if (!defined('FLUENTFORM')) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
$this->actionArgNum = 3;
|
||||
$this->triggerName = 'fluentform_submission_inserted';
|
||||
$this->priority = 25;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function getTrigger()
|
||||
{
|
||||
return [
|
||||
'category' => __('Fluent Forms', 'fluent-crm'),
|
||||
'label' => __('New Form Submission (Fluent Forms)', 'fluent-crm'),
|
||||
'description' => __('This Funnel will be initiated when a new form submission has been submitted', 'fluent-crm'),
|
||||
'icon' => 'fc-icon-fluentforms',
|
||||
];
|
||||
}
|
||||
|
||||
public function getFunnelSettingsDefaults()
|
||||
{
|
||||
return [
|
||||
'form_id' => '',
|
||||
'primary_fields' => [
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'email' => ''
|
||||
],
|
||||
'other_fields' => [
|
||||
[
|
||||
'field_key' => '',
|
||||
'field_value' => ''
|
||||
]
|
||||
],
|
||||
'subscription_status' => 'subscribed'
|
||||
];
|
||||
}
|
||||
|
||||
public function getSettingsFields($funnel)
|
||||
{
|
||||
$valueOptions = $this->getValueOptions($funnel);
|
||||
|
||||
$formId = Arr::get($funnel->settings, 'form_id');
|
||||
|
||||
$subtitle = __('This Funnel will be initiated when a new form submission has been submitted.', 'fluent-crm');
|
||||
|
||||
if ($formId) {
|
||||
$subtitle .= ' Use shortcode <b> [fluentform id="' . $formId . '"] </b> to show the form in your WordPress page/posts. <a target="_blank" href="' . admin_url('admin.php?page=fluent_forms&route=editor&form_id=' . $formId) . '">Edit The Form</a>';
|
||||
}
|
||||
|
||||
$secondaryFields = apply_filters('fluent_crm/fluentform_other_map_fields',
|
||||
FunnelHelper::getSecondaryContactFieldMaps()
|
||||
);
|
||||
|
||||
return [
|
||||
'title' => __('New Fluent Forms Submission Funnel', 'fluent-crm'),
|
||||
'sub_title' => $subtitle,
|
||||
'fields' => [
|
||||
'form_id' => [
|
||||
'type' => 'reload_field_selection',
|
||||
'label' => __('Select your form', 'fluent-crm'),
|
||||
'options' => $this->getForms($funnel)
|
||||
],
|
||||
'primary_fields' => [
|
||||
'label' => __('Map Primary Data', 'fluent-crm'),
|
||||
'type' => 'form-group-mapper',
|
||||
'value_options' => $valueOptions,
|
||||
'local_label' => __('Contact Field (CRM)', 'fluent-crm'),
|
||||
'remote_label' => __('Form Field', 'fluent-crm'),
|
||||
'fields' => FunnelHelper::getPrimaryContactFieldMaps()
|
||||
],
|
||||
'other_fields' => [
|
||||
'label' => __('Map Other Data', 'fluent-crm'),
|
||||
'type' => 'form-many-drop-down-mapper',
|
||||
'value_options' => $valueOptions,
|
||||
'local_label' => __('Select Contact Property', 'fluent-crm'),
|
||||
'remote_label' => __('Select Form Field', 'fluent-crm'),
|
||||
'local_placeholder' => __('Select Contact Property', 'fluent-crm'),
|
||||
'remote_placeholder' => __('Select Form Property', 'fluent-crm'),
|
||||
'fields' => $secondaryFields
|
||||
],
|
||||
'subscription_status' => [
|
||||
'type' => 'select',
|
||||
'is_multiple' => false,
|
||||
'options' => [
|
||||
[
|
||||
'id' => 'subscribed',
|
||||
'title' => 'Subscribed'
|
||||
],
|
||||
[
|
||||
'id' => 'pending',
|
||||
'title' => 'Pending'
|
||||
]
|
||||
],
|
||||
'label' => __('Subscription Status', 'fluent-crm'),
|
||||
'placeholder' => __('Select Status', 'fluent-crm')
|
||||
],
|
||||
'subscription_status_info' => [
|
||||
'type' => 'html',
|
||||
'info' => '<b>An Automated double-optin email will be sent if the contact is new or not subscribed already</b>',
|
||||
'dependency' => [
|
||||
'depends_on' => 'subscription_status',
|
||||
'operator' => '=',
|
||||
'value' => 'pending'
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
protected function getForms($funnel)
|
||||
{
|
||||
return fluentCrmDb()->table('fluentform_forms')
|
||||
->select('id', 'title')
|
||||
->orderBy('id', 'DESC')
|
||||
->get();
|
||||
}
|
||||
|
||||
protected function getValueOptions($funnel)
|
||||
{
|
||||
$formId = Arr::get($funnel->settings, 'form_id');
|
||||
if (!$formId) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$form = fluentCrmDb()->table('fluentform_forms')
|
||||
->find($formId);
|
||||
|
||||
if(!$form) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$formFields = FormFieldsParser::getShortCodeInputs(
|
||||
$form, [
|
||||
'admin_label'
|
||||
]);
|
||||
|
||||
$formattedInputs = [];
|
||||
|
||||
foreach ($formFields as $inputKey => $input) {
|
||||
$formattedInputs[] = [
|
||||
'id' => '{inputs.' . $inputKey . '}',
|
||||
'title' => $input['admin_label']
|
||||
];
|
||||
}
|
||||
|
||||
return $formattedInputs;
|
||||
}
|
||||
|
||||
public function getFunnelConditionDefaults($funnel)
|
||||
{
|
||||
return [
|
||||
'run_only_one' => 'yes'
|
||||
];
|
||||
}
|
||||
|
||||
public function getConditionFields($funnel)
|
||||
{
|
||||
return [
|
||||
'run_only_one' => [
|
||||
'type' => 'yes_no_check',
|
||||
'label' => '',
|
||||
'check_label' => __('Run this automation only once per contact. If unchecked then it will over-write existing flow', 'fluent-crm'),
|
||||
'help' => __('If you enable this then this will run only once per customer otherwise, It will delete the existing automation flow and start new', 'fluent-crm'),
|
||||
'options' => FunnelHelper::getUpdateOptions()
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function handle($funnel, $originalArgs)
|
||||
{
|
||||
$insertId = $originalArgs[0];
|
||||
$formData = $originalArgs[1];
|
||||
$form = $originalArgs[2];
|
||||
|
||||
$processedValues = $funnel->settings;
|
||||
|
||||
if (Arr::get($processedValues, 'form_id') != $form->id) {
|
||||
return; // not our form
|
||||
}
|
||||
|
||||
$processedValues['primary_fields']['ip'] = '{submission.ip}';
|
||||
|
||||
$processedValues = ShortCodeParser::parse($processedValues, $insertId, $formData);
|
||||
|
||||
if (!is_email(Arr::get($processedValues, 'primary_fields.email'))) {
|
||||
return;
|
||||
}
|
||||
|
||||
$subscriberData = Arr::get($processedValues, 'primary_fields', []);
|
||||
|
||||
$subscriberData['custom_values'] = [];
|
||||
|
||||
foreach (Arr::get($processedValues, 'other_fields', []) as $otherField) {
|
||||
if (!empty($otherField['field_key']) && !empty($otherField['field_value'])) {
|
||||
$key = $otherField['field_key'];
|
||||
if (strpos($key, '.') !== false) {
|
||||
$subscriberData['custom_values'][str_replace('custom.', '', $key)] = $otherField['field_value'];
|
||||
} else {
|
||||
$subscriberData[$key] = $otherField['field_value'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$willProcess = $this->isProcessable($funnel, $subscriberData);
|
||||
|
||||
$willProcess = apply_filters('fluentcrm_funnel_will_process_' . $this->triggerName, $willProcess, $funnel, $subscriberData, $originalArgs);
|
||||
if (!$willProcess) {
|
||||
return;
|
||||
}
|
||||
|
||||
$subscriberData['status'] = $processedValues['subscription_status'];
|
||||
|
||||
$entry = fluentFormApi('submissions')->find($insertId);
|
||||
|
||||
if ($entry && $entry->status == 'confirmed') {
|
||||
$subscriberData['status'] = 'subscribed';
|
||||
}
|
||||
|
||||
if (!empty($subscriberData['country'])) {
|
||||
$country = FunnelHelper::getCountryShortName($subscriberData['country']);
|
||||
if ($country) {
|
||||
$subscriberData['country'] = $country;
|
||||
} else {
|
||||
unset($subscriberData['country']);
|
||||
}
|
||||
}
|
||||
|
||||
(new FunnelProcessor())->startFunnelSequence($funnel, $subscriberData, [
|
||||
'source_trigger_name' => $this->triggerName,
|
||||
'source_ref_id' => $insertId,
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
private function isProcessable($funnel, $subscriberData)
|
||||
{
|
||||
$subscriber = FunnelHelper::getSubscriber($subscriberData['email']);
|
||||
if ($subscriber && FunnelHelper::ifAlreadyInFunnel($funnel->id, $subscriber->id)) {
|
||||
$conditions = $funnel->conditions;
|
||||
// check update_type
|
||||
$runMultiple = Arr::get($conditions, 'run_only_one') == 'no';
|
||||
|
||||
// if run multiple then delete
|
||||
if ($runMultiple) {
|
||||
FunnelHelper::removeSubscribersFromFunnel($funnel->id, [$subscriber->id]);
|
||||
}
|
||||
|
||||
return $runMultiple;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+164
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
namespace FluentCrm\App\Services\Funnel\Triggers;
|
||||
|
||||
use FluentCrm\App\Services\Funnel\BaseTrigger;
|
||||
use FluentCrm\App\Services\Funnel\FunnelHelper;
|
||||
use FluentCrm\App\Services\Funnel\FunnelProcessor;
|
||||
use FluentCrm\Framework\Support\Arr;
|
||||
|
||||
class FluentFormSubscriptionCancelledTrigger extends BaseTrigger
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
if (!defined('FLUENTFORM')) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
$this->actionArgNum = 3;
|
||||
$this->triggerName = 'fluentform/subscription_payment_canceled';
|
||||
$this->priority = 25;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function getTrigger()
|
||||
{
|
||||
return [
|
||||
'category' => __('Fluent Forms', 'fluent-crm'),
|
||||
'label' => __('Subscription Cancelled (Fluent Forms)', 'fluent-crm'),
|
||||
'description' => __('This Funnel will be initiated when a subscription is cancelled via Fluent Forms.', 'fluent-crm'),
|
||||
'icon' => 'fc-icon-fluentforms',
|
||||
];
|
||||
}
|
||||
|
||||
public function getFunnelSettingsDefaults()
|
||||
{
|
||||
return [
|
||||
'subscription_status' => 'subscribed'
|
||||
];
|
||||
}
|
||||
|
||||
public function getSettingsFields($funnel)
|
||||
{
|
||||
return [
|
||||
'title' => __('Subscription Cancelled Funnel', 'fluent-crm'),
|
||||
'sub_title' => __('This Funnel will be initiated when a subscription is cancelled via Fluent Forms.', 'fluent-crm'),
|
||||
'fields' => [
|
||||
'subscription_status' => [
|
||||
'type' => 'select',
|
||||
'is_multiple' => false,
|
||||
'options' => [
|
||||
[
|
||||
'id' => 'subscribed',
|
||||
'title' => __('Subscribed', 'fluent-crm')
|
||||
],
|
||||
[
|
||||
'id' => 'pending',
|
||||
'title' => __('Pending', 'fluent-crm')
|
||||
]
|
||||
],
|
||||
'label' => __('Subscription Status', 'fluent-crm'),
|
||||
'placeholder' => __('Select Status', 'fluent-crm')
|
||||
],
|
||||
'subscription_status_info' => [
|
||||
'type' => 'html',
|
||||
'info' => sprintf('<b>%s</b>', __('An automated double-opt-in email will be sent if the contact is new or not already subscribed.', 'fluent-crm')),
|
||||
'dependency' => [
|
||||
'depends_on' => 'subscription_status',
|
||||
'operator' => '=',
|
||||
'value' => 'pending'
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
protected function getForms($funnel)
|
||||
{
|
||||
return fluentCrmDb()->table('fluentform_forms')
|
||||
->select('id', 'title')
|
||||
->orderBy('id', 'DESC')
|
||||
->get();
|
||||
}
|
||||
|
||||
public function getFunnelConditionDefaults($funnel)
|
||||
{
|
||||
return [
|
||||
'form_ids' => []
|
||||
];
|
||||
}
|
||||
|
||||
public function getConditionFields($funnel)
|
||||
{
|
||||
return [
|
||||
'form_ids' => [
|
||||
'type' => 'multi-select',
|
||||
'label' => __('Target Forms', 'fluent-crm'),
|
||||
'options' => $this->getForms($funnel),
|
||||
'inline_help' => __('Keep it blank to run for any form', '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)', 'fluent-crm'),
|
||||
'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', 'fluent-crm')
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function handle($funnel, $originalArgs)
|
||||
{
|
||||
[$subscription, $submission, $vendorData] = $originalArgs;
|
||||
|
||||
$userId = $submission->user_id;
|
||||
|
||||
$subscriberData = FunnelHelper::prepareUserData($userId);
|
||||
|
||||
if (empty($subscriberData['email'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$willProcess = $this->isProcessable($funnel, $subscription, $subscriberData);
|
||||
|
||||
$willProcess = apply_filters('fluentcrm_funnel_will_process_' . $this->triggerName, $willProcess, $funnel, $subscriberData, $originalArgs);
|
||||
if (!$willProcess) {
|
||||
return;
|
||||
}
|
||||
|
||||
$subscriberData = wp_parse_args($subscriberData, $funnel->settings);
|
||||
|
||||
$subscriberData['status'] = $subscriberData['subscription_status'];
|
||||
unset($subscriberData['subscription_status']);
|
||||
|
||||
(new FunnelProcessor())->startFunnelSequence($funnel, $subscriberData, [
|
||||
'source_trigger_name' => $this->triggerName,
|
||||
'source_ref_id' => $subscription->id
|
||||
]);
|
||||
}
|
||||
|
||||
private function isProcessable($funnel, $subscription, $subscriberData)
|
||||
{
|
||||
$conditions = $funnel->conditions;
|
||||
// check the Form Ids
|
||||
if ($conditions['form_ids']) {
|
||||
if (!in_array($subscription->form_id, $conditions['form_ids'])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$subscriber = FunnelHelper::getSubscriber($subscriberData['email']);
|
||||
|
||||
// check run_only_one
|
||||
if ($subscriber && FunnelHelper::ifAlreadyInFunnel($funnel->id, $subscriber->id)) {
|
||||
$multipleRun = Arr::get($conditions, 'run_multiple') == 'yes';
|
||||
if ($multipleRun) {
|
||||
FunnelHelper::removeSubscribersFromFunnel($funnel->id, [$subscriber->id]);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+242
@@ -0,0 +1,242 @@
|
||||
<?php
|
||||
namespace FluentCrm\App\Services\Funnel\Triggers;
|
||||
|
||||
|
||||
use FluentCrm\App\Services\Funnel\BaseTrigger;
|
||||
use FluentCrm\App\Services\Funnel\FunnelHelper;
|
||||
use FluentCrm\App\Services\Funnel\FunnelProcessor;
|
||||
use FluentCrm\Framework\Support\Arr;
|
||||
use FluentForm\App\Modules\Form\FormFieldsParser;
|
||||
use FluentForm\App\Services\FormBuilder\ShortCodeParser;
|
||||
|
||||
class FluentFormSubscriptionPaymentReceivedTrigger extends BaseTrigger
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
if (!defined('FLUENTFORM')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->actionArgNum = 2;
|
||||
$this->triggerName = 'fluentform/subscription_payment_active';
|
||||
$this->priority = 25;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function getTrigger()
|
||||
{
|
||||
return [
|
||||
'category' => __('Fluent Forms', 'fluent-crm'),
|
||||
'label' => __('Subscription Payment Received (Fluent Forms)', 'fluent-crm'),
|
||||
'description' => __('This Funnel will be initiated when a subscription payment is successfully received through Fluent Forms.', 'fluent-crm'),
|
||||
'icon' => 'fc-icon-fluentforms',
|
||||
];
|
||||
}
|
||||
|
||||
public function getFunnelSettingsDefaults()
|
||||
{
|
||||
return [
|
||||
'form_id' => '',
|
||||
'primary_fields' => [
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'email' => ''
|
||||
],
|
||||
'other_fields' => [
|
||||
[
|
||||
'field_key' => '',
|
||||
'field_value' => ''
|
||||
]
|
||||
],
|
||||
'subscription_status' => 'subscribed'
|
||||
];
|
||||
}
|
||||
|
||||
public function getSettingsFields($funnel)
|
||||
{
|
||||
$valueOptions = $this->getValueOptions($funnel);
|
||||
$secondaryFields = apply_filters('fluent_crm/fluentform_other_map_fields',
|
||||
FunnelHelper::getSecondaryContactFieldMaps()
|
||||
);
|
||||
|
||||
return [
|
||||
'title' => __('Subscription Payment Received', 'fluent-crm'),
|
||||
'sub_title' => __('This Funnel will be initiated when a subscription payment is successfully received through Fluent Forms.', 'fluent-crm'),
|
||||
'fields' => [
|
||||
'form_id' => [
|
||||
'type' => 'reload_field_selection',
|
||||
'label' => __('Select your form', 'fluent-crm'),
|
||||
'options' => $this->getForms($funnel)
|
||||
],
|
||||
'primary_fields' => [
|
||||
'label' => __('Map Primary Data', 'fluent-crm'),
|
||||
'type' => 'form-group-mapper',
|
||||
'value_options' => $valueOptions,
|
||||
'local_label' => __('Contact Field (CRM)', 'fluent-crm'),
|
||||
'remote_label' => __('Form Field', 'fluent-crm'),
|
||||
'fields' => FunnelHelper::getPrimaryContactFieldMaps()
|
||||
],
|
||||
'other_fields' => [
|
||||
'label' => __('Map Other Data', 'fluent-crm'),
|
||||
'type' => 'form-many-drop-down-mapper',
|
||||
'value_options' => $valueOptions,
|
||||
'local_label' => __('Select Contact Property', 'fluent-crm'),
|
||||
'remote_label' => __('Select Form Field', 'fluent-crm'),
|
||||
'local_placeholder' => __('Select Contact Property', 'fluent-crm'),
|
||||
'remote_placeholder' => __('Select Form Property', 'fluent-crm'),
|
||||
'fields' => $secondaryFields
|
||||
],
|
||||
'subscription_status' => [
|
||||
'type' => 'select',
|
||||
'is_multiple' => false,
|
||||
'options' => [
|
||||
[
|
||||
'id' => 'subscribed',
|
||||
'title' => 'Subscribed'
|
||||
],
|
||||
[
|
||||
'id' => 'pending',
|
||||
'title' => 'Pending'
|
||||
]
|
||||
],
|
||||
'label' => __('Subscription Status', 'fluent-crm'),
|
||||
'placeholder' => __('Select Status', 'fluent-crm')
|
||||
],
|
||||
'subscription_status_info' => [
|
||||
'type' => 'html',
|
||||
'info' => '<b>An Automated double-optin email will be sent if the contact is new or not subscribed already</b>',
|
||||
'dependency' => [
|
||||
'depends_on' => 'subscription_status',
|
||||
'operator' => '=',
|
||||
'value' => 'pending'
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
protected function getValueOptions($funnel)
|
||||
{
|
||||
$formId = Arr::get($funnel->settings, 'form_id');
|
||||
if (!$formId) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$form = fluentCrmDb()->table('fluentform_forms')
|
||||
->find($formId);
|
||||
|
||||
if(!$form) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$formFields = FormFieldsParser::getShortCodeInputs(
|
||||
$form, [
|
||||
'admin_label'
|
||||
]);
|
||||
|
||||
$formattedInputs = [];
|
||||
|
||||
foreach ($formFields as $inputKey => $input) {
|
||||
$formattedInputs[] = [
|
||||
'id' => '{inputs.' . $inputKey . '}',
|
||||
'title' => $input['admin_label']
|
||||
];
|
||||
}
|
||||
|
||||
return $formattedInputs;
|
||||
}
|
||||
|
||||
protected function getForms($funnel)
|
||||
{
|
||||
return fluentCrmDb()->table('fluentform_forms')
|
||||
->select('id', 'title')
|
||||
->orderBy('id', 'DESC')
|
||||
->get();
|
||||
}
|
||||
|
||||
public function getFunnelConditionDefaults($funnel)
|
||||
{
|
||||
return [
|
||||
'run_only_one' => 'yes'
|
||||
];
|
||||
}
|
||||
|
||||
public function getConditionFields($funnel)
|
||||
{
|
||||
return [
|
||||
'run_only_one' => [
|
||||
'type' => 'yes_no_check',
|
||||
'label' => '',
|
||||
'check_label' => __('Run this automation only once per contact. If unchecked then it will over-write existing flow', 'fluent-crm'),
|
||||
'help' => __('If you enable this then this will run only once per customer otherwise, It will delete the existing automation flow and start new', 'fluent-crm'),
|
||||
'options' => FunnelHelper::getUpdateOptions()
|
||||
],
|
||||
];
|
||||
}
|
||||
public function handle($funnel, $originalArgs)
|
||||
{
|
||||
[$submission, $formResponse] = $originalArgs;
|
||||
$formData = $formResponse->response;
|
||||
|
||||
$processedValues = $funnel->settings;
|
||||
$insertId = $submission->submission_id;
|
||||
|
||||
if (Arr::get($processedValues, 'form_id') != $formResponse->form_id) {
|
||||
return; // not our form
|
||||
}
|
||||
|
||||
$processedValues = ShortCodeParser::parse($processedValues, $insertId, $formData);
|
||||
|
||||
$subscriberData = Arr::get($processedValues, 'primary_fields', []);
|
||||
$subscriberData['custom_values'] = $this->processOtherFields(Arr::get($processedValues, 'other_fields', []));
|
||||
|
||||
$willProcess = $this->isProcessable($funnel, $subscriberData);
|
||||
|
||||
$willProcess = apply_filters('fluentcrm_funnel_will_process_' . $this->triggerName, $willProcess, $funnel, $subscriberData, $originalArgs);
|
||||
if (!$willProcess) {
|
||||
return;
|
||||
}
|
||||
$subscriberData['status'] = $processedValues['subscription_status'];
|
||||
|
||||
(new FunnelProcessor())->startFunnelSequence($funnel, $subscriberData, [
|
||||
'source_trigger_name' => $this->triggerName,
|
||||
'source_ref_id' => $insertId,
|
||||
]);
|
||||
}
|
||||
|
||||
private function processOtherFields(array $otherFields)
|
||||
{
|
||||
$customValues = [];
|
||||
foreach ($otherFields as $otherField) {
|
||||
if (!empty($otherField['field_key']) && !empty($otherField['field_value'])) {
|
||||
$key = $otherField['field_key'];
|
||||
if (strpos($key, '.') !== false) {
|
||||
$customValues[str_replace('custom.', '', $key)] = $otherField['field_value'];
|
||||
} else {
|
||||
$customValues[$key] = $otherField['field_value'];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $customValues;
|
||||
}
|
||||
|
||||
private function isProcessable($funnel, $subscriberData)
|
||||
{
|
||||
$subscriber = FunnelHelper::getSubscriber($subscriberData['email']);
|
||||
if ($subscriber && FunnelHelper::ifAlreadyInFunnel($funnel->id, $subscriber->id)) {
|
||||
$conditions = $funnel->conditions;
|
||||
// check update_type
|
||||
$runMultiple = Arr::get($conditions, 'run_only_one') == 'no';
|
||||
|
||||
// if run multiple then delete
|
||||
if ($runMultiple) {
|
||||
FunnelHelper::removeSubscribersFromFunnel($funnel->id, [$subscriber->id]);
|
||||
}
|
||||
|
||||
return $runMultiple;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
namespace FluentCrm\App\Services\Funnel\Triggers;
|
||||
|
||||
use FluentCrm\App\Services\Funnel\BaseTrigger;
|
||||
use FluentCrm\App\Services\Funnel\FunnelHelper;
|
||||
use FluentCrm\App\Services\Funnel\FunnelProcessor;
|
||||
use FluentCrm\Framework\Support\Arr;
|
||||
|
||||
class UserRegistrationTrigger extends BaseTrigger
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->triggerName = 'user_register';
|
||||
$this->priority = 10;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function getTrigger()
|
||||
{
|
||||
return [
|
||||
'category' => __('WordPress Triggers', 'fluent-crm'),
|
||||
'label' => __('New User Sign Up', 'fluent-crm'),
|
||||
'description' => __('This Funnel will be initiated when a new user has been registered in your site', 'fluent-crm'),
|
||||
'icon' => 'fc-icon-wp_new_user_signup',
|
||||
];
|
||||
}
|
||||
|
||||
public function getFunnelSettingsDefaults()
|
||||
{
|
||||
return [
|
||||
'subscription_status' => 'subscribed'
|
||||
];
|
||||
}
|
||||
|
||||
public function getSettingsFields($funnel)
|
||||
{
|
||||
return [
|
||||
'title' => __('New User Sign Up Funnel', 'fluent-crm'),
|
||||
'sub_title' => __('This Funnel will be initiated when a new user has been registered in your site', 'fluent-crm'),
|
||||
'fields' => [
|
||||
'subscription_status' => [
|
||||
'type' => 'option_selectors',
|
||||
'option_key' => 'editable_statuses',
|
||||
'is_multiple' => false,
|
||||
'label' => __('Subscription Status', 'fluent-crm'),
|
||||
'placeholder' => __('Select Status', 'fluent-crm')
|
||||
],
|
||||
'subscription_status_info' => [
|
||||
'type' => 'html',
|
||||
'info' => '<b>'.__('An Automated double-optin email will be sent for new subscribers', 'fluent-crm').'</b>',
|
||||
'dependency' => [
|
||||
'depends_on' => 'subscription_status',
|
||||
'operator' => '=',
|
||||
'value' => 'pending'
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function getFunnelConditionDefaults($funnel)
|
||||
{
|
||||
return [
|
||||
'update_type' => 'update', // skip_all_actions, skip_update_if_exist
|
||||
'user_roles' => []
|
||||
];
|
||||
}
|
||||
|
||||
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' => FunnelHelper::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' => FunnelHelper::getUserRoles(),
|
||||
'inline_help' => __('Leave blank to run for all user roles', 'fluent-crm')
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function handle($funnel, $originalArgs)
|
||||
{
|
||||
$userId = $originalArgs[0];
|
||||
$subscriberData = FunnelHelper::prepareUserData($userId);
|
||||
if (empty($subscriberData['email'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
$willProcess = $this->isProcessable($funnel, $subscriberData);
|
||||
$willProcess = apply_filters('fluentcrm_funnel_will_process_' . $this->triggerName, $willProcess, $funnel, $subscriberData, $originalArgs);
|
||||
if (!$willProcess) {
|
||||
return;
|
||||
}
|
||||
|
||||
$subscriberData = wp_parse_args($subscriberData, $funnel->settings);
|
||||
$subscriberData['status'] = $subscriberData['subscription_status'];
|
||||
unset($subscriberData['subscription_status']);
|
||||
|
||||
(new FunnelProcessor())->startFunnelSequence($funnel, $subscriberData, [
|
||||
'source_trigger_name' => $this->triggerName,
|
||||
'source_ref_id' => $userId
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
private function isProcessable($funnel, $subscriberData)
|
||||
{
|
||||
$user = get_user_by('ID', $subscriberData['user_id']);
|
||||
|
||||
$conditions = $funnel->conditions;
|
||||
// check update_type
|
||||
$updateType = Arr::get($conditions, 'update_type');
|
||||
|
||||
$subscriber = FunnelHelper::getSubscriber($subscriberData['email']);
|
||||
if ($updateType == 'skip_all_if_exist' && $subscriber) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// check run_only_one
|
||||
if ($subscriber && FunnelHelper::ifAlreadyInFunnel($funnel->id, $subscriber->id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// check user roles
|
||||
if ($roles = Arr::get($conditions, 'user_roles', [])) {
|
||||
$userRoles = array_values($user->roles);
|
||||
return !!array_intersect($userRoles, $roles);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user