| @@ -283,14 +283,8 @@ | ||
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | public function installPlugin(Request $request) |
| 286 | 286 | { |
| 287 | - if (!current_user_can('install_plugins')) { | |
| 288 | - return $this->sendError([ | |
| 289 | - 'message' => __('You do not have permission to install plugins', 'fluent-community') | |
| 290 | - ]); | |
| 291 | - } | |
| 292 | - | |
| 293 | 287 | $pluginSlug = $request->get('plugin'); |
| 294 | 288 | |
| 295 | 289 | $addons = $this->getAddons(); |
| 296 | 290 | |
| @@ -299,8 +293,39 @@ | ||
| 299 | 293 | } |
| 300 | 294 | |
| 301 | 295 | $details = $addons[$pluginSlug]; |
| 302 | 296 | |
| 297 | + $pluginFile = $pluginSlug . '/' . $pluginSlug . '.php'; | |
| 298 | + | |
| 299 | + // Already on disk but deactivated: that is an activation, so it answers to | |
| 300 | + // activate_plugins and needs no download. | |
| 301 | + if (!$details['is_repo'] && $this->isPluginInstalled($pluginFile)) { | |
| 302 | + if (!current_user_can('activate_plugins')) { | |
| 303 | + return $this->sendError(['message' => __('You do not have permission to activate plugins', 'fluent-community')]); | |
| 304 | + } | |
| 305 | + | |
| 306 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 307 | + | |
| 308 | + if (!is_plugin_active($pluginFile)) { | |
| 309 | + $activated = activate_plugin($pluginFile); | |
| 310 | + | |
| 311 | + if (is_wp_error($activated)) { | |
| 312 | + return $this->sendError(['message' => wp_kses_post($activated->get_error_message())]); | |
| 313 | + } | |
| 314 | + } | |
| 315 | + | |
| 316 | + return [ | |
| 317 | + 'message' => __('Plugin has been activated Successfully', 'fluent-community'), | |
| 318 | + 'is_installed' => true | |
| 319 | + ]; | |
| 320 | + } | |
| 321 | + | |
| 322 | + if (!current_user_can('install_plugins')) { | |
| 323 | + return $this->sendError([ | |
| 324 | + 'message' => __('You do not have permission to install plugins', 'fluent-community') | |
| 325 | + ]); | |
| 326 | + } | |
| 327 | + | |
| 303 | 328 | if ($details['is_repo']) { |
| 304 | 329 | $plugin = [ |
| 305 | 330 | 'name' => $details['title'], |
| 306 | 331 | 'repo-slug' => $pluginSlug, |
| @@ -311,13 +336,15 @@ | ||
| 311 | 336 | } catch (\Exception $e) { |
| 312 | 337 | return $this->sendError(['message' => $e->getMessage()]); |
| 313 | 338 | } |
| 314 | 339 | } else { |
| 340 | + $installHook = ''; | |
| 341 | + | |
| 315 | 342 | if ($pluginSlug == 'fluent-messaging') { |
| 316 | 343 | if (!defined('FLUENT_COMMUNITY_PRO')) { |
| 317 | 344 | return $this->sendError(['message' => __('FluentMessaging is a Pro Plugin. Please install FluentCommunity Pro first.', 'fluent-community')]); |
| 318 | 345 | } |
| 319 | - do_action('fluent_community/install_messaging_plugin'); | |
| 346 | + $installHook = 'fluent_community/install_messaging_plugin'; | |
| 320 | 347 | } else if ($pluginSlug == 'fluent-player') { |
| 321 | 348 | |
| 322 | 349 | if (!defined('FLUENT_COMMUNITY_PRO')) { |
| 323 | 350 | return $this->sendError(['message' => __('FluentPlayer requires the pro version of FluentCommunity. Please install FluentCommunity Pro first.', 'fluent-community')]); |
| @@ -322,15 +349,22 @@ | ||
| 322 | 349 | if (!defined('FLUENT_COMMUNITY_PRO')) { |
| 323 | 350 | return $this->sendError(['message' => __('FluentPlayer requires the pro version of FluentCommunity. Please install FluentCommunity Pro first.', 'fluent-community')]); |
| 324 | 351 | } |
| 325 | 352 | |
| 326 | - do_action('fluent_community/install_fluent_player_plugin'); | |
| 353 | + $installHook = 'fluent_community/install_fluent_player_plugin'; | |
| 327 | 354 | } else if ($pluginSlug == 'fluent-notify') { |
| 328 | - try { | |
| 329 | - do_action('fluent_community/install_fluent_notify_plugin'); | |
| 330 | - } catch (\Exception $e) { | |
| 331 | - return $this->sendError(['message' => $e->getMessage()]); | |
| 332 | - } | |
| 355 | + $installHook = 'fluent_community/install_fluent_notify_plugin'; | |
| 356 | + } | |
| 357 | + | |
| 358 | + if (!$installHook || !has_action($installHook)) { | |
| 359 | + return $this->sendError(['message' => __('This plugin can not be installed from here. Please install it manually.', 'fluent-community')]); | |
| 360 | + } | |
| 361 | + | |
| 362 | + try { | |
| 363 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- every assigned value is a literal fluent_community/ prefixed hook, guarded above | |
| 364 | + do_action($installHook); | |
| 365 | + } catch (\Exception $e) { | |
| 366 | + return $this->sendError(['message' => $e->getMessage()]); | |
| 333 | 367 | } |
| 334 | 368 | } |
| 335 | 369 | |
| 336 | 370 | return [ |