| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Modules\Integrations\FluentCRM; |
| 4 |
|
| 5 |
use FluentCommunity\Modules\Course\Model\Course; |
| 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 CourseCompletedTrigger extends BaseTrigger |
| 12 |
{ |
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
$this->triggerName = 'fluent_community/course/completed'; |
| 16 |
$this->priority = 31; |
| 17 |
$this->actionArgNum = 2; |
| 18 |
parent::__construct(); |
| 19 |
} |
| 20 |
|
| 21 |
public function getTrigger() |
| 22 |
{ |
| 23 |
return [ |
| 24 |
'category' => __('Community', 'fluent-community'), |
| 25 |
'label' => __('Completed a course', 'fluent-community'), |
| 26 |
'description' => __('This automation will be initiated when a user completes course.', 'fluent-community'), |
| 27 |
'icon' => 'fc-icon-wp_new_user_signup', |
| 28 |
'svg_icon' => '<svg viewBox="0 0 64 64" fill="none"><rect x="12" y="10" width="40" height="34" rx="6" stroke="currentColor" stroke-width="3"/><line x1="20" y1="20" x2="44" y2="20" stroke="currentColor" stroke-width="3" stroke-linecap="round"/><line x1="20" y1="28" x2="38" y2="28" stroke="currentColor" stroke-width="3" stroke-linecap="round"/><circle cx="46" cy="42" r="9" fill="currentColor"/><path d="M42.8 42.3l2.6 2.6L49.5 39.8" stroke="#fff" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/><path d="M24 44v12l6-4 6 4V44" stroke="currentColor" stroke-width="3" stroke-linejoin="round"/></svg>' |
| 29 |
]; |
| 30 |
} |
| 31 |
|
| 32 |
public function getSettingsFields($funnel) |
| 33 |
{ |
| 34 |
return [ |
| 35 |
'title' => __('Completed a course', 'fluent-community'), |
| 36 |
'sub_title' => __('This automation will be initiated when a user completes a course.', 'fluent-community'), |
| 37 |
'fields' => [ |
| 38 |
'subscription_status' => [ |
| 39 |
'type' => 'option_selectors', |
| 40 |
'option_key' => 'editable_statuses', |
| 41 |
'is_multiple' => false, |
| 42 |
'label' => __('Subscription Status', 'fluent-community'), |
| 43 |
'placeholder' => __('Select Status', 'fluent-community') |
| 44 |
], |
| 45 |
'subscription_status_info' => [ |
| 46 |
'type' => 'html', |
| 47 |
'info' => '<b>' . __('An Automated double-opt-in email will be sent for new subscribers', 'fluent-community') . '</b>', |
| 48 |
'dependency' => [ |
| 49 |
'depends_on' => 'subscription_status', |
| 50 |
'operator' => '=', |
| 51 |
'value' => 'pending' |
| 52 |
] |
| 53 |
] |
| 54 |
] |
| 55 |
]; |
| 56 |
} |
| 57 |
|
| 58 |
public function getFunnelSettingsDefaults() |
| 59 |
{ |
| 60 |
return [ |
| 61 |
'subscription_status' => 'subscribed' |
| 62 |
]; |
| 63 |
} |
| 64 |
|
| 65 |
public function getConditionFields($funnel) |
| 66 |
{ |
| 67 |
$courses = Course::orderBy('title', 'ASC')->select(['id', 'title'])->get(); |
| 68 |
|
| 69 |
return [ |
| 70 |
'update_type' => [ |
| 71 |
'type' => 'radio', |
| 72 |
'label' => __('If Contact Already Exists?', 'fluent-community'), |
| 73 |
'help' => __('Please specify what will happen if the subscriber already exists in the database', 'fluent-community'), |
| 74 |
'options' => FunnelHelper::getUpdateOptions() |
| 75 |
], |
| 76 |
'course_ids' => [ |
| 77 |
'type' => 'multi-select', |
| 78 |
'is_multiple' => true, |
| 79 |
'label' => __('Targeted Courses', 'fluent-community'), |
| 80 |
'help' => __('Select which courses this automation is for.', 'fluent-community'), |
| 81 |
'placeholder' => __('Select Courses', 'fluent-community'), |
| 82 |
'options' => $courses, |
| 83 |
'inline_help' => __('Leave blank to run for all courses', 'fluent-community') |
| 84 |
], |
| 85 |
'run_multiple' => [ |
| 86 |
'type' => 'yes_no_check', |
| 87 |
'label' => '', |
| 88 |
'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'), |
| 89 |
'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') |
| 90 |
] |
| 91 |
]; |
| 92 |
} |
| 93 |
|
| 94 |
public function getFunnelConditionDefaults($funnel) |
| 95 |
{ |
| 96 |
return [ |
| 97 |
'update_type' => 'update', // skip_all_actions, skip_update_if_exist |
| 98 |
'course_ids' => [], |
| 99 |
'run_multiple' => 'yes' |
| 100 |
]; |
| 101 |
} |
| 102 |
|
| 103 |
public function handle($funnel, $originalArgs) |
| 104 |
{ |
| 105 |
$course = $originalArgs[0]; |
| 106 |
$userId = $originalArgs[1]; |
| 107 |
|
| 108 |
$user = get_user_by('ID', $userId); |
| 109 |
|
| 110 |
if (!$user || !$this->isProcessable($funnel, $user, $course)) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
$subscriberData = FunnelHelper::prepareUserData($user); |
| 115 |
|
| 116 |
$subscriberData = wp_parse_args($subscriberData, $funnel->settings); |
| 117 |
$subscriberData['status'] = $subscriberData['subscription_status']; |
| 118 |
unset($subscriberData['subscription_status']); |
| 119 |
|
| 120 |
(new FunnelProcessor())->startFunnelSequence($funnel, $subscriberData, [ |
| 121 |
'source_trigger_name' => $this->triggerName, |
| 122 |
'source_ref_id' => $course->id |
| 123 |
]); |
| 124 |
} |
| 125 |
|
| 126 |
private function isProcessable($funnel, $user, $course) |
| 127 |
{ |
| 128 |
$conditions = $funnel->conditions; |
| 129 |
// check update_type |
| 130 |
$updateType = Arr::get($conditions, 'update_type'); |
| 131 |
|
| 132 |
$subscriber = FunnelHelper::getSubscriber($user->user_email); |
| 133 |
if ($updateType == 'skip_all_if_exist' && $subscriber) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
// check user roles |
| 138 |
if ($checkIds = Arr::get($conditions, 'course_ids', [])) { |
| 139 |
if (!in_array($course->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 |
|