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