PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.1.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.1.0
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 / FluentForms / Bootstrap.php

Bootstrap.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.1.0, at Modules/Integrations/FluentForms/Bootstrap.php

373 lines 12.7 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\FluentForms;
4
5 use FluentCommunity\App\Models\BaseSpace;
6 use FluentCommunity\App\Services\ProfileHelper;
7 use FluentForm\App\Helpers\Helper;
8 use \FluentForm\App\Http\Controllers\IntegrationManagerController;
9 use FluentCommunity\Framework\Support\Arr;
10
11
12 class Bootstrap extends IntegrationManagerController
13 {
14
15 public $hasGlobalMenu = false;
16 public $disableGlobalSettings = 'yes';
17
18 public function __construct()
19 {
20 parent::__construct(
21 null,
22 'Fluent Community',
23 'fluent_community',
24 '_fluentform_fluent_community_settings',
25 'fluent_community_feeds',
26 10
27 );
28
29 $this->logo = FLUENT_COMMUNITY_PLUGIN_URL . 'assets/images/logo.png';
30
31 $this->description = __('Connect Fluent Forms with Fluent Community', 'fluent-community');
32
33 $this->registerAdminHooks();
34
35 add_filter('fluentform/notifying_async_fluent_community', '__return_false');
36 }
37
38
39 public function getIntegrationDefaults($settings, $formId)
40 {
41 $fields = [
42 'name' => '',
43 'space_ids' => [],
44 'Email' => '',
45 'username' => '',
46 'enableAutoLogin' => false,
47 'sendEmailToNewUser' => false,
48 'conditionals' => [
49 'conditions' => [],
50 'status' => false,
51 'type' => 'all'
52 ],
53 'enabled' => true
54 ];
55
56
57 return apply_filters('fluent_community/fluentform__defaults', $fields, $formId);
58 }
59
60 public function getMergeFields($list, $listId, $formId)
61 {
62 return [];
63 }
64
65 public function getSettingsFields($settings, $formId)
66 {
67 $fieldSettings = [
68 'fields' => [
69 [
70 'key' => 'name',
71 'label' => __('Name', 'fluent-community'),
72 'required' => true,
73 'placeholder' => __('Your Feed Name', 'fluent-community'),
74 'component' => 'text'
75 ],
76 [
77 'key' => 'space_ids',
78 'label' => __('Select Spaces or Courses to Enroll', 'fluent-community'),
79 'placeholder' => __('Choose options', 'fluent-community'),
80 'required' => true,
81 'is_multiple' => true,
82 'component' => 'select',
83 'options' => $this->getAllSpacesCourses()
84 ],
85 [
86 'key' => 'custom_fields',
87 'require_list' => false,
88 'label' => __('Map Fields', 'fluent-community'),
89 'tips' => __('Associate your user fields to the appropriate Fluent Forms fields by selecting the appropriate form field from the list.', 'fluent-community'),
90 'component' => 'map_fields',
91 'field_label_remote' => __('User Profile Field', 'fluent-community'),
92 'field_label_local' => __('Form Field', 'fluent-community'),
93 'primary_fileds' => $this->communityMapFields()
94 ],
95 [
96 'require_list' => false,
97 'title' => __('For new users', 'fluent-community'),
98 'html_info' => '<h4 style="font-size: 16px;">' . __('For New Users - Data mapping') . '</h4><p>' . __('These settings will apply only if the provided email address is not a registered WordPress user', 'fluent-community') . '</p>',
99 'component' => 'html_info',
100 ],
101 [
102 'require_list' => false,
103 'key' => 'full_name',
104 'component' => 'value_text',
105 'label' => __('Full Name (Only for new users)', 'fluent_community')
106 ],
107 [
108 'require_list' => false,
109 'key' => 'password',
110 'component' => 'value_text',
111 'label' => __('Password (Only for new users)', 'fluent_community'),
112 'inline_tip' => __('Keep empty to auto generated password', 'fluent_community')
113 ],
114 [
115 'require_list' => false,
116 'key' => 'enableAutoLogin',
117 'label' => __('Auto Login & Password Reset Email (For New Users Only)', 'fluent-community'),
118 'checkbox_label' => __('Allow the user login automatically after Form submission', 'fluent-community'),
119 'component' => 'checkbox-single',
120 ],
121 [
122 'require_list' => false,
123 'key' => 'sendEmailToNewUser',
124 'checkbox_label' => __('Send default WordPress welcome email to user after registration',
125 'fluent-community'),
126 'component' => 'checkbox-single',
127 ],
128 [
129 'require_list' => false,
130 'key' => 'conditionals',
131 'label' => __('Conditional Logics', 'fluent-community'),
132 'tips' => __('Allow this integration conditionally based on your submission values',
133 'fluentformpro'),
134 'component' => 'conditional_block'
135 ],
136 ],
137 'button_require_list' => false,
138 'integration_title' => $this->title
139 ];
140
141
142 return $fieldSettings;
143 }
144
145 public function getAllSpacesCourses()
146 {
147 $allSpaces = \FluentCommunity\App\Models\BaseSpace::query()->withoutGlobalScopes()
148 ->whereIn('type', ['course', 'community'])
149 ->select(['id', 'title', 'type'])
150 ->orderBy('title', 'ASC')
151 ->get();
152
153 $formattedSpaces = [];
154 foreach ($allSpaces as $space) {
155 $type = $space->type == 'course' ? __('Course', 'fluent-comunity') : __('Space', 'fluent-community');
156 $formattedSpaces[$space->id] = "{$space->title} ({$type})";
157 }
158 return $formattedSpaces;
159 }
160
161 public function communityMapFields()
162 {
163 $fields = [
164 [
165 'key' => 'email',
166 'label' => __('Email Address', 'fluent_community'),
167 'input_options' => 'emails',
168 'required' => true,
169 ]
170 ];
171
172 return apply_filters('fluentform/user_registration_map_fields', $fields);
173 }
174
175 public function pushIntegration($integrations, $formId)
176 {
177 $integrations[$this->integrationKey] = [
178 'category' => 'wp_core',
179 'disable_global_settings' => 'yes',
180 'logo' => '',
181 'title' => sprintf(__(' %s Integration', 'fluent-community'), 'Fluent Community'),
182 'is_active' => $this->isConfigured()
183 ];
184
185 return $integrations;
186 }
187
188 public function isConfigured()
189 {
190 return true;
191 }
192
193 public function isEnabled()
194 {
195 return true;
196 }
197
198 public function notify($feed, $formData, $entry, $form)
199 {
200 $processedValues = Arr::get($feed, 'processedValues', []);
201 $spaceIds = Arr::get($processedValues, 'space_ids', []);
202
203 if (!$spaceIds) {
204 return $this->addLog(
205 $feed['settings']['name'],
206 'failed',
207 'No Course/Space selected',
208 $form->id,
209 $entry->id,
210 $this->integrationKey
211 );
212 }
213
214 $spaces = BaseSpace::query()->whereIn('id', $spaceIds)->get();
215
216 if ($spaces->isEmpty()) {
217 return $this->addLog(
218 $feed['settings']['name'],
219 'failed',
220 'No Course/Space found',
221 $form->id,
222 $entry->id,
223 $this->integrationKey
224 );
225 }
226
227 $emailKey = Arr::get($processedValues, 'email');
228 $userFullName = Arr::get($processedValues, 'full_name');
229 $password = Arr::get($processedValues, 'password');
230
231 $isNewUser = false;
232
233 if ($entry->user_id) {
234 $userId = $entry->user_id;
235 } else {
236 $emailAddress = Arr::get($formData, $emailKey);
237 $existingUser = get_user_by('ID', $emailAddress);
238
239 if ($existingUser) {
240 $userId = $existingUser->ID;
241 } else {
242 $userId = $this->registerUser([
243 'email' => $emailAddress,
244 'full_name' => $userFullName,
245 'password' => $password
246 ]);
247 if (is_wp_error($userId)) {
248 return $this->addLog(
249 $feed['settings']['name'],
250 'failed',
251 'Failed to create user. Reason: ' . $userId->get_error_message(),
252 $form->id,
253 $entry->id,
254 $this->integrationKey
255 );
256 return false;
257 }
258
259 $isNewUser = true;
260
261 if (Arr::isTrue($processedValues, 'sendEmailToNewUser')) {
262 // This will send an email with password setup link
263 \wp_new_user_notification($userId, null, 'user');
264 }
265 }
266 }
267
268 $successSpaces = [];
269
270 foreach ($spaces as $space) {
271 if (\FluentCommunity\App\Services\Helper::addToSpace($space, $userId)) {
272 $successSpaces[] = $space->title;
273 }
274 }
275
276 if ($isNewUser && Arr::isTrue($processedValues, 'enableAutoLogin')) {
277 $this->maybeLogin($userId, $entry);
278 }
279
280 return $this->addLog(
281 $feed['settings']['name'],
282 'success',
283 'Joined Course/Spaces: ' . implode(',', $successSpaces),
284 $form->id,
285 $entry->id,
286 $this->integrationKey
287 );
288 }
289
290 /**
291 * Create a new user
292 *
293 * @param array $feed
294 * @param array $formData
295 * @param object $entry
296 * @param object $form
297 * @param string $integrationKey
298 *
299 * @return int|\WP_Error
300 */
301 protected function registerUser($userData)
302 {
303 $email = Arr::get($userData, 'email');
304 if (!is_email($email)) {
305 return new \WP_Error('invalid_email', 'Invalid email address');
306 }
307
308 $password = trim(Arr::get($userData, 'password'));
309 if (!$password) {
310 $password = wp_generate_password(8);
311 }
312
313 $nameArray = explode(' ', (string)Arr::get($userData, 'full_name'));
314
315 $firstName = array_shift($nameArray);
316 $lastName = implode(' ', $nameArray);
317
318 $userName = ProfileHelper::createUserNameFromStrings($email, array_filter([
319 $firstName,
320 $lastName
321 ]));
322
323 $userData = [
324 'role' => get_option('default_role', 'subscriber'),
325 'user_email' => $email,
326 'user_login' => $userName,
327 'user_pass' => $password,
328 'first_name' => $firstName,
329 'last_name' => $lastName
330 ];
331
332 $userId = wp_insert_user($userData);
333
334 if (is_wp_error($userId)) {
335 return $userId;
336 }
337
338 return $userId;
339 }
340
341 protected function addLog($title, $status, $description, $formId, $entryId, $integrationKey)
342 {
343 $logData = [
344 'title' => $title,
345 'status' => $status,
346 'description' => $description,
347 'parent_source_id' => $formId,
348 'source_id' => $entryId,
349 'component' => $integrationKey,
350 'source_type' => 'submission_item'
351 ];
352
353 do_action('fluentform/log_data', $logData);
354
355 return true;
356 }
357
358 protected function maybeLogin($userId, $entry = false)
359 {
360 // check if it's payment success page
361 // or direct url
362 if (isset($_REQUEST['fluentform_payment_api_notify']) && $entry) {
363 // This payment IPN request so let's keep a reference for real request
364 Helper::setSubmissionMeta($entry->id, '_make_auto_login', $userId, $entry->form_id);
365 return;
366 }
367
368 wp_clear_auth_cookie();
369 wp_set_current_user($userId);
370 wp_set_auth_cookie($userId);
371 }
372 }
373