1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SEOmatic plugin for Craft CMS |
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; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use nystudio107\seomatic\base\MetaItem; |
16
|
|
|
use nystudio107\seomatic\helpers\ArrayHelper; |
17
|
|
|
use nystudio107\seomatic\helpers\MetaValue as MetaValueHelper; |
18
|
|
|
use nystudio107\seomatic\Seomatic; |
19
|
|
|
use yii\helpers\Html; |
20
|
|
|
use yii\helpers\Inflector; |
21
|
|
|
use function count; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author nystudio107 |
25
|
|
|
* @package Seomatic |
26
|
|
|
* @since 3.0.0 |
27
|
|
|
*/ |
28
|
|
|
class MetaLink extends MetaItem |
29
|
|
|
{ |
30
|
|
|
// Constants |
31
|
|
|
// ========================================================================= |
32
|
|
|
|
33
|
|
|
public const ITEM_TYPE = 'MetaLink'; |
34
|
|
|
|
35
|
|
|
public const ARRAY_PROPERTIES = [ |
36
|
|
|
'href', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
// Static Methods |
40
|
|
|
// ========================================================================= |
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
public $crossorigin; |
45
|
|
|
|
46
|
|
|
// Public Properties |
47
|
|
|
// ========================================================================= |
48
|
|
|
/** |
49
|
|
|
* @var string|array |
50
|
|
|
*/ |
51
|
|
|
public $href; |
52
|
|
|
/** |
53
|
|
|
* @var string|array |
54
|
|
|
*/ |
55
|
|
|
public $hreflang; |
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
public $media; |
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
public $rel; |
64
|
|
|
/** |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
public $sizes; |
68
|
|
|
/** |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
public $type; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param null|string $tagType |
75
|
|
|
* @param array $config |
76
|
|
|
* |
77
|
|
|
* @return MetaLink |
78
|
|
|
*/ |
79
|
|
|
public static function create($tagType = null, array $config = []): MetaLink |
80
|
|
|
{ |
81
|
|
|
$tagType = $tagType ? Inflector::variablize($tagType) : $tagType; |
82
|
|
|
foreach ($config as $key => $value) { |
83
|
|
|
ArrayHelper::rename($config, $key, Inflector::variablize($key)); |
84
|
|
|
} |
85
|
|
|
$className = MetaLink::class; |
86
|
|
|
if ($tagType) { |
87
|
|
|
// Potentially load a sub-type of MetaTag |
88
|
|
|
$tagClassName = 'nystudio107\\seomatic\\models\\metalink\\' . ucfirst($tagType) . 'Link'; |
89
|
|
|
/** @var $model MetaLink */ |
90
|
|
|
if (class_exists($tagClassName)) { |
91
|
|
|
$className = $tagClassName; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return new $className($config); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Public Methods |
99
|
|
|
// ========================================================================= |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @inheritdoc |
103
|
|
|
*/ |
104
|
|
|
public function init(): void |
105
|
|
|
{ |
106
|
|
|
parent::init(); |
107
|
|
|
|
108
|
|
|
// Make sure we have a valid key |
109
|
|
|
$this->key = $this->key ?: lcfirst($this->rel); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritdoc |
114
|
|
|
*/ |
115
|
|
|
public function rules(): array |
116
|
|
|
{ |
117
|
|
|
$rules = parent::rules(); |
118
|
|
|
$rules = array_merge($rules, [ |
119
|
|
|
[['crossorigin', 'media', 'rel', 'sizes', 'type'], 'string'], |
120
|
|
|
['crossorigin', 'in', 'range' => [ |
121
|
|
|
'anonymous', |
122
|
|
|
'use-credentials', |
123
|
|
|
]], |
124
|
|
|
['href', 'validateStringOrArray'], |
125
|
|
|
['hreflang', 'validateStringOrArray'], |
126
|
|
|
['rel', 'required'], |
127
|
|
|
['rel', 'in', 'range' => [ |
128
|
|
|
'alternate', |
129
|
|
|
'author', |
130
|
|
|
'canonical', |
131
|
|
|
'creator', |
132
|
|
|
'dns-prefetch', |
133
|
|
|
'help', |
134
|
|
|
'home', |
135
|
|
|
'icon', |
136
|
|
|
'license', |
137
|
|
|
'next', |
138
|
|
|
'pingback', |
139
|
|
|
'preconnect', |
140
|
|
|
'prefetch', |
141
|
|
|
'preload', |
142
|
|
|
'prerender', |
143
|
|
|
'prev', |
144
|
|
|
'publisher', |
145
|
|
|
'search', |
146
|
|
|
'stylesheet', |
147
|
|
|
]], |
148
|
|
|
]); |
149
|
|
|
|
150
|
|
|
return $rules; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @inheritdoc |
155
|
|
|
*/ |
156
|
|
|
public function fields(): array |
157
|
|
|
{ |
158
|
|
|
$fields = parent::fields(); |
|
|
|
|
159
|
|
|
if ($this->scenario === 'default') { |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return parent::fields(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @inheritdoc |
167
|
|
|
*/ |
168
|
|
|
public function prepForRender(&$data): bool |
169
|
|
|
{ |
170
|
|
|
$shouldRender = parent::prepForRender($data); |
171
|
|
|
if ($shouldRender) { |
172
|
|
|
MetaValueHelper::parseArray($data); |
173
|
|
|
// Only render if there's more than one attribute |
174
|
|
|
if (count($data) > 1) { |
175
|
|
|
// Special-case scenarios |
176
|
|
|
if (Seomatic::$devMode) { |
177
|
|
|
} |
178
|
|
|
} else { |
179
|
|
|
if (Seomatic::$devMode) { |
180
|
|
|
$error = Craft::t( |
181
|
|
|
'seomatic', |
182
|
|
|
'{tagtype} tag `{key}` did not render because it is missing attributes.', |
183
|
|
|
['tagtype' => 'Link', 'key' => $this->key] |
184
|
|
|
); |
185
|
|
|
Craft::info('WARNING - ' . $error, __METHOD__); |
186
|
|
|
} |
187
|
|
|
$shouldRender = false; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $shouldRender; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @inheritdoc |
196
|
|
|
*/ |
197
|
|
|
public function render(array $params = []): string |
198
|
|
|
{ |
199
|
|
|
$html = ''; |
200
|
|
|
$linebreak = ''; |
201
|
|
|
// If `devMode` is enabled, make it more human-readable |
202
|
|
|
if (Seomatic::$devMode) { |
203
|
|
|
$linebreak = PHP_EOL; |
204
|
|
|
} |
205
|
|
|
$configs = $this->tagAttributesArray(); |
206
|
|
|
foreach ($configs as $config) { |
207
|
|
|
if ($this->prepForRender($config)) { |
208
|
|
|
ksort($config); |
209
|
|
|
$html .= Html::tag('link', '', $config) . $linebreak; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $html; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @inheritdoc |
218
|
|
|
*/ |
219
|
|
|
public function renderAttributes(array $params = []): array |
220
|
|
|
{ |
221
|
|
|
$attributes = []; |
222
|
|
|
|
223
|
|
|
$configs = $this->tagAttributesArray(); |
224
|
|
|
foreach ($configs as $config) { |
225
|
|
|
if ($this->prepForRender($config)) { |
226
|
|
|
ksort($config); |
227
|
|
|
$attributes[] = $config; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
if (count($attributes) === 1) { |
231
|
|
|
$attributes = $attributes[0]; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return $attributes; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|