| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Modules\Integrations\FluentCRM; |
| 4 |
|
| 5 |
use FluentCommunity\Modules\Course\Model\Course; |
| 6 |
use FluentCommunity\Modules\Course\Model\CourseTopic; |
| 7 |
use FluentCrm\App\Services\Funnel\BaseTrigger; |
| 8 |
use FluentCrm\App\Services\Funnel\FunnelHelper; |
| 9 |
use FluentCrm\App\Services\Funnel\FunnelProcessor; |
| 10 |
use FluentCrm\Framework\Support\Arr; |
| 11 |
|
| 12 |
class LessonCompletedTrigger extends BaseTrigger |
| 13 |
{ |
| 14 |
public function __construct() |
| 15 |
{ |
| 16 |
$this->triggerName = 'fluent_community/course/lesson_completed'; |
| 17 |
$this->priority = 50; |
| 18 |
$this->actionArgNum = 2; |
| 19 |
parent::__construct(); |
| 20 |
} |
| 21 |
|
| 22 |
public function getTrigger() |
| 23 |
{ |
| 24 |
return [ |
| 25 |
'category' => __('Community', 'fluent-community'), |
| 26 |
'label' => __('Lesson Completed', 'fluent-community'), |
| 27 |
'icon' => 'fc-icon-wp_new_user_signup', |
| 28 |
'description' => __('This funnel runs when a student completes a lesson', 'fluent-community'), |
| 29 |
'svg_icon' => '<svg viewBox="0 0 24 24" fill="none"><rect x="3" y="4" width="16" height="14" rx="3" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><line x1="7" y1="9" x2="15" y2="9" stroke="currentColor" stroke-width="2" stroke-linecap="round"/><line x1="7" y1="12" x2="13" y2="12" stroke="currentColor" stroke-width="2" stroke-linecap="round"/><circle cx="18" cy="18" r="4" fill="currentColor"/><path d="M16.5 18l1.3 1.3L20 17" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>' |
| 30 |
]; |
| 31 |
} |
| 32 |
|
| 33 |
public function getFunnelSettingsDefaults() |
| 34 |
{ |
| 35 |
return [ |
| 36 |
'subscription_status' => 'subscribed' |
| 37 |
]; |
| 38 |
} |
| 39 |
|
| 40 |
public function getSettingsFields($funnel) |
| 41 |
{ |
| 42 |
return [ |
| 43 |
'title' => __('Student completes a Lesson in FluentCommunity', 'fluent-community'), |
| 44 |
'sub_title' => __('This Funnel will start when a student completes a lesson', 'fluent-community'), |
| 45 |
'fields' => [ |
| 46 |
'subscription_status' => [ |
| 47 |
'type' => 'option_selectors', |
| 48 |
'option_key' => 'editable_statuses', |
| 49 |
'is_multiple' => false, |
| 50 |
'label' => __('Subscription Status', 'fluent-community'), |
| 51 |
'placeholder' => __('Select Status', 'fluent-community') |
| 52 |
], |
| 53 |
'subscription_status_info' => [ |
| 54 |
'type' => 'html', |
| 55 |
'info' => '<b>'.__('An Automated double-opt-in email will be sent for new subscribers', 'fluent-community').'</b>', |
| 56 |
'dependency' => [ |
| 57 |
'depends_on' => 'subscription_status', |
| 58 |
'operator' => '=', |
| 59 |
'value' => 'pending' |
| 60 |
] |
| 61 |
] |
| 62 |
] |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
public function getFunnelConditionDefaults($funnel) |
| 67 |
{ |
| 68 |
return [ |
| 69 |
'update_type' => 'update', // skip_all_actions, skip_update_if_exist |
| 70 |
'lesson_ids' => [] |
| 71 |
]; |
| 72 |
} |
| 73 |
|
| 74 |
public function getConditionFields($funnel) |
| 75 |
{ |
| 76 |
return [ |
| 77 |
'update_type' => [ |
| 78 |
'type' => 'radio', |
| 79 |
'label' => __('If Contact Already Exists?', 'fluent-community'), |
| 80 |
'help' => __('Please specify what will happen if the subscriber already exists in the database', 'fluent-community'), |
| 81 |
'options' => FunnelHelper::getUpdateOptions() |
| 82 |
], |
| 83 |
'lesson_ids' => [ |
| 84 |
'type' => 'grouped-select', |
| 85 |
'label' => __('Target Lessons', 'fluent-community'), |
| 86 |
'help' => __('Select which lessons this automation will run for', 'fluent-community'), |
| 87 |
'options' => $this->getLessonsByCourseGroup(), |
| 88 |
'is_multiple' => true, |
| 89 |
'inline_help' => __('Keep it blank to run on any lesson', 'fluent-community') |
| 90 |
], |
| 91 |
'run_multiple' => [ |
| 92 |
'type' => 'yes_no_check', |
| 93 |
'label' => '', |
| 94 |
'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-community'), |
| 95 |
'inline_help' => __('If you enable, it will restart the automation for a contact if the contact is already in the automation. Otherwise, it will skip if it already exists.', 'fluent-community') |
| 96 |
] |
| 97 |
]; |
| 98 |
} |
| 99 |
|
| 100 |
public function handle($funnel, $originalArgs) |
| 101 |
{ |
| 102 |
$userId = $originalArgs[1]; |
| 103 |
$lesson = $originalArgs[0]; |
| 104 |
|
| 105 |
$subscriberData = FunnelHelper::prepareUserData($userId); |
| 106 |
|
| 107 |
$subscriberData['source'] = 'fluent-community'; |
| 108 |
|
| 109 |
if (empty($subscriberData['email'])) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
$willProcess = $this->isProcessable($funnel, $lesson->id, $subscriberData); |
| 114 |
|
| 115 |
$willProcess = apply_filters('fluentcrm_funnel_will_process_' . $this->triggerName, $willProcess, $funnel, $subscriberData, $originalArgs); |
| 116 |
if (!$willProcess) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
$subscriberData = wp_parse_args($subscriberData, $funnel->settings); |
| 121 |
|
| 122 |
$subscriberData['status'] = $subscriberData['subscription_status']; |
| 123 |
unset($subscriberData['subscription_status']); |
| 124 |
|
| 125 |
(new FunnelProcessor())->startFunnelSequence($funnel, $subscriberData, [ |
| 126 |
'source_trigger_name' => $this->triggerName, |
| 127 |
'source_ref_id' => $lesson->id |
| 128 |
]); |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
private function isProcessable($funnel, $lessonId, $subscriberData) |
| 133 |
{ |
| 134 |
$conditions = $funnel->conditions; |
| 135 |
// check update_type |
| 136 |
$updateType = Arr::get($conditions, 'update_type'); |
| 137 |
|
| 138 |
$subscriber = FunnelHelper::getSubscriber($subscriberData['email']); |
| 139 |
if ($subscriber && $updateType == 'skip_all_if_exist') { |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
// check the products ids |
| 144 |
if ($conditions['lesson_ids']) { |
| 145 |
return in_array($lessonId, $conditions['lesson_ids']); |
| 146 |
} |
| 147 |
|
| 148 |
// check run_only_one |
| 149 |
if ($subscriber && FunnelHelper::ifAlreadyInFunnel($funnel->id, $subscriber->id)) { |
| 150 |
$multipleRun = Arr::get($conditions, 'run_multiple') == 'yes'; |
| 151 |
if ($multipleRun) { |
| 152 |
FunnelHelper::removeSubscribersFromFunnel($funnel->id, [$subscriber->id]); |
| 153 |
} else { |
| 154 |
return false; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return true; |
| 159 |
} |
| 160 |
|
| 161 |
public function getLessonsByCourseGroup() |
| 162 |
{ |
| 163 |
$courses = Course::query()->orderBy('title', 'ASC')->get(); |
| 164 |
|
| 165 |
$groups = []; |
| 166 |
foreach ($courses as $course) { |
| 167 |
$group = [ |
| 168 |
'title' => $course->title, |
| 169 |
'slug' => $course->slug, |
| 170 |
'options' => [] |
| 171 |
]; |
| 172 |
|
| 173 |
$sections = CourseTopic::where('space_id', $course->id) |
| 174 |
->orderBy('priority', 'ASC') |
| 175 |
->with(['lessons']) |
| 176 |
->get(); |
| 177 |
|
| 178 |
|
| 179 |
foreach ($sections as $section) { |
| 180 |
foreach ($section->lessons as $lesson) { |
| 181 |
$group['options'][] = [ |
| 182 |
'id' => (string) $lesson->id, |
| 183 |
'title' => $section->title .': '.$lesson->title |
| 184 |
]; |
| 185 |
} |
| 186 |
} |
| 187 |
$groups[] = $group; |
| 188 |
} |
| 189 |
return $groups; |
| 190 |
} |
| 191 |
} |
| 192 |
|