| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSnippets\App\Http\Controllers; |
| 4 |
|
| 5 |
|
| 6 |
use FluentSnippets\App\Helpers\Arr; |
| 7 |
use FluentSnippets\App\Helpers\Helper; |
| 8 |
use FluentSnippets\App\Model\Snippet; |
| 9 |
|
| 10 |
class SettingsController |
| 11 |
{ |
| 12 |
public static function getSettings(\WP_REST_Request $request) |
| 13 |
{ |
| 14 |
if ($restricted = self::isBlockedRequest()) { |
| 15 |
return $restricted; |
| 16 |
} |
| 17 |
|
| 18 |
$config = Helper::getIndexedConfig(); |
| 19 |
|
| 20 |
$defaults = [ |
| 21 |
'auto_disable' => 'yes', |
| 22 |
'auto_publish' => 'no', |
| 23 |
'remove_on_uninstall' => 'no' |
| 24 |
]; |
| 25 |
|
| 26 |
if (!$config || !is_array($config) || empty($config['meta'])) { |
| 27 |
$config = $defaults; |
| 28 |
} else { |
| 29 |
$config = \FluentSnippets\App\Helpers\Arr::only($config['meta'], array_keys($defaults)); |
| 30 |
$config = array_filter($config); |
| 31 |
} |
| 32 |
|
| 33 |
$settings = wp_parse_args($config, $defaults); |
| 34 |
|
| 35 |
return [ |
| 36 |
'settings' => $settings, |
| 37 |
'is_standalone' => defined('FLUENT_SNIPPETS_RUNNING_MU'), |
| 38 |
'secret_url' => site_url('index.php?fluent_snippets=1&snippet_secret=' . Helper::getSecretKey()) |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
public static function saveSettings(\WP_REST_Request $request) |
| 43 |
{ |
| 44 |
if ($restricted = self::isBlockedRequest()) { |
| 45 |
return $restricted; |
| 46 |
} |
| 47 |
|
| 48 |
$settings = $request->get_param('settings'); |
| 49 |
|
| 50 |
if (!is_array($settings)) { |
| 51 |
return new \WP_Error('invalid_settings', 'Invalid settings'); |
| 52 |
} |
| 53 |
|
| 54 |
$defaults = [ |
| 55 |
'auto_disable' => 'yes', |
| 56 |
'auto_publish' => 'no', |
| 57 |
'remove_on_uninstall' => 'no' |
| 58 |
]; |
| 59 |
|
| 60 |
$settings = Arr::only($settings, array_keys($defaults)); |
| 61 |
$settings = array_filter($settings); |
| 62 |
|
| 63 |
$config = Helper::getIndexedConfig(); |
| 64 |
|
| 65 |
if (!$config) { |
| 66 |
Helper::cacheSnippetIndex(); |
| 67 |
} |
| 68 |
|
| 69 |
$config = Helper::getIndexedConfig(); |
| 70 |
|
| 71 |
if (!$config) { |
| 72 |
return new \WP_Error('invalid_config', 'Config file could not be generated'); |
| 73 |
} |
| 74 |
|
| 75 |
$config['meta']['auto_disable'] = sanitize_text_field($settings['auto_disable']); |
| 76 |
$config['meta']['auto_publish'] = sanitize_text_field($settings['auto_publish']); |
| 77 |
$config['meta']['remove_on_uninstall'] = sanitize_text_field($settings['remove_on_uninstall']); |
| 78 |
|
| 79 |
$config = Helper::saveIndexedConfig($config); |
| 80 |
|
| 81 |
if (is_wp_error($config)) { |
| 82 |
return $config; |
| 83 |
} |
| 84 |
|
| 85 |
return [ |
| 86 |
'message' => __('Settings has been successfully updated', 'easy-code-manager'), |
| 87 |
'settings' => $settings |
| 88 |
]; |
| 89 |
} |
| 90 |
|
| 91 |
public static function disableSafeMode(\WP_REST_Request $request) |
| 92 |
{ |
| 93 |
if ($restricted = self::isBlockedRequest()) { |
| 94 |
return $restricted; |
| 95 |
} |
| 96 |
|
| 97 |
$config = Helper::getIndexedConfig(); |
| 98 |
|
| 99 |
if (!$config) { |
| 100 |
return new \WP_Error('invalid_config', 'Config file could not be generated'); |
| 101 |
} |
| 102 |
|
| 103 |
$config['meta']['force_disabled'] = 'no'; |
| 104 |
|
| 105 |
$config = Helper::saveIndexedConfig($config); |
| 106 |
|
| 107 |
return [ |
| 108 |
'message' => __('Safe mode has been disabled', 'easy-code-manager') |
| 109 |
]; |
| 110 |
} |
| 111 |
|
| 112 |
public static function configStandAloneSystem(\WP_REST_Request $request) |
| 113 |
{ |
| 114 |
if ($restricted = self::isBlockedRequest()) { |
| 115 |
return $restricted; |
| 116 |
} |
| 117 |
|
| 118 |
$isEnable = $request->get_param('enable') == 'yes'; |
| 119 |
|
| 120 |
if ($isEnable == 'yes') { |
| 121 |
$result = Helper::enableStandAlone(); |
| 122 |
$message = __('Standalone mode has been activated', 'easy-code-manager'); |
| 123 |
} else { |
| 124 |
$message = __('Standalone mode has been deactivated', 'easy-code-manager'); |
| 125 |
$result = Helper::disableStandAlone(); |
| 126 |
} |
| 127 |
|
| 128 |
if (is_wp_error($result)) { |
| 129 |
return $result; |
| 130 |
} |
| 131 |
|
| 132 |
return [ |
| 133 |
'message' => $message, |
| 134 |
'is_standalone' => defined('FLUENT_SNIPPETS_RUNNING_MU'), |
| 135 |
]; |
| 136 |
} |
| 137 |
|
| 138 |
private static function isBlockedRequest() |
| 139 |
{ |
| 140 |
if (current_user_can('unfiltered_html') && current_user_can('manage_options')) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
return new \WP_Error('invalid_request', 'You do not have permission to perform this action. Required Permission: unfiltered_html & manage_options'); |
| 145 |
} |
| 146 |
|
| 147 |
public static function getRestOptions(\WP_REST_Request $request) |
| 148 |
{ |
| 149 |
$optionKey = $request->get_param('rest_key'); |
| 150 |
$options = []; |
| 151 |
|
| 152 |
if ($optionKey == 'tax_term_groups') { |
| 153 |
// Get public taxonomies |
| 154 |
$taxonomies = get_taxonomies([ |
| 155 |
'public' => true |
| 156 |
]); |
| 157 |
|
| 158 |
$taxonomies = array_keys($taxonomies); |
| 159 |
$terms = get_terms([ |
| 160 |
'taxonomy' => $taxonomies, |
| 161 |
'suppress_filter' => true, |
| 162 |
'hide_empty' => false, |
| 163 |
'number' => 9000 |
| 164 |
]); |
| 165 |
|
| 166 |
foreach ($terms as $term) { |
| 167 |
if (!isset($formattedTaxGroups[$term->taxonomy])) { |
| 168 |
$options[$term->taxonomy] = [ |
| 169 |
'label' => $term->taxonomy, |
| 170 |
'options' => [], |
| 171 |
]; |
| 172 |
} |
| 173 |
|
| 174 |
$options[$term->taxonomy]['options'][] = [ |
| 175 |
'id' => (string)$term->term_id, |
| 176 |
'title' => $term->name, |
| 177 |
]; |
| 178 |
} |
| 179 |
|
| 180 |
return [ |
| 181 |
'options' => $options, |
| 182 |
'is_cachable' => true, |
| 183 |
]; |
| 184 |
} |
| 185 |
|
| 186 |
if ($optionKey == 'post_cpt_groups') { |
| 187 |
|
| 188 |
$publicPostTypes = get_post_types([ |
| 189 |
'public' => true |
| 190 |
]); |
| 191 |
|
| 192 |
$posts = get_posts([ |
| 193 |
'post_type' => array_keys($publicPostTypes), |
| 194 |
'numberposts' => 200, |
| 195 |
'post_status' => ['publish', 'draft', 'private'], |
| 196 |
's' => sanitize_text_field($request->get_param('search')), |
| 197 |
'search_columns' => ['post_title'] |
| 198 |
]); |
| 199 |
|
| 200 |
$requestValues = $request->get_param('values'); |
| 201 |
|
| 202 |
if (empty($requestValues) || !is_array($requestValues)) { |
| 203 |
$requestValues = []; |
| 204 |
} |
| 205 |
|
| 206 |
$includedIds = []; |
| 207 |
|
| 208 |
foreach ($posts as $post) { |
| 209 |
if (!isset($options[$post->post_type])) { |
| 210 |
$options[$post->post_type] = [ |
| 211 |
'label' => ucfirst($post->post_type), |
| 212 |
'options' => [], |
| 213 |
]; |
| 214 |
} |
| 215 |
|
| 216 |
$includedIds[] = $post->ID; |
| 217 |
|
| 218 |
$options[$post->post_type]['options'][] = [ |
| 219 |
'id' => (string)$post->ID, |
| 220 |
'title' => $post->post_title, |
| 221 |
]; |
| 222 |
} |
| 223 |
|
| 224 |
$restIds = array_diff($requestValues, $includedIds); |
| 225 |
$restIds = array_filter($restIds, 'is_int'); |
| 226 |
|
| 227 |
if ($restIds) { |
| 228 |
$restPosts = get_posts([ |
| 229 |
'post_type' => 'any', |
| 230 |
'numberposts' => 200, |
| 231 |
'post_status' => ['publish', 'draft', 'private'], |
| 232 |
'post__in' => $restIds |
| 233 |
]); |
| 234 |
|
| 235 |
foreach ($restPosts as $post) { |
| 236 |
if (!isset($options[$post->post_type])) { |
| 237 |
$options[$post->post_type] = [ |
| 238 |
'label' => ucfirst($post->post_type), |
| 239 |
'options' => [], |
| 240 |
]; |
| 241 |
} |
| 242 |
|
| 243 |
$options[$post->post_type]['options'][] = [ |
| 244 |
'id' => (string)$post->ID, |
| 245 |
'title' => $post->post_title, |
| 246 |
]; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
return [ |
| 252 |
'options' => $options, |
| 253 |
'is_cachable' => false, |
| 254 |
]; |
| 255 |
} |
| 256 |
|
| 257 |
if ($optionKey == 'fluentcrm_tags') { |
| 258 |
if (!defined('FLUENTCRM')) { |
| 259 |
return [ |
| 260 |
'options' => [], |
| 261 |
'is_cachable' => true |
| 262 |
]; |
| 263 |
} |
| 264 |
|
| 265 |
$tags = \FluentCrm\App\Models\Tag::orderBy('title', 'ASC')->get(); |
| 266 |
foreach ($tags as $tag) { |
| 267 |
$options[] = [ |
| 268 |
'id' => (string)$tag->id, |
| 269 |
'title' => $tag->title, |
| 270 |
]; |
| 271 |
} |
| 272 |
|
| 273 |
return [ |
| 274 |
'options' => $options, |
| 275 |
'is_cachable' => true |
| 276 |
]; |
| 277 |
|
| 278 |
} |
| 279 |
|
| 280 |
if ($optionKey == 'fluentcrm_lists') { |
| 281 |
if (!defined('FLUENTCRM')) { |
| 282 |
return [ |
| 283 |
'options' => [], |
| 284 |
'is_cachable' => true |
| 285 |
]; |
| 286 |
} |
| 287 |
|
| 288 |
$tags = \FluentCrm\App\Models\Lists::orderBy('title', 'ASC')->get(); |
| 289 |
foreach ($tags as $tag) { |
| 290 |
$options[] = [ |
| 291 |
'id' => (string)$tag->id, |
| 292 |
'title' => $tag->title, |
| 293 |
]; |
| 294 |
} |
| 295 |
|
| 296 |
return [ |
| 297 |
'options' => $options, |
| 298 |
'is_cachable' => true |
| 299 |
]; |
| 300 |
|
| 301 |
} |
| 302 |
|
| 303 |
return [ |
| 304 |
'options' => $options |
| 305 |
]; |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
public static function installPlugin(\WP_REST_Request $request) |
| 310 |
{ |
| 311 |
$pluginSlug = $request->get_param('plugin_slug'); |
| 312 |
$plugin = [ |
| 313 |
'name' => $pluginSlug, |
| 314 |
'repo-slug' => $pluginSlug, |
| 315 |
'file' => $pluginSlug . '.php' |
| 316 |
]; |
| 317 |
|
| 318 |
$UrlMaps = [ |
| 319 |
'fluent-smtp' => [ |
| 320 |
'admin_url' => admin_url('options-general.php?page=fluent-mail#/'), |
| 321 |
'title' => __('Go to FluentSMTP Dashboard', 'fluent-smtp') |
| 322 |
], |
| 323 |
'fluentform' => [ |
| 324 |
'admin_url' => admin_url('admin.php?page=fluent_forms'), |
| 325 |
'title' => __('Go to Fluent Forms Dashboard', 'fluent-smtp') |
| 326 |
], |
| 327 |
'fluent-crm' => [ |
| 328 |
'admin_url' => admin_url('admin.php?page=fluentcrm-admin'), |
| 329 |
'title' => __('Go to FluentCRM Dashboard', 'fluent-smtp') |
| 330 |
], |
| 331 |
'ninja-tables' => [ |
| 332 |
'admin_url' => admin_url('admin.php?page=ninja_tables#/'), |
| 333 |
'title' => __('Go to Ninja Tables Dashboard', 'fluent-smtp') |
| 334 |
] |
| 335 |
]; |
| 336 |
|
| 337 |
if (!isset($UrlMaps[$pluginSlug]) || (defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS)) { |
| 338 |
|
| 339 |
return new \WP_Error('permission_error', 'Sorry, You can not install this plugin'); |
| 340 |
} |
| 341 |
|
| 342 |
try { |
| 343 |
self::backgroundInstaller($plugin); |
| 344 |
return [ |
| 345 |
'message' => __('Plugin has been successfully installed.', 'fluent-smtp'), |
| 346 |
'info' => $UrlMaps[$pluginSlug] |
| 347 |
]; |
| 348 |
} catch (\Exception $exception) { |
| 349 |
return new \WP_Error('install_error', $exception->getMessage()); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
private static function backgroundInstaller($plugin_to_install) |
| 354 |
{ |
| 355 |
if (!empty($plugin_to_install['repo-slug'])) { |
| 356 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 357 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 358 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 359 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 360 |
|
| 361 |
WP_Filesystem(); |
| 362 |
|
| 363 |
$skin = new \Automatic_Upgrader_Skin(); |
| 364 |
$upgrader = new \WP_Upgrader($skin); |
| 365 |
$installed_plugins = array_reduce(array_keys(\get_plugins()), array(__CLASS__, 'associate_plugin_file'), array()); |
| 366 |
$plugin_slug = $plugin_to_install['repo-slug']; |
| 367 |
$plugin_file = isset($plugin_to_install['file']) ? $plugin_to_install['file'] : $plugin_slug . '.php'; |
| 368 |
$installed = false; |
| 369 |
$activate = false; |
| 370 |
|
| 371 |
// See if the plugin is installed already. |
| 372 |
if (isset($installed_plugins[$plugin_file])) { |
| 373 |
$installed = true; |
| 374 |
$activate = !is_plugin_active($installed_plugins[$plugin_file]); |
| 375 |
} |
| 376 |
|
| 377 |
// Install this thing! |
| 378 |
if (!$installed) { |
| 379 |
// Suppress feedback. |
| 380 |
ob_start(); |
| 381 |
|
| 382 |
try { |
| 383 |
$plugin_information = plugins_api( |
| 384 |
'plugin_information', |
| 385 |
array( |
| 386 |
'slug' => $plugin_slug, |
| 387 |
'fields' => array( |
| 388 |
'short_description' => false, |
| 389 |
'sections' => false, |
| 390 |
'requires' => false, |
| 391 |
'rating' => false, |
| 392 |
'ratings' => false, |
| 393 |
'downloaded' => false, |
| 394 |
'last_updated' => false, |
| 395 |
'added' => false, |
| 396 |
'tags' => false, |
| 397 |
'homepage' => false, |
| 398 |
'donate_link' => false, |
| 399 |
'author_profile' => false, |
| 400 |
'author' => false, |
| 401 |
), |
| 402 |
) |
| 403 |
); |
| 404 |
|
| 405 |
if (is_wp_error($plugin_information)) { |
| 406 |
throw new \Exception($plugin_information->get_error_message()); |
| 407 |
} |
| 408 |
|
| 409 |
$package = $plugin_information->download_link; |
| 410 |
$download = $upgrader->download_package($package); |
| 411 |
|
| 412 |
if (is_wp_error($download)) { |
| 413 |
throw new \Exception($download->get_error_message()); |
| 414 |
} |
| 415 |
|
| 416 |
$working_dir = $upgrader->unpack_package($download, true); |
| 417 |
|
| 418 |
if (is_wp_error($working_dir)) { |
| 419 |
throw new \Exception($working_dir->get_error_message()); |
| 420 |
} |
| 421 |
|
| 422 |
$result = $upgrader->install_package( |
| 423 |
array( |
| 424 |
'source' => $working_dir, |
| 425 |
'destination' => WP_PLUGIN_DIR, |
| 426 |
'clear_destination' => false, |
| 427 |
'abort_if_destination_exists' => false, |
| 428 |
'clear_working' => true, |
| 429 |
'hook_extra' => array( |
| 430 |
'type' => 'plugin', |
| 431 |
'action' => 'install', |
| 432 |
), |
| 433 |
) |
| 434 |
); |
| 435 |
|
| 436 |
if (is_wp_error($result)) { |
| 437 |
throw new \Exception($result->get_error_message()); |
| 438 |
} |
| 439 |
|
| 440 |
$activate = true; |
| 441 |
|
| 442 |
} catch (\Exception $e) { |
| 443 |
throw new \Exception($e->getMessage()); |
| 444 |
} |
| 445 |
|
| 446 |
// Discard feedback. |
| 447 |
ob_end_clean(); |
| 448 |
} |
| 449 |
|
| 450 |
wp_clean_plugins_cache(); |
| 451 |
|
| 452 |
// Activate this thing. |
| 453 |
if ($activate) { |
| 454 |
try { |
| 455 |
$result = activate_plugin($installed ? $installed_plugins[$plugin_file] : $plugin_slug . '/' . $plugin_file); |
| 456 |
|
| 457 |
if (is_wp_error($result)) { |
| 458 |
throw new \Exception($result->get_error_message()); |
| 459 |
} |
| 460 |
} catch (\Exception $e) { |
| 461 |
throw new \Exception($e->getMessage()); |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
public static function associate_plugin_file($plugins, $key) |
| 468 |
{ |
| 469 |
$path = explode('/', $key); |
| 470 |
$filename = end($path); |
| 471 |
$plugins[$filename] = $key; |
| 472 |
return $plugins; |
| 473 |
} |
| 474 |
} |
| 475 |
|