1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SEOmatic plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* A turnkey SEO implementation for Craft CMS that is comprehensive, powerful, |
6
|
|
|
* and flexible |
7
|
|
|
* |
8
|
|
|
* @link https://nystudio107.com |
9
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace nystudio107\seomatic\models\metalink; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use craft\helpers\StringHelper; |
16
|
|
|
use nystudio107\seomatic\helpers\UrlHelper; |
17
|
|
|
use nystudio107\seomatic\models\MetaLink; |
18
|
|
|
use nystudio107\seomatic\Seomatic; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author nystudio107 |
22
|
|
|
* @package Seomatic |
23
|
|
|
* @since 3.0.0 |
24
|
|
|
*/ |
25
|
|
|
class CanonicalLink extends MetaLink |
26
|
|
|
{ |
27
|
|
|
// Constants |
28
|
|
|
// ========================================================================= |
29
|
|
|
|
30
|
|
|
const ITEM_TYPE = 'CanonicalLink'; |
31
|
|
|
|
32
|
|
|
// Static Methods |
33
|
|
|
// ========================================================================= |
34
|
|
|
|
35
|
|
|
// Public Properties |
36
|
|
|
// ========================================================================= |
37
|
|
|
|
38
|
|
|
// Public Methods |
39
|
|
|
// ========================================================================= |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
public function init(): void |
45
|
|
|
{ |
46
|
|
|
parent::init(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
|
|
public function rules(): array |
53
|
|
|
{ |
54
|
|
|
$rules = parent::rules(); |
55
|
|
|
$rules = array_merge($rules, [ |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
return $rules; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
|
|
public function prepForRender(&$data): bool |
65
|
|
|
{ |
66
|
|
|
$shouldRender = parent::prepForRender($data); |
67
|
|
|
if ($shouldRender) { |
68
|
|
|
$request = Craft::$app->getRequest(); |
69
|
|
|
$response = Craft::$app->getResponse(); |
70
|
|
|
// Don't render a canonical url for http status codes >= 400 |
71
|
|
|
if (!$request->isConsoleRequest |
72
|
|
|
&& !Seomatic::$previewingMetaContainers |
73
|
|
|
&& $response->statusCode >= 400 |
74
|
|
|
) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
// Don't render a canonical URL if this page isn't meant to be indexed as per: |
78
|
|
|
// https://www.reddit.com/r/TechSEO/comments/8yahdr/2_questions_about_the_canonical_tag/e2dey9i/?context=1 |
79
|
|
|
$robots = Seomatic::$seomaticVariable->tag->get('robots'); |
80
|
|
|
if ($robots !== null && $robots->include && !Seomatic::$previewingMetaContainers && !Seomatic::$settings->alwaysIncludeCanonicalUrls) { |
81
|
|
|
$robotsArray = $robots->renderAttributes(); |
82
|
|
|
$contentArray = explode(',', $robotsArray['content'] ?? ''); |
83
|
|
|
if (in_array('noindex', $contentArray, true) || in_array('none', $contentArray, true)) { |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
// Ensure the href is a full url |
88
|
|
|
if (!empty($data['href'])) { |
89
|
|
|
if (Seomatic::$settings->lowercaseCanonicalUrl) { |
90
|
|
|
$data['href'] = StringHelper::toLowerCase($data['href']); |
91
|
|
|
} |
92
|
|
|
$url = UrlHelper::absoluteUrlWithProtocol($data['href']); |
93
|
|
|
// The URL should be stripped of its query string already, but because |
94
|
|
|
// Craft adds the `tokenParam` URL param back in via UrlHelper, strip it again |
95
|
|
|
if (Seomatic::$plugin->metaContainers->paginationPage === '1') { |
96
|
|
|
$token = Craft::$app->getConfig()->getGeneral()->tokenParam; |
97
|
|
|
$url = preg_replace('~([?&])' . $token . '=[^&]*~', '$1', $url); |
98
|
|
|
$url = rtrim($url, '?&'); |
99
|
|
|
$url = str_replace(['&&', '?&'], ['&', '?'], $url); |
100
|
|
|
$data['href'] = $url; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $shouldRender; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|