PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / app / Modules / Integrations / FluentPlugins / FluentCommunityConnect.php

FluentCommunityConnect.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Modules/Integrations/FluentPlugins/FluentCommunityConnect.php

339 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Modules\Integrations\FluentPlugins;
4
5 use FluentCart\App\Modules\Integrations\BaseIntegrationManager;
6 use FluentCart\App\Services\AuthService;
7 use FluentCart\App\Vite;
8 use FluentCart\Framework\Support\Arr;
9 use FluentCommunity\App\Models\Space;
10 use FluentCommunity\App\Models\SpaceUserPivot;
11 use FluentCommunity\App\Models\User;
12 use FluentCommunity\App\Models\XProfile;
13 use FluentCommunity\App\Services\Helper;
14 use FluentCommunity\Modules\Course\Model\Course;
15 use FluentCommunity\Modules\Course\Services\CourseHelper;
16
17 class FluentCommunityConnect extends BaseIntegrationManager
18 {
19
20 protected $runOnBackgroundForProduct = false;
21
22 public $category = 'lms';
23
24 public function __construct()
25 {
26 parent::__construct('FluentCommunity', 'fluent_community', 12);
27
28 $this->description = __('Create a fast and responsive community and LMS without slowing down your server – no bloat, just performance. Sale your course or memberships with FluentCart + FluentCommunity Integration.', 'fluent-cart');
29 $this->logo = Vite::getAssetUrl('images/integrations/fluent-community.svg');
30 $this->disableGlobalSettings = true;
31 $this->installable = 'fluent-community/fluent-community.php';
32 }
33
34 public function isConfigured()
35 {
36 return defined('FLUENT_COMMUNITY_PLUGIN_VERSION');
37 }
38
39 public function getApiSettings()
40 {
41 return [
42 'status' => defined('FLUENT_COMMUNITY_PLUGIN_VERSION'),
43 'api_key' => ''
44 ];
45 }
46
47 public function getIntegrationDefaults($settings)
48 {
49 return [
50 'enabled' => 'yes',
51 'name' => '',
52 'space_ids' => [],
53 'remove_space_ids' => [],
54 'course_ids' => [],
55 'remove_course_ids' => [],
56 'event_trigger' => [],
57 'tag_ids_selection_type' => 'simple',
58 'mark_as_verified' => 'no',
59 'watch_on_access_revoke' => 'yes'
60 ];
61 }
62
63 public function getSettingsFields($settings, $args = [])
64 {
65 $spaces = Space::orderBy('title', 'ASC')->select(['id', 'title', 'parent_id'])
66 ->with(['group'])
67 ->get();
68 $formattedSpaces = [];
69 foreach ($spaces as $space) {
70 $title = $space->title;
71
72 if ($space->group) {
73 $title .= ' (' . $space->group->title . ')';
74 }
75
76 $formattedSpaces[(string)$space->id] = $title;
77 }
78
79 $formattedCourses = [];
80
81 $isCourseEnabled = Helper::isFeatureEnabled('course_module');
82
83 if ($isCourseEnabled) {
84 $courses = Course::orderBy('title', 'ASC')->select(['id', 'title'])->get();
85
86 $formattedCourses = [];
87 foreach ($courses as $course) {
88 $formattedCourses[(string)$course->id] = $course->title;
89 }
90 }
91
92 $fields = [
93 'name' => [
94 'key' => 'name',
95 'label' => __('Feed Title', 'fluent-cart'),
96 'required' => true,
97 'placeholder' => __('Name', 'fluent-cart'),
98 'component' => 'text',
99 'inline_tip' => __('Name of this feed, it will be used to identify this feed in the list of feeds', 'fluent-cart')
100 ],
101 'space_ids' => [
102 'key' => 'space_ids',
103 'label' => __('Add to Spaces', 'fluent-cart'),
104 'placeholder' => __('Select FluentCommunity Spaces', 'fluent-cart'),
105 'inline_tip' => __('Select the FluentCommunity Spaces you would like to add.', 'fluent-cart'),
106 'component' => 'select',
107 'is_multiple' => true,
108 'required' => false,
109 'options' => $formattedSpaces
110 ],
111 'course_ids' => [
112 'key' => 'course_ids',
113 'require_list' => false,
114 'label' => __('Add to Courses', 'fluent-cart'),
115 'placeholder' => __('Select Courses', 'fluent-cart'),
116 'component' => 'select',
117 'is_multiple' => true,
118 'options' => $formattedCourses,
119 'inline_tip' => __('Select the courses you would like to enroll the customer to', 'fluent-cart')
120 ],
121 'remove_space_ids' => [
122 'key' => 'remove_space_ids',
123 'label' => __('Remove From Spaces', 'fluent-cart'),
124 'placeholder' => __('Select Spaces', 'fluent-cart'),
125 'inline_tip' => __('Select the Spaces you would like to remove from your spaces.', 'fluent-cart'),
126 'component' => 'select',
127 'is_multiple' => true,
128 'required' => false,
129 'options' => $formattedSpaces
130 ],
131 'remove_course_ids' => [
132 'key' => 'remove_course_ids',
133 'require_list' => false,
134 'label' => __('Remove From Courses', 'fluent-cart'),
135 'placeholder' => __('Select Courses', 'fluent-cart'),
136 'component' => 'select',
137 'is_multiple' => true,
138 'options' => $formattedCourses,
139 'inline_tip' => __('Select the courses you would like to remove from the customer', 'fluent-cart')
140 ],
141 'mark_as_verified' => [
142 'key' => 'mark_as_verified',
143 'component' => 'yes-no-checkbox',
144 'checkbox_label' => __('Mark the community profile as verified', 'fluent-cart'),
145 'inline_tip' => __('If you enable this, the user will be marked as verified in FluentCommunity', 'fluent-cart')
146 ],
147 'watch_on_access_revoke' => [
148 'key' => 'watch_on_access_revoke',
149 'component' => 'yes-no-checkbox',
150 'checkbox_label' => __('Remove from selected Courses/Spaces on Refund or Subscription Access Expiration ', 'fluent-cart'),
151 'inline_tip' => __('If you enable this, on refund or subscription validity expiration, the selected spaces and courses will be removed from the customer.', 'fluent-cart')
152 ]
153 ];
154
155 if (!$isCourseEnabled) {
156 unset($fields['course_ids']);
157 unset($fields['remove_course_ids']);
158 }
159
160 $fields = array_values($fields);
161
162 $fields[] = $this->actionFields();
163
164 return [
165 'fields' => $fields,
166 'button_require_list' => false,
167 'integration_title' => __('FluentCommunity', 'fluent-cart')
168 ];
169 }
170
171 /*
172 * For Handling Notifications broadcast
173 */
174 public function processAction($order, $eventData)
175 {
176 $feedConfig = Arr::get($eventData, 'feed', []);
177 $isRevokedHook = Arr::get($eventData, 'is_revoke_hook') === 'yes';
178 $customer = $order->customer;
179
180 // check exits
181 $courseIds = array_filter((array)Arr::get($feedConfig, 'course_ids', []), 'intval');
182 $spaceIds = array_filter((array)Arr::get($feedConfig, 'space_ids', []), 'intval');
183 $removeCourseIds = array_filter((array)Arr::get($feedConfig, 'remove_course_ids', []), 'intval');
184 $removeSpaceIds = array_filter((array)Arr::get($feedConfig, 'remove_space_ids', []), 'intval');
185 $markAsVerified = Arr::get($feedConfig, 'mark_as_verified', '') == 'yes';
186
187 $allSpaceCourseIds = array_merge($courseIds, $spaceIds);
188
189 $userId = $customer->getWpUserId(true);
190
191 if ($isRevokedHook) {
192 if (!$userId || !$allSpaceCourseIds) {
193 return;
194 }
195
196 $xProfile = XProfile::where('user_id', $userId)->first();
197 if (!$xProfile) {
198 return; // no xprofile found
199 }
200
201 $pivots = SpaceUserPivot::whereIn('space_id', $allSpaceCourseIds)
202 ->where('user_id', $userId)
203 ->get();
204
205 $removedIds = [];
206
207 foreach ($pivots as $pivot) {
208 $prevOrderIds = Arr::get($pivot->meta, 'fct_ids', []);
209 if ($prevOrderIds || in_array($order->id, $prevOrderIds)) {
210 // remove the order id from the meta
211 $newOrderIds = array_filter($prevOrderIds, function ($id) use ($order) {
212 return $id != $order->id;
213 });
214 if ($newOrderIds) {
215 $existingMeta = $pivot->meta;
216 $existingMeta['fct_ids'] = array_values($newOrderIds);
217 $pivot->meta = $existingMeta;
218 $pivot->save();
219 // we are just skipping it as other orders are still giving access
220 } else {
221 Helper::removeFromSpace($pivot->space_id, $userId, 'by_admin');
222 $removedIds[] = $pivot->space_id;
223 }
224 continue;
225 }
226 Helper::removeFromSpace($pivot->space_id, $userId, 'by_admin');
227 $removedIds[] = $pivot->space_id;
228 }
229
230 if ($removedIds) {
231 $order->addLog(
232 __('FluentCommunity Access Removed', 'fluent-cart'),
233 sprintf(
234 /* translators: %s is the space ids */
235 __('User has been removed from spaces and courses: %s', 'fluent-cart'), implode(', ', $removedIds)),
236 'info',
237 'FluentCommunity'
238 );
239 }
240
241 return;
242 }
243
244 if (!$userId) {
245 $userId = AuthService::createUserFromCustomer($customer);
246 if (is_wp_error($userId)) {
247 $order->addLog(
248 __('User creation failed from FluentCommunity Integration', 'fluent-cart'),
249 $userId->get_error_message(),
250 'error',
251 'FluentCommunity'
252 );
253 return;
254 }
255 }
256
257 if (!$userId) {
258 return false;
259 }
260
261 $communityUser = User::find($userId);
262
263 if (!$communityUser) {
264 return;
265 }
266
267 $xprofile = $communityUser->syncXProfile(true);
268
269 if (!$xprofile) {
270 return;
271 }
272
273 if ($markAsVerified) {
274 $xprofile->is_verified = 1;
275 $xprofile->save();
276 }
277
278 // we will remove the spaces and courses from the user
279 if ($removeSpaceIds) {
280 foreach ($removeSpaceIds as $spaceId) {
281 Helper::removeFromSpace($spaceId, $userId, 'by_admin');
282 }
283 }
284
285 if ($removeCourseIds) {
286 CourseHelper::leaveCourses($removeCourseIds, $userId, 'by_admin');
287 }
288
289
290 if ($spaceIds) {
291 foreach ($spaceIds as $spaceId) {
292 $pivot = SpaceUserPivot::where('space_id', $spaceId)
293 ->where('user_id', $userId)
294 ->first();
295 if ($pivot) {
296 $existingMeta = $pivot->meta;
297 $prevOrderIds = Arr::get($existingMeta, 'fct_ids', []);
298 $prevOrderIds[] = $order->id;
299 $existingMeta['fct_ids'] = array_values(array_unique($prevOrderIds));
300 $pivot->meta = $existingMeta;
301 $pivot->save();
302 // we are just updating the meta with this order id
303 } else {
304 Helper::addToSpace($spaceId, $userId, 'member', 'by_admin');
305 }
306 }
307 }
308
309 if ($courseIds) {
310 foreach ($courseIds as $courseId) {
311 $pivot = SpaceUserPivot::where('space_id', $courseId)
312 ->where('user_id', $userId)
313 ->first();
314 if ($pivot) {
315 $existingMeta = $pivot->meta;
316 $prevOrderIds = Arr::get($existingMeta, 'fct_ids', []);
317 $prevOrderIds[] = $order->id;
318 $existingMeta['fct_ids'] = array_values(array_unique($prevOrderIds));
319 $pivot->meta = $existingMeta;
320 $pivot->save();
321 // we are just updating the meta with this order id
322 } else {
323 Helper::addToSpace($courseId, $userId, 'student', 'by_admin');
324 }
325 }
326 }
327
328 $order->addLog(
329 __('FluentCommunity Integration Success', 'fluent-cart'),
330 sprintf(
331 /* translators: 1: space ids, 2: course ids */
332 __('User has been added to spaces: %1$s and courses: %2$s', 'fluent-cart'), implode(', ', $spaceIds), implode(', ', $courseIds)),
333 'info',
334 'FluentCommunity'
335 );
336 }
337
338 }
339