-
Notifications
You must be signed in to change notification settings - Fork 611
/
PluginsController.php
169 lines (146 loc) · 5.08 KB
/
PluginsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\controllers;
use Craft;
use craft\base\PluginInterface;
use craft\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\Response;
/**
* The PluginsController class is a controller that handles various plugin related tasks such installing, uninstalling,
* enabling, disabling and saving plugin settings in the control panel.
* Note that all actions in the controller require an authenticated Craft session via [[allowAnonymous]].
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class PluginsController extends Controller
{
/**
* @inheritdoc
*/
public function beforeAction($action): bool
{
if (!parent::beforeAction($action)) {
return false;
}
// All plugin actions require an admin
$this->requireAdmin();
return true;
}
/**
* Installs a plugin.
*
* @return Response|null
*/
public function actionInstallPlugin(): ?Response
{
$this->requirePostRequest();
$pluginHandle = $this->request->getRequiredBodyParam('pluginHandle');
$edition = $this->request->getBodyParam('edition');
$success = Craft::$app->getPlugins()->installPlugin($pluginHandle, $edition);
return $success ?
$this->asSuccess(Craft::t('app', 'Plugin installed.')) :
$this->asFailure(Craft::t('app', 'Couldn’t install plugin.'));
}
/**
* Installs a plugin.
*
* @return Response
*/
public function actionSwitchEdition(): Response
{
$this->requirePostRequest();
$pluginHandle = $this->request->getRequiredBodyParam('pluginHandle');
$edition = $this->request->getRequiredBodyParam('edition');
Craft::$app->getPlugins()->switchEdition($pluginHandle, $edition);
return $this->asSuccess(Craft::t('app', 'Plugin edition changed.'));
}
/**
* Uninstalls a plugin.
*
* @return Response|null
*/
public function actionUninstallPlugin(): ?Response
{
$this->requirePostRequest();
$pluginHandle = $this->request->getRequiredBodyParam('pluginHandle');
$success = Craft::$app->getPlugins()->uninstallPlugin($pluginHandle);
return $success ?
$this->asSuccess(Craft::t('app', 'Plugin uninstalled.')) :
$this->asFailure(Craft::t('app', 'Couldn’t uninstall plugin.'));
}
/**
* Edits a plugin’s settings.
*
* @param string $handle The plugin’s handle
* @param PluginInterface|null $plugin The plugin, if there were validation errors
* @return mixed
* @throws NotFoundHttpException if the requested plugin cannot be found
*/
public function actionEditPluginSettings(string $handle, ?PluginInterface $plugin = null): mixed
{
if (
$plugin === null &&
($plugin = Craft::$app->getPlugins()->getPlugin($handle)) === null
) {
throw new NotFoundHttpException('Plugin not found');
}
return $plugin->getSettingsResponse();
}
/**
* Enables a plugin.
*
* @return Response|null
*/
public function actionEnablePlugin(): ?Response
{
$this->requirePostRequest();
$pluginHandle = $this->request->getRequiredBodyParam('pluginHandle');
$success = Craft::$app->getPlugins()->enablePlugin($pluginHandle);
return $success ?
$this->asSuccess(Craft::t('app', 'Plugin enabled.')) :
$this->asFailure(Craft::t('app', 'Couldn’t enable plugin.'));
}
/**
* Disables a plugin.
*
* @return Response|null
*/
public function actionDisablePlugin(): ?Response
{
$this->requirePostRequest();
$pluginHandle = $this->request->getRequiredBodyParam('pluginHandle');
$success = Craft::$app->getPlugins()->disablePlugin($pluginHandle);
return $success ?
$this->asSuccess(Craft::t('app', 'Plugin disabled.')) :
$this->asFailure(Craft::t('app', 'Couldn’t disable plugin.'));
}
/**
* Saves a plugin’s settings.
*
* @return Response|null
* @throws NotFoundHttpException if the requested plugin cannot be found
*/
public function actionSavePluginSettings(): ?Response
{
$this->requirePostRequest();
$pluginHandle = $this->request->getRequiredBodyParam('pluginHandle');
$settings = $this->request->getBodyParam('settings', []);
$plugin = Craft::$app->getPlugins()->getPlugin($pluginHandle);
if ($plugin === null) {
throw new NotFoundHttpException('Plugin not found');
}
$success = Craft::$app->getPlugins()->savePluginSettings($plugin, $settings);
return $success ?
$this->asSuccess(Craft::t('app', 'Plugin settings saved.')) :
$this->asFailure(
Craft::t('app', 'Couldn’t save plugin settings.'),
routeParams: ['plugin' => $plugin]
);
}
}