| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCommunity\App\Functions\Utility; |
| 6 |
use FluentCommunity\App\Models\User; |
| 7 |
use FluentCommunity\App\Services\CustomSanitizer; |
| 8 |
use FluentCommunity\App\Services\FeedsHelper; |
| 9 |
use FluentCommunity\App\Services\Helper; |
| 10 |
use FluentCommunity\App\Services\OnboardingService; |
| 11 |
use FluentCommunity\Framework\Http\Request\Request; |
| 12 |
use FluentCommunity\Framework\Support\Arr; |
| 13 |
|
| 14 |
class AdminController extends Controller |
| 15 |
{ |
| 16 |
public function getGeneralSettings(Request $request) |
| 17 |
{ |
| 18 |
$settings = Helper::generalSettings(false); |
| 19 |
|
| 20 |
$userRoles = wp_roles()->roles; |
| 21 |
|
| 22 |
$formatedRoles = []; |
| 23 |
foreach ($userRoles as $role => $roleData) { |
| 24 |
$formatedRoles[$role] = $roleData['name']; |
| 25 |
} |
| 26 |
|
| 27 |
unset($formatedRoles['administrator']); |
| 28 |
|
| 29 |
return [ |
| 30 |
'settings' => $settings, |
| 31 |
'user_roles' => $formatedRoles |
| 32 |
]; |
| 33 |
} |
| 34 |
|
| 35 |
public function saveGeneralSettings(Request $request) |
| 36 |
{ |
| 37 |
$inputs = $request->get('settings', []); |
| 38 |
$settings = Helper::generalSettings(false); |
| 39 |
$inputs = Arr::only($inputs, array_keys($settings)); |
| 40 |
|
| 41 |
$settings['logo'] = Arr::get($inputs, 'logo', ''); |
| 42 |
$settings['white_logo'] = Arr::get($inputs, 'white_logo', ''); |
| 43 |
$settings['featured_image'] = Arr::get($inputs, 'featured_image', ''); |
| 44 |
|
| 45 |
if (!empty($settings['logo'])) { |
| 46 |
$settings['logo'] = sanitize_url($settings['logo']); |
| 47 |
} |
| 48 |
|
| 49 |
if (!empty($settings['white_logo'])) { |
| 50 |
$settings['white_logo'] = sanitize_url($settings['white_logo']); |
| 51 |
} |
| 52 |
|
| 53 |
if (!empty($settings['featured_image'])) { |
| 54 |
$settings['featured_image'] = sanitize_url($settings['featured_image']); |
| 55 |
} |
| 56 |
|
| 57 |
if (!empty($settings['site_title'])) { |
| 58 |
$settings['site_title'] = sanitize_text_field(Arr::get($inputs, 'site_title', '')); |
| 59 |
} |
| 60 |
|
| 61 |
$settings['disable_global_posts'] = sanitize_text_field(Arr::get($inputs, 'disable_global_posts', 'no')); |
| 62 |
$settings['access'] = Arr::get($inputs, 'access', []); |
| 63 |
$settings['auth_content'] = wp_kses_post(Arr::get($inputs, 'auth_content', '')); |
| 64 |
$settings['auth_redirect'] = sanitize_url(Arr::get($inputs, 'auth_redirect', '')); |
| 65 |
$settings['restricted_role_content'] = wp_kses_post(Arr::get($inputs, 'restricted_role_content', '')); |
| 66 |
$settings['auth_url'] = sanitize_url(Arr::get($inputs, 'auth_url', '')); |
| 67 |
$media = Helper::getMediaFromUrl($settings['logo']); |
| 68 |
$whiteMedia = Helper::getMediaFromUrl($settings['white_logo']); |
| 69 |
|
| 70 |
$featuredMedia = Helper::getMediaFromUrl($settings['featured_image']); |
| 71 |
|
| 72 |
if ($media) { |
| 73 |
$settings['logo'] = $media->public_url; |
| 74 |
$media->update([ |
| 75 |
'is_active' => true, |
| 76 |
'user_id' => get_current_user_id(), |
| 77 |
'object_source' => 'general' |
| 78 |
]); |
| 79 |
} |
| 80 |
|
| 81 |
if ($whiteMedia) { |
| 82 |
$settings['white_logo'] = $whiteMedia->public_url; |
| 83 |
$whiteMedia->update([ |
| 84 |
'is_active' => true, |
| 85 |
'user_id' => get_current_user_id(), |
| 86 |
'object_source' => 'general' |
| 87 |
]); |
| 88 |
} |
| 89 |
|
| 90 |
if ($featuredMedia) { |
| 91 |
$settings['featured_image'] = $featuredMedia->public_url; |
| 92 |
$featuredMedia->update([ |
| 93 |
'is_active' => true, |
| 94 |
'user_id' => get_current_user_id(), |
| 95 |
'object_source' => 'general' |
| 96 |
]); |
| 97 |
} |
| 98 |
|
| 99 |
$slugChanged = false; |
| 100 |
if ($settings['slugs'] != $inputs['slugs'] && !defined('FLUENT_COMMUNITY_PORTAL_SLUG')) { |
| 101 |
$settings['slugs'] = $inputs['slugs']; |
| 102 |
$slugChanged = true; |
| 103 |
} |
| 104 |
|
| 105 |
update_option('fluent_community_settings', $settings); |
| 106 |
|
| 107 |
$redirectUrl = ''; |
| 108 |
|
| 109 |
if ($slugChanged) { |
| 110 |
// flush rewrite rules |
| 111 |
flush_rewrite_rules(true); |
| 112 |
Helper::generalSettings(false); |
| 113 |
$redirectUrl = Helper::baseUrl('admin/settings/general'); |
| 114 |
} |
| 115 |
|
| 116 |
return [ |
| 117 |
'message' => __('Settings has been saved successfully', 'fluent-community'), |
| 118 |
'redirect_url' => $redirectUrl |
| 119 |
]; |
| 120 |
} |
| 121 |
|
| 122 |
public function getEmailSettings(Request $request) |
| 123 |
{ |
| 124 |
return [ |
| 125 |
'email_settings' => Utility::getEmailNotificationSettings() |
| 126 |
]; |
| 127 |
} |
| 128 |
|
| 129 |
public function saveEmailSettings(Request $request) |
| 130 |
{ |
| 131 |
$prevSettings = Utility::getEmailNotificationSettings(); |
| 132 |
$newSettings = $request->get('email_settings', []); |
| 133 |
|
| 134 |
$newSettings = wp_parse_args($newSettings, $prevSettings); |
| 135 |
$newSettings = CustomSanitizer::santizeEmailSettings($newSettings); |
| 136 |
|
| 137 |
Utility::updateOption('global_email_settings', $newSettings); |
| 138 |
|
| 139 |
/* |
| 140 |
* We are unscheduling all digest schedules if the settings has been changed or disabled |
| 141 |
*/ |
| 142 |
$isScheduleChanged = ($newSettings['digest_mail_day'] != $prevSettings['digest_mail_day']) || ($newSettings['daily_digest_time'] != $prevSettings['daily_digest_time']); |
| 143 |
if ($isScheduleChanged) { |
| 144 |
if (as_next_scheduled_action('fluent_community_send_daily_digest_init', null, 'fluent-community')) { |
| 145 |
as_unschedule_all_actions('fluent_community_send_daily_digest_init', null, 'fluent-community'); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return [ |
| 150 |
'email_settings' => $newSettings, |
| 151 |
'message' => __('Email notification settings has been updated', 'fluent-community') |
| 152 |
]; |
| 153 |
} |
| 154 |
|
| 155 |
public function getStorageSettings(Request $request) |
| 156 |
{ |
| 157 |
if (!defined('FLUENT_COMMUNITY_PRO')) { |
| 158 |
$config = [ |
| 159 |
'driver' => 'local' |
| 160 |
]; |
| 161 |
} else { |
| 162 |
$config = \FluentCommunityPro\App\Modules\CloudStorage\StorageHelper::getConfig('view'); |
| 163 |
} |
| 164 |
|
| 165 |
return apply_filters('fluent_community/storage_settings_response', [ |
| 166 |
'config' => $config |
| 167 |
]); |
| 168 |
} |
| 169 |
|
| 170 |
public function updateStorageSettings(Request $request) |
| 171 |
{ |
| 172 |
if (!defined('FLUENT_COMMUNITY_PRO')) { |
| 173 |
return $this->sendError([ |
| 174 |
'message' => 'Sorry, you can not update this config. Please activate pro' |
| 175 |
]); |
| 176 |
} |
| 177 |
|
| 178 |
if (defined('FLUENT_COMMUNITY_CLOUD_STORAGE') && FLUENT_COMMUNITY_CLOUD_STORAGE) { |
| 179 |
return $this->sendError([ |
| 180 |
'message' => 'You can not update the storage settings as it is defined in the config file' |
| 181 |
]); |
| 182 |
} |
| 183 |
|
| 184 |
$config = $request->get('config', []); |
| 185 |
|
| 186 |
$driver = Arr::get($config, 'driver', 'local'); |
| 187 |
|
| 188 |
$validation = [ |
| 189 |
'driver' => 'required' |
| 190 |
]; |
| 191 |
|
| 192 |
if ($driver == 'cloudflare_r2') { |
| 193 |
$validation = [ |
| 194 |
'driver' => 'required', |
| 195 |
'access_key' => 'required', |
| 196 |
'secret_key' => 'required', |
| 197 |
'bucket' => 'required', |
| 198 |
'public_url' => 'required|url', |
| 199 |
'account_id' => 'required', |
| 200 |
]; |
| 201 |
} else if ($driver == 'amazon_s3') { |
| 202 |
$validation = [ |
| 203 |
'driver' => 'required', |
| 204 |
'access_key' => 'required', |
| 205 |
'secret_key' => 'required', |
| 206 |
'bucket' => 'required' |
| 207 |
]; |
| 208 |
} |
| 209 |
|
| 210 |
$this->validate($config, $validation); |
| 211 |
|
| 212 |
if ($driver === 'cloudflare_r2' || $driver === 'amazon_s3') { |
| 213 |
|
| 214 |
$previousConfig = \FluentCommunityPro\App\Modules\CloudStorage\StorageHelper::getConfig(); |
| 215 |
if ($config['access_key'] == 'FCOM_ENCRYPTED_DATA_KEY') { |
| 216 |
$config['access_key'] = Arr::get($previousConfig, 'access_key'); |
| 217 |
} |
| 218 |
|
| 219 |
if ($config['secret_key'] == 'FCOM_ENCRYPTED_DATA_KEY') { |
| 220 |
$config['secret_key'] = Arr::get($previousConfig, 'secret_key'); |
| 221 |
} |
| 222 |
|
| 223 |
$driver = (new \FluentCommunityPro\App\Modules\CloudStorage\CloudStorageModule)->getConnectionDriver($config); |
| 224 |
|
| 225 |
if (!$driver) { |
| 226 |
return $this->sendError([ |
| 227 |
'message' => 'Could not connect to the remote storage service. Please check your credentials' |
| 228 |
]); |
| 229 |
} |
| 230 |
|
| 231 |
$test = $driver->testConnection(); |
| 232 |
if (!$test || is_wp_error($test)) { |
| 233 |
return $this->sendError([ |
| 234 |
'message' => 'Could not connect to the remote storage service. Error: ' . is_wp_error($test) ? $test->get_error_message() : 'Unknow Error' |
| 235 |
]); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
$config = Arr::only($config, [ |
| 240 |
'driver', |
| 241 |
'access_key', |
| 242 |
'secret_key', |
| 243 |
'bucket', |
| 244 |
'public_url', |
| 245 |
'account_id', |
| 246 |
'sub_folder', |
| 247 |
's3_endpoint' |
| 248 |
]); |
| 249 |
|
| 250 |
if ($config['driver'] == 'local') { |
| 251 |
$config = [ |
| 252 |
'driver' => 'local' |
| 253 |
]; |
| 254 |
|
| 255 |
$featureConfig = Utility::getFeaturesConfig(); |
| 256 |
$featureConfig['cloud_storage'] = 'no'; |
| 257 |
Utility::updateOption('fluent_community_features', $featureConfig); |
| 258 |
} else { |
| 259 |
$featureConfig = Utility::getFeaturesConfig(); |
| 260 |
$featureConfig['cloud_storage'] = 'yes'; |
| 261 |
Utility::updateOption('fluent_community_features', $featureConfig); |
| 262 |
} |
| 263 |
|
| 264 |
$config = array_filter($config); |
| 265 |
\FluentCommunityPro\App\Modules\CloudStorage\StorageHelper::updateConfig($config); |
| 266 |
|
| 267 |
return [ |
| 268 |
'message' => __('Storage settings has been updated successfully', 'fluent-community') |
| 269 |
]; |
| 270 |
} |
| 271 |
|
| 272 |
public function getWelcomeBannerSettings(Request $request) |
| 273 |
{ |
| 274 |
return [ |
| 275 |
'settings' => Helper::getWelcomeBannerSettings() |
| 276 |
]; |
| 277 |
} |
| 278 |
|
| 279 |
public function updateWelcomeBannerSettings(Request $request) |
| 280 |
{ |
| 281 |
$settings = $request->get('settings', []); |
| 282 |
|
| 283 |
$settings = CustomSanitizer::sanitizeWelcomeBannerSettings($settings); |
| 284 |
|
| 285 |
if (Arr::get($settings, 'login.enabled') == 'yes') { |
| 286 |
$settings['login']['description_rendered'] = FeedsHelper::mdToHtml(Arr::get($settings, 'login.description')); |
| 287 |
} |
| 288 |
|
| 289 |
if (Arr::get($settings, 'logout.enabled') == 'yes') { |
| 290 |
$settings['logout']['description_rendered'] = FeedsHelper::mdToHtml(Arr::get($settings, 'logout.description')); |
| 291 |
if (Arr::get($settings, 'logout.buttonLabel') && Arr::get($settings, 'logout.useCustomUrl') != 'yes') { |
| 292 |
$settings['logout']['buttonLink'] = Helper::baseUrl('?fcom_action=auth'); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
Utility::updateOption('welcome_banner_settings', $settings); |
| 297 |
Utility::setCache('welcome_banner_settings', $settings, WEEK_IN_SECONDS); |
| 298 |
|
| 299 |
return [ |
| 300 |
'message' => __('Welcome banner settings has been updated successfully', 'fluent-community'), |
| 301 |
'settings' => $settings |
| 302 |
]; |
| 303 |
} |
| 304 |
|
| 305 |
public function getOnBoardingSettings() |
| 306 |
{ |
| 307 |
$settings = Helper::generalSettings(); |
| 308 |
|
| 309 |
$currentUser = get_user_by('ID', get_current_user_id()); |
| 310 |
|
| 311 |
$settings['has_fluentcrm'] = defined('FLUENTCRM') ? 'yes' : 'no'; |
| 312 |
$settings['has_fluentsmtp'] = defined('FLUENTMAIL_PLUGIN_FILE') ? 'yes' : 'no'; |
| 313 |
$settings['template'] = ''; |
| 314 |
$settings['install_fluentcrm'] = 'yes'; |
| 315 |
$settings['install_fluentsmtp'] = 'yes'; |
| 316 |
$settings['subscribe_to_newsletter'] = 'yes'; |
| 317 |
$settings['share_data'] = 'no'; |
| 318 |
$settings['user_full_name'] = $currentUser->first_name ? $currentUser->first_name . ' ' . $currentUser->last_name : $currentUser->display_name; |
| 319 |
$settings['user_email_address'] = $currentUser->user_email; |
| 320 |
|
| 321 |
return [ |
| 322 |
'settings' => $settings |
| 323 |
]; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
public function saveOnBoardingSettings(Request $request) |
| 328 |
{ |
| 329 |
$inputs = $request->get('settings', []); |
| 330 |
$settings = Helper::generalSettings(); |
| 331 |
|
| 332 |
if (Arr::get($inputs, 'site_title')) { |
| 333 |
$settings['site_title'] = sanitize_text_field(Arr::get($inputs, 'site_title')); |
| 334 |
} |
| 335 |
|
| 336 |
$logo = Arr::get($inputs, 'logo'); |
| 337 |
|
| 338 |
if ($logo) { |
| 339 |
$media = Helper::getMediaFromUrl($logo); |
| 340 |
if ($media) { |
| 341 |
$settings['logo'] = $media->public_url; |
| 342 |
$media->update([ |
| 343 |
'is_active' => true, |
| 344 |
'user_id' => get_current_user_id(), |
| 345 |
'object_source' => 'onboarding' |
| 346 |
]); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
if (Arr::get($inputs, 'slug') && empty($settings['is_slug_defined'])) { |
| 351 |
$settings['slug'] = sanitize_title(Arr::get($inputs, 'slug')); |
| 352 |
} |
| 353 |
update_option('fluent_community_settings', $settings, 'yes'); |
| 354 |
|
| 355 |
// Email Subscription Settings |
| 356 |
$subscriptionSettings = array_filter([ |
| 357 |
'user_id' => get_current_user_id(), |
| 358 |
'subscribe_to_newsletter' => sanitize_text_field(Arr::get($inputs, 'subscribe_to_newsletter', 'no')), |
| 359 |
'share_data' => sanitize_text_field(Arr::get($inputs, 'share_data', 'no')), |
| 360 |
'user_full_name' => sanitize_text_field(Arr::get($inputs, 'user_full_name', '')), |
| 361 |
'user_email_address' => sanitize_email(Arr::get($inputs, 'user_email_address', '')) |
| 362 |
]); |
| 363 |
|
| 364 |
Utility::updateOption('onboarding_sub_settings', $subscriptionSettings); |
| 365 |
OnboardingService::maybeCreateSpaceTemplates(Arr::get($inputs, 'template')); |
| 366 |
|
| 367 |
$installableAddons = array_filter(array_keys([ |
| 368 |
'fluent-crm' => Arr::get($inputs, 'install_fluentcrm', 'no') == 'yes', |
| 369 |
'fluent-smtp' => Arr::get($inputs, 'install_fluentsmtp', 'no') == 'yes', |
| 370 |
])); |
| 371 |
|
| 372 |
// Install Plugins which are checked |
| 373 |
OnboardingService::installAddons($installableAddons); |
| 374 |
|
| 375 |
// Maybe Optin User to Newsletter |
| 376 |
OnboardingService::maybeOptinUserToNewsletter($subscriptionSettings); |
| 377 |
|
| 378 |
// flash the permalinks |
| 379 |
flush_rewrite_rules(true); |
| 380 |
|
| 381 |
return [ |
| 382 |
'message' => __('Onboarding settings has been updated', 'fluent-community') |
| 383 |
]; |
| 384 |
} |
| 385 |
|
| 386 |
public function changePortalSlug(Request $request) |
| 387 |
{ |
| 388 |
$newSlug = sanitize_title($request->get('new_slug', '')); |
| 389 |
|
| 390 |
if (!$newSlug) { |
| 391 |
return $this->sendError([ |
| 392 |
'message' => __('Slug can not be empty', 'fluent-community') |
| 393 |
]); |
| 394 |
} |
| 395 |
|
| 396 |
if (defined('FLUENT_COMMUNITY_PORTAL_SLUG')) { |
| 397 |
return $this->sendError([ |
| 398 |
'message' => __('You can not change the slug as it is defined in the config file', 'fluent-community') |
| 399 |
]); |
| 400 |
} |
| 401 |
|
| 402 |
$settings = Helper::generalSettings(); |
| 403 |
|
| 404 |
if ($settings['slug'] == $newSlug) { |
| 405 |
$settings['slug'] = $newSlug; |
| 406 |
|
| 407 |
update_option('fluent_community_settings', $settings); |
| 408 |
flush_rewrite_rules(true); |
| 409 |
delete_option('rewrite_rules'); |
| 410 |
} |
| 411 |
|
| 412 |
return [ |
| 413 |
'message' => __('Slug has been changed successfully', 'fluent-community') |
| 414 |
]; |
| 415 |
} |
| 416 |
|
| 417 |
} |
| 418 |
|