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