nystudio107 /
craft-retour
| 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/ |
||||||||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||||||||
| 9 | * @copyright Copyright (c) 2019 nystudio107 |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 10 | */ |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 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 |
||||||||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||||||||
| 27 | * @package Retour |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 28 | * @since 3.1.26 |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 29 | */ |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 30 | class RetourResolver extends Resolver |
||||||||
| 31 | { |
||||||||
| 32 | /** |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 33 | * @inheritDoc |
||||||||
| 34 | */ |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 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 */ |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 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)) { |
||||||||
|
0 ignored issues
–
show
The method
excludeUri() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||||
| 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); |
||||||||
|
0 ignored issues
–
show
The method
incrementStatistics() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||||
| 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 |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 102 | * @param array $arguments |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 103 | * @param $context |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 104 | * @param ResolveInfo $resolveInfo |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 105 | * @return array |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 106 | * @throws SiteNotFoundException |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 107 | */ |
||||||||
| 108 | public static function resolveAll($source, array $arguments, $context, ResolveInfo $resolveInfo) |
||||||||
|
0 ignored issues
–
show
The parameter
$source is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$resolveInfo is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$context is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||||
| 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 |