| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | namespace FluentSupport\App\Hooks\Handlers; |
| 4 | 4 | |
| 5 | 5 | use FluentSupport\App\App; |
| 6 | 6 | use FluentSupport\App\Modules\PermissionManager; |
| 7 | +use FluentSupport\App\Services\FluentAuthBridge; | |
| 7 | 8 | use FluentSupport\App\Services\Helper; |
| 8 | 9 | use FluentSupport\App\Services\Includes\CountryNames; |
| 9 | 10 | use FluentSupport\App\Vite; |
| 10 | 11 | use FluentSupport\Framework\Support\Arr; |
| @@ -21,12 +22,13 @@ | ||
| 21 | 22 | add_shortcode('fluent_support_signup', array($this, 'registrationForm')); |
| 22 | 23 | add_shortcode('fluent_support_auth', array($this, 'authForm')); |
| 23 | 24 | add_shortcode('fluent_support_reset_password', array($this, 'restPasswordForm')); |
| 24 | 25 | add_action('wp_ajax_fluent_support_renew_rest_nonce', [$this, 'maybeRenewNonce']); |
| 26 | + | |
| 27 | + FluentAuthBridge::register(); | |
| 25 | 28 | } |
| 26 | 29 | |
| 27 | 30 | /** |
| 28 | - * loginForm will generate html for login form | |
| 29 | 31 | * @param $attributes |
| 30 | 32 | * @return string |
| 31 | 33 | */ |
| 32 | 34 | public function loginForm($attributes) |
| @@ -45,11 +47,10 @@ | ||
| 45 | 47 | |
| 46 | 48 | $this->handleAlreadyLoggedIn($attributes); |
| 47 | 49 | |
| 48 | 50 | if ($this->authProvider() == 'fluent_auth') { |
| 49 | - // Will be handled by FluentAuth plugin | |
| 50 | 51 | $attributes['redirect_to'] = $redirect; |
| 51 | - return (new \FluentAuth\App\Hooks\Handlers\CustomAuthHandler())->loginForm($attributes); | |
| 52 | + return $this->renderFluentAuthForms($attributes); | |
| 52 | 53 | } |
| 53 | 54 | |
| 54 | 55 | $this->loadAssets(); |
| 55 | 56 | |
| @@ -103,9 +104,8 @@ | ||
| 103 | 104 | return $return; |
| 104 | 105 | } |
| 105 | 106 | |
| 106 | 107 | /** |
| 107 | - * registrationForm method will generate html for sign up form | |
| 108 | 108 | * @param $attributes |
| 109 | 109 | * @return string |
| 110 | 110 | */ |
| 111 | 111 | public function registrationForm($attributes) |
| @@ -116,13 +116,23 @@ | ||
| 116 | 116 | |
| 117 | 117 | $attributes = $this->getShortcodes($attributes); |
| 118 | 118 | $this->handleAlreadyLoggedIn($attributes); |
| 119 | 119 | |
| 120 | + if (!Helper::isSignupEnabled()) { | |
| 121 | + if ($attributes['hide'] == 'true') { | |
| 122 | + return ''; | |
| 123 | + } | |
| 124 | + | |
| 125 | + return '<p>' . __('User registration is not enabled on this site.', 'fluent-support') . '</p>'; | |
| 126 | + } | |
| 127 | + | |
| 120 | 128 | if ($this->authProvider() == 'fluent_auth') { |
| 121 | - return (new \FluentAuth\App\Hooks\Handlers\CustomAuthHandler())->registrationForm($attributes); | |
| 129 | + return FluentAuthBridge::render(function () use ($attributes) { | |
| 130 | + return (new \FluentAuth\App\Hooks\Handlers\CustomAuthHandler())->registrationForm($attributes); | |
| 131 | + }, $attributes['redirect_to']); | |
| 122 | 132 | } |
| 123 | 133 | |
| 124 | - $customFieldsKey = apply_filters('fluent_support/custom_registration_form_fields_key', Helper::getBusinessSettings('custom_registration_form_field')); | |
| 134 | + $customFieldsKey = Helper::getCustomRegistrationFieldKeys(); | |
| 125 | 135 | |
| 126 | 136 | if (!empty($customFieldsKey)) { |
| 127 | 137 | add_filter('fluent_support/registration_form_fields', function($fields) use ($customFieldsKey) { |
| 128 | 138 | return $this->addCustomFieldsToRegistrationForm($fields,$customFieldsKey); |
| @@ -136,9 +146,8 @@ | ||
| 136 | 146 | |
| 137 | 147 | return $this->buildRegistrationForm($registrationFields, $hide, $attributes); |
| 138 | 148 | } |
| 139 | 149 | |
| 140 | - // This method `buildRegistrationForm` will generate html for sign up form | |
| 141 | 150 | private function buildRegistrationForm($registrationFields, $hide, $attributes) |
| 142 | 151 | { |
| 143 | 152 | $registrationForm = '<div class="fst_registration_wrapper ' . $hide . '"><form id="fstRegistrationForm" class="fs_registration_form" method="post" name="fs_registration_form">'; |
| 144 | 153 | |
| @@ -175,9 +184,11 @@ | ||
| 175 | 184 | |
| 176 | 185 | $attributes = $this->getShortcodes($attributes); |
| 177 | 186 | |
| 178 | 187 | if ($this->authProvider() == 'fluent_auth') { |
| 179 | - return (new \FluentAuth\App\Hooks\Handlers\CustomAuthHandler())->restPasswordForm($attributes); | |
| 188 | + return FluentAuthBridge::render(function () use ($attributes) { | |
| 189 | + return (new \FluentAuth\App\Hooks\Handlers\CustomAuthHandler())->restPasswordForm($attributes); | |
| 190 | + }, $attributes['redirect_to']); | |
| 180 | 191 | } |
| 181 | 192 | |
| 182 | 193 | $this->handleAlreadyLoggedIn($attributes); |
| 183 | 194 | |
| @@ -188,9 +199,8 @@ | ||
| 188 | 199 | |
| 189 | 200 | return $this->buildResetPassForm($resetPasswordFields, $hide, $attributes); |
| 190 | 201 | } |
| 191 | 202 | |
| 192 | - // This method `buildResetPassForm` will generate html for password reset form | |
| 193 | 203 | private function buildResetPassForm($resetPasswordFields, $hide, $attributes) |
| 194 | 204 | { |
| 195 | 205 | $restePasswordForm = '<div class="fst_reset_pass_wrapper ' . $hide . '"><form id="fstResetPasswordForm" class="fs_reset_pass_form" method="post" name="fs_reset_pass_form">'; |
| 196 | 206 | |
| @@ -209,9 +219,10 @@ | ||
| 209 | 219 | return $restePasswordForm; |
| 210 | 220 | } |
| 211 | 221 | |
| 212 | 222 | /** |
| 213 | - * authForm will render the login form html | |
| 223 | + * Login form with signup and reset links, or FluentAuth's form when it is the provider. | |
| 224 | + * | |
| 214 | 225 | * @param $attributes |
| 215 | 226 | * @return string |
| 216 | 227 | */ |
| 217 | 228 | public function authForm($attributes) |
| @@ -223,9 +234,9 @@ | ||
| 223 | 234 | |
| 224 | 235 | $attributes = $this->getShortcodes($attributes, true); |
| 225 | 236 | |
| 226 | 237 | if ($this->authProvider() == 'fluent_auth') { |
| 227 | - return (new \FluentAuth\App\Hooks\Handlers\CustomAuthHandler())->authForm($attributes); | |
| 238 | + return $this->renderFluentAuthForms($attributes); | |
| 228 | 239 | } |
| 229 | 240 | |
| 230 | 241 | return $this->loginForm($attributes); |
| 231 | 242 | } |
| @@ -230,9 +241,37 @@ | ||
| 230 | 241 | return $this->loginForm($attributes); |
| 231 | 242 | } |
| 232 | 243 | |
| 233 | 244 | /** |
| 234 | - * renderField method will generate html for a field | |
| 245 | + * FluentAuth's login form, plus its signup and reset forms (hidden) when the shortcode | |
| 246 | + * shows them. Its "Create an Account" and "Reset Password" links open a form that must | |
| 247 | + * already be on the page, and its own authForm() always adds reset, so we pick the | |
| 248 | + * forms here. FluentAuth renders and processes each one. | |
| 249 | + * | |
| 250 | + * @param array $attributes | |
| 251 | + * @return string | |
| 252 | + */ | |
| 253 | + private function renderFluentAuthForms($attributes) | |
| 254 | + { | |
| 255 | + return FluentAuthBridge::render(function () use ($attributes) { | |
| 256 | + $handler = new \FluentAuth\App\Hooks\Handlers\CustomAuthHandler(); | |
| 257 | + $hidden = array_merge($attributes, ['hide' => 'true']); | |
| 258 | + | |
| 259 | + $html = '<div class="fls_auth_wrapper">' . $handler->loginForm($attributes); | |
| 260 | + | |
| 261 | + if ($attributes['show-signup'] == 'true') { | |
| 262 | + $html .= $handler->registrationForm($hidden); | |
| 263 | + } | |
| 264 | + | |
| 265 | + if ($attributes['show-reset-password'] == 'true') { | |
| 266 | + $html .= $handler->restPasswordForm($hidden); | |
| 267 | + } | |
| 268 | + | |
| 269 | + return $html . '</div>'; | |
| 270 | + }, $attributes['redirect_to']); | |
| 271 | + } | |
| 272 | + | |
| 273 | + /** | |
| 235 | 274 | * @param $fieldName |
| 236 | 275 | * @param $field |
| 237 | 276 | * @return string |
| 238 | 277 | */ |
| @@ -248,9 +287,9 @@ | ||
| 248 | 287 | $html .= '<div class="fst_field_label ' . $isRequired . '"><label for="' . Arr::get($field, 'id') . '">' . $label . '</label></div>'; |
| 249 | 288 | } |
| 250 | 289 | |
| 251 | 290 | $textTypes = ['text', 'email', 'password', 'number', 'date']; |
| 252 | - $selectTypes = ['select']; // Add 'select' type for dropdowns | |
| 291 | + $selectTypes = ['select']; | |
| 253 | 292 | |
| 254 | 293 | if (in_array($fieldType, $textTypes)) { |
| 255 | 294 | $html .= $this->renderTextInput($fieldName, $field); |
| 256 | 295 | } elseif (in_array($fieldType, $selectTypes)) { |
| @@ -283,14 +322,11 @@ | ||
| 283 | 322 | return '<div class="fs_input_wrap"><input ' . $atts . ' /></div>'; |
| 284 | 323 | } |
| 285 | 324 | |
| 286 | 325 | /** |
| 287 | - * Render a select input field based on the provided field configuration. | |
| 288 | - * | |
| 289 | - * @param string $fieldName The name of the select input field. | |
| 290 | - * @param array $field The field configuration containing options and attributes. | |
| 291 | - * | |
| 292 | - * @return string The rendered select input field HTML. | |
| 326 | + * @param string $fieldName | |
| 327 | + * @param array $field Field config with options and attributes. | |
| 328 | + * @return string | |
| 293 | 329 | */ |
| 294 | 330 | private function renderSelectInput($fieldName, $field) |
| 295 | 331 | { |
| 296 | 332 | $choices = Arr::get($field, 'options', []); |
| @@ -324,9 +360,8 @@ | ||
| 324 | 360 | return $atts; |
| 325 | 361 | } |
| 326 | 362 | |
| 327 | 363 | /** |
| 328 | - * getSignupFields method will return the list of fields that will be used for sign up form | |
| 329 | 364 | * @return mixed |
| 330 | 365 | */ |
| 331 | 366 | public static function getSignupFields() |
| 332 | 367 | { |
| @@ -484,9 +519,9 @@ | ||
| 484 | 519 | |
| 485 | 520 | protected function getShortcodes($attributes, $isAuthForm = false) |
| 486 | 521 | { |
| 487 | 522 | /* |
| 488 | - * Filter shortcode behavior for agent | |
| 523 | + * Filter the default attributes of the auth shortcodes. | |
| 489 | 524 | * |
| 490 | 525 | * @since v1.0.0 |
| 491 | 526 | * |
| 492 | 527 | * @param array $shortCodeDefaults |
| @@ -500,8 +535,13 @@ | ||
| 500 | 535 | ]); |
| 501 | 536 | |
| 502 | 537 | $attributes = shortcode_atts($shortCodeDefaults, $attributes); |
| 503 | 538 | |
| 539 | + // No signup form or link while the site does not allow registration. | |
| 540 | + if (!Helper::isSignupEnabled()) { | |
| 541 | + $attributes['show-signup'] = 'false'; | |
| 542 | + } | |
| 543 | + | |
| 504 | 544 | if (isset($attributes['redirect-to'])) { |
| 505 | 545 | $attributes['redirect-to'] = esc_url_raw($attributes['redirect-to']); |
| 506 | 546 | $attributes['redirect_to'] = $attributes['redirect-to']; |
| 507 | 547 | } |
| @@ -544,9 +584,8 @@ | ||
| 544 | 584 | } |
| 545 | 585 | |
| 546 | 586 | wp_enqueue_script('fluent_support_login_helper', Vite::getEnqueuePath('portal/js/login_helper.js'), [], FLUENT_SUPPORT_VERSION); |
| 547 | 587 | |
| 548 | - //Get Recaptcha settings and enqueue recaptcha script | |
| 549 | 588 | $reCaptchaSettingsData = Meta::where('object_type', '_fs_recaptcha_settings')->first(); |
| 550 | 589 | $reCaptchaData = ($reCaptchaSettingsData) ? Helper::safeUnserialize($reCaptchaSettingsData->value, []) : ''; |
| 551 | 590 | |
| 552 | 591 | if (!empty($reCaptchaData) && isset($reCaptchaData['is_enabled']) && $reCaptchaData['is_enabled'] == "true") { |
| @@ -580,9 +619,9 @@ | ||
| 580 | 619 | public function maybeRenewNonce() |
| 581 | 620 | { |
| 582 | 621 | if (!PermissionManager::currentUserPermissions()) { |
| 583 | 622 | wp_send_json([ |
| 584 | - 'error' => 'You do not have permission to do this' | |
| 623 | + 'error' => __('You do not have permission to do this.', 'fluent-support') | |
| 585 | 624 | ], 403); |
| 586 | 625 | } |
| 587 | 626 | |
| 588 | 627 | wp_send_json([ |