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) 2019 nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\retour\gql\resolvers; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use craft\base\Element; |
16
|
|
|
use craft\errors\SiteNotFoundException; |
17
|
|
|
use craft\gql\base\Resolver; |
18
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
19
|
|
|
use nystudio107\retour\helpers\UrlHelper; |
20
|
|
|
use nystudio107\retour\Retour; |
21
|
|
|
use Throwable; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class RetourResolver |
25
|
|
|
* |
26
|
|
|
* @author nystudio107 |
|
|
|
|
27
|
|
|
* @package Retour |
|
|
|
|
28
|
|
|
* @since 3.1.26 |
|
|
|
|
29
|
|
|
*/ |
|
|
|
|
30
|
|
|
class RetourResolver extends Resolver |
31
|
|
|
{ |
32
|
|
|
/** |
|
|
|
|
33
|
|
|
* @inheritDoc |
34
|
|
|
*/ |
|
|
|
|
35
|
|
|
public static function resolve(mixed $source, array $arguments, mixed $context, ResolveInfo $resolveInfo): mixed |
36
|
|
|
{ |
37
|
|
|
// If our source is an Element, extract the URI and siteId from it |
38
|
|
|
if ($source instanceof Element) { |
39
|
|
|
/** Element $source */ |
|
|
|
|
40
|
|
|
$uri = $source->uri; |
41
|
|
|
$siteId = $source->siteId; |
42
|
|
|
} else { |
43
|
|
|
// Otherwise, use the passed in arguments, or defaults |
44
|
|
|
$uri = $arguments['uri'] ?? '/'; |
45
|
|
|
$siteId = $arguments['siteId'] ?? null; |
46
|
|
|
if (isset($arguments['site'])) { |
47
|
|
|
$site = Craft::$app->getSites()->getSiteByHandle($arguments['site']); |
48
|
|
|
if ($site !== null) { |
49
|
|
|
$siteId = $site->id; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
$uri = trim($uri === '/' ? '__home__' : $uri); |
54
|
|
|
|
55
|
|
|
$redirect = null; |
56
|
|
|
|
57
|
|
|
// Strip the query string if `alwaysStripQueryString` is set |
58
|
|
|
if (Retour::$settings->alwaysStripQueryString) { |
59
|
|
|
$uri = UrlHelper::stripQueryString($uri); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!Retour::$plugin->redirects->excludeUri($uri)) { |
|
|
|
|
63
|
|
|
$redirect = Retour::$plugin->redirects->findRedirectMatch($uri, $uri, $siteId); |
64
|
|
|
|
65
|
|
|
if ($redirect === null && Craft::$app->getElements()->getElementByUri(trim($uri, '/'), $siteId) === null) { |
66
|
|
|
// Set the `site` virtual field |
67
|
|
|
$redirect['site'] = null; |
68
|
|
|
$redirect['siteId'] = $siteId; |
69
|
|
|
if (isset($redirect['siteId']) && (int)$redirect['siteId'] !== 0) { |
70
|
|
|
$site = Craft::$app->getSites()->getSiteById((int)$redirect['siteId']); |
71
|
|
|
if ($site !== null) { |
72
|
|
|
$redirect['site'] = $site->handle; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
// Increment the stats |
76
|
|
|
Retour::$plugin->statistics->incrementStatistics($uri, false, $siteId); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
if ($redirect !== null && isset($redirect['redirectDestUrl'])) { |
80
|
|
|
$dest = $redirect['redirectDestUrl']; |
81
|
|
|
$path = $redirect['redirectDestUrl']; |
82
|
|
|
// Combine the URL and path together, merging them as appropriate |
83
|
|
|
try { |
84
|
|
|
if (!UrlHelper::isAbsoluteUrl($dest) && !UrlHelper::pathHasSitePrefix($path)) { |
85
|
|
|
$dest = UrlHelper::siteUrl('/', null, null, $siteId); |
86
|
|
|
$dest = UrlHelper::mergeUrlWithPath($dest, $path); |
87
|
|
|
$dest = parse_url($dest, PHP_URL_PATH); |
88
|
|
|
} |
89
|
|
|
} catch (Throwable $e) { |
90
|
|
|
// That's ok |
91
|
|
|
} |
92
|
|
|
$redirect['redirectDestUrl'] = $dest; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $redirect; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return all static redirects for a site. |
100
|
|
|
* |
101
|
|
|
* @param $source |
|
|
|
|
102
|
|
|
* @param array $arguments |
|
|
|
|
103
|
|
|
* @param $context |
|
|
|
|
104
|
|
|
* @param ResolveInfo $resolveInfo |
|
|
|
|
105
|
|
|
* @return array |
|
|
|
|
106
|
|
|
* @throws SiteNotFoundException |
|
|
|
|
107
|
|
|
*/ |
108
|
|
|
public static function resolveAll($source, array $arguments, $context, ResolveInfo $resolveInfo) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$siteId = $arguments['siteId'] ?? Craft::$app->getSites()->getCurrentSite()->id; |
111
|
|
|
|
112
|
|
|
$redirects = Retour::$plugin->redirects->getAllStaticRedirects(null, $siteId); |
113
|
|
|
|
114
|
|
|
return $redirects; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|