| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentBooking\App\App; |
| 6 |
use FluentBooking\App\Services\GlobalModules\GlobalModules; |
| 7 |
use FluentBooking\App\Hooks\Handlers\AdminMenuHandler; |
| 8 |
use FluentBooking\App\Services\Helper; |
| 9 |
use FluentBooking\App\Services\Libs\Countries; |
| 10 |
use FluentBooking\Framework\Http\Request\Request; |
| 11 |
use FluentBooking\Framework\Support\Arr; |
| 12 |
|
| 13 |
class SettingsController extends Controller |
| 14 |
{ |
| 15 |
public function getSettingsMenu() |
| 16 |
{ |
| 17 |
return [ |
| 18 |
'menu_items' => AdminMenuHandler::settingsMenuItems() |
| 19 |
]; |
| 20 |
} |
| 21 |
|
| 22 |
public function getGeneralSettings() |
| 23 |
{ |
| 24 |
$settings = Helper::getGlobalSettings(); |
| 25 |
|
| 26 |
$settings['emailingFields'] = [ |
| 27 |
'from_name' => [ |
| 28 |
'wrapper_class' => 'fc_item_half', |
| 29 |
'type' => 'input-text', |
| 30 |
'placeholder' => __('From Name for emails', 'fluent-booking'), |
| 31 |
'label' => __('From Name', 'fluent-booking'), |
| 32 |
'help' => __('Default Name that will be used to send email)', 'fluent-booking') |
| 33 |
], |
| 34 |
'from_email' => [ |
| 35 |
'wrapper_class' => 'fc_item_half', |
| 36 |
'type' => 'input-or-select', |
| 37 |
'placeholder' => 'name@domain.com', |
| 38 |
'data_type' => 'email', |
| 39 |
'options' => Helper::getVerifiedSenders(), |
| 40 |
'label' => __('From Email Address', 'fluent-booking'), |
| 41 |
'help' => __('Provide Valid Email Address that will be used to send emails', 'fluent-booking'), |
| 42 |
'inline_help' => __('email as per your domain/SMTP settings', 'fluent-booking') |
| 43 |
], |
| 44 |
'reply_to_name' => [ |
| 45 |
'wrapper_class' => 'fc_item_half', |
| 46 |
'type' => 'input-text', |
| 47 |
'placeholder' => __('Reply to Name', 'fluent-booking'), |
| 48 |
'label' => __('Reply to Name (Optional)', 'fluent-booking'), |
| 49 |
'help' => __('Default Reply to Name (Optional)', 'fluent-booking') |
| 50 |
], |
| 51 |
'reply_to_email' => [ |
| 52 |
'wrapper_class' => 'fc_item_half', |
| 53 |
'type' => 'input-text', |
| 54 |
'placeholder' => 'name@domain.com', |
| 55 |
'data_type' => 'email', |
| 56 |
'label' => __('Reply to Email (Optional)', 'fluent-booking'), |
| 57 |
'help' => __('Default Reply to Email (Optional)', 'fluent-booking') |
| 58 |
], |
| 59 |
'use_host_name' => [ |
| 60 |
'wrapper_class' => 'fc_full_width fc_mb_0', |
| 61 |
'type' => 'inline-checkbox', |
| 62 |
'checkbox_label' => __('Use host name as From Name for booking emails to guests', 'fluent-booking'), |
| 63 |
'true_label' => 'yes', |
| 64 |
'false_label' => 'no', |
| 65 |
], |
| 66 |
'use_host_email_on_reply' => [ |
| 67 |
'wrapper_class' => 'fc_full_width fc_mb_0', |
| 68 |
'type' => 'inline-checkbox', |
| 69 |
'checkbox_label' => __('Use host email for reply-to value for booking emails to guests', 'fluent-booking'), |
| 70 |
'true_label' => 'yes', |
| 71 |
'false_label' => 'no', |
| 72 |
], |
| 73 |
'attach_ics_on_confirmation' => [ |
| 74 |
'wrapper_class' => 'fc_full_width fc_mb_0', |
| 75 |
'type' => 'inline-checkbox', |
| 76 |
'checkbox_label' => __('Include ICS file attachment in booking confirmation emails', 'fluent-booking'), |
| 77 |
'true_label' => 'yes', |
| 78 |
'false_label' => 'no', |
| 79 |
], |
| 80 |
'email_footer' => [ |
| 81 |
'wrapper_class' => 'fc_full_width fc_mb_0 fc_wp_editor', |
| 82 |
'type' => 'wp-editor-field', |
| 83 |
'label' => __('Email Footer for Booking related emails (Optional)', 'fluent-booking'), |
| 84 |
'inline_help' => __('You may include your business name, address etc here, for example: <br />You have received this email because signed up for an event or made a booking on our website.', 'fluent-booking') |
| 85 |
] |
| 86 |
]; |
| 87 |
|
| 88 |
$settings['all_countries'] = Countries::get(); |
| 89 |
|
| 90 |
return apply_filters('fluent_booking/general_settings', $settings); |
| 91 |
} |
| 92 |
|
| 93 |
public function updateGeneralSettings(Request $request) |
| 94 |
{ |
| 95 |
$settings = [ |
| 96 |
'emailing' => $request->get('emailing', []), |
| 97 |
'administration' => $request->get('administration', []), |
| 98 |
]; |
| 99 |
|
| 100 |
$formattedSettings = []; |
| 101 |
|
| 102 |
foreach ($settings as $settingKey => $setting) { |
| 103 |
$santizedSettings = array_map('sanitize_text_field', $setting); |
| 104 |
if ($settingKey == 'emailing') { |
| 105 |
$santizedSettings['email_footer'] = wp_kses_post($setting['email_footer']); |
| 106 |
} |
| 107 |
$formattedSettings[$settingKey] = $santizedSettings; |
| 108 |
} |
| 109 |
$formattedSettings['time_format'] = $request->get('timeFormat'); |
| 110 |
|
| 111 |
update_option('_fluent_booking_settings', $formattedSettings, 'no'); |
| 112 |
|
| 113 |
return [ |
| 114 |
'message' => __('Settings updated successfully', 'fluent-booking'), |
| 115 |
'settings' => $formattedSettings |
| 116 |
]; |
| 117 |
} |
| 118 |
|
| 119 |
public function updatePaymentSettings(Request $request) |
| 120 |
{ |
| 121 |
$paymentSettings = $request->get('payments', []); |
| 122 |
|
| 123 |
$currency = Arr::get($paymentSettings, 'currency'); |
| 124 |
$isActive = Arr::get($paymentSettings, 'is_active', 'no'); |
| 125 |
|
| 126 |
update_option('fluent_booking_global_payment_settings', [ |
| 127 |
'currency' => sanitize_text_field($currency), |
| 128 |
'is_active' => ($isActive == 'yes') ? 'yes' : 'no' |
| 129 |
], 'no'); |
| 130 |
|
| 131 |
return [ |
| 132 |
'message' => __('Settings updated successfully', 'fluent-booking') |
| 133 |
]; |
| 134 |
} |
| 135 |
|
| 136 |
public function updateThemeSettings(Request $request) |
| 137 |
{ |
| 138 |
$themeSettings = sanitize_text_field($request->get('theme')); |
| 139 |
|
| 140 |
$bookingOption = get_option('_fluent_booking_settings', []); |
| 141 |
|
| 142 |
$bookingOption['theme'] = $themeSettings; |
| 143 |
|
| 144 |
update_option('_fluent_booking_settings', $bookingOption, 'no'); |
| 145 |
|
| 146 |
return [ |
| 147 |
'message' => __('Settings updated successfully', 'fluent-booking') |
| 148 |
]; |
| 149 |
} |
| 150 |
|
| 151 |
public function getGlobalModules(Request $request) |
| 152 |
{ |
| 153 |
$settings = Helper::getGlobalModuleSettings(); |
| 154 |
|
| 155 |
|
| 156 |
if (!$settings) { |
| 157 |
$settings = (object)[]; |
| 158 |
} |
| 159 |
|
| 160 |
$featuresPrefs = Helper::getPrefSettins(false); |
| 161 |
|
| 162 |
if (empty($featuresPrefs['frontend']['render_type'])) { |
| 163 |
$featuresPrefs['frontend']['render_type'] = 'standalone'; |
| 164 |
} |
| 165 |
|
| 166 |
$featuresPrefs['panel_url'] = Helper::getAppBaseUrl(); |
| 167 |
|
| 168 |
return [ |
| 169 |
'settings' => $settings, |
| 170 |
'modules' => (new GlobalModules())->getAllModules(), |
| 171 |
'featureModules' => $featuresPrefs |
| 172 |
]; |
| 173 |
} |
| 174 |
|
| 175 |
public function updateGlobalModules(Request $request) |
| 176 |
{ |
| 177 |
$settings = $request->get('settings', []); |
| 178 |
|
| 179 |
$modules = (new GlobalModules())->getAllModules(); |
| 180 |
|
| 181 |
$formattedModules = []; |
| 182 |
|
| 183 |
foreach ($settings as $settingKey => $value) { |
| 184 |
if (!isset($modules[$settingKey])) { |
| 185 |
continue; |
| 186 |
} |
| 187 |
$formattedModules[$settingKey] = $value == 'yes' ? 'yes' : 'no'; |
| 188 |
} |
| 189 |
|
| 190 |
Helper::updateGlobalModuleSettings($formattedModules); |
| 191 |
|
| 192 |
return [ |
| 193 |
'message' => __('Settings updated successfully', 'fluent-booking'), |
| 194 |
]; |
| 195 |
} |
| 196 |
|
| 197 |
public function getPages(Request $request) |
| 198 |
{ |
| 199 |
|
| 200 |
$db = App::getInstance('db'); |
| 201 |
|
| 202 |
$allPages = $db->table('posts')->where('post_type', 'page') |
| 203 |
->where('post_status', 'publish') |
| 204 |
->select(['ID', 'post_title', 'post_name']) |
| 205 |
->orderBy('post_title', 'ASC') |
| 206 |
->get(); |
| 207 |
|
| 208 |
$pages = []; |
| 209 |
foreach ($allPages as $page) { |
| 210 |
$pages[] = [ |
| 211 |
'id' => $page->ID, |
| 212 |
'name' => $page->post_name, |
| 213 |
'title' => $page->post_title ? $page->post_title : __('(no title)', 'fluent-boards') |
| 214 |
]; |
| 215 |
} |
| 216 |
|
| 217 |
return [ |
| 218 |
'pages' => $pages |
| 219 |
]; |
| 220 |
} |
| 221 |
|
| 222 |
public function saveAddonsSettings(Request $request) |
| 223 |
{ |
| 224 |
if (!defined('FLUENT_BOOKING_PRO_DIR_FILE')) { |
| 225 |
return $this->sendError([ |
| 226 |
'message' => __('This feature is only available in FluentBooking Pro', 'fluent-booking') |
| 227 |
]); |
| 228 |
} |
| 229 |
|
| 230 |
$settings = $request->get('settings', []); |
| 231 |
|
| 232 |
$prefSettings = Helper::getPrefSettins(false); |
| 233 |
|
| 234 |
$settings = wp_parse_args($settings, $prefSettings); |
| 235 |
|
| 236 |
$settings = Arr::only($settings, array_keys($prefSettings)); |
| 237 |
$settings['frontend']['slug'] = sanitize_title($settings['frontend']['slug']); |
| 238 |
|
| 239 |
if (empty($settings['frontend']['slug'])) { |
| 240 |
$settings['frontend']['slug'] = 'projects'; |
| 241 |
} |
| 242 |
|
| 243 |
if (defined('FLUENT_BOOKING_ADMIN_SLUG') && FLUENT_BOOKING_FRONT_SLUG) { |
| 244 |
$settings['frontend']['slug'] = FLUENT_BOOKING_FRONT_SLUG; |
| 245 |
} |
| 246 |
|
| 247 |
do_action('fluent_booking/saving_addons', $settings, $prefSettings); |
| 248 |
|
| 249 |
update_option('fluent_booking_modules', $settings, 'yes'); |
| 250 |
|
| 251 |
return $this->sendSuccess([ |
| 252 |
'message' => __('Settings are saved', 'fluent-booking'), |
| 253 |
'featureModules' => $settings |
| 254 |
]); |
| 255 |
} |
| 256 |
|
| 257 |
public function installPlugin(Request $request) |
| 258 |
{ |
| 259 |
$pluginId = $request->get('plugin_id'); |
| 260 |
|
| 261 |
$allowedPlugins = [ |
| 262 |
'fluent-smtp' => [ |
| 263 |
'name' => 'FluentSMTP', |
| 264 |
'repo-slug' => 'fluent-smtp', |
| 265 |
'file' => 'fluent-smtp.php', |
| 266 |
], |
| 267 |
'fluent-booking' => [ |
| 268 |
'name' => 'FluentBooking', |
| 269 |
'repo-slug' => 'fluent-booking', |
| 270 |
'file' => 'fluent-booking.php', |
| 271 |
], |
| 272 |
'fluentform' => [ |
| 273 |
'name' => 'FluentForm', |
| 274 |
'repo-slug' => 'fluentform', |
| 275 |
'file' => 'fluentform.php', |
| 276 |
], |
| 277 |
'fluent-crm' => [ |
| 278 |
'name' => 'FluentCRM', |
| 279 |
'repo-slug' => 'fluent-crm', |
| 280 |
'file' => 'fluent-crm.php', |
| 281 |
], |
| 282 |
'fluent-boards' => [ |
| 283 |
'name' => 'FluentBoards', |
| 284 |
'repo-slug' => 'fluent-boards', |
| 285 |
'file' => 'fluent-boards.php', |
| 286 |
], |
| 287 |
]; |
| 288 |
|
| 289 |
if (!isset($allowedPlugins[$pluginId])) { |
| 290 |
return $this->sendError([ |
| 291 |
'message' => __('This action is not allowed', 'fluent-booking') |
| 292 |
]); |
| 293 |
} |
| 294 |
|
| 295 |
$this->backgroundInstaller($allowedPlugins[$pluginId], $pluginId); |
| 296 |
|
| 297 |
return $this->sendSuccess([ |
| 298 |
'message' => __('Plugin is being installed in the background', 'fluent-booking') |
| 299 |
]); |
| 300 |
} |
| 301 |
|
| 302 |
private function backgroundInstaller($plugin_to_install, $plugin_id) |
| 303 |
{ |
| 304 |
if (!empty($plugin_to_install['repo-slug'])) { |
| 305 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 306 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 307 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 308 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 309 |
|
| 310 |
WP_Filesystem(); |
| 311 |
|
| 312 |
$skin = new \Automatic_Upgrader_Skin(); |
| 313 |
$upgrader = new \WP_Upgrader($skin); |
| 314 |
$installed_plugins = array_reduce(array_keys(\get_plugins()), array($this, 'associate_plugin_file'), array()); |
| 315 |
$plugin_slug = $plugin_to_install['repo-slug']; |
| 316 |
$plugin_file = isset($plugin_to_install['file']) ? $plugin_to_install['file'] : $plugin_slug . '.php'; |
| 317 |
$installed = false; |
| 318 |
$activate = false; |
| 319 |
|
| 320 |
// See if the plugin is installed already. |
| 321 |
if (isset($installed_plugins[$plugin_file])) { |
| 322 |
$installed = true; |
| 323 |
$activate = !is_plugin_active($installed_plugins[$plugin_file]); |
| 324 |
} |
| 325 |
|
| 326 |
// Install this thing! |
| 327 |
if (!$installed) { |
| 328 |
// Suppress feedback. |
| 329 |
ob_start(); |
| 330 |
|
| 331 |
try { |
| 332 |
$plugin_information = plugins_api( |
| 333 |
'plugin_information', |
| 334 |
array( |
| 335 |
'slug' => $plugin_slug, |
| 336 |
'fields' => array( |
| 337 |
'short_description' => false, |
| 338 |
'sections' => false, |
| 339 |
'requires' => false, |
| 340 |
'rating' => false, |
| 341 |
'ratings' => false, |
| 342 |
'downloaded' => false, |
| 343 |
'last_updated' => false, |
| 344 |
'added' => false, |
| 345 |
'tags' => false, |
| 346 |
'homepage' => false, |
| 347 |
'donate_link' => false, |
| 348 |
'author_profile' => false, |
| 349 |
'author' => false, |
| 350 |
), |
| 351 |
) |
| 352 |
); |
| 353 |
|
| 354 |
if (is_wp_error($plugin_information)) { |
| 355 |
throw new \Exception($plugin_information->get_error_message()); |
| 356 |
} |
| 357 |
|
| 358 |
$package = $plugin_information->download_link; |
| 359 |
$download = $upgrader->download_package($package); |
| 360 |
|
| 361 |
if (is_wp_error($download)) { |
| 362 |
throw new \Exception($download->get_error_message()); |
| 363 |
} |
| 364 |
|
| 365 |
$working_dir = $upgrader->unpack_package($download, true); |
| 366 |
|
| 367 |
if (is_wp_error($working_dir)) { |
| 368 |
throw new \Exception($working_dir->get_error_message()); |
| 369 |
} |
| 370 |
|
| 371 |
$result = $upgrader->install_package( |
| 372 |
array( |
| 373 |
'source' => $working_dir, |
| 374 |
'destination' => WP_PLUGIN_DIR, |
| 375 |
'clear_destination' => false, |
| 376 |
'abort_if_destination_exists' => false, |
| 377 |
'clear_working' => true, |
| 378 |
'hook_extra' => array( |
| 379 |
'type' => 'plugin', |
| 380 |
'action' => 'install', |
| 381 |
), |
| 382 |
) |
| 383 |
); |
| 384 |
|
| 385 |
if (is_wp_error($result)) { |
| 386 |
throw new \Exception($result->get_error_message()); |
| 387 |
} |
| 388 |
|
| 389 |
$activate = true; |
| 390 |
|
| 391 |
} catch (\Exception $e) { |
| 392 |
} |
| 393 |
|
| 394 |
// Discard feedback. |
| 395 |
ob_end_clean(); |
| 396 |
} |
| 397 |
|
| 398 |
wp_clean_plugins_cache(); |
| 399 |
|
| 400 |
// Activate this thing. |
| 401 |
if ($activate) { |
| 402 |
try { |
| 403 |
$result = activate_plugin($installed ? $installed_plugins[$plugin_file] : $plugin_slug . '/' . $plugin_file); |
| 404 |
|
| 405 |
if (is_wp_error($result)) { |
| 406 |
throw new \Exception($result->get_error_message()); |
| 407 |
} |
| 408 |
} catch (\Exception $e) { |
| 409 |
} |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
private function associate_plugin_file($plugins, $key) |
| 415 |
{ |
| 416 |
$path = explode('/', $key); |
| 417 |
$filename = end($path); |
| 418 |
$plugins[$filename] = $key; |
| 419 |
return $plugins; |
| 420 |
} |
| 421 |
} |
| 422 |
|