| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
|
| 6 |
use FluentSupport\App\Models\MailBox; |
| 7 |
use FluentSupport\App\Models\Meta; |
| 8 |
use FluentSupport\App\Services\EmailNotification\Settings; |
| 9 |
use FluentSupport\App\Services\Helper; |
| 10 |
use FluentSupport\Framework\Request\Request; |
| 11 |
|
| 12 |
/** |
| 13 |
* SettingsController class is responsible for all settings |
| 14 |
* This class is responsible for all request related to settings under global settings tab |
| 15 |
* @package FluentSupport\App\Http\Controllers |
| 16 |
* |
| 17 |
* @version 1.0.0 |
| 18 |
*/ |
| 19 |
class SettingsController extends Controller |
| 20 |
{ |
| 21 |
/** |
| 22 |
* getSettings method will return the settings by settings key |
| 23 |
* @param Request $request |
| 24 |
* @return array|array[] |
| 25 |
*/ |
| 26 |
public function getSettings(Request $request) |
| 27 |
{ |
| 28 |
$settingsKey = $request->get('settings_key'); |
| 29 |
|
| 30 |
return (new Settings)->get($settingsKey); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* getIntegrationSettings method will return the settings for integration |
| 35 |
* @param Request $request |
| 36 |
* @return array |
| 37 |
*/ |
| 38 |
public function getIntegrationSettings(Request $request) |
| 39 |
{ |
| 40 |
$settings = Meta::where('object_type', 'integration_settings')->get(); |
| 41 |
$integrationSettings = []; |
| 42 |
foreach ($settings as $index => $setting) { |
| 43 |
$data = maybe_unserialize($setting->value); |
| 44 |
if (!empty($data['status']) && $data && $data['status'] == 'yes') { |
| 45 |
$integrationSettings[] = $setting->key; |
| 46 |
} |
| 47 |
} |
| 48 |
return $integrationSettings; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* saveSettings method will save the requested settings data by setting key |
| 53 |
* @param Request $request |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
public function saveSettings(Request $request) |
| 57 |
{ |
| 58 |
$settingsKey = $request->get('settings_key'); |
| 59 |
$settings = wp_unslash($request->get('settings')); |
| 60 |
|
| 61 |
(new Settings)->save($settingsKey, $settings); |
| 62 |
|
| 63 |
return [ |
| 64 |
'message' => __('Settings has been updated', 'fluent-support') |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* getPages method will return the list of pages created in WP |
| 70 |
* @return array |
| 71 |
*/ |
| 72 |
public function getPages() |
| 73 |
{ |
| 74 |
return [ |
| 75 |
'pages' => Helper::getWPPages() |
| 76 |
]; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* setupPortal method will setup the support portal |
| 81 |
* @param Request $request |
| 82 |
* @return array |
| 83 |
* @throws \FluentSupport\Framework\Validator\ValidationException |
| 84 |
*/ |
| 85 |
public function setupPortal(Request $request) |
| 86 |
{ |
| 87 |
$mailbox = $request->get('mailbox'); |
| 88 |
$this->validate($mailbox, [ |
| 89 |
'name' => 'required', |
| 90 |
'email' => 'required|email', |
| 91 |
'box_type' => 'required' |
| 92 |
]); |
| 93 |
|
| 94 |
$settings = $request->get('global_settings'); |
| 95 |
|
| 96 |
$createPage = $settings['create_portal_page'] == 'yes'; |
| 97 |
|
| 98 |
if (!$createPage && empty($settings['portal_page_id'])) { |
| 99 |
return $this->sendError([ |
| 100 |
'message' => __('Please select a page or enable create page', 'fluent-support') |
| 101 |
]); |
| 102 |
} |
| 103 |
|
| 104 |
if ($createPage) { |
| 105 |
// we have to create the page |
| 106 |
$page_id = wp_insert_post( |
| 107 |
array( |
| 108 |
'comment_status' => 'close', |
| 109 |
'ping_status' => 'close', |
| 110 |
'post_author' => get_current_user_id(), |
| 111 |
'post_title' => __('Support Portal', 'fluent-support'), |
| 112 |
'post_status' => 'publish', |
| 113 |
'post_content' => '[fluent_support_portal]', |
| 114 |
'post_type' => 'page' |
| 115 |
) |
| 116 |
); |
| 117 |
} else { |
| 118 |
$page_id = intval($settings['portal_page_id']); |
| 119 |
} |
| 120 |
|
| 121 |
$newMailBox = MailBox::first(); |
| 122 |
if (!$newMailBox) { |
| 123 |
$mailbox['is_default'] = 'yes'; |
| 124 |
$mailbox['created_by'] = get_current_user_id(); |
| 125 |
$mailbox['settings']['admin_email_address'] = $mailbox['email']; |
| 126 |
$newMailBox = MailBox::create($mailbox); |
| 127 |
} |
| 128 |
|
| 129 |
$settingsClass = new Settings(); |
| 130 |
$globalSettings = $settingsClass->globalBusinessSettings(); |
| 131 |
|
| 132 |
$globalSettings['portal_page_id'] = $page_id; |
| 133 |
|
| 134 |
$settingsClass->save('global_business_settings', $globalSettings); |
| 135 |
|
| 136 |
|
| 137 |
if (defined('WC_PLUGIN_FILE')) { |
| 138 |
// URL Flash |
| 139 |
flush_rewrite_rules(false); |
| 140 |
} |
| 141 |
|
| 142 |
return [ |
| 143 |
'mailbox' => $newMailBox, |
| 144 |
'global_settings' => $globalSettings, |
| 145 |
'mailboxes' => MailBox::select(['id', 'name', 'settings'])->get(), |
| 146 |
'has_fluentform' => defined('FLUENTFORM') |
| 147 |
]; |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* getFluentCRMSettings method will return the settings for Fluent CRM |
| 153 |
* @param Request $request |
| 154 |
* @return array |
| 155 |
*/ |
| 156 |
public function getFluentCRMSettings(Request $request) |
| 157 |
{ |
| 158 |
if (defined('FLUENTCRM')) { |
| 159 |
$settingDefault = [ |
| 160 |
'enabled' => 'no', |
| 161 |
'default_status' => 'subscribed', |
| 162 |
'assigned_list' => '', |
| 163 |
'assigned_tags' => [] |
| 164 |
]; |
| 165 |
|
| 166 |
$settings = Helper::getOption('_fluentcrm_intergration_settings'); |
| 167 |
|
| 168 |
$settings = wp_parse_args($settings, $settingDefault); |
| 169 |
|
| 170 |
$settingsFields = [ |
| 171 |
'enabled' => [ |
| 172 |
'type' => 'inline-checkbox', |
| 173 |
'true_label' => 'yes', |
| 174 |
'false_label' => 'no', |
| 175 |
'checkbox_label' => __('Enable FluentCRM Integration', 'fluent-support') |
| 176 |
], |
| 177 |
'default_status' => [ |
| 178 |
'type' => 'input-radio', |
| 179 |
'label' => __('Default status for new contacts', 'fluent-support'), |
| 180 |
'options' => [ |
| 181 |
[ |
| 182 |
'id' => 'subscribed', |
| 183 |
'label' => __('Subscribed', 'fluent-support') |
| 184 |
], |
| 185 |
[ |
| 186 |
'id' => 'pending', |
| 187 |
'label' => __('Pending', 'fluent-support') |
| 188 |
] |
| 189 |
], |
| 190 |
'dependency' => [ |
| 191 |
'depends_on' => 'enabled', |
| 192 |
'operator' => '=', |
| 193 |
'value' => 'yes' |
| 194 |
], |
| 195 |
'inline_help' => __('Select the default status for new contacts. If you select pending and it\'s a new contact then a double optin email will be sent', 'fluent-support') |
| 196 |
], |
| 197 |
'assigned_list' => [ |
| 198 |
'type' => 'input-options', |
| 199 |
'label' => __('Add to FluentCRM list (optional)', 'fluent-crm'), |
| 200 |
'options' => \FluentCrm\App\Models\Lists::select(['id', 'title'])->orderBy('title', 'ASC')->get(), |
| 201 |
'dependency' => [ |
| 202 |
'depends_on' => 'enabled', |
| 203 |
'operator' => '=', |
| 204 |
'value' => 'yes' |
| 205 |
], |
| 206 |
], |
| 207 |
'assigned_tags' => [ |
| 208 |
'type' => 'input-options', |
| 209 |
'multiple' => true, |
| 210 |
'label' => __('Add to Tags', 'fluent-crm'), |
| 211 |
'options' => \FluentCrm\App\Models\Tag::select(['id', 'title'])->orderBy('title', 'ASC')->get(), |
| 212 |
'dependency' => [ |
| 213 |
'depends_on' => 'enabled', |
| 214 |
'operator' => '=', |
| 215 |
'value' => 'yes' |
| 216 |
] |
| 217 |
] |
| 218 |
]; |
| 219 |
|
| 220 |
return [ |
| 221 |
'is_installed' => true, |
| 222 |
'settings' => $settings, |
| 223 |
'settings_fields' => $settingsFields, |
| 224 |
'fluentcrm_logo' => FLUENT_SUPPORT_PLUGIN_URL . 'assets/images/fluentcrm-logo.svg' |
| 225 |
]; |
| 226 |
} |
| 227 |
|
| 228 |
return [ |
| 229 |
'is_installed' => false, |
| 230 |
'fluentcrm_logo' => FLUENT_SUPPORT_PLUGIN_URL . 'assets/images/fluentcrm-logo.svg' |
| 231 |
]; |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
public function setupInstallation(Request $request) |
| 237 |
{ |
| 238 |
$installFluentForm = $request->get('install_fluentform', 'no'); |
| 239 |
|
| 240 |
if ($installFluentForm == 'yes' && !defined('FLUENTFORM')) { |
| 241 |
$this->installFluentForm(); |
| 242 |
} |
| 243 |
|
| 244 |
$optinEmail = $request->get('optin_email', 'no'); |
| 245 |
if ($optinEmail && is_email($optinEmail)) { |
| 246 |
$this->shareEmail($optinEmail); |
| 247 |
} |
| 248 |
|
| 249 |
$shareEssential = $request->get('share_essentials', 'no'); |
| 250 |
if ($shareEssential == 'yes') { |
| 251 |
Helper::updateOption('_share_essential', $shareEssential); |
| 252 |
} |
| 253 |
|
| 254 |
return $this->sendSuccess([ |
| 255 |
'message' => __('Installation has been completed', 'fluent-support') |
| 256 |
]); |
| 257 |
|
| 258 |
} |
| 259 |
|
| 260 |
private function shareEmail($optinEmail) |
| 261 |
{ |
| 262 |
$user = get_user_by('ID', get_current_user_id()); |
| 263 |
$data = [ |
| 264 |
'answers' => [ |
| 265 |
'website' => site_url(), |
| 266 |
'email' => $optinEmail, |
| 267 |
'first_name' => $user->first_name, |
| 268 |
'last_name' => $user->last_name, |
| 269 |
'name' => $user->display_name, |
| 270 |
'has_fluentform' => defined('FLUENTFORM') ? 'yes' : 'no' |
| 271 |
], |
| 272 |
'questions' => [ |
| 273 |
'website' => 'website', |
| 274 |
'first_name' => 'first_name', |
| 275 |
'last_name' => 'last_name', |
| 276 |
'email' => 'email', |
| 277 |
'name' => 'name', |
| 278 |
'has_fluentform' => 'has_fluentform' |
| 279 |
], |
| 280 |
'user' => [ |
| 281 |
'email' => $optinEmail |
| 282 |
], |
| 283 |
'fb_capture' => 1, |
| 284 |
'form_id' => 77 |
| 285 |
]; |
| 286 |
|
| 287 |
$url = add_query_arg($data, 'https://wpmanageninja.com/'); |
| 288 |
|
| 289 |
wp_remote_post($url, [ |
| 290 |
'sslverify' => false |
| 291 |
]); |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* installFluentCRM method will install Fluent CRM plugin |
| 296 |
* @return array |
| 297 |
*/ |
| 298 |
public function installFluentCRM() |
| 299 |
{ |
| 300 |
|
| 301 |
if (defined('FLUENTCRM')) { |
| 302 |
return [ |
| 303 |
'is_installed' => true, |
| 304 |
'message' => __('FluentCRM plugin has been installed and activated successfully', 'fluent-support') |
| 305 |
]; |
| 306 |
} |
| 307 |
|
| 308 |
if (!current_user_can('install_plugins')) { |
| 309 |
return $this->sendError([ |
| 310 |
'message' => __('Sorry! you do not have permission to install plugin', 'fluent-crm') |
| 311 |
]); |
| 312 |
} |
| 313 |
|
| 314 |
$plugin_id = 'fluent-crm'; |
| 315 |
$plugin = [ |
| 316 |
'name' => 'Fluent CRM', |
| 317 |
'repo-slug' => 'fluent-crm', |
| 318 |
'file' => 'fluent-crm.php', |
| 319 |
]; |
| 320 |
|
| 321 |
$this->backgroundInstaller($plugin, $plugin_id); |
| 322 |
|
| 323 |
if (defined('FLUENTCRM')) { |
| 324 |
return [ |
| 325 |
'is_installed' => true, |
| 326 |
'message' => __('FluentCRM plugin has been installed and activated successfully', 'fluent-support') |
| 327 |
]; |
| 328 |
} else { |
| 329 |
return $this->sendError([ |
| 330 |
'message' => __('Sorry! FluentCRM could not be installed. Please install manually', 'fluent-support') |
| 331 |
]); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
public function installFluentForm() |
| 336 |
{ |
| 337 |
|
| 338 |
if (defined('FLUENTFORM')) { |
| 339 |
return [ |
| 340 |
'is_installed' => true, |
| 341 |
'message' => __('Fluent Forms plugin has been installed and activated successfully', 'fluent-support') |
| 342 |
]; |
| 343 |
} |
| 344 |
|
| 345 |
if (!current_user_can('install_plugins')) { |
| 346 |
return $this->sendError([ |
| 347 |
'message' => __('Sorry! you do not have permission to install plugin', 'fluent-crm') |
| 348 |
]); |
| 349 |
} |
| 350 |
|
| 351 |
$plugin_id = 'fluent-crm'; |
| 352 |
$plugin = [ |
| 353 |
'name' => 'Fluent CRM', |
| 354 |
'repo-slug' => 'fluent-crm', |
| 355 |
'file' => 'fluent-crm.php', |
| 356 |
]; |
| 357 |
|
| 358 |
$this->backgroundInstaller($plugin, $plugin_id); |
| 359 |
|
| 360 |
if (defined('FLUENTCRM')) { |
| 361 |
return [ |
| 362 |
'is_installed' => true, |
| 363 |
'message' => __('Fluent Forms plugin has been installed and activated successfully', 'fluent-support') |
| 364 |
]; |
| 365 |
} else { |
| 366 |
return [ |
| 367 |
'is_installed' => false, |
| 368 |
'message' => __('Fluent Forms could not be installed', 'fluent-support') |
| 369 |
]; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
private function backgroundInstaller($plugin_to_install, $plugin_id) |
| 374 |
{ |
| 375 |
if (!empty($plugin_to_install['repo-slug'])) { |
| 376 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 377 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 378 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 379 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 380 |
|
| 381 |
WP_Filesystem(); |
| 382 |
|
| 383 |
$skin = new \Automatic_Upgrader_Skin(); |
| 384 |
$upgrader = new \WP_Upgrader($skin); |
| 385 |
$installed_plugins = array_reduce(array_keys(\get_plugins()), array($this, 'associate_plugin_file'), array()); |
| 386 |
$plugin_slug = $plugin_to_install['repo-slug']; |
| 387 |
$plugin_file = isset($plugin_to_install['file']) ? $plugin_to_install['file'] : $plugin_slug . '.php'; |
| 388 |
$installed = false; |
| 389 |
$activate = false; |
| 390 |
|
| 391 |
// See if the plugin is installed already. |
| 392 |
if (isset($installed_plugins[$plugin_file])) { |
| 393 |
$installed = true; |
| 394 |
$activate = !is_plugin_active($installed_plugins[$plugin_file]); |
| 395 |
} |
| 396 |
|
| 397 |
// Install this thing! |
| 398 |
if (!$installed) { |
| 399 |
// Suppress feedback. |
| 400 |
ob_start(); |
| 401 |
|
| 402 |
try { |
| 403 |
$plugin_information = plugins_api( |
| 404 |
'plugin_information', |
| 405 |
array( |
| 406 |
'slug' => $plugin_slug, |
| 407 |
'fields' => array( |
| 408 |
'short_description' => false, |
| 409 |
'sections' => false, |
| 410 |
'requires' => false, |
| 411 |
'rating' => false, |
| 412 |
'ratings' => false, |
| 413 |
'downloaded' => false, |
| 414 |
'last_updated' => false, |
| 415 |
'added' => false, |
| 416 |
'tags' => false, |
| 417 |
'homepage' => false, |
| 418 |
'donate_link' => false, |
| 419 |
'author_profile' => false, |
| 420 |
'author' => false, |
| 421 |
), |
| 422 |
) |
| 423 |
); |
| 424 |
|
| 425 |
if (is_wp_error($plugin_information)) { |
| 426 |
throw new \Exception($plugin_information->get_error_message()); |
| 427 |
} |
| 428 |
|
| 429 |
$package = $plugin_information->download_link; |
| 430 |
$download = $upgrader->download_package($package); |
| 431 |
|
| 432 |
if (is_wp_error($download)) { |
| 433 |
throw new \Exception($download->get_error_message()); |
| 434 |
} |
| 435 |
|
| 436 |
$working_dir = $upgrader->unpack_package($download, true); |
| 437 |
|
| 438 |
if (is_wp_error($working_dir)) { |
| 439 |
throw new \Exception($working_dir->get_error_message()); |
| 440 |
} |
| 441 |
|
| 442 |
$result = $upgrader->install_package( |
| 443 |
array( |
| 444 |
'source' => $working_dir, |
| 445 |
'destination' => WP_PLUGIN_DIR, |
| 446 |
'clear_destination' => false, |
| 447 |
'abort_if_destination_exists' => false, |
| 448 |
'clear_working' => true, |
| 449 |
'hook_extra' => array( |
| 450 |
'type' => 'plugin', |
| 451 |
'action' => 'install', |
| 452 |
), |
| 453 |
) |
| 454 |
); |
| 455 |
|
| 456 |
if (is_wp_error($result)) { |
| 457 |
throw new \Exception($result->get_error_message()); |
| 458 |
} |
| 459 |
|
| 460 |
$activate = true; |
| 461 |
|
| 462 |
} catch (\Exception $e) { |
| 463 |
} |
| 464 |
|
| 465 |
// Discard feedback. |
| 466 |
ob_end_clean(); |
| 467 |
} |
| 468 |
|
| 469 |
wp_clean_plugins_cache(); |
| 470 |
|
| 471 |
// Activate this thing. |
| 472 |
if ($activate) { |
| 473 |
try { |
| 474 |
$result = activate_plugin($installed ? $installed_plugins[$plugin_file] : $plugin_slug . '/' . $plugin_file); |
| 475 |
|
| 476 |
if (is_wp_error($result)) { |
| 477 |
throw new \Exception($result->get_error_message()); |
| 478 |
} |
| 479 |
} catch (\Exception $e) { |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
private function associate_plugin_file($plugins, $key) |
| 486 |
{ |
| 487 |
$path = explode('/', $key); |
| 488 |
$filename = end($path); |
| 489 |
$plugins[$filename] = $key; |
| 490 |
return $plugins; |
| 491 |
} |
| 492 |
|
| 493 |
} |
| 494 |
|