| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCommunity\App\Functions\Utility; |
| 6 |
use FluentCommunity\App\Services\Helper; |
| 7 |
use FluentCommunity\Framework\Http\Request\Request; |
| 8 |
use FluentCommunity\Framework\Support\Arr; |
| 9 |
use FluentCommunity\Framework\Support\Sanitizer; |
| 10 |
|
| 11 |
class SettingController extends Controller |
| 12 |
{ |
| 13 |
public function getFeatures() |
| 14 |
{ |
| 15 |
return [ |
| 16 |
'features' => Utility::getFeaturesConfig(), |
| 17 |
'addOns' => $this->getAddons() |
| 18 |
]; |
| 19 |
} |
| 20 |
|
| 21 |
public function setFeatures(Request $request) |
| 22 |
{ |
| 23 |
$request->validate([ |
| 24 |
'features.giphy_api_key' => 'required_if:features.giphy_module,yes|string', |
| 25 |
]); |
| 26 |
|
| 27 |
$data = $request->get('features', []); |
| 28 |
|
| 29 |
$data = Sanitizer::sanitize($data, [ |
| 30 |
'leader_board_module' => 'sanitize_text_field', |
| 31 |
'course_module' => 'sanitize_text_field', |
| 32 |
'giphy_module' => 'sanitize_text_field', |
| 33 |
'giphy_api_key' => 'sanitize_text_field', |
| 34 |
'cloud_storage' => 'sanitize_text_field', |
| 35 |
'emoji_module' => 'sanitize_text_field', |
| 36 |
'user_badge' => 'sanitize_text_field', |
| 37 |
]); |
| 38 |
|
| 39 |
Utility::updateOption('fluent_community_features', $data); |
| 40 |
|
| 41 |
return [ |
| 42 |
'message' => __('Features saved successfully.', 'fluent-community') |
| 43 |
]; |
| 44 |
} |
| 45 |
|
| 46 |
public function getMenuSettings(Request $request) |
| 47 |
{ |
| 48 |
$menuGroups = Helper::getMenuItemsGroup('edit'); |
| 49 |
$formattedGroups = []; |
| 50 |
|
| 51 |
foreach ($menuGroups as $indexName => $menuGroup) { |
| 52 |
if (is_array($menuGroup)) { |
| 53 |
$formattedGroups[$indexName] = array_values($menuGroup); |
| 54 |
} else { |
| 55 |
$formattedGroups[$indexName] = []; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
if (empty($formattedGroups['afterCommunityLinkGroups']) || !is_array($formattedGroups['afterCommunityLinkGroups'])) { |
| 61 |
$formattedGroups['afterCommunityLinkGroups'] = [ |
| 62 |
[ |
| 63 |
'title' => __('Links', 'fluent-community'), |
| 64 |
'slug' => 'custom_footer_links', |
| 65 |
'items' => [] |
| 66 |
] |
| 67 |
]; |
| 68 |
} else { |
| 69 |
foreach ($formattedGroups['afterCommunityLinkGroups'] as &$group) { |
| 70 |
if (is_array($group) && !empty($group['items'])) { |
| 71 |
$group['items'] = array_values($group['items']); |
| 72 |
} else { |
| 73 |
$group = [ |
| 74 |
'items' => [] |
| 75 |
]; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return [ |
| 81 |
'menuSettings' => $formattedGroups |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
public function saveMenuSettings(Request $request) |
| 86 |
{ |
| 87 |
$menuSettings = $request->get('menuSettings', []); |
| 88 |
|
| 89 |
$mainMenuItems = $menuSettings['mainMenuItems'] ?? []; |
| 90 |
$profileDropdownItems = $menuSettings['profileDropdownItems'] ?? []; |
| 91 |
$beforeCommunityMenuItems = $menuSettings['beforeCommunityMenuItems'] ?? []; |
| 92 |
$afterCommunityLinkGroups = $menuSettings['afterCommunityLinkGroups'] ?? []; |
| 93 |
|
| 94 |
$previousSettings = Helper::getMenuItemsGroup('edit'); |
| 95 |
|
| 96 |
$menuSettings['mainMenuItems'] = $this->formatMenuGroup($mainMenuItems, $previousSettings['mainMenuItems']); |
| 97 |
$menuSettings['profileDropdownItems'] = $this->formatMenuGroup($profileDropdownItems, $previousSettings['profileDropdownItems']); |
| 98 |
$menuSettings['beforeCommunityMenuItems'] = $this->formatMenuGroup($beforeCommunityMenuItems, $previousSettings['beforeCommunityMenuItems']); |
| 99 |
|
| 100 |
$formattedAfterCommunityGroups = []; |
| 101 |
foreach ($afterCommunityLinkGroups as $afterCommunityLinkGroup) { |
| 102 |
if (empty($afterCommunityLinkGroup['items']) || empty($afterCommunityLinkGroup['title'])) { |
| 103 |
continue; |
| 104 |
} |
| 105 |
|
| 106 |
$formattedAfterCommunityGroups[] = [ |
| 107 |
'title' => sanitize_text_field($afterCommunityLinkGroup['title']), |
| 108 |
'slug' => $afterCommunityLinkGroup['slug'] ?: 'custom_footer_group_' . time(), |
| 109 |
'items' => $this->formatMenuGroup($afterCommunityLinkGroup['items'], []) |
| 110 |
]; |
| 111 |
} |
| 112 |
|
| 113 |
$menuSettings['afterCommunityLinkGroups'] = $formattedAfterCommunityGroups; |
| 114 |
$menuSettings = Arr::only($menuSettings, ['mainMenuItems', 'profileDropdownItems', 'beforeCommunityMenuItems', 'afterCommunityLinkGroups']); |
| 115 |
|
| 116 |
Utility::updateOption('fluent_community_menu_groups', $menuSettings); |
| 117 |
|
| 118 |
return [ |
| 119 |
'message' => __('Menu settings saved successfully.', 'fluent-community') |
| 120 |
]; |
| 121 |
} |
| 122 |
|
| 123 |
private function formatMenuGroup($mainMenuItems, $savedMenuItems) |
| 124 |
{ |
| 125 |
$formattedMainMenuItems = []; |
| 126 |
foreach ($mainMenuItems as $item) { |
| 127 |
$slug = (string)Arr::get($item, 'slug'); |
| 128 |
if (!Arr::get($item, 'title') || !Arr::get($item, 'permalink')) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
if (!$slug) { |
| 133 |
$slug = 'custom_' . sanitize_title(Arr::get($item, 'title')) . time(); |
| 134 |
$item['slug'] = $slug; |
| 135 |
$item['is_custom'] = 'yes'; |
| 136 |
} |
| 137 |
|
| 138 |
$defaultItem = Arr::get($savedMenuItems, $slug, []); |
| 139 |
if ($defaultItem) { |
| 140 |
$preservedKeys = ['is_system', 'is_locked', 'is_unavailable', 'slug']; |
| 141 |
foreach ($preservedKeys as $key) { |
| 142 |
if (isset($defaultItem[$key])) { |
| 143 |
$item[$key] = Arr::get($defaultItem, $key); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
$item = \FluentCommunity\App\Services\CustomSanitizer::sanitizeMenuLink($item); |
| 149 |
|
| 150 |
$formattedMainMenuItems[$slug] = $item; |
| 151 |
} |
| 152 |
|
| 153 |
return $formattedMainMenuItems; |
| 154 |
} |
| 155 |
|
| 156 |
public function getAddons() |
| 157 |
{ |
| 158 |
return [ |
| 159 |
'fluent-messaging' => [ |
| 160 |
'is_repo' => false, |
| 161 |
'title' => __('FluentCommunity Chat', 'fluent-community'), |
| 162 |
'logo' => Helper::assetUrl('images/brands/fluent-messages.svg'), |
| 163 |
'is_installed' => defined('FLUENT_MESSAGING_CHAT_VERSION'), |
| 164 |
'learn_more_url' => 'https://fluentcommunity.co', |
| 165 |
'settings_url' => Helper::baseUrl('chat'), |
| 166 |
'action_text' => $this->isPluginInstalled('fluent-messaging/fluent-messaging.php') ? __('Active FluentCommunity Chat', 'fluent-community') : __('Install FluentCommunity Chat', 'fluent-community'), |
| 167 |
'description' => __('FluentCommunity Chat is a real-time chat plugin for WordPress. It allows you to create a chat room for your community members.', 'fluent-community') |
| 168 |
], |
| 169 |
'fluent-crm' => [ |
| 170 |
'is_repo' => true, |
| 171 |
'title' => __('FluentCRM', 'fluent-community'), |
| 172 |
'logo' => Helper::assetUrl('images/brands/fluentcrm.svg'), |
| 173 |
'is_installed' => defined('FLUENTCRM'), |
| 174 |
'learn_more_url' => 'https://fluentcrm.com', |
| 175 |
'settings_url' => admin_url('admin.php?page=fluentcrm-admin#/'), |
| 176 |
'action_text' => $this->isPluginInstalled('fluent-crm/fluent-crm.php') ? __('Active FluentCRM', 'fluent-community') : __('Install FluentCRM', 'fluent-community'), |
| 177 |
'description' => __('The Best Email Marketing Automation Plugin for WordPress. Capture, Segment and Automate your Marketing.', 'fluent-community') |
| 178 |
], |
| 179 |
'fluentform' => [ |
| 180 |
'is_repo' => true, |
| 181 |
'title' => __('Fluent Forms', 'fluent-community'), |
| 182 |
'logo' => Helper::assetUrl('images/brands/fluentform.png'), |
| 183 |
'is_installed' => defined('FLUENTFORM'), |
| 184 |
'learn_more_url' => 'https://wordpress.org/plugins/fluentform/', |
| 185 |
'settings_url' => admin_url('admin.php?page=fluent_forms'), |
| 186 |
'action_text' => $this->isPluginInstalled('fluent-form/fluent-form.php') ? __('Active Fluent Forms', 'fluent-community') : __('Install Fluent Forms', 'fluent-community'), |
| 187 |
'description' => __('Collect leads and build any type of forms, accept payments, connect with your CRM with the Fastest Contact Form Builder Plugin for WordPress', 'fluent-community') |
| 188 |
], |
| 189 |
'fluent-smtp' => [ |
| 190 |
'is_repo' => true, |
| 191 |
'title' => __('Fluent SMTP', 'fluent-community'), |
| 192 |
'logo' => Helper::assetUrl('images/brands/fluent-smtp.svg'), |
| 193 |
'is_installed' => defined('FLUENTMAIL'), |
| 194 |
'learn_more_url' => 'https://wordpress.org/plugins/fluent-smtp/', |
| 195 |
'settings_url' => admin_url('options-general.php?page=fluent-mail#/'), |
| 196 |
'action_text' => $this->isPluginInstalled('fluent-smtp/fluent-smtp.php') ? __('Active Fluent SMTP', 'fluent-community') : __('Install Fluent SMTP', 'fluent-community'), |
| 197 |
'description' => __('Fluent SMTP is the ultimate SMTP and SES plugin for WordPress. Connect with any SMTP service, including SendGrid, Mailgun, SES, Sendinblue, PepiPost, Google, Microsoft, and more.', 'fluent-community') |
| 198 |
], |
| 199 |
'fluent-support' => [ |
| 200 |
'is_repo' => true, |
| 201 |
'title' => __('Fluent Support', 'fluent-community'), |
| 202 |
'logo' => Helper::assetUrl('images/brands/fluent-support.svg'), |
| 203 |
'is_installed' => defined('FLUENT_SUPPORT_VERSION'), |
| 204 |
'learn_more_url' => 'https://wordpress.org/plugins/fluent-connect/', |
| 205 |
'settings_url' => admin_url('admin.php?page=fluent-support#/'), |
| 206 |
'action_text' => $this->isPluginInstalled('fluent-support/fluent-support.php') ? __('Active Fluent Support', 'fluent-community') : __('Install Fluent Support', 'fluent-community'), |
| 207 |
'description' => __('WordPress Helpdesk and Customer Support Ticket Plugin. Provide awesome support and manage customer queries right from your WordPress dashboard.', 'fluent-community') |
| 208 |
] |
| 209 |
]; |
| 210 |
} |
| 211 |
|
| 212 |
private function isPluginInstalled($plugin) |
| 213 |
{ |
| 214 |
return file_exists(WP_PLUGIN_DIR . '/' . $plugin); |
| 215 |
} |
| 216 |
|
| 217 |
public function installPlugin(Request $request) |
| 218 |
{ |
| 219 |
if (!current_user_can('install_plugins')) { |
| 220 |
return $this->sendError([ |
| 221 |
'message' => 'You do not have permission to install plugins' |
| 222 |
]); |
| 223 |
} |
| 224 |
|
| 225 |
$pluginSlug = $request->get('plugin'); |
| 226 |
|
| 227 |
$addons = $this->getAddons(); |
| 228 |
|
| 229 |
if (!isset($addons[$pluginSlug])) { |
| 230 |
return $this->sendError('Invalid Plugin'); |
| 231 |
} |
| 232 |
|
| 233 |
$details = $addons[$pluginSlug]; |
| 234 |
|
| 235 |
if ($details['is_repo']) { |
| 236 |
$plugin = [ |
| 237 |
'name' => $details['title'], |
| 238 |
'repo-slug' => $pluginSlug, |
| 239 |
'file' => $pluginSlug . '.php', |
| 240 |
]; |
| 241 |
|
| 242 |
$this->backgroundInstaller($plugin, $pluginSlug); |
| 243 |
} else { |
| 244 |
if ($pluginSlug == 'fluent-messaging') { |
| 245 |
|
| 246 |
if (!defined('FLUENT_COMMUNITY_PRO')) { |
| 247 |
return $this->sendError('Fluent Messaging is a Pro Plugin. Please install Fluent Space Pro first.'); |
| 248 |
} |
| 249 |
|
| 250 |
do_action('fleunt_community/install_messaging_plugin'); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
return [ |
| 255 |
'message' => __('Plugin has been installed Successfully', 'fluent-community'), |
| 256 |
'is_installed' => true |
| 257 |
]; |
| 258 |
} |
| 259 |
|
| 260 |
private function backgroundInstaller($plugin_to_install, $plugin_id) |
| 261 |
{ |
| 262 |
if (!empty($plugin_to_install['repo-slug'])) { |
| 263 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 264 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 265 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 266 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 267 |
|
| 268 |
WP_Filesystem(); |
| 269 |
|
| 270 |
$skin = new \Automatic_Upgrader_Skin(); |
| 271 |
$upgrader = new \WP_Upgrader($skin); |
| 272 |
$installed_plugins = array_reduce(array_keys(\get_plugins()), array($this, 'associate_plugin_file'), |
| 273 |
array()); |
| 274 |
$plugin_slug = $plugin_to_install['repo-slug']; |
| 275 |
$plugin_file = isset($plugin_to_install['file']) ? $plugin_to_install['file'] : $plugin_slug . '.php'; |
| 276 |
$installed = false; |
| 277 |
$activate = false; |
| 278 |
|
| 279 |
// See if the plugin is installed already. |
| 280 |
if (isset($installed_plugins[$plugin_file])) { |
| 281 |
$installed = true; |
| 282 |
$activate = !is_plugin_active($installed_plugins[$plugin_file]); |
| 283 |
} |
| 284 |
|
| 285 |
// Install this thing! |
| 286 |
if (!$installed) { |
| 287 |
// Suppress feedback. |
| 288 |
ob_start(); |
| 289 |
|
| 290 |
try { |
| 291 |
$plugin_information = plugins_api( |
| 292 |
'plugin_information', |
| 293 |
array( |
| 294 |
'slug' => $plugin_slug, |
| 295 |
'fields' => array( |
| 296 |
'short_description' => false, |
| 297 |
'sections' => false, |
| 298 |
'requires' => false, |
| 299 |
'rating' => false, |
| 300 |
'ratings' => false, |
| 301 |
'downloaded' => false, |
| 302 |
'last_updated' => false, |
| 303 |
'added' => false, |
| 304 |
'tags' => false, |
| 305 |
'homepage' => false, |
| 306 |
'donate_link' => false, |
| 307 |
'author_profile' => false, |
| 308 |
'author' => false, |
| 309 |
), |
| 310 |
) |
| 311 |
); |
| 312 |
|
| 313 |
if (is_wp_error($plugin_information)) { |
| 314 |
throw new \Exception($plugin_information->get_error_message()); |
| 315 |
} |
| 316 |
|
| 317 |
$package = $plugin_information->download_link; |
| 318 |
$download = $upgrader->download_package($package); |
| 319 |
|
| 320 |
if (is_wp_error($download)) { |
| 321 |
throw new \Exception($download->get_error_message()); |
| 322 |
} |
| 323 |
|
| 324 |
$working_dir = $upgrader->unpack_package($download, true); |
| 325 |
|
| 326 |
if (is_wp_error($working_dir)) { |
| 327 |
throw new \Exception($working_dir->get_error_message()); |
| 328 |
} |
| 329 |
|
| 330 |
$result = $upgrader->install_package( |
| 331 |
array( |
| 332 |
'source' => $working_dir, |
| 333 |
'destination' => WP_PLUGIN_DIR, |
| 334 |
'clear_destination' => false, |
| 335 |
'abort_if_destination_exists' => false, |
| 336 |
'clear_working' => true, |
| 337 |
'hook_extra' => array( |
| 338 |
'type' => 'plugin', |
| 339 |
'action' => 'install', |
| 340 |
), |
| 341 |
) |
| 342 |
); |
| 343 |
|
| 344 |
if (is_wp_error($result)) { |
| 345 |
throw new \Exception($result->get_error_message()); |
| 346 |
} |
| 347 |
|
| 348 |
$activate = true; |
| 349 |
} catch (\Exception $e) { |
| 350 |
} |
| 351 |
|
| 352 |
// Discard feedback. |
| 353 |
ob_end_clean(); |
| 354 |
} |
| 355 |
|
| 356 |
wp_clean_plugins_cache(); |
| 357 |
|
| 358 |
// Activate this thing. |
| 359 |
if ($activate) { |
| 360 |
try { |
| 361 |
$result = activate_plugin($installed ? $installed_plugins[$plugin_file] : $plugin_slug . '/' . $plugin_file); |
| 362 |
|
| 363 |
if (is_wp_error($result)) { |
| 364 |
throw new \Exception($result->get_error_message()); |
| 365 |
} |
| 366 |
} catch (\Exception $e) { |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
private function associate_plugin_file($plugins, $key) |
| 373 |
{ |
| 374 |
$path = explode('/', $key); |
| 375 |
$filename = end($path); |
| 376 |
$plugins[$filename] = $key; |
| 377 |
return $plugins; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
public function getCustomizationSettings(Request $request) |
| 382 |
{ |
| 383 |
return [ |
| 384 |
'settings' => Utility::getCustomizationSettings() |
| 385 |
]; |
| 386 |
} |
| 387 |
|
| 388 |
public function updateCustomizationSettings(Request $request) |
| 389 |
{ |
| 390 |
$settings = $request->get('settings', []); |
| 391 |
|
| 392 |
$yesNoFields = [ |
| 393 |
'dark_mode', 'fixed_page_header', 'show_powered_by', 'feed_link_on_sidebar', 'fixed_sidebar', 'icon_on_header_menu' |
| 394 |
]; |
| 395 |
|
| 396 |
foreach ($settings as $key => $value) { |
| 397 |
if (in_array($key, $yesNoFields)) { |
| 398 |
$settings[$key] = $value == 'yes' ? 'yes' : 'no'; |
| 399 |
} else if ($key == 'affiliate_id') { |
| 400 |
$settings[$key] = (int)$value; |
| 401 |
if (!$settings[$key]) { |
| 402 |
$settings[$key] = ''; |
| 403 |
} |
| 404 |
} else { |
| 405 |
$settings[$key] = sanitize_text_field($value); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
Utility::updateCustomizationSettings($settings); |
| 410 |
|
| 411 |
return [ |
| 412 |
'message' => __('Customization settings have been saved successfully.', 'fluent-community') |
| 413 |
]; |
| 414 |
} |
| 415 |
|
| 416 |
public function getPrivacySettings(Request $request) |
| 417 |
{ |
| 418 |
return [ |
| 419 |
'settings' => Utility::getPrivacySettings() |
| 420 |
]; |
| 421 |
} |
| 422 |
|
| 423 |
public function updatePrivacySettings(Request $request) |
| 424 |
{ |
| 425 |
$settings = $request->get('settings', []); |
| 426 |
Utility::updatePrivacySettings($settings); |
| 427 |
|
| 428 |
return [ |
| 429 |
'message' => __('Privacy settings have been saved successfully.', 'fluent-community') |
| 430 |
]; |
| 431 |
} |
| 432 |
|
| 433 |
} |
| 434 |
|