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\Element; |
14
|
|
|
use craft\base\ElementInterface; |
|
|
|
|
15
|
|
|
use craft\base\Field; |
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 |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
public string $redirectSrcMatch = 'pathonly'; |
34
|
|
|
public int $redirectHttpCode = 301; |
35
|
|
|
|
36
|
|
|
static protected bool $allowShortLinkUpdates = true; |
37
|
|
|
|
38
|
|
|
// Static Methods |
39
|
|
|
// ========================================================================= |
40
|
|
|
|
41
|
|
|
/** |
|
|
|
|
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
|
|
|
|
44
|
|
|
public static function displayName(): string |
45
|
|
|
{ |
46
|
|
|
return Craft::t('retour', 'Short Link'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Prevent element updates from updating the short link redirects. |
51
|
|
|
*/ |
|
|
|
|
52
|
|
|
public static function preventShortLinkUpdates(): void |
53
|
|
|
{ |
54
|
|
|
self::$allowShortLinkUpdates = false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Allow element updates to update the short link redirects. |
59
|
|
|
*/ |
|
|
|
|
60
|
|
|
public static function allowShortLinkUpdates(): void |
61
|
|
|
{ |
62
|
|
|
self::$allowShortLinkUpdates = true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Public Methods |
66
|
|
|
// ========================================================================= |
67
|
|
|
|
68
|
|
|
/** |
|
|
|
|
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
|
|
|
|
71
|
|
|
public function getInputHtml($value, ElementInterface $element = null): string |
72
|
|
|
{ |
73
|
|
|
// Render the input template |
74
|
|
|
return Craft::$app->getView()->renderTemplate( |
75
|
|
|
'retour/_components/fields/ShortLink_input', |
76
|
|
|
[ |
77
|
|
|
'name' => $this->handle, |
78
|
|
|
'value' => $value, |
79
|
|
|
'field' => $this, |
80
|
|
|
] |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
|
|
|
|
85
|
|
|
* @inheritdoc |
86
|
|
|
*/ |
|
|
|
|
87
|
|
|
public function getSettingsHtml(): string |
88
|
|
|
{ |
89
|
|
|
return Craft::$app->getView()->renderTemplate('retour/_components/fields/ShortLink_settings', |
|
|
|
|
90
|
|
|
[ |
91
|
|
|
'field' => $this, |
92
|
|
|
]); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
|
|
|
|
96
|
|
|
* @inheritdoc |
97
|
|
|
*/ |
|
|
|
|
98
|
|
|
public function afterElementSave(ElementInterface $element, bool $isNew): void |
99
|
|
|
{ |
100
|
|
|
if (!self::$allowShortLinkUpdates || $element->getIsDraft() || !$element->getSite()->hasUrls) { |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$value = $element->getFieldValue($this->handle); |
105
|
|
|
// Return for propagating elements |
106
|
|
|
if ($this->redirectSrcMatch === 'pathonly') { |
107
|
|
|
$parentElement = ElementHelper::rootElement($element); |
108
|
|
|
if ($this->translationMethod === Field::TRANSLATION_METHOD_NONE && ($element->propagating || $parentElement->propagating)) { |
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
} else if (!empty($value) && !StringHelper::startsWith($value, 'http')) { |
112
|
|
|
$value = UrlHelper::siteUrl($value, null, null, $element->siteId); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$parentElement = ElementHelper::rootElement($element); |
116
|
|
|
RetourPlugin::$plugin->redirects->removeElementRedirect($parentElement, false); |
|
|
|
|
117
|
|
|
|
118
|
|
|
if (!empty($value)) { |
119
|
|
|
$redirectSrcMatch = $this->redirectSrcMatch; |
120
|
|
|
|
121
|
|
|
if ($this->translationMethod !== Field::TRANSLATION_METHOD_NONE) { |
122
|
|
|
if (!UrlHelper::isAbsoluteUrl($value)) { |
123
|
|
|
$value = UrlHelper::siteUrl($value, null, null, $parentElement->siteId); |
124
|
|
|
$redirectSrcMatch = 'fullurl'; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
RetourPlugin::$plugin->redirects->enableElementRedirect($parentElement, $value, $redirectSrcMatch, $this->redirectHttpCode); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
parent::afterElementSave($element, $isNew); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
|
|
|
|
135
|
|
|
* @inheritdoc |
136
|
|
|
*/ |
|
|
|
|
137
|
|
|
public function afterElementDelete(ElementInterface $element): void |
138
|
|
|
{ |
139
|
|
|
if (!$element->getIsCanonical()) { |
140
|
|
|
return; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
RetourPlugin::$plugin->redirects->removeElementRedirect($element, true, true); |
144
|
|
|
parent::afterElementDelete($element); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
|
|
|
|
148
|
|
|
* @inheritdoc |
149
|
|
|
*/ |
|
|
|
|
150
|
|
|
public function getTableAttributeHtml($value, ElementInterface $element): string |
151
|
|
|
{ |
152
|
|
|
$decoded = Json::decodeIfJson($value); |
153
|
|
|
if ($decoded) { |
154
|
|
|
return $decoded['legacyUrl'] ?? ''; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// Render the input template |
158
|
|
|
return $value; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|