| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | namespace FluentCommunity\App\Services; |
| 4 | 4 | |
| 5 | 5 | use FluentCommunity\App\Functions\Utility; |
| 6 | 6 | use FluentCommunity\Framework\Support\Arr; |
| 7 | +use FluentCommunity\Modules\Auth\AuthHelper; | |
| 7 | 8 | |
| 8 | 9 | class AuthenticationService |
| 9 | 10 | { |
| 10 | 11 | public static function getAuthForm($view = 'login') |
| @@ -15,83 +16,96 @@ | ||
| 15 | 16 | } |
| 16 | 17 | |
| 17 | 18 | public static function getAuthSettings() |
| 18 | 19 | { |
| 19 | - return Utility::getFromCache('auth_settings', function () { | |
| 20 | + $siteSettings = Helper::generalSettings(); | |
| 20 | 21 | |
| 21 | - $siteSettings = Helper::generalSettings(); | |
| 22 | + $siteLogo = Arr::get($siteSettings, 'logo'); | |
| 23 | + $siteTitle = Arr::get($siteSettings, 'site_title'); | |
| 22 | 24 | |
| 23 | - $siteLogo = Arr::get($siteSettings, 'logo'); | |
| 24 | - $siteTitle = Arr::get($siteSettings, 'site_title'); | |
| 25 | - | |
| 26 | - $defaults = [ | |
| 27 | - 'login' => [ | |
| 28 | - 'banner' => [ | |
| 29 | - 'hidden' => false, | |
| 30 | - 'type' => 'banner', | |
| 31 | - 'position' => 'left', | |
| 32 | - 'logo' => $siteLogo, | |
| 33 | - 'title' => 'Welcome to ' . $siteTitle, | |
| 34 | - 'description' => 'Join our community and start your journey to success', | |
| 35 | - 'title_color' => '#19283a', | |
| 36 | - 'text_color' => '#525866', | |
| 37 | - 'background_image' => '', | |
| 38 | - 'background_color' => '#F5F7FA' | |
| 39 | - ], | |
| 40 | - 'form' => [ | |
| 41 | - 'type' => 'form', | |
| 42 | - 'position' => 'right', | |
| 43 | - 'title' => 'Login to ' . $siteTitle, | |
| 44 | - 'description' => 'Enter your email and password to login', | |
| 45 | - 'title_color' => '#19283a', | |
| 46 | - 'text_color' => '#525866', | |
| 47 | - 'button_label' => 'Login', | |
| 48 | - 'button_color' => '#2B2E33', | |
| 49 | - 'button_label_color' => '#ffffff', | |
| 50 | - 'background_image' => '', | |
| 51 | - 'background_color' => '#ffffff' | |
| 52 | - ] | |
| 25 | + $defaults = [ | |
| 26 | + 'login' => [ | |
| 27 | + 'banner' => [ | |
| 28 | + 'hidden' => false, | |
| 29 | + 'type' => 'banner', | |
| 30 | + 'position' => 'left', | |
| 31 | + 'logo' => $siteLogo, | |
| 32 | + /* translators: %s is replaced by the title of the site */ | |
| 33 | + 'title' => sprintf(__('Welcome to %s', 'fluent-community'), $siteTitle), | |
| 34 | + 'description' => __('Join our community and start your journey to success', 'fluent-community'), | |
| 35 | + 'title_color' => '#19283a', | |
| 36 | + 'text_color' => '#525866', | |
| 37 | + 'background_image' => '', | |
| 38 | + 'background_color' => '#F5F7FA' | |
| 53 | 39 | ], |
| 54 | - 'signup' => [ | |
| 55 | - 'banner' => [ | |
| 56 | - 'hidden' => false, | |
| 57 | - 'type' => 'banner', | |
| 58 | - 'position' => 'left', | |
| 59 | - 'logo' => $siteLogo, | |
| 60 | - 'title' => 'Welcome to ' . $siteTitle, | |
| 61 | - 'description' => 'Join our community and start your journey to success', | |
| 62 | - 'title_color' => '#19283a', | |
| 63 | - 'text_color' => '#525866', | |
| 64 | - 'background_image' => '', | |
| 65 | - 'background_color' => '#F5F7FA', | |
| 66 | - ], | |
| 67 | - 'form' => [ | |
| 68 | - 'type' => 'form', | |
| 69 | - 'position' => 'right', | |
| 70 | - 'title' => 'Sign Up to ' . $siteTitle, | |
| 71 | - 'description' => 'Create an account to get started', | |
| 72 | - 'button_label' => 'Sign up', | |
| 73 | - 'title_color' => '#19283a', | |
| 74 | - 'text_color' => '#525866', | |
| 75 | - 'button_color' => '#2B2E33', | |
| 76 | - 'button_label_color' => '#ffffff', | |
| 77 | - 'background_image' => '', | |
| 78 | - 'background_color' => '#ffffff', | |
| 79 | - ] | |
| 40 | + 'form' => [ | |
| 41 | + 'type' => 'form', | |
| 42 | + 'position' => 'right', | |
| 43 | + /* translators: %s is replaced by the title of the site */ | |
| 44 | + 'title' => sprintf(__('Login to %s', 'fluent-community'), $siteTitle), | |
| 45 | + 'description' => __('Enter your email and password to login', 'fluent-community'), | |
| 46 | + 'title_color' => '#19283a', | |
| 47 | + 'text_color' => '#525866', | |
| 48 | + 'button_label' => __('Login', 'fluent-community'), | |
| 49 | + 'button_color' => '#2B2E33', | |
| 50 | + 'button_label_color' => '#ffffff', | |
| 51 | + 'background_image' => '', | |
| 52 | + 'background_color' => '#ffffff' | |
| 80 | 53 | ] |
| 81 | - ]; | |
| 54 | + ], | |
| 55 | + 'signup' => [ | |
| 56 | + 'banner' => [ | |
| 57 | + 'hidden' => false, | |
| 58 | + 'type' => 'banner', | |
| 59 | + 'position' => 'left', | |
| 60 | + 'logo' => $siteLogo, | |
| 61 | + /* translators: %s is replaced by the title of the site */ | |
| 62 | + 'title' => sprintf(__('Welcome to %s', 'fluent-community'), $siteTitle), | |
| 63 | + 'description' => __('Join our community and start your journey to success', 'fluent-community'), | |
| 64 | + 'title_color' => '#19283a', | |
| 65 | + 'text_color' => '#525866', | |
| 66 | + 'background_image' => '', | |
| 67 | + 'background_color' => '#F5F7FA', | |
| 68 | + ], | |
| 69 | + 'form' => [ | |
| 70 | + 'type' => 'form', | |
| 71 | + 'position' => 'right', | |
| 72 | + /* translators: %s is replaced by the title of the site */ | |
| 73 | + 'title' => sprintf(__('Sign Up to %s', 'fluent-community'), $siteTitle), | |
| 74 | + 'description' => __('Create an account to get started', 'fluent-community'), | |
| 75 | + 'button_label' => __('Sign up', 'fluent-community'), | |
| 76 | + 'terms_label' => '', | |
| 77 | + 'title_color' => '#19283a', | |
| 78 | + 'text_color' => '#525866', | |
| 79 | + 'button_color' => '#2B2E33', | |
| 80 | + 'button_label_color' => '#ffffff', | |
| 81 | + 'background_image' => '', | |
| 82 | + 'background_color' => '#ffffff', | |
| 83 | + ] | |
| 84 | + ] | |
| 85 | + ]; | |
| 82 | 86 | |
| 83 | - $settings = Utility::getOption('auth_settings', []); | |
| 87 | + $settings = Utility::getOption('auth_settings', []); | |
| 84 | 88 | |
| 85 | - return wp_parse_args($settings, $defaults); | |
| 89 | + $authSettings = wp_parse_args($settings, $defaults); | |
| 86 | 90 | |
| 87 | - }, WEEK_IN_SECONDS); | |
| 91 | + if (!Arr::get($authSettings, 'signup.form.fields.terms')) { | |
| 92 | + $termsText = AuthHelper::getTermsText(); | |
| 93 | + $authSettings['signup']['form']['fields']['terms'] = [ | |
| 94 | + 'disabled' => false, | |
| 95 | + 'required' => true, | |
| 96 | + 'type' => 'inline_checkbox', | |
| 97 | + 'label' => Helper::htmlToMd($termsText), | |
| 98 | + 'inline_label' => $termsText | |
| 99 | + ]; | |
| 100 | + } | |
| 101 | + | |
| 102 | + return apply_filters('fluent_community/auth/settings', $authSettings); | |
| 88 | 103 | } |
| 89 | 104 | |
| 90 | 105 | public static function getFormattedAuthSettings($view = 'login') |
| 91 | 106 | { |
| 92 | 107 | $authSettings = self::getAuthSettings(); |
| 93 | - | |
| 94 | 108 | $settings = Arr::get($authSettings, $view, []); |
| 95 | 109 | |
| 96 | 110 | foreach ($settings as &$setting) { |
| 97 | 111 | if (Arr::get($setting, 'description_rendered')) { |
| @@ -104,35 +118,77 @@ | ||
| 104 | 118 | } |
| 105 | 119 | |
| 106 | 120 | public static function formatAuthSettings($settingFields) |
| 107 | 121 | { |
| 108 | - $textFields = ['type', 'title', 'button_label', 'position']; | |
| 109 | - $colorFields = ['title_color', 'text_color', 'button_color', 'button_label_color', 'background_color']; | |
| 110 | - $urlFields = ['logo', 'background_image']; | |
| 122 | + $currentSettings = self::getAuthSettings(); | |
| 111 | 123 | |
| 124 | + $textFields = ['type', 'title', 'button_label', 'position', 'title_color', 'text_color', 'button_color', 'button_label_color', 'background_color']; | |
| 125 | + $mediaFields = ['logo', 'background_image']; | |
| 126 | + | |
| 112 | 127 | $formattedFields = []; |
| 113 | - | |
| 114 | 128 | foreach ($settingFields as $section => $settings) { |
| 115 | 129 | foreach ($settings as $key => $setting) { |
| 130 | + if (!is_array($setting)) { | |
| 131 | + continue; | |
| 132 | + } | |
| 116 | 133 | $textValues = array_map('sanitize_text_field', Arr::only($setting, $textFields)); |
| 117 | - $colorValues = array_map('sanitize_hex_color', Arr::only($setting, $colorFields)); | |
| 118 | - $urlValues = array_map('sanitize_url', Arr::only($setting, $urlFields)); | |
| 134 | + $mediaUrls = array_map('sanitize_url', Arr::only($setting, $mediaFields)); | |
| 119 | 135 | |
| 120 | - $formattedField = array_merge($textValues, $colorValues, $urlValues); | |
| 136 | + $mediaUrls = self::handleMediaUrls($mediaUrls, $currentSettings[$section][$key], $section); | |
| 121 | 137 | |
| 138 | + $formattedField = array_merge($textValues, $mediaUrls); | |
| 139 | + | |
| 140 | + $formattedField['description'] = wp_kses_post(wp_unslash(Arr::get($setting, 'description'))); | |
| 141 | + | |
| 142 | + $formattedField['description_rendered'] = wp_kses_post(FeedsHelper::mdToHtml($formattedField['description'])); | |
| 143 | + | |
| 122 | 144 | $formattedField['hidden'] = Arr::isTrue($setting, 'hidden'); |
| 123 | 145 | |
| 124 | - $formattedField['description'] = wp_kses_post(Arr::get($setting, 'description')); | |
| 146 | + $formattedFields[$section][$key] = $formattedField; | |
| 147 | + } | |
| 148 | + } | |
| 125 | 149 | |
| 126 | - $formattedField['description_rendered'] = FeedsHelper::mdToHtml($formattedField['description']); | |
| 150 | + if (Arr::get($settingFields, 'signup.form.fields.terms')) { | |
| 151 | + $termsField = Arr::get($settingFields, 'signup.form.fields.terms'); | |
| 152 | + $termsField['disabled'] = Arr::isTrue($termsField, 'disabled'); | |
| 153 | + $termsField['required'] = Arr::isTrue($termsField, 'required'); | |
| 154 | + $termsField['label'] = wp_kses_post(wp_unslash($termsField['label'])); | |
| 155 | + $termsField['inline_label'] = wp_kses_post(FeedsHelper::mdToHtml(wp_unslash($termsField['label']))); | |
| 156 | + $formattedFields['signup']['form']['fields']['terms'] = $termsField; | |
| 157 | + } | |
| 127 | 158 | |
| 128 | - if (Arr::has($setting, 'fields')) { | |
| 129 | - $formattedField['fields'] = array_map('sanitize_text_field', $setting['fields']); | |
| 159 | + return $formattedFields; | |
| 160 | + } | |
| 161 | + | |
| 162 | + public static function getCustomSignupPageUrl() | |
| 163 | + { | |
| 164 | + $portalSettings = Helper::generalSettings(); | |
| 165 | + | |
| 166 | + return Arr::get($portalSettings, 'custom_signup_url'); | |
| 167 | + } | |
| 168 | + | |
| 169 | + protected static function handleMediaUrls($mediaUrls, $currentSetting, $section) | |
| 170 | + { | |
| 171 | + foreach ($mediaUrls as $key => $url) { | |
| 172 | + $currentImgUrl = Arr::get($currentSetting, $key); | |
| 173 | + if ($url) { | |
| 174 | + $media = Helper::getMediaFromUrl($url); | |
| 175 | + if (!$media || $media->is_active) { | |
| 176 | + $mediaUrls[$key] = $currentImgUrl; | |
| 177 | + continue; | |
| 130 | 178 | } |
| 131 | 179 | |
| 132 | - $formattedFields[$section][$key] = $formattedField; | |
| 180 | + $mediaUrls[$key] = $media->public_url; | |
| 181 | + | |
| 182 | + $media->update([ | |
| 183 | + 'is_active' => true, | |
| 184 | + 'user_id' => get_current_user_id(), | |
| 185 | + 'sub_object_id' => null, | |
| 186 | + 'object_source' => 'auth_' . $section . '_' . $key | |
| 187 | + ]); | |
| 133 | 188 | } |
| 134 | 189 | } |
| 135 | 190 | |
| 136 | - return $formattedFields; | |
| 191 | + return $mediaUrls; | |
| 137 | 192 | } |
| 193 | + | |
| 138 | 194 | } |