| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Modules\Integrations\FluentCRM; |
| 4 |
|
| 5 |
use FluentCommunity\App\Models\Space; |
| 6 |
use FluentCrm\App\Services\Funnel\BaseTrigger; |
| 7 |
use FluentCrm\App\Services\Funnel\FunnelHelper; |
| 8 |
use FluentCrm\App\Services\Funnel\FunnelProcessor; |
| 9 |
use FluentCrm\Framework\Support\Arr; |
| 10 |
|
| 11 |
class SpaceJoinTrigger extends BaseTrigger |
| 12 |
{ |
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
$this->triggerName = 'fluent_community/space/joined'; |
| 16 |
$this->priority = 20; |
| 17 |
$this->actionArgNum = 2; |
| 18 |
parent::__construct(); |
| 19 |
} |
| 20 |
|
| 21 |
public function getTrigger() |
| 22 |
{ |
| 23 |
return [ |
| 24 |
'category' => __('Community', 'fluent-community'), |
| 25 |
'label' => __('Joined in a Space', 'fluent-community'), |
| 26 |
'description' => __('This Funnel will be initiated when a user join in a space', 'fluent-community'), |
| 27 |
'icon' => 'fc-icon-wp_new_user_signup', |
| 28 |
]; |
| 29 |
} |
| 30 |
|
| 31 |
public function getSettingsFields($funnel) |
| 32 |
{ |
| 33 |
return [ |
| 34 |
'title' => __('Joined in a Space', 'fluent-community'), |
| 35 |
'sub_title' => __('This Funnel will be initiated when a user join in a Space', 'fluent-community'), |
| 36 |
'fields' => [ |
| 37 |
'subscription_status' => [ |
| 38 |
'type' => 'option_selectors', |
| 39 |
'option_key' => 'editable_statuses', |
| 40 |
'is_multiple' => false, |
| 41 |
'label' => __('Subscription Status', 'fluent-community'), |
| 42 |
'placeholder' => __('Select Status', 'fluent-community') |
| 43 |
], |
| 44 |
'subscription_status_info' => [ |
| 45 |
'type' => 'html', |
| 46 |
'info' => '<b>' . __('An Automated double-optin email will be sent for new subscribers', 'fluent-community') . '</b>', |
| 47 |
'dependency' => [ |
| 48 |
'depends_on' => 'subscription_status', |
| 49 |
'operator' => '=', |
| 50 |
'value' => 'pending' |
| 51 |
] |
| 52 |
] |
| 53 |
] |
| 54 |
]; |
| 55 |
} |
| 56 |
|
| 57 |
public function getFunnelSettingsDefaults() |
| 58 |
{ |
| 59 |
return [ |
| 60 |
'subscription_status' => 'subscribed' |
| 61 |
]; |
| 62 |
} |
| 63 |
|
| 64 |
public function getConditionFields($funnel) |
| 65 |
{ |
| 66 |
$communities = Space::orderBy('title', 'ASC')->select(['id', 'title'])->get(); |
| 67 |
|
| 68 |
return [ |
| 69 |
'update_type' => [ |
| 70 |
'type' => 'radio', |
| 71 |
'label' => __('If Contact Already Exist?', 'fluent-community'), |
| 72 |
'help' => __('Please specify what will happen if the subscriber already exists in the database', 'fluent-community'), |
| 73 |
'options' => FunnelHelper::getUpdateOptions() |
| 74 |
], |
| 75 |
'community_ids' => [ |
| 76 |
'type' => 'multi-select', |
| 77 |
'is_multiple' => true, |
| 78 |
'label' => __('Targeted Communitues', 'fluent-community'), |
| 79 |
'help' => __('Select which for which commnunities this automation Funnel for', 'fluent-community'), |
| 80 |
'placeholder' => __('Select Communities', 'fluent-community'), |
| 81 |
'options' => $communities, |
| 82 |
'inline_help' => __('Leave blank to run for all communities', 'fluent-community') |
| 83 |
], |
| 84 |
'run_multiple' => [ |
| 85 |
'type' => 'yes_no_check', |
| 86 |
'label' => '', |
| 87 |
'check_label' => __('Restart the automation multiple times for a contact for this event. (Only enable this if you want to restart the automation for the same contact)', 'fluent-community'), |
| 88 |
'inline_help' => __('If you enable this, it will restart the automation for a contact even if they are already in the automation. Otherwise, it will skip if the contact already exists.', 'fluent-community') |
| 89 |
] |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
public function getFunnelConditionDefaults($funnel) |
| 94 |
{ |
| 95 |
return [ |
| 96 |
'update_type' => 'update', // skip_all_actions, skip_update_if_exist |
| 97 |
'community_ids' => [], |
| 98 |
'run_multiple' => 'yes' |
| 99 |
]; |
| 100 |
} |
| 101 |
|
| 102 |
public function handle($funnel, $originalArgs) |
| 103 |
{ |
| 104 |
$space = $originalArgs[0]; |
| 105 |
$userId = $originalArgs[1]; |
| 106 |
|
| 107 |
$user = get_user_by('ID', $userId); |
| 108 |
|
| 109 |
if (!$user || !$this->isProcessable($funnel, $user, $space)) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
$subscriberData = FunnelHelper::prepareUserData($user); |
| 114 |
|
| 115 |
$subscriberData = wp_parse_args($subscriberData, $funnel->settings); |
| 116 |
$subscriberData['status'] = $subscriberData['subscription_status']; |
| 117 |
unset($subscriberData['subscription_status']); |
| 118 |
|
| 119 |
(new FunnelProcessor())->startFunnelSequence($funnel, $subscriberData, [ |
| 120 |
'source_trigger_name' => $this->triggerName, |
| 121 |
'source_ref_id' => $space->id |
| 122 |
]); |
| 123 |
} |
| 124 |
|
| 125 |
private function isProcessable($funnel, $user, $space) |
| 126 |
{ |
| 127 |
$conditions = $funnel->conditions; |
| 128 |
// check update_type |
| 129 |
$updateType = Arr::get($conditions, 'update_type'); |
| 130 |
|
| 131 |
$subscriber = FunnelHelper::getSubscriber($user->user_email); |
| 132 |
if ($updateType == 'skip_all_if_exist' && $subscriber) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
// check user roles |
| 137 |
if ($checkIds = Arr::get($conditions, 'community_ids', [])) { |
| 138 |
$checkIds = (array) $checkIds; |
| 139 |
if (!in_array($space->id, $checkIds)) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
// check run_only_one |
| 145 |
if ($subscriber && FunnelHelper::ifAlreadyInFunnel($funnel->id, $subscriber->id)) { |
| 146 |
$multipleRun = Arr::get($conditions, 'run_multiple') == 'yes'; |
| 147 |
if ($multipleRun) { |
| 148 |
FunnelHelper::removeSubscribersFromFunnel($funnel->id, [$subscriber->id]); |
| 149 |
} |
| 150 |
return $multipleRun; |
| 151 |
} |
| 152 |
|
| 153 |
return true; |
| 154 |
} |
| 155 |
} |
| 156 |
|