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/ |
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\retour\services; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use craft\base\Component; |
16
|
|
|
use craft\base\Element; |
17
|
|
|
use craft\helpers\ElementHelper; |
18
|
|
|
use nystudio107\retour\events\RedirectEvent; |
19
|
|
|
use nystudio107\retour\helpers\UrlHelper; |
20
|
|
|
use nystudio107\retour\Retour; |
21
|
|
|
use yii\base\Exception; |
22
|
|
|
|
23
|
|
|
/** |
|
|
|
|
24
|
|
|
* @author nystudio107 |
|
|
|
|
25
|
|
|
* @package Retour |
|
|
|
|
26
|
|
|
* @since 3.1.64 |
|
|
|
|
27
|
|
|
*/ |
|
|
|
|
28
|
|
|
class Events extends Component |
29
|
|
|
{ |
30
|
|
|
// Constants |
31
|
|
|
// ========================================================================= |
32
|
|
|
|
33
|
|
|
/** |
|
|
|
|
34
|
|
|
* @event RedirectEvent The event that is triggered before an Entry redirect is automatically |
35
|
|
|
* saved when an Entry's URI is changed (assuming the **Create Entry Redirects** setting is enabled). |
36
|
|
|
* You may set [[RedirectEvent::isValid]] to `false` to prevent the redirect from getting saved. |
37
|
|
|
* |
38
|
|
|
* ```php |
39
|
|
|
* use nystudio107\retour\services\Events; |
40
|
|
|
* use nystudio107\retour\events\RedirectEvent; |
41
|
|
|
* |
42
|
|
|
* Event::on(Events::class, |
43
|
|
|
* Events::EVENT_BEFORE_SAVE_ENTRY_REDIRECT, |
44
|
|
|
* function(RedirectEvent $event) { |
45
|
|
|
* // potentially set $event->isValid; |
46
|
|
|
* } |
47
|
|
|
* ); |
48
|
|
|
* ``` |
49
|
|
|
*/ |
50
|
|
|
public const EVENT_BEFORE_SAVE_ENTRY_REDIRECT = 'beforeSaveEntryRedirect'; |
51
|
|
|
|
52
|
|
|
// Public Properties |
53
|
|
|
// ========================================================================= |
54
|
|
|
|
55
|
|
|
/** |
|
|
|
|
56
|
|
|
* @var array The URIs for the element before it was saved |
57
|
|
|
*/ |
58
|
|
|
public array $oldElementUris = []; |
59
|
|
|
|
60
|
|
|
/** |
|
|
|
|
61
|
|
|
* @var array A list of element ids that have their redirects already created |
62
|
|
|
*/ |
63
|
|
|
protected array $elementsWithCreatedRedirects = []; |
64
|
|
|
|
65
|
|
|
// Public Methods |
66
|
|
|
// ========================================================================= |
67
|
|
|
|
68
|
|
|
/** |
|
|
|
|
69
|
|
|
* @param $element |
|
|
|
|
70
|
|
|
*/ |
|
|
|
|
71
|
|
|
public function stashElementUris($element): void |
72
|
|
|
{ |
73
|
|
|
// Stash the old URLs by element id, and do so only once, |
74
|
|
|
// in case we are called more than once per request |
75
|
|
|
if (empty($this->oldElementUris[$element->id])) { |
76
|
|
|
$this->oldElementUris[$element->id] = $this->getAllElementUris($element); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
|
|
|
|
81
|
|
|
* @param Element $element |
|
|
|
|
82
|
|
|
*/ |
|
|
|
|
83
|
|
|
public function handleElementUriChange(Element $element): void |
84
|
|
|
{ |
85
|
|
|
if (array_key_exists($element->id, $this->elementsWithCreatedRedirects)) { |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
$this->elementsWithCreatedRedirects[] = $element->id; |
89
|
|
|
|
90
|
|
|
$uris = $this->getAllElementUris($element); |
91
|
|
|
if (!empty($this->oldElementUris[$element->id])) { |
92
|
|
|
$oldElementUris = $this->oldElementUris[$element->id]; |
93
|
|
|
foreach ($uris as $siteId => $newUri) { |
94
|
|
|
if (!empty($oldElementUris[$siteId])) { |
95
|
|
|
$oldUri = $oldElementUris[$siteId]; |
96
|
|
|
Craft::debug( |
97
|
|
|
Craft::t( |
98
|
|
|
'retour', |
99
|
|
|
'Comparing old: {oldUri} to new: {newUri}', |
100
|
|
|
['oldUri' => print_r($oldUri, true), 'newUri' => print_r($newUri, true)] |
101
|
|
|
), |
102
|
|
|
__METHOD__ |
103
|
|
|
); |
104
|
|
|
// Handle the siteId |
105
|
|
|
$redirectSiteId = null; |
106
|
|
|
if (Craft::$app->getIsMultiSite()) { |
107
|
|
|
$redirectSiteId = $siteId; |
108
|
|
|
} |
109
|
|
|
// Make sure the URIs are not the same |
110
|
|
|
if (strcmp($oldUri, $newUri) !== 0) { |
111
|
|
|
// Handle trailing slash config setting |
112
|
|
|
if (Craft::$app->config->general->addTrailingSlashesToUrls) { |
113
|
|
|
$oldUri = rtrim($oldUri, '/') . '/'; |
114
|
|
|
$newUri = rtrim($newUri, '/') . '/'; |
115
|
|
|
} |
116
|
|
|
// Handle the URL match type |
117
|
|
|
if (Retour::$settings->uriChangeRedirectSrcMatch === 'fullurl') { |
118
|
|
|
try { |
119
|
|
|
if ($redirectSiteId !== null) { |
120
|
|
|
$redirectSiteId = (int)$redirectSiteId; |
121
|
|
|
} |
122
|
|
|
$oldUri = \craft\helpers\UrlHelper::siteUrl($oldUri, null, null, $redirectSiteId); |
123
|
|
|
$newUri = UrlHelper::siteUrl($newUri, null, null, $redirectSiteId); |
124
|
|
|
} catch (Exception $e) { |
125
|
|
|
// That's fine |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
$redirectConfig = [ |
129
|
|
|
'id' => 0, |
130
|
|
|
'redirectMatchType' => 'exactmatch', |
131
|
|
|
'redirectHttpCode' => 301, |
132
|
|
|
'redirectSrcMatch' => Retour::$settings->uriChangeRedirectSrcMatch, |
133
|
|
|
'redirectSrcUrl' => $oldUri, |
134
|
|
|
'redirectDestUrl' => $newUri, |
135
|
|
|
'siteId' => $redirectSiteId, |
136
|
|
|
]; |
137
|
|
|
// Trigger a 'beforeSaveEntryRedirect' event |
138
|
|
|
$isNew = (int)$redirectConfig['id'] === 0; |
139
|
|
|
$event = new RedirectEvent([ |
|
|
|
|
140
|
|
|
'isNew' => $isNew, |
141
|
|
|
'legacyUrl' => $redirectConfig['redirectSrcUrl'], |
142
|
|
|
'destinationUrl' => $redirectConfig['redirectDestUrl'], |
143
|
|
|
'matchType' => $redirectConfig['redirectSrcMatch'], |
144
|
|
|
'redirectType' => $redirectConfig['redirectHttpCode'], |
145
|
|
|
'siteId' => $redirectConfig['siteId'], |
146
|
|
|
]); |
|
|
|
|
147
|
|
|
$this->trigger(self::EVENT_BEFORE_SAVE_ENTRY_REDIRECT, $event); |
148
|
|
|
if (!$event->isValid) { |
149
|
|
|
return; |
150
|
|
|
} |
151
|
|
|
Retour::$plugin->redirects->saveRedirect($redirectConfig); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the URIs for each site for the element |
160
|
|
|
* |
161
|
|
|
* @param Element $element |
|
|
|
|
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
|
|
protected function getAllElementUris(Element $element): array |
166
|
|
|
{ |
167
|
|
|
$uris = []; |
168
|
|
|
if (!ElementHelper::isDraftOrRevision($element)) { |
169
|
|
|
$sites = Craft::$app->getSites()->getAllSites(); |
170
|
|
|
foreach ($sites as $site) { |
171
|
|
|
$uri = Craft::$app->getElements()->getElementUriForSite($element->id, $site->id); |
172
|
|
|
if ($uri !== null) { |
173
|
|
|
$uris[$site->id] = $uri; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
Craft::debug( |
179
|
|
|
Craft::t( |
180
|
|
|
'retour', |
181
|
|
|
'Getting Element URIs: {uris}', |
182
|
|
|
['uris' => print_r($uris, true)] |
183
|
|
|
), |
184
|
|
|
__METHOD__ |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
return $uris; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|