1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SEOmatic plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* @link https://nystudio107.com/ |
6
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
7
|
|
|
* @license https://nystudio107.com/license |
|
|
|
|
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace nystudio107\seomatic\controllers; |
11
|
|
|
|
12
|
|
|
use nystudio107\seomatic\Seomatic; |
13
|
|
|
use nystudio107\seomatic\models\MetaJsonLdContainer; |
14
|
|
|
use nystudio107\seomatic\models\MetaLinkContainer; |
15
|
|
|
use nystudio107\seomatic\models\MetaScriptContainer; |
16
|
|
|
use nystudio107\seomatic\models\MetaTitleContainer; |
17
|
|
|
use nystudio107\seomatic\models\MetaTagContainer; |
18
|
|
|
|
19
|
|
|
use Craft; |
20
|
|
|
use craft\web\Controller; |
21
|
|
|
|
22
|
|
|
use yii\web\Response; |
23
|
|
|
use yii\web\JsonResponseFormatter; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author nystudio107 |
|
|
|
|
27
|
|
|
* @package Seomatic |
28
|
|
|
* @since 3.0.0 |
|
|
|
|
29
|
|
|
*/ |
|
|
|
|
30
|
|
|
class MetaContainerController extends Controller |
31
|
|
|
{ |
32
|
|
|
// Properties |
33
|
|
|
// ========================================================================= |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
|
|
protected $allowAnonymous = [ |
39
|
|
|
'all-meta-containers', |
40
|
|
|
'all-meta-containers-for-site', |
41
|
|
|
'meta-title-container', |
42
|
|
|
'meta-title-container-for-site', |
43
|
|
|
'meta-tag-container', |
44
|
|
|
'meta-tag-container-for-site', |
45
|
|
|
'meta-link-container', |
46
|
|
|
'meta-link-container-for-site', |
47
|
|
|
'meta-script-container', |
48
|
|
|
'meta-script-container-for-site', |
49
|
|
|
'meta-json-ld-container', |
50
|
|
|
'meta-json-ld-container-for-site', |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
// Public Methods |
54
|
|
|
// ========================================================================= |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return all of the containers |
58
|
|
|
* URI: /actions/seomatic/meta-container/all-meta-containers?path=&siteId= |
59
|
|
|
* |
60
|
|
|
* @param string $uri |
|
|
|
|
61
|
|
|
* @param int $siteId |
|
|
|
|
62
|
|
|
* @param bool $asArray |
|
|
|
|
63
|
|
|
* |
64
|
|
|
* @return Response |
65
|
|
|
*/ |
66
|
|
|
public function actionAllMetaContainers(string $uri, int $siteId = null, bool $asArray = false): Response |
67
|
|
|
{ |
68
|
|
|
$result = $this->getContainerArrays( |
69
|
|
|
[ |
70
|
|
|
MetaTitleContainer::CONTAINER_TYPE, |
71
|
|
|
MetaTagContainer::CONTAINER_TYPE, |
72
|
|
|
MetaLinkContainer::CONTAINER_TYPE, |
73
|
|
|
MetaScriptContainer::CONTAINER_TYPE, |
74
|
|
|
MetaJsonLdContainer::CONTAINER_TYPE, |
75
|
|
|
], |
76
|
|
|
$uri, |
77
|
|
|
$siteId, |
78
|
|
|
$asArray |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
return $this->asJson($result); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Return all of the containers for a given site handle |
86
|
|
|
* URI: /actions/seomatic/meta-container/all-meta-containers-for-site?path=&site= |
87
|
|
|
* |
88
|
|
|
* @param string $uri |
|
|
|
|
89
|
|
|
* @param string $site |
|
|
|
|
90
|
|
|
* @param bool $asArray |
|
|
|
|
91
|
|
|
* |
92
|
|
|
* @return Response |
93
|
|
|
*/ |
94
|
|
|
public function actionAllMetaContainersForSite(string $uri, string $site = null, bool $asArray = false): Response |
95
|
|
|
{ |
96
|
|
|
$siteId = Craft::$app->getSites()->getSiteByHandle($site)->id; |
|
|
|
|
97
|
|
|
|
98
|
|
|
return $this->actionAllMetaContainers($uri, $siteId, $asArray); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Return the MetaTitleContainer |
103
|
|
|
* URI: /actions/seomatic/meta-container/meta-title-container?path=&siteId= |
104
|
|
|
* |
105
|
|
|
* @param string $uri |
|
|
|
|
106
|
|
|
* @param int $siteId |
|
|
|
|
107
|
|
|
* @param bool $asArray |
|
|
|
|
108
|
|
|
* |
109
|
|
|
* @return Response |
110
|
|
|
*/ |
111
|
|
|
public function actionMetaTitleContainer(string $uri, int $siteId = null, bool $asArray = false): Response |
112
|
|
|
{ |
113
|
|
|
$result = $this->getContainerArrays( |
114
|
|
|
[ |
115
|
|
|
MetaTitleContainer::CONTAINER_TYPE, |
116
|
|
|
], |
117
|
|
|
$uri, |
118
|
|
|
$siteId, |
119
|
|
|
$asArray |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
return $this->asJson($result); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Return the MetaTitleContainer for a given site handle |
127
|
|
|
* URI: /actions/seomatic/meta-container/meta-title-container-for-site?path=&site= |
128
|
|
|
* |
129
|
|
|
* @param string $uri |
|
|
|
|
130
|
|
|
* @param string $site |
|
|
|
|
131
|
|
|
* @param bool $asArray |
|
|
|
|
132
|
|
|
* |
133
|
|
|
* @return Response |
134
|
|
|
*/ |
135
|
|
|
public function actionMetaTitleContainerForSite(string $uri, string $site = null, bool $asArray = false): Response |
136
|
|
|
{ |
137
|
|
|
$siteId = Craft::$app->getSites()->getSiteByHandle($site)->id; |
|
|
|
|
138
|
|
|
|
139
|
|
|
return $this->actionMetaTitleContainer($uri, $siteId, $asArray); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Return the MetaTagContainer |
144
|
|
|
* URI: /actions/seomatic/meta-container/meta-tag-container?path=&siteId= |
145
|
|
|
* |
146
|
|
|
* @param string $uri |
|
|
|
|
147
|
|
|
* @param int $siteId |
|
|
|
|
148
|
|
|
* @param bool $asArray |
|
|
|
|
149
|
|
|
* |
150
|
|
|
* @return Response |
151
|
|
|
*/ |
152
|
|
|
public function actionMetaTagContainer(string $uri, int $siteId = null, bool $asArray = false): Response |
153
|
|
|
{ |
154
|
|
|
$result = $this->getContainerArrays( |
155
|
|
|
[ |
156
|
|
|
MetaTagContainer::CONTAINER_TYPE, |
157
|
|
|
], |
158
|
|
|
$uri, |
159
|
|
|
$siteId, |
160
|
|
|
$asArray |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
return $this->asJson($result); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Return the MetaTagContainer for a given site handle |
168
|
|
|
* URI: /actions/seomatic/meta-container/meta-tag-container-for-site?path=&site= |
169
|
|
|
* |
170
|
|
|
* @param string $uri |
|
|
|
|
171
|
|
|
* @param string $site |
|
|
|
|
172
|
|
|
* @param bool $asArray |
|
|
|
|
173
|
|
|
* |
174
|
|
|
* @return Response |
175
|
|
|
*/ |
176
|
|
|
public function actionMetaTagContainerForSite(string $uri, string $site = null, bool $asArray = false): Response |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
$siteId = Craft::$app->getSites()->getSiteByHandle($site)->id; |
|
|
|
|
179
|
|
|
|
180
|
|
|
return $this->actionMetaTagContainer($uri, $siteId, $asArray); |
181
|
|
|
} |
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Return the MetaLinkContainer |
185
|
|
|
* URI: /actions/seomatic/meta-container/meta-link-container?path=&siteId= |
186
|
|
|
* |
187
|
|
|
* @param string $uri |
|
|
|
|
188
|
|
|
* @param int $siteId |
|
|
|
|
189
|
|
|
* @param bool $asArray |
|
|
|
|
190
|
|
|
* |
191
|
|
|
* @return Response |
192
|
|
|
*/ |
193
|
|
|
public function actionMetaLinkContainer(string $uri, int $siteId = null, bool $asArray = false): Response |
194
|
|
|
{ |
195
|
|
|
$result = $this->getContainerArrays( |
196
|
|
|
[ |
197
|
|
|
MetaLinkContainer::CONTAINER_TYPE, |
198
|
|
|
], |
199
|
|
|
$uri, |
200
|
|
|
$siteId, |
201
|
|
|
$asArray |
202
|
|
|
); |
203
|
|
|
|
204
|
|
|
return $this->asJson($result); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Return the MetaLinkContainer for a given site handle |
209
|
|
|
* URI: /actions/seomatic/meta-container/meta-link-container-for-site?path=&site= |
210
|
|
|
* |
211
|
|
|
* @param string $uri |
|
|
|
|
212
|
|
|
* @param string $site |
|
|
|
|
213
|
|
|
* @param bool $asArray |
|
|
|
|
214
|
|
|
* |
215
|
|
|
* @return Response |
216
|
|
|
*/ |
217
|
|
|
public function actionMetaLinkContainerForSite(string $uri, string $site = null, bool $asArray = false): Response |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
$siteId = Craft::$app->getSites()->getSiteByHandle($site)->id; |
|
|
|
|
220
|
|
|
|
221
|
|
|
return $this->actionMetaLinkContainer($uri, $siteId, $asArray); |
222
|
|
|
} |
|
|
|
|
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Return the MetaScriptContainer |
226
|
|
|
* URI: /actions/seomatic/meta-container/meta-script-container?path=&siteId= |
227
|
|
|
* |
228
|
|
|
* @param string $uri |
|
|
|
|
229
|
|
|
* @param int $siteId |
|
|
|
|
230
|
|
|
* @param bool $asArray |
|
|
|
|
231
|
|
|
* |
232
|
|
|
* @return Response |
233
|
|
|
*/ |
234
|
|
|
public function actionMetaScriptContainer(string $uri, int $siteId = null, bool $asArray = false): Response |
235
|
|
|
{ |
236
|
|
|
$result = $this->getContainerArrays( |
237
|
|
|
[ |
238
|
|
|
MetaScriptContainer::CONTAINER_TYPE, |
239
|
|
|
], |
240
|
|
|
$uri, |
241
|
|
|
$siteId, |
242
|
|
|
$asArray |
243
|
|
|
); |
244
|
|
|
|
245
|
|
|
return $this->asJson($result); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Return the MetaScriptContainer for a given site handle |
250
|
|
|
* URI: /actions/seomatic/meta-container/meta-script-container-for-site?path=&site= |
251
|
|
|
* |
252
|
|
|
* @param string $uri |
|
|
|
|
253
|
|
|
* @param string $site |
|
|
|
|
254
|
|
|
* @param bool $asArray |
|
|
|
|
255
|
|
|
* |
256
|
|
|
* @return Response |
257
|
|
|
*/ |
258
|
|
|
public function actionMetaScriptContainerForSite(string $uri, string $site = null, bool $asArray = false): Response |
|
|
|
|
259
|
|
|
{ |
260
|
|
|
$siteId = Craft::$app->getSites()->getSiteByHandle($site)->id; |
|
|
|
|
261
|
|
|
|
262
|
|
|
return $this->actionMetaScriptContainer($uri, $siteId, $asArray); |
263
|
|
|
} |
|
|
|
|
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Return the MetaJsonLdContainer |
267
|
|
|
* URI: /actions/seomatic/meta-container/meta-json-ld-container?path=&siteId= |
268
|
|
|
* |
269
|
|
|
* @param string $uri |
|
|
|
|
270
|
|
|
* @param int $siteId |
|
|
|
|
271
|
|
|
* @param bool $asArray |
|
|
|
|
272
|
|
|
* |
273
|
|
|
* @return Response |
274
|
|
|
*/ |
275
|
|
|
public function actionMetaJsonLdContainer(string $uri, int $siteId = null, bool $asArray = false): Response |
276
|
|
|
{ |
277
|
|
|
$result = $this->getContainerArrays( |
278
|
|
|
[ |
279
|
|
|
MetaJsonLdContainer::CONTAINER_TYPE, |
280
|
|
|
], |
281
|
|
|
$uri, |
282
|
|
|
$siteId, |
283
|
|
|
$asArray |
284
|
|
|
); |
285
|
|
|
|
286
|
|
|
return $this->asJson($result); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
|
|
|
|
290
|
|
|
* Return the MetaJsonLdContainer for a given site handle |
291
|
|
|
* URI: /actions/seomatic/meta-container/meta-json-ld-container-for-site?path=&site= |
292
|
|
|
* |
293
|
|
|
* @param string $uri |
|
|
|
|
294
|
|
|
* @param int $siteId |
|
|
|
|
295
|
|
|
* @param bool $asArray |
|
|
|
|
296
|
|
|
* |
297
|
|
|
* @return Response |
298
|
|
|
*/ |
299
|
|
|
public function actionMetaJsonLdContainerForSite(string $uri, string $site = null, bool $asArray = false): Response |
300
|
|
|
{ |
301
|
|
|
$siteId = Craft::$app->getSites()->getSiteByHandle($site)->id; |
|
|
|
|
302
|
|
|
|
303
|
|
|
return $this->actionMetaJsonLdContainer($uri, $siteId, $asArray); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
// Protected Methods |
307
|
|
|
// ========================================================================= |
308
|
|
|
|
309
|
|
|
protected function getContainerArrays( |
|
|
|
|
310
|
|
|
array $containerKeys, |
311
|
|
|
string $uri, |
312
|
|
|
int $siteId = null, |
313
|
|
|
bool $asArray = false |
314
|
|
|
): array { |
315
|
|
|
$result = []; |
316
|
|
|
|
317
|
|
|
// Load the meta containers and parse our globals |
318
|
|
|
Seomatic::$plugin->metaContainers->previewMetaContainers($uri, $siteId, true); |
319
|
|
|
|
320
|
|
|
// Iterate through the desired $containerKeys |
321
|
|
|
foreach ($containerKeys as $containerKey) { |
322
|
|
|
if ($asArray) { |
323
|
|
|
$result[$containerKey] = Seomatic::$plugin->metaContainers->renderContainersArrayByType( |
324
|
|
|
$containerKey |
325
|
|
|
); |
326
|
|
|
} else { |
327
|
|
|
$result[$containerKey] = Seomatic::$plugin->metaContainers->renderContainersByType( |
328
|
|
|
$containerKey |
329
|
|
|
); |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
// use "pretty" output in debug mode |
333
|
|
|
Craft::$app->response->formatters[Response::FORMAT_JSON] = [ |
334
|
|
|
'class' => JsonResponseFormatter::class, |
335
|
|
|
'prettyPrint' => YII_DEBUG, |
336
|
|
|
]; |
337
|
|
|
|
338
|
|
|
return $result; |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|