1 | <?php |
||||||
2 | /** |
||||||
3 | * Retour plugin for Craft CMS |
||||||
4 | * |
||||||
5 | * @link https://nystudio107.com/ |
||||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||||
6 | * @copyright Copyright (c) 2022 nystudio107 |
||||||
0 ignored issues
–
show
|
|||||||
7 | * @license https://nystudio107.com/license |
||||||
0 ignored issues
–
show
|
|||||||
8 | */ |
||||||
0 ignored issues
–
show
|
|||||||
9 | |||||||
10 | namespace nystudio107\retour\fields; |
||||||
11 | |||||||
12 | use Craft; |
||||||
13 | use craft\base\ElementInterface; |
||||||
0 ignored issues
–
show
The type
craft\base\ElementInterface was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||||
14 | use craft\base\Field; |
||||||
15 | use craft\base\InlineEditableFieldInterface; |
||||||
16 | use craft\base\PreviewableFieldInterface; |
||||||
17 | use craft\helpers\ElementHelper; |
||||||
18 | use craft\helpers\Json; |
||||||
19 | use craft\helpers\UrlHelper; |
||||||
20 | use nystudio107\retour\Retour as RetourPlugin; |
||||||
21 | use yii\helpers\StringHelper; |
||||||
22 | |||||||
23 | /** |
||||||
0 ignored issues
–
show
|
|||||||
24 | * @author nystudio107 |
||||||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||||||
25 | * @package Retour |
||||||
0 ignored issues
–
show
|
|||||||
26 | * @since 4.1.0 |
||||||
0 ignored issues
–
show
|
|||||||
27 | * |
||||||
28 | * @property-read string $contentColumnType |
||||||
29 | */ |
||||||
0 ignored issues
–
show
|
|||||||
30 | class ShortLink extends Field implements PreviewableFieldInterface, InlineEditableFieldInterface |
||||||
31 | { |
||||||
32 | protected static bool $allowShortLinkUpdates = true; |
||||||
33 | public string $redirectSrcMatch = 'pathonly'; |
||||||
34 | public int $redirectHttpCode = 301; |
||||||
35 | |||||||
36 | // Static Methods |
||||||
37 | // ========================================================================= |
||||||
38 | |||||||
39 | /** |
||||||
0 ignored issues
–
show
|
|||||||
40 | * @inheritdoc |
||||||
41 | */ |
||||||
0 ignored issues
–
show
|
|||||||
42 | public static function displayName(): string |
||||||
43 | { |
||||||
44 | return Craft::t('retour', 'Short Link'); |
||||||
45 | } |
||||||
46 | |||||||
47 | /** |
||||||
0 ignored issues
–
show
|
|||||||
48 | * @inheritdoc |
||||||
49 | */ |
||||||
0 ignored issues
–
show
|
|||||||
50 | public static function icon(): string |
||||||
51 | { |
||||||
52 | return '@nystudio107/retour/icon-mask.svg'; |
||||||
53 | } |
||||||
54 | |||||||
55 | /** |
||||||
56 | * Prevent element updates from updating the short link redirects. |
||||||
57 | */ |
||||||
0 ignored issues
–
show
|
|||||||
58 | public static function preventShortLinkUpdates(): void |
||||||
59 | { |
||||||
60 | self::$allowShortLinkUpdates = false; |
||||||
61 | } |
||||||
62 | |||||||
63 | /** |
||||||
64 | * Allow element updates to update the short link redirects. |
||||||
65 | */ |
||||||
0 ignored issues
–
show
|
|||||||
66 | public static function allowShortLinkUpdates(): void |
||||||
67 | { |
||||||
68 | self::$allowShortLinkUpdates = true; |
||||||
69 | } |
||||||
70 | |||||||
71 | // Public Methods |
||||||
72 | // ========================================================================= |
||||||
73 | |||||||
74 | /** |
||||||
0 ignored issues
–
show
|
|||||||
75 | * @inheritdoc |
||||||
76 | */ |
||||||
0 ignored issues
–
show
|
|||||||
77 | public function getInputHtml($value, ElementInterface $element = null): string |
||||||
78 | { |
||||||
79 | // Render the input template |
||||||
80 | return Craft::$app->getView()->renderTemplate( |
||||||
81 | 'retour/_components/fields/ShortLink_input', |
||||||
82 | [ |
||||||
83 | 'name' => $this->handle, |
||||||
84 | 'value' => $value, |
||||||
85 | 'field' => $this, |
||||||
86 | ] |
||||||
87 | ); |
||||||
88 | } |
||||||
89 | |||||||
90 | /** |
||||||
0 ignored issues
–
show
|
|||||||
91 | * @inheritdoc |
||||||
92 | */ |
||||||
0 ignored issues
–
show
|
|||||||
93 | public function getSettingsHtml(): string |
||||||
94 | { |
||||||
95 | return Craft::$app->getView()->renderTemplate('retour/_components/fields/ShortLink_settings', |
||||||
0 ignored issues
–
show
|
|||||||
96 | [ |
||||||
97 | 'field' => $this, |
||||||
98 | ]); |
||||||
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.
![]() |
|||||||
99 | } |
||||||
100 | |||||||
101 | /** |
||||||
0 ignored issues
–
show
|
|||||||
102 | * @inheritdoc |
||||||
103 | */ |
||||||
0 ignored issues
–
show
|
|||||||
104 | public function getPreviewHtml($value, ElementInterface $element): string |
||||||
105 | { |
||||||
106 | $decoded = Json::decodeIfJson($value); |
||||||
107 | if (is_array($decoded)) { |
||||||
108 | $value = $decoded['legacyUrl'] ?? ''; |
||||||
109 | } |
||||||
110 | // Render the preview template |
||||||
111 | return Craft::$app->getView()->renderTemplate( |
||||||
112 | 'retour/_components/fields/ShortLink_preview', |
||||||
113 | [ |
||||||
114 | 'name' => $this->handle, |
||||||
115 | 'value' => $value, |
||||||
116 | 'field' => $this, |
||||||
117 | ] |
||||||
118 | ); |
||||||
119 | } |
||||||
120 | |||||||
121 | /** |
||||||
0 ignored issues
–
show
|
|||||||
122 | * @inheritdoc |
||||||
123 | */ |
||||||
0 ignored issues
–
show
|
|||||||
124 | public function afterElementSave(ElementInterface $element, bool $isNew): void |
||||||
125 | { |
||||||
126 | if (!self::$allowShortLinkUpdates || $element->getIsDraft() || !$element->getSite()->hasUrls) { |
||||||
127 | return; |
||||||
128 | } |
||||||
129 | |||||||
130 | $value = $element->getFieldValue($this->handle); |
||||||
131 | // Return for propagating elements |
||||||
132 | if ($this->redirectSrcMatch === 'pathonly') { |
||||||
133 | $parentElement = ElementHelper::rootElement($element); |
||||||
0 ignored issues
–
show
The function
craft\helpers\ElementHelper::rootElement() has been deprecated: in 5.4.0. Use [[ElementInterface::getRootOwner()]] instead.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||||
134 | if ($this->translationMethod === Field::TRANSLATION_METHOD_NONE && ($element->propagating || $parentElement->propagating)) { |
||||||
135 | return; |
||||||
136 | } |
||||||
137 | } elseif (!empty($value) && !StringHelper::startsWith($value, 'http')) { |
||||||
138 | $value = UrlHelper::siteUrl($value, null, null, $element->siteId); |
||||||
139 | } |
||||||
140 | |||||||
141 | $parentElement = ElementHelper::rootElement($element); |
||||||
0 ignored issues
–
show
The function
craft\helpers\ElementHelper::rootElement() has been deprecated: in 5.4.0. Use [[ElementInterface::getRootOwner()]] instead.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||||
142 | RetourPlugin::$plugin->redirects->removeElementRedirect($parentElement, false); |
||||||
0 ignored issues
–
show
The method
removeElementRedirect() 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. ![]() |
|||||||
143 | |||||||
144 | if (!empty($value)) { |
||||||
145 | $redirectSrcMatch = $this->redirectSrcMatch; |
||||||
146 | |||||||
147 | if ($this->translationMethod !== Field::TRANSLATION_METHOD_NONE) { |
||||||
148 | if (!UrlHelper::isAbsoluteUrl($value)) { |
||||||
149 | $value = UrlHelper::siteUrl($value, null, null, $parentElement->siteId); |
||||||
150 | $redirectSrcMatch = 'fullurl'; |
||||||
151 | } |
||||||
152 | } |
||||||
153 | |||||||
154 | RetourPlugin::$plugin->redirects->enableElementRedirect($parentElement, $value, $redirectSrcMatch, $this->redirectHttpCode); |
||||||
155 | } |
||||||
156 | |||||||
157 | parent::afterElementSave($element, $isNew); |
||||||
158 | } |
||||||
159 | |||||||
160 | /** |
||||||
0 ignored issues
–
show
|
|||||||
161 | * @inheritdoc |
||||||
162 | */ |
||||||
0 ignored issues
–
show
|
|||||||
163 | public function afterElementDelete(ElementInterface $element): void |
||||||
164 | { |
||||||
165 | if (!$element->getIsCanonical()) { |
||||||
166 | return; |
||||||
167 | } |
||||||
168 | |||||||
169 | RetourPlugin::$plugin->redirects->removeElementRedirect($element, true, true); |
||||||
170 | parent::afterElementDelete($element); |
||||||
171 | } |
||||||
172 | } |
||||||
173 |