PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.97
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.97
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / Modules / Integrations / FluentCRM / CourseEnrollmentTrigger.php

CourseEnrollmentTrigger.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.97, at Modules/Integrations/FluentCRM/CourseEnrollmentTrigger.php

156 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Modules\Integrations\FluentCRM;
4
5 use FluentCommunity\App\Models\Space;
6 use FluentCommunity\Modules\Course\Model\Course;
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 CourseEnrollmentTrigger extends BaseTrigger
13 {
14 public function __construct()
15 {
16 $this->triggerName = 'fluent_community/course/enrolled';
17 $this->priority = 20;
18 $this->actionArgNum = 2;
19 parent::__construct();
20 }
21
22 public function getTrigger()
23 {
24 return [
25 'category' => __('Community', 'fluent-community'),
26 'label' => __('Enrolled in a course', 'fluent-community'),
27 'description' => __('This automation will be initiated when a user enrolls in a course.', 'fluent-community'),
28 'icon' => 'fc-icon-wp_new_user_signup',
29 ];
30 }
31
32 public function getSettingsFields($funnel)
33 {
34 return [
35 'title' => __('Enrolled in a course', 'fluent-community'),
36 'sub_title' => __('This automation will be initiated when a user enrolls in 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 Exist?', '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 Communities', 'fluent-community'),
82 'options' => $courses,
83 'inline_help' => __('Leave blank to run for all communities', '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