| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Services; |
| 4 |
|
| 5 |
use FluentCommunity\App\Functions\Utility; |
| 6 |
use FluentCommunity\Framework\Support\Arr; |
| 7 |
use FluentCommunity\Modules\Auth\AuthHelper; |
| 8 |
|
| 9 |
class AuthenticationService |
| 10 |
{ |
| 11 |
public static function getAuthForm($view = 'login') |
| 12 |
{ |
| 13 |
$settings = self::getAuthSettings(); |
| 14 |
|
| 15 |
return Arr::get($settings, $view, []); |
| 16 |
} |
| 17 |
|
| 18 |
public static function getAuthSettings() |
| 19 |
{ |
| 20 |
$siteSettings = Helper::generalSettings(); |
| 21 |
|
| 22 |
$siteLogo = Arr::get($siteSettings, 'logo'); |
| 23 |
$siteTitle = Arr::get($siteSettings, 'site_title'); |
| 24 |
|
| 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' |
| 39 |
], |
| 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' |
| 53 |
] |
| 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 |
]; |
| 86 |
|
| 87 |
$settings = Utility::getOption('auth_settings', []); |
| 88 |
|
| 89 |
$authSettings = wp_parse_args($settings, $defaults); |
| 90 |
|
| 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); |
| 103 |
} |
| 104 |
|
| 105 |
public static function getFormattedAuthSettings($view = 'login') |
| 106 |
{ |
| 107 |
$authSettings = self::getAuthSettings(); |
| 108 |
$settings = Arr::get($authSettings, $view, []); |
| 109 |
|
| 110 |
foreach ($settings as &$setting) { |
| 111 |
if (Arr::get($setting, 'description_rendered')) { |
| 112 |
$setting['description'] = $setting['description_rendered']; |
| 113 |
unset($setting['description_rendered']); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return $settings; |
| 118 |
} |
| 119 |
|
| 120 |
public static function formatAuthSettings($settingFields) |
| 121 |
{ |
| 122 |
$currentSettings = self::getAuthSettings(); |
| 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 |
|
| 127 |
$formattedFields = []; |
| 128 |
foreach ($settingFields as $section => $settings) { |
| 129 |
foreach ($settings as $key => $setting) { |
| 130 |
if (!is_array($setting)) { |
| 131 |
continue; |
| 132 |
} |
| 133 |
$textValues = array_map('sanitize_text_field', Arr::only($setting, $textFields)); |
| 134 |
$mediaUrls = array_map('sanitize_url', Arr::only($setting, $mediaFields)); |
| 135 |
|
| 136 |
$mediaUrls = self::handleMediaUrls($mediaUrls, $currentSettings[$section][$key], $section); |
| 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 |
|
| 144 |
$formattedField['hidden'] = Arr::isTrue($setting, 'hidden'); |
| 145 |
|
| 146 |
$formattedFields[$section][$key] = $formattedField; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 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 |
} |
| 158 |
|
| 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; |
| 178 |
} |
| 179 |
|
| 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 |
]); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
return $mediaUrls; |
| 192 |
} |
| 193 |
|
| 194 |
} |
| 195 |
|