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; |
13
|
|
|
|
14
|
|
|
use nystudio107\seomatic\Seomatic; |
15
|
|
|
use nystudio107\seomatic\base\NonceItem; |
16
|
|
|
use nystudio107\seomatic\helpers\PluginTemplate as PluginTemplateHelper; |
17
|
|
|
|
18
|
|
|
use Craft; |
19
|
|
|
|
20
|
|
|
use yii\web\View; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author nystudio107 |
|
|
|
|
24
|
|
|
* @package Seomatic |
25
|
|
|
* @since 3.0.0 |
26
|
|
|
*/ |
27
|
|
|
class MetaScript extends NonceItem |
28
|
|
|
{ |
29
|
|
|
// Constants |
30
|
|
|
// ========================================================================= |
31
|
|
|
|
32
|
|
|
const ITEM_TYPE = 'MetaScript'; |
33
|
|
|
|
34
|
|
|
// Static Methods |
35
|
|
|
// ========================================================================= |
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public $name; |
40
|
|
|
|
41
|
|
|
// Public Properties |
42
|
|
|
// ========================================================================= |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
public $description; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
public $templatePath; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
public $templateString; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var int |
61
|
|
|
*/ |
62
|
|
|
public $position = View::POS_HEAD; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var |
66
|
|
|
*/ |
67
|
|
|
public $bodyTemplatePath; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var |
71
|
|
|
*/ |
72
|
|
|
public $bodyTemplateString; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var int |
76
|
|
|
*/ |
77
|
|
|
public $bodyPosition = View::POS_BEGIN; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var array |
81
|
|
|
*/ |
82
|
|
|
public $vars; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var array |
86
|
|
|
*/ |
87
|
|
|
public $dataLayer = []; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array $config |
|
|
|
|
91
|
|
|
* |
92
|
|
|
* @return MetaScript |
93
|
|
|
*/ |
94
|
|
|
public static function create(array $config = []): MetaScript |
95
|
|
|
{ |
96
|
|
|
$model = new MetaScript($config); |
97
|
|
|
// Load $templateString from the source template if it's not set |
98
|
|
|
if (empty($model->templateString) && !empty($model->templatePath)) { |
99
|
|
|
$model->templateString = $model->loadTemplate($model->templatePath); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Load $templateString from the source template if it's not set |
103
|
|
|
if (empty($model->bodyTemplateString) && !empty($model->bodyTemplatePath)) { |
104
|
|
|
$model->bodyTemplateString = $model->loadTemplate($model->bodyTemplatePath); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $model; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// Public Methods |
111
|
|
|
// ========================================================================= |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritdoc |
115
|
|
|
*/ |
116
|
|
|
public function init() |
117
|
|
|
{ |
118
|
|
|
parent::init(); |
119
|
|
|
|
120
|
|
|
$this->key = $this->key ?: lcfirst($this->name); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Load the existing template into a string |
125
|
|
|
* |
126
|
|
|
* @param string $templatePath |
|
|
|
|
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function loadTemplate(string $templatePath): string |
131
|
|
|
{ |
132
|
|
|
$result = ''; |
133
|
|
|
// Try it from our plugin directory first |
134
|
|
|
$path = Craft::getAlias('@nystudio107/seomatic/templates/') |
135
|
|
|
.$templatePath; |
136
|
|
|
if (file_exists($path)) { |
137
|
|
|
$result = @file_get_contents($path); |
138
|
|
|
} else { |
139
|
|
|
// Next try it from the Craft template directory |
140
|
|
|
$path = Craft::getAlias('@templates/') |
141
|
|
|
.$templatePath; |
142
|
|
|
if (file_exists($path)) { |
143
|
|
|
$result = @file_get_contents($path); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $result; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @inheritdoc |
152
|
|
|
*/ |
153
|
|
|
public function rules() |
154
|
|
|
{ |
155
|
|
|
$rules = parent::rules(); |
156
|
|
|
$rules = array_merge($rules, [ |
157
|
|
|
[ |
158
|
|
|
[ |
159
|
|
|
'name', |
160
|
|
|
'description', |
161
|
|
|
'templatePath', |
162
|
|
|
'templateString', |
163
|
|
|
'bodyTemplatePath', |
164
|
|
|
'bodyTemplateString', |
165
|
|
|
], |
166
|
|
|
'string', |
167
|
|
|
], |
168
|
|
|
[ |
169
|
|
|
[ |
170
|
|
|
'position', |
171
|
|
|
], |
172
|
|
|
'integer', |
173
|
|
|
], |
174
|
|
|
[ |
175
|
|
|
[ |
176
|
|
|
'name', |
177
|
|
|
'description', |
178
|
|
|
'templatePath', |
179
|
|
|
'templateString', |
180
|
|
|
'bodyTemplatePath', |
181
|
|
|
'bodyTemplateString', |
182
|
|
|
'position', |
183
|
|
|
], |
184
|
|
|
'required', |
185
|
|
|
], |
186
|
|
|
[['vars'], 'safe'], |
187
|
|
|
[['dataLayer'], 'safe'], |
188
|
|
|
]); |
189
|
|
|
|
190
|
|
|
return $rules; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @inheritdoc |
195
|
|
|
*/ |
196
|
|
|
public function fields() |
197
|
|
|
{ |
198
|
|
|
$fields = parent::fields(); |
199
|
|
|
switch ($this->scenario) { |
200
|
|
|
case 'render': |
|
|
|
|
201
|
|
|
$fields = array_diff_key( |
202
|
|
|
$fields, |
203
|
|
|
array_flip([ |
204
|
|
|
'name', |
205
|
|
|
'description', |
206
|
|
|
'environment', |
207
|
|
|
'dependencies', |
208
|
|
|
]) |
209
|
|
|
); |
210
|
|
|
break; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $fields; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
|
|
|
|
217
|
|
|
* @inheritdoc |
218
|
|
|
*/ |
219
|
|
|
public function prepForRender(&$data): bool |
220
|
|
|
{ |
221
|
|
|
$shouldRender = parent::prepForRender($data); |
222
|
|
|
if ($shouldRender) { |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $shouldRender; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Render the script body HTML |
230
|
|
|
* |
231
|
|
|
* @param array $params |
|
|
|
|
232
|
|
|
* |
233
|
|
|
* @return string |
234
|
|
|
*/ |
235
|
|
|
public function renderBodyHtml(array $params = []): string |
236
|
|
|
{ |
237
|
|
|
$html = ''; |
238
|
|
|
if (!empty($this->bodyTemplatePath) && $this->prepForRender($params)) { |
239
|
|
|
$variables = $this->normalizeScriptVars(); |
240
|
|
|
if (!empty($this->bodyTemplateString)) { |
241
|
|
|
$html = PluginTemplateHelper::renderStringTemplate($this->bodyTemplateString, $variables); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return $html; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
|
|
|
|
249
|
|
|
* @inheritdoc |
250
|
|
|
*/ |
251
|
|
|
public function render(array $params = []): string |
252
|
|
|
{ |
253
|
|
|
$html = ''; |
254
|
|
|
if (!empty($this->templatePath) && $this->prepForRender($params)) { |
255
|
|
|
$variables = $this->normalizeScriptVars(); |
256
|
|
|
$html = PluginTemplateHelper::renderStringTemplate($this->templateString, $variables); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
if (empty($html) && !empty($this->templatePath)) { |
260
|
|
|
$html = '/* '.$this->name.Craft::t('seomatic', ' script did not render').' */'.PHP_EOL; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $html; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
|
|
|
|
267
|
|
|
* @inheritdoc |
268
|
|
|
*/ |
269
|
|
|
public function renderAttributes(array $params = []): array |
270
|
|
|
{ |
271
|
|
|
$attributes = []; |
272
|
|
|
|
273
|
|
|
if ($this->prepForRender($options)) { |
274
|
|
|
$variables = $this->normalizeScriptVars(); |
275
|
|
|
if (!empty($this->templateString)) { |
276
|
|
|
$attributes['script'] |
277
|
|
|
= PluginTemplateHelper::renderStringTemplate($this->templateString, $variables); |
278
|
|
|
} |
279
|
|
|
if (!empty($this->bodyTemplateString)) { |
280
|
|
|
$attributes['bodyScript'] |
281
|
|
|
= PluginTemplateHelper::renderStringTemplate($this->bodyTemplateString, $variables); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
return $attributes; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Normalize the script variables by parsing them as environment variables, and trimming whitespace |
290
|
|
|
* |
291
|
|
|
* @return array |
292
|
|
|
*/ |
293
|
|
|
private function normalizeScriptVars(): array |
|
|
|
|
294
|
|
|
{ |
295
|
|
|
$variables = array_merge($this->vars, [ |
296
|
|
|
'dataLayer' => $this->dataLayer, |
297
|
|
|
]); |
298
|
|
|
if (Seomatic::$craft31) { |
299
|
|
|
foreach ($variables as $key => $value) { |
300
|
|
|
if (!empty($value['value']) && \is_string($value['value'])) { |
301
|
|
|
$variables[$key]['value'] = Craft::parseEnv($value['value']); |
302
|
|
|
$variables[$key]['value'] = trim($value['value']); |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return $variables; |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|