1 | <?php |
||||||
2 | /** |
||||||
3 | * Retour plugin for Craft CMS |
||||||
4 | * |
||||||
5 | * Retour allows you to intelligently redirect legacy URLs, so that you don't |
||||||
6 | * lose SEO value when rebuilding & restructuring a website |
||||||
7 | * |
||||||
8 | * @link https://nystudio107.com/ |
||||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||||
9 | * @copyright Copyright (c) 2018 nystudio107 |
||||||
0 ignored issues
–
show
|
|||||||
10 | */ |
||||||
0 ignored issues
–
show
|
|||||||
11 | |||||||
12 | namespace nystudio107\retour\controllers; |
||||||
13 | |||||||
14 | use Craft; |
||||||
15 | use craft\errors\MissingComponentException; |
||||||
16 | use craft\helpers\UrlHelper; |
||||||
17 | use craft\web\Controller; |
||||||
18 | use craft\web\UrlManager; |
||||||
19 | use nystudio107\retour\assetbundles\retour\RetourAsset; |
||||||
20 | use nystudio107\retour\helpers\Permission as PermissionHelper; |
||||||
21 | use nystudio107\retour\models\Settings; |
||||||
22 | use nystudio107\retour\Retour; |
||||||
23 | use yii\base\InvalidConfigException; |
||||||
24 | use yii\web\BadRequestHttpException; |
||||||
25 | use yii\web\ForbiddenHttpException; |
||||||
26 | use yii\web\NotFoundHttpException; |
||||||
27 | use yii\web\Response; |
||||||
28 | |||||||
29 | /** |
||||||
0 ignored issues
–
show
|
|||||||
30 | * @author nystudio107 |
||||||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||||||
31 | * @package Retour |
||||||
0 ignored issues
–
show
|
|||||||
32 | * @since 3.0.0 |
||||||
0 ignored issues
–
show
|
|||||||
33 | */ |
||||||
0 ignored issues
–
show
|
|||||||
34 | class SettingsController extends Controller |
||||||
35 | { |
||||||
36 | // Constants |
||||||
37 | // ========================================================================= |
||||||
38 | |||||||
39 | protected const DOCUMENTATION_URL = 'https://github.com/nystudio107/craft-retour/'; |
||||||
40 | |||||||
41 | // Protected Properties |
||||||
42 | // ========================================================================= |
||||||
43 | |||||||
44 | protected array|bool|int $allowAnonymous = []; |
||||||
45 | |||||||
46 | // Public Methods |
||||||
47 | // ========================================================================= |
||||||
48 | |||||||
49 | /** |
||||||
50 | * Plugin settings |
||||||
51 | * |
||||||
52 | * @param Settings|bool|null $settings |
||||||
0 ignored issues
–
show
|
|||||||
53 | * |
||||||
54 | * @return Response The rendered result |
||||||
55 | * @throws ForbiddenHttpException |
||||||
56 | */ |
||||||
57 | public function actionPluginSettings(Settings|bool|null $settings = null): Response |
||||||
58 | { |
||||||
59 | $variables = []; |
||||||
60 | PermissionHelper::controllerPermissionCheck('retour:settings'); |
||||||
61 | if ($settings === null) { |
||||||
62 | $settings = Retour::$settings; |
||||||
63 | } |
||||||
64 | /** @var Settings $settings */ |
||||||
0 ignored issues
–
show
|
|||||||
65 | $pluginName = $settings->pluginName; |
||||||
66 | $templateTitle = Craft::t('retour', 'Settings'); |
||||||
67 | $view = Craft::$app->getView(); |
||||||
68 | // Asset bundle |
||||||
69 | try { |
||||||
70 | $view->registerAssetBundle(RetourAsset::class); |
||||||
71 | } catch (InvalidConfigException $e) { |
||||||
72 | Craft::error($e->getMessage(), __METHOD__); |
||||||
73 | } |
||||||
74 | $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
||||||
75 | '@nystudio107/retour/web/assets/dist', |
||||||
76 | true |
||||||
0 ignored issues
–
show
The call to
yii\web\AssetManager::getPublishedUrl() has too many arguments starting with true .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||||
77 | ); |
||||||
78 | // Basic variables |
||||||
79 | $variables['fullPageForm'] = true; |
||||||
80 | $variables['docsUrl'] = self::DOCUMENTATION_URL; |
||||||
81 | $variables['pluginName'] = $pluginName; |
||||||
82 | $variables['title'] = $templateTitle; |
||||||
83 | $variables['crumbs'] = [ |
||||||
84 | [ |
||||||
85 | 'label' => $pluginName, |
||||||
86 | 'url' => UrlHelper::cpUrl('retour'), |
||||||
87 | ], |
||||||
88 | [ |
||||||
89 | 'label' => $templateTitle, |
||||||
90 | 'url' => UrlHelper::cpUrl('retour/settings'), |
||||||
91 | ], |
||||||
92 | ]; |
||||||
93 | $variables['docTitle'] = "{$pluginName} - {$templateTitle}"; |
||||||
94 | $variables['selectedSubnavItem'] = 'settings'; |
||||||
95 | $variables['settings'] = $settings; |
||||||
96 | |||||||
97 | // Render the template |
||||||
98 | return $this->renderTemplate('retour/settings', $variables); |
||||||
99 | } |
||||||
100 | |||||||
101 | /** |
||||||
102 | * Saves a plugin’s settings. |
||||||
103 | * |
||||||
104 | * @return Response|null |
||||||
105 | * @throws NotFoundHttpException if the requested plugin cannot be found |
||||||
106 | * @throws BadRequestHttpException |
||||||
107 | * @throws MissingComponentException |
||||||
108 | * @throws ForbiddenHttpException |
||||||
109 | */ |
||||||
110 | public function actionSavePluginSettings(): ?Response |
||||||
111 | { |
||||||
112 | PermissionHelper::controllerPermissionCheck('retour:settings'); |
||||||
113 | $this->requirePostRequest(); |
||||||
114 | $pluginHandle = Craft::$app->getRequest()->getRequiredBodyParam('pluginHandle'); |
||||||
115 | $settings = Craft::$app->getRequest()->getBodyParam('settings', []); |
||||||
116 | $plugin = Craft::$app->getPlugins()->getPlugin($pluginHandle); |
||||||
117 | |||||||
118 | if ($plugin === null) { |
||||||
119 | throw new NotFoundHttpException('Plugin not found'); |
||||||
120 | } |
||||||
121 | |||||||
122 | if (!is_array($settings['additionalHeaders'])) { |
||||||
123 | $settings['additionalHeaders'] = []; |
||||||
124 | } |
||||||
125 | if (!is_array($settings['excludePatterns'])) { |
||||||
126 | $settings['excludePatterns'] = []; |
||||||
127 | } |
||||||
128 | if (!Craft::$app->getPlugins()->savePluginSettings($plugin, $settings)) { |
||||||
129 | Craft::$app->getSession()->setError(Craft::t('app', "Couldn't save plugin settings.")); |
||||||
130 | |||||||
131 | // Send the plugin back to the template |
||||||
132 | /** @var UrlManager $urlManager */ |
||||||
0 ignored issues
–
show
|
|||||||
133 | $urlManager = Craft::$app->getUrlManager(); |
||||||
134 | $urlManager->setRouteParams([ |
||||||
0 ignored issues
–
show
|
|||||||
135 | 'plugin' => $plugin, |
||||||
136 | ]); |
||||||
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||||||
137 | |||||||
138 | return null; |
||||||
139 | } |
||||||
140 | |||||||
141 | Retour::$plugin->clearAllCaches(); |
||||||
0 ignored issues
–
show
The method
clearAllCaches() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
142 | Craft::$app->getSession()->setNotice(Craft::t('app', 'Plugin settings saved.')); |
||||||
143 | |||||||
144 | return $this->redirectToPostedUrl(); |
||||||
145 | } |
||||||
146 | } |
||||||
147 |