|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Webperf plugin for Craft CMS 3.x |
|
4
|
|
|
* |
|
5
|
|
|
* Monitor the performance of your webpages through real-world user timing data |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace nystudio107\webperf\controllers; |
|
12
|
|
|
|
|
13
|
|
|
use nystudio107\webperf\Webperf; |
|
14
|
|
|
use nystudio107\webperf\assetbundles\webperf\WebperfAsset; |
|
15
|
|
|
use nystudio107\webperf\helpers\Permission as PermissionHelper; |
|
16
|
|
|
use nystudio107\webperf\models\Settings; |
|
17
|
|
|
|
|
18
|
|
|
use Craft; |
|
|
|
|
|
|
19
|
|
|
use craft\helpers\UrlHelper; |
|
|
|
|
|
|
20
|
|
|
use craft\web\Controller; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
|
|
|
23
|
|
|
use yii\web\NotFoundHttpException; |
|
|
|
|
|
|
24
|
|
|
use yii\web\Response; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
|
|
|
|
|
27
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
28
|
|
|
* @package Webperf |
|
|
|
|
|
|
29
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
30
|
|
|
*/ |
|
|
|
|
|
|
31
|
|
|
class SettingsController extends Controller |
|
32
|
|
|
{ |
|
33
|
|
|
// Constants |
|
34
|
|
|
// ========================================================================= |
|
35
|
|
|
|
|
36
|
|
|
const DOCUMENTATION_URL = 'https://github.com/nystudio107/craft-webperf/'; |
|
37
|
|
|
|
|
38
|
|
|
// Protected Properties |
|
39
|
|
|
// ========================================================================= |
|
40
|
|
|
|
|
41
|
|
|
protected $allowAnonymous = []; |
|
42
|
|
|
|
|
43
|
|
|
// Public Methods |
|
44
|
|
|
// ========================================================================= |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Plugin settings |
|
48
|
|
|
* |
|
49
|
|
|
* @param null|bool|Settings $settings |
|
|
|
|
|
|
50
|
|
|
* |
|
51
|
|
|
* @return Response The rendered result |
|
52
|
|
|
* @throws \yii\web\ForbiddenHttpException |
|
53
|
|
|
*/ |
|
54
|
|
|
public function actionPluginSettings($settings = null): Response |
|
55
|
|
|
{ |
|
56
|
|
|
$variables = []; |
|
57
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:settings'); |
|
58
|
|
|
if ($settings === null) { |
|
59
|
|
|
$settings = Webperf::$settings; |
|
60
|
|
|
} |
|
61
|
|
|
/** @var Settings $settings */ |
|
|
|
|
|
|
62
|
|
|
$pluginName = $settings->pluginName; |
|
63
|
|
|
$templateTitle = Craft::t('webperf', 'Settings'); |
|
64
|
|
|
$view = Craft::$app->getView(); |
|
65
|
|
|
// Asset bundle |
|
66
|
|
|
try { |
|
67
|
|
|
$view->registerAssetBundle(WebperfAsset::class); |
|
68
|
|
|
} catch (InvalidConfigException $e) { |
|
69
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
|
70
|
|
|
} |
|
71
|
|
|
$variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
|
72
|
|
|
'@nystudio107/webperf/assetbundles/webperf/dist', |
|
73
|
|
|
true |
|
74
|
|
|
); |
|
75
|
|
|
// Basic variables |
|
76
|
|
|
$variables['fullPageForm'] = true; |
|
77
|
|
|
$variables['docsUrl'] = self::DOCUMENTATION_URL; |
|
78
|
|
|
$variables['pluginName'] = $pluginName; |
|
79
|
|
|
$variables['title'] = $templateTitle; |
|
80
|
|
|
$variables['crumbs'] = [ |
|
81
|
|
|
[ |
|
82
|
|
|
'label' => $pluginName, |
|
83
|
|
|
'url' => UrlHelper::cpUrl('webperf'), |
|
84
|
|
|
], |
|
85
|
|
|
[ |
|
86
|
|
|
'label' => $templateTitle, |
|
87
|
|
|
'url' => UrlHelper::cpUrl('webperf/settings'), |
|
88
|
|
|
], |
|
89
|
|
|
]; |
|
90
|
|
|
$variables['docTitle'] = "{$pluginName} - {$templateTitle}"; |
|
91
|
|
|
$variables['selectedSubnavItem'] = 'settings'; |
|
92
|
|
|
$variables['settings'] = $settings; |
|
93
|
|
|
|
|
94
|
|
|
// Render the template |
|
95
|
|
|
return $this->renderTemplate('webperf/settings', $variables); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Saves a plugin’s settings. |
|
100
|
|
|
* |
|
101
|
|
|
* @return Response|null |
|
102
|
|
|
* @throws NotFoundHttpException if the requested plugin cannot be found |
|
103
|
|
|
* @throws \yii\web\BadRequestHttpException |
|
104
|
|
|
* @throws \craft\errors\MissingComponentException |
|
105
|
|
|
* @throws \yii\web\ForbiddenHttpException |
|
106
|
|
|
*/ |
|
107
|
|
|
public function actionSavePluginSettings() |
|
108
|
|
|
{ |
|
109
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:settings'); |
|
110
|
|
|
$this->requirePostRequest(); |
|
111
|
|
|
$pluginHandle = Craft::$app->getRequest()->getRequiredBodyParam('pluginHandle'); |
|
112
|
|
|
$settings = Craft::$app->getRequest()->getBodyParam('settings', []); |
|
113
|
|
|
$plugin = Craft::$app->getPlugins()->getPlugin($pluginHandle); |
|
114
|
|
|
|
|
115
|
|
|
if ($plugin === null) { |
|
116
|
|
|
throw new NotFoundHttpException('Plugin not found'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if (!Craft::$app->getPlugins()->savePluginSettings($plugin, $settings)) { |
|
120
|
|
|
Craft::$app->getSession()->setError(Craft::t('app', "Couldn't save plugin settings.")); |
|
121
|
|
|
|
|
122
|
|
|
// Send the plugin back to the template |
|
123
|
|
|
Craft::$app->getUrlManager()->setRouteParams([ |
|
|
|
|
|
|
124
|
|
|
'plugin' => $plugin, |
|
125
|
|
|
]); |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
return null; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
Webperf::$plugin->clearAllCaches(); |
|
131
|
|
|
Craft::$app->getSession()->setNotice(Craft::t('app', 'Plugin settings saved.')); |
|
132
|
|
|
|
|
133
|
|
|
return $this->redirectToPostedUrl(); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|