|
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) 2019 nystudio107 |
|
|
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace nystudio107\retour\gql\resolvers; |
|
13
|
|
|
|
|
14
|
|
|
use Craft; |
|
15
|
|
|
use craft\base\Element; |
|
16
|
|
|
use craft\gql\base\Resolver; |
|
17
|
|
|
|
|
18
|
|
|
use craft\helpers\UrlHelper; |
|
19
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
20
|
|
|
use nystudio107\retour\Retour; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class RetourResolver |
|
24
|
|
|
* |
|
25
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
26
|
|
|
* @package Retour |
|
|
|
|
|
|
27
|
|
|
* @since 3.1.26 |
|
|
|
|
|
|
28
|
|
|
*/ |
|
|
|
|
|
|
29
|
|
|
class RetourResolver extends Resolver |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
|
|
|
|
|
32
|
|
|
* @inheritDoc |
|
33
|
|
|
*/ |
|
|
|
|
|
|
34
|
|
|
public static function resolve($source, array $arguments, $context, ResolveInfo $resolveInfo) |
|
35
|
|
|
{ |
|
36
|
|
|
// If our source is an Element, extract the URI and siteId from it |
|
37
|
|
|
if ($source instanceof Element) { |
|
38
|
|
|
/** Element $source */ |
|
|
|
|
|
|
39
|
|
|
$uri = $source->uri; |
|
40
|
|
|
$siteId = $source->siteId; |
|
41
|
|
|
} else { |
|
42
|
|
|
// Otherwise use the passed in arguments, or defaults |
|
43
|
|
|
$uri = $arguments['uri'] ?? '/'; |
|
44
|
|
|
$siteId = $arguments['siteId'] ?? null; |
|
45
|
|
|
} |
|
46
|
|
|
$uri = trim($uri === '/' ? '__home__' : $uri); |
|
47
|
|
|
|
|
48
|
|
|
$redirect = null; |
|
49
|
|
|
// Strip the query string if `alwaysStripQueryString` is set |
|
50
|
|
|
if (Retour::$settings->alwaysStripQueryString) { |
|
51
|
|
|
$uri = UrlHelper::stripQueryString($uri); |
|
52
|
|
|
} |
|
53
|
|
|
if (!Retour::$plugin->redirects->excludeUri($uri)) { |
|
54
|
|
|
$redirect = Retour::$plugin->redirects->findRedirectMatch($uri, $uri, $siteId); |
|
55
|
|
|
if ($redirect === null && Craft::$app->getElements()->getElementByUri(trim($uri, '/'), $siteId) === null) { |
|
56
|
|
|
// Increment the stats |
|
57
|
|
|
Retour::$plugin->statistics->incrementStatistics($uri, false); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $redirect; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|