| 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 |
class SettingsController extends Controller |
| 13 |
{ |
| 14 |
public function getSettings(Request $request) |
| 15 |
{ |
| 16 |
$settingsKey = $request->get('settings_key'); |
| 17 |
|
| 18 |
return (new Settings)->get($settingsKey); |
| 19 |
} |
| 20 |
|
| 21 |
public function getIntegrationSettings(Request $request) |
| 22 |
{ |
| 23 |
$settings = Meta::where('object_type', 'integration_settings')->get(); |
| 24 |
$integrationSettings = []; |
| 25 |
foreach ($settings as $index => $setting) { |
| 26 |
$data = maybe_unserialize($setting->value); |
| 27 |
if (!empty($data['status']) && $data && $data['status'] == 'yes') { |
| 28 |
$integrationSettings[] = $setting->key; |
| 29 |
} |
| 30 |
} |
| 31 |
return $integrationSettings; |
| 32 |
} |
| 33 |
|
| 34 |
public function saveSettings(Request $request) |
| 35 |
{ |
| 36 |
$settingsKey = $request->get('settings_key'); |
| 37 |
$settings = wp_unslash($request->get('settings')); |
| 38 |
|
| 39 |
(new Settings)->save($settingsKey, $settings); |
| 40 |
|
| 41 |
return [ |
| 42 |
'message' => __('Settings has been updated', 'fluent-support') |
| 43 |
]; |
| 44 |
} |
| 45 |
|
| 46 |
public function getPages() |
| 47 |
{ |
| 48 |
return [ |
| 49 |
'pages' => Helper::getWPPages() |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
public function setupPortal(Request $request) |
| 54 |
{ |
| 55 |
$mailbox = $request->get('mailbox'); |
| 56 |
$this->validate($mailbox, [ |
| 57 |
'name' => 'required', |
| 58 |
'email' => 'required|email', |
| 59 |
'box_type' => 'required' |
| 60 |
]); |
| 61 |
|
| 62 |
$settings = $request->get('global_settings'); |
| 63 |
|
| 64 |
$createPage = $settings['create_portal_page'] == 'yes'; |
| 65 |
|
| 66 |
if (!$createPage && empty($settings['portal_page_id'])) { |
| 67 |
return $this->sendError([ |
| 68 |
'message' => __('Please select a page or enable create page', 'fluent-support') |
| 69 |
]); |
| 70 |
} |
| 71 |
|
| 72 |
if ($createPage) { |
| 73 |
// we have to create the page |
| 74 |
$page_id = wp_insert_post( |
| 75 |
array( |
| 76 |
'comment_status' => 'close', |
| 77 |
'ping_status' => 'close', |
| 78 |
'post_author' => get_current_user_id(), |
| 79 |
'post_title' => __('Support Portal', 'fluent-support'), |
| 80 |
'post_status' => 'publish', |
| 81 |
'post_content' => '[fluent_support_portal]', |
| 82 |
'post_type' => 'page' |
| 83 |
) |
| 84 |
); |
| 85 |
} else { |
| 86 |
$page_id = intval($settings['portal_page_id']); |
| 87 |
} |
| 88 |
|
| 89 |
$newMailBox = MailBox::first(); |
| 90 |
if (!$newMailBox) { |
| 91 |
$mailbox['is_default'] = 'yes'; |
| 92 |
$mailbox['created_by'] = get_current_user_id(); |
| 93 |
$mailbox['settings']['admin_email_address'] = $mailbox['email']; |
| 94 |
$newMailBox = MailBox::create($mailbox); |
| 95 |
} |
| 96 |
|
| 97 |
$settingsClass = new Settings(); |
| 98 |
$globalSettings = $settingsClass->globalBusinessSettings(); |
| 99 |
|
| 100 |
$globalSettings['portal_page_id'] = $page_id; |
| 101 |
|
| 102 |
$settingsClass->save('global_business_settings', $globalSettings); |
| 103 |
|
| 104 |
|
| 105 |
if(defined('WC_PLUGIN_FILE')) { |
| 106 |
// URL Flash |
| 107 |
flush_rewrite_rules(false); |
| 108 |
} |
| 109 |
|
| 110 |
return [ |
| 111 |
'mailbox' => $newMailBox, |
| 112 |
'global_settings' => $globalSettings, |
| 113 |
'mailboxes' => MailBox::select(['id', 'name', 'settings'])->get() |
| 114 |
]; |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
public function getFluentCRMSettings(Request $request) |
| 119 |
{ |
| 120 |
if (defined('FLUENTCRM')) { |
| 121 |
$settingDefault = [ |
| 122 |
'enabled' => 'no', |
| 123 |
'default_status' => 'subscribed', |
| 124 |
'assigned_list' => '', |
| 125 |
'assigned_tags' => [] |
| 126 |
]; |
| 127 |
|
| 128 |
$settings = Helper::getOption('_fluentcrm_intergration_settings'); |
| 129 |
|
| 130 |
$settings = wp_parse_args($settings, $settingDefault); |
| 131 |
|
| 132 |
$settingsFields = [ |
| 133 |
'enabled' => [ |
| 134 |
'type' => 'inline-checkbox', |
| 135 |
'true_label' => 'yes', |
| 136 |
'false_label' => 'no', |
| 137 |
'checkbox_label' => __('Enable FluentCRM Integration', 'fluent-support') |
| 138 |
], |
| 139 |
'default_status' => [ |
| 140 |
'type' => 'input-radio', |
| 141 |
'label' => __('Default status for new contacts', 'fluent-support'), |
| 142 |
'options' => [ |
| 143 |
[ |
| 144 |
'id' => 'subscribed', |
| 145 |
'label' => __('Subscribed', 'fluent-support') |
| 146 |
], |
| 147 |
[ |
| 148 |
'id' => 'pending', |
| 149 |
'label' => __('Pending', 'fluent-support') |
| 150 |
] |
| 151 |
], |
| 152 |
'dependency' => [ |
| 153 |
'depends_on' => 'enabled', |
| 154 |
'operator' => '=', |
| 155 |
'value' => 'yes' |
| 156 |
], |
| 157 |
'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') |
| 158 |
], |
| 159 |
'assigned_list' => [ |
| 160 |
'type' => 'input-options', |
| 161 |
'label' => __('Add to FluentCRM list (optional)', 'fluent-crm'), |
| 162 |
'options' => \FluentCrm\App\Models\Lists::select(['id', 'title'])->orderBy('title', 'ASC')->get(), |
| 163 |
'dependency' => [ |
| 164 |
'depends_on' => 'enabled', |
| 165 |
'operator' => '=', |
| 166 |
'value' => 'yes' |
| 167 |
], |
| 168 |
], |
| 169 |
'assigned_tags' => [ |
| 170 |
'type' => 'input-options', |
| 171 |
'multiple' => true, |
| 172 |
'label' => __('Add to Tags', 'fluent-crm'), |
| 173 |
'options' => \FluentCrm\App\Models\Tag::select(['id', 'title'])->orderBy('title', 'ASC')->get(), |
| 174 |
'dependency' => [ |
| 175 |
'depends_on' => 'enabled', |
| 176 |
'operator' => '=', |
| 177 |
'value' => 'yes' |
| 178 |
] |
| 179 |
] |
| 180 |
]; |
| 181 |
|
| 182 |
return [ |
| 183 |
'is_installed' => true, |
| 184 |
'settings' => $settings, |
| 185 |
'settings_fields' => $settingsFields, |
| 186 |
'fluentcrm_logo' => FLUENT_SUPPORT_PLUGIN_URL.'assets/images/fluentcrm-logo.svg' |
| 187 |
]; |
| 188 |
} |
| 189 |
|
| 190 |
return [ |
| 191 |
'is_installed' => false, |
| 192 |
'fluentcrm_logo' => FLUENT_SUPPORT_PLUGIN_URL.'assets/images/fluentcrm-logo.svg' |
| 193 |
]; |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
public function installFluentCRM() |
| 198 |
{ |
| 199 |
|
| 200 |
if (defined('FLUENTCRM')) { |
| 201 |
return [ |
| 202 |
'is_installed' => true, |
| 203 |
'message' => __('FluentCRM plugin has been installed and activated successfully', 'fluent-support') |
| 204 |
]; |
| 205 |
} |
| 206 |
|
| 207 |
if (!current_user_can('install_plugins')) { |
| 208 |
return $this->sendError([ |
| 209 |
'message' => __('Sorry! you do not have permission to install plugin', 'fluent-crm') |
| 210 |
]); |
| 211 |
} |
| 212 |
|
| 213 |
$plugin_id = 'fluent-connect'; |
| 214 |
$plugin = [ |
| 215 |
'name' => 'Fluent CRM', |
| 216 |
'repo-slug' => 'fluent-crm', |
| 217 |
'file' => 'fluent-crm.php', |
| 218 |
]; |
| 219 |
|
| 220 |
$this->backgroundInstaller($plugin, $plugin_id); |
| 221 |
|
| 222 |
if (defined('FLUENTCRM')) { |
| 223 |
return [ |
| 224 |
'is_installed' => true, |
| 225 |
'message' => __('FluentCRM plugin has been installed and activated successfully', 'fluent-support') |
| 226 |
]; |
| 227 |
} else { |
| 228 |
return $this->sendError([ |
| 229 |
'message' => __('Sorry! FluentCRM could not be installed. Please install manually', 'fluent-support') |
| 230 |
]); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
private function backgroundInstaller($plugin_to_install, $plugin_id) |
| 235 |
{ |
| 236 |
if (!empty($plugin_to_install['repo-slug'])) { |
| 237 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 238 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 239 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 240 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 241 |
|
| 242 |
WP_Filesystem(); |
| 243 |
|
| 244 |
$skin = new \Automatic_Upgrader_Skin(); |
| 245 |
$upgrader = new \WP_Upgrader($skin); |
| 246 |
$installed_plugins = array_reduce(array_keys(\get_plugins()), array($this, 'associate_plugin_file'), array()); |
| 247 |
$plugin_slug = $plugin_to_install['repo-slug']; |
| 248 |
$plugin_file = isset($plugin_to_install['file']) ? $plugin_to_install['file'] : $plugin_slug . '.php'; |
| 249 |
$installed = false; |
| 250 |
$activate = false; |
| 251 |
|
| 252 |
// See if the plugin is installed already. |
| 253 |
if (isset($installed_plugins[$plugin_file])) { |
| 254 |
$installed = true; |
| 255 |
$activate = !is_plugin_active($installed_plugins[$plugin_file]); |
| 256 |
} |
| 257 |
|
| 258 |
// Install this thing! |
| 259 |
if (!$installed) { |
| 260 |
// Suppress feedback. |
| 261 |
ob_start(); |
| 262 |
|
| 263 |
try { |
| 264 |
$plugin_information = plugins_api( |
| 265 |
'plugin_information', |
| 266 |
array( |
| 267 |
'slug' => $plugin_slug, |
| 268 |
'fields' => array( |
| 269 |
'short_description' => false, |
| 270 |
'sections' => false, |
| 271 |
'requires' => false, |
| 272 |
'rating' => false, |
| 273 |
'ratings' => false, |
| 274 |
'downloaded' => false, |
| 275 |
'last_updated' => false, |
| 276 |
'added' => false, |
| 277 |
'tags' => false, |
| 278 |
'homepage' => false, |
| 279 |
'donate_link' => false, |
| 280 |
'author_profile' => false, |
| 281 |
'author' => false, |
| 282 |
), |
| 283 |
) |
| 284 |
); |
| 285 |
|
| 286 |
if (is_wp_error($plugin_information)) { |
| 287 |
throw new \Exception($plugin_information->get_error_message()); |
| 288 |
} |
| 289 |
|
| 290 |
$package = $plugin_information->download_link; |
| 291 |
$download = $upgrader->download_package($package); |
| 292 |
|
| 293 |
if (is_wp_error($download)) { |
| 294 |
throw new \Exception($download->get_error_message()); |
| 295 |
} |
| 296 |
|
| 297 |
$working_dir = $upgrader->unpack_package($download, true); |
| 298 |
|
| 299 |
if (is_wp_error($working_dir)) { |
| 300 |
throw new \Exception($working_dir->get_error_message()); |
| 301 |
} |
| 302 |
|
| 303 |
$result = $upgrader->install_package( |
| 304 |
array( |
| 305 |
'source' => $working_dir, |
| 306 |
'destination' => WP_PLUGIN_DIR, |
| 307 |
'clear_destination' => false, |
| 308 |
'abort_if_destination_exists' => false, |
| 309 |
'clear_working' => true, |
| 310 |
'hook_extra' => array( |
| 311 |
'type' => 'plugin', |
| 312 |
'action' => 'install', |
| 313 |
), |
| 314 |
) |
| 315 |
); |
| 316 |
|
| 317 |
if (is_wp_error($result)) { |
| 318 |
throw new \Exception($result->get_error_message()); |
| 319 |
} |
| 320 |
|
| 321 |
$activate = true; |
| 322 |
|
| 323 |
} catch (\Exception $e) { |
| 324 |
} |
| 325 |
|
| 326 |
// Discard feedback. |
| 327 |
ob_end_clean(); |
| 328 |
} |
| 329 |
|
| 330 |
wp_clean_plugins_cache(); |
| 331 |
|
| 332 |
// Activate this thing. |
| 333 |
if ($activate) { |
| 334 |
try { |
| 335 |
$result = activate_plugin($installed ? $installed_plugins[$plugin_file] : $plugin_slug . '/' . $plugin_file); |
| 336 |
|
| 337 |
if (is_wp_error($result)) { |
| 338 |
throw new \Exception($result->get_error_message()); |
| 339 |
} |
| 340 |
} catch (\Exception $e) { |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
private function associate_plugin_file($plugins, $key) |
| 347 |
{ |
| 348 |
$path = explode('/', $key); |
| 349 |
$filename = end($path); |
| 350 |
$plugins[$filename] = $key; |
| 351 |
return $plugins; |
| 352 |
} |
| 353 |
|
| 354 |
} |
| 355 |
|