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