1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Retour plugin for Craft CMS |
4
|
|
|
* |
5
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
6
|
|
|
* @copyright Copyright (c) 2022 nystudio107 |
|
|
|
|
7
|
|
|
* @license https://nystudio107.com/license |
|
|
|
|
8
|
|
|
*/ |
|
|
|
|
9
|
|
|
|
10
|
|
|
namespace nystudio107\retour\fields; |
11
|
|
|
|
12
|
|
|
use Craft; |
13
|
|
|
use craft\base\ElementInterface; |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
24
|
|
|
* @author nystudio107 |
|
|
|
|
25
|
|
|
* @package Retour |
|
|
|
|
26
|
|
|
* @since 4.1.0 |
|
|
|
|
27
|
|
|
* |
28
|
|
|
* @property-read string $contentColumnType |
29
|
|
|
*/ |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
|
|
|
|
42
|
|
|
public static function displayName(): string |
43
|
|
|
{ |
44
|
|
|
return Craft::t('retour', 'Short Link'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
|
|
|
|
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
|
|
|
|
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
|
|
|
*/ |
|
|
|
|
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
|
|
|
*/ |
|
|
|
|
66
|
|
|
public static function allowShortLinkUpdates(): void |
67
|
|
|
{ |
68
|
|
|
self::$allowShortLinkUpdates = true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Public Methods |
72
|
|
|
// ========================================================================= |
73
|
|
|
|
74
|
|
|
/** |
|
|
|
|
75
|
|
|
* @inheritdoc |
76
|
|
|
*/ |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
91
|
|
|
* @inheritdoc |
92
|
|
|
*/ |
|
|
|
|
93
|
|
|
public function getSettingsHtml(): string |
94
|
|
|
{ |
95
|
|
|
return Craft::$app->getView()->renderTemplate('retour/_components/fields/ShortLink_settings', |
|
|
|
|
96
|
|
|
[ |
97
|
|
|
'field' => $this, |
98
|
|
|
]); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
|
|
|
|
102
|
|
|
* @inheritdoc |
103
|
|
|
*/ |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
122
|
|
|
* @inheritdoc |
123
|
|
|
*/ |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
142
|
|
|
RetourPlugin::$plugin->redirects->removeElementRedirect($parentElement, false); |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
161
|
|
|
* @inheritdoc |
162
|
|
|
*/ |
|
|
|
|
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
|
|
|
|