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