|
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\helpers\Container as ContainerHelper; |
|
14
|
|
|
use nystudio107\seomatic\models\MetaJsonLdContainer; |
|
15
|
|
|
use nystudio107\seomatic\models\MetaLinkContainer; |
|
16
|
|
|
use nystudio107\seomatic\models\MetaScriptContainer; |
|
17
|
|
|
use nystudio107\seomatic\models\MetaSiteVars; |
|
18
|
|
|
use nystudio107\seomatic\models\MetaTitleContainer; |
|
19
|
|
|
use nystudio107\seomatic\models\MetaTagContainer; |
|
20
|
|
|
|
|
21
|
|
|
use Craft; |
|
22
|
|
|
use craft\web\Controller; |
|
23
|
|
|
|
|
24
|
|
|
use yii\web\Response; |
|
25
|
|
|
use yii\web\JsonResponseFormatter; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
29
|
|
|
* @package Seomatic |
|
30
|
|
|
* @since 3.0.0 |
|
31
|
|
|
*/ |
|
32
|
|
|
class MetaContainerController extends Controller |
|
33
|
|
|
{ |
|
34
|
|
|
// Protected Properties |
|
35
|
|
|
// ========================================================================= |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inheritdoc |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $allowAnonymous = [ |
|
41
|
|
|
'all-meta-containers', |
|
42
|
|
|
'meta-title-container', |
|
43
|
|
|
'meta-tag-container', |
|
44
|
|
|
'meta-link-container', |
|
45
|
|
|
'meta-script-container', |
|
46
|
|
|
'meta-json-ld-container', |
|
47
|
|
|
'meta-site-vars-container', |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
// Public Methods |
|
51
|
|
|
// ========================================================================= |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
|
|
|
|
|
54
|
|
|
* @inheritDoc |
|
55
|
|
|
*/ |
|
56
|
|
|
public function beforeAction($action) |
|
57
|
|
|
{ |
|
58
|
|
|
if (!Seomatic::$settings->enableMetaContainerEndpoint) { |
|
59
|
|
|
$this->allowAnonymous = false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return parent::beforeAction($action); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Return all of the containers |
|
67
|
|
|
* URI: /actions/seomatic/meta-container/all-meta-containers?uri=/ |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $uri |
|
|
|
|
|
|
70
|
|
|
* @param int $siteId |
|
|
|
|
|
|
71
|
|
|
* @param bool $asArray |
|
|
|
|
|
|
72
|
|
|
* |
|
73
|
|
|
* @return Response |
|
74
|
|
|
*/ |
|
75
|
|
|
public function actionAllMetaContainers(string $uri, int $siteId = null, bool $asArray = false): Response |
|
76
|
|
|
{ |
|
77
|
|
|
$result = ContainerHelper::getContainerArrays( |
|
78
|
|
|
[ |
|
79
|
|
|
MetaTitleContainer::CONTAINER_TYPE, |
|
80
|
|
|
MetaTagContainer::CONTAINER_TYPE, |
|
81
|
|
|
MetaLinkContainer::CONTAINER_TYPE, |
|
82
|
|
|
MetaScriptContainer::CONTAINER_TYPE, |
|
83
|
|
|
MetaJsonLdContainer::CONTAINER_TYPE, |
|
84
|
|
|
MetaSiteVars::CONTAINER_TYPE, |
|
85
|
|
|
], |
|
86
|
|
|
$uri, |
|
87
|
|
|
$siteId, |
|
88
|
|
|
$asArray |
|
89
|
|
|
); |
|
90
|
|
|
// use "pretty" output in debug mode |
|
91
|
|
|
Craft::$app->response->formatters[Response::FORMAT_JSON] = [ |
|
92
|
|
|
'class' => JsonResponseFormatter::class, |
|
93
|
|
|
'prettyPrint' => YII_DEBUG, |
|
94
|
|
|
]; |
|
95
|
|
|
|
|
96
|
|
|
return $this->asJson($result); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Return the MetaTitleContainer |
|
101
|
|
|
* URI: /actions/seomatic/meta-container/meta-title-container?uri=/ |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $uri |
|
|
|
|
|
|
104
|
|
|
* @param int $siteId |
|
|
|
|
|
|
105
|
|
|
* @param bool $asArray |
|
|
|
|
|
|
106
|
|
|
* |
|
107
|
|
|
* @return Response |
|
108
|
|
|
*/ |
|
109
|
|
|
public function actionMetaTitleContainer(string $uri, int $siteId = null, bool $asArray = false): Response |
|
110
|
|
|
{ |
|
111
|
|
|
$result = ContainerHelper::getContainerArrays( |
|
112
|
|
|
[ |
|
113
|
|
|
MetaTitleContainer::CONTAINER_TYPE, |
|
114
|
|
|
], |
|
115
|
|
|
$uri, |
|
116
|
|
|
$siteId, |
|
117
|
|
|
$asArray |
|
118
|
|
|
); |
|
119
|
|
|
// use "pretty" output in debug mode |
|
120
|
|
|
Craft::$app->response->formatters[Response::FORMAT_JSON] = [ |
|
121
|
|
|
'class' => JsonResponseFormatter::class, |
|
122
|
|
|
'prettyPrint' => YII_DEBUG, |
|
123
|
|
|
]; |
|
124
|
|
|
|
|
125
|
|
|
return $this->asJson($result); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Return the MetaTagContainer |
|
130
|
|
|
* URI: /actions/seomatic/meta-container/meta-tag-container?uri=/ |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $uri |
|
|
|
|
|
|
133
|
|
|
* @param int $siteId |
|
|
|
|
|
|
134
|
|
|
* @param bool $asArray |
|
|
|
|
|
|
135
|
|
|
* |
|
136
|
|
|
* @return Response |
|
137
|
|
|
*/ |
|
138
|
|
|
public function actionMetaTagContainer(string $uri, int $siteId = null, bool $asArray = false): Response |
|
139
|
|
|
{ |
|
140
|
|
|
$result = ContainerHelper::getContainerArrays( |
|
141
|
|
|
[ |
|
142
|
|
|
MetaTagContainer::CONTAINER_TYPE, |
|
143
|
|
|
], |
|
144
|
|
|
$uri, |
|
145
|
|
|
$siteId, |
|
146
|
|
|
$asArray |
|
147
|
|
|
); |
|
148
|
|
|
// use "pretty" output in debug mode |
|
149
|
|
|
Craft::$app->response->formatters[Response::FORMAT_JSON] = [ |
|
150
|
|
|
'class' => JsonResponseFormatter::class, |
|
151
|
|
|
'prettyPrint' => YII_DEBUG, |
|
152
|
|
|
]; |
|
153
|
|
|
|
|
154
|
|
|
return $this->asJson($result); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Return the MetaLinkContainer |
|
159
|
|
|
* URI: /actions/seomatic/meta-container/meta-link-container?uri=/ |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $uri |
|
|
|
|
|
|
162
|
|
|
* @param int $siteId |
|
|
|
|
|
|
163
|
|
|
* @param bool $asArray |
|
|
|
|
|
|
164
|
|
|
* |
|
165
|
|
|
* @return Response |
|
166
|
|
|
*/ |
|
167
|
|
|
public function actionMetaLinkContainer(string $uri, int $siteId = null, bool $asArray = false): Response |
|
168
|
|
|
{ |
|
169
|
|
|
$result = ContainerHelper::getContainerArrays( |
|
170
|
|
|
[ |
|
171
|
|
|
MetaLinkContainer::CONTAINER_TYPE, |
|
172
|
|
|
], |
|
173
|
|
|
$uri, |
|
174
|
|
|
$siteId, |
|
175
|
|
|
$asArray |
|
176
|
|
|
); |
|
177
|
|
|
// use "pretty" output in debug mode |
|
178
|
|
|
Craft::$app->response->formatters[Response::FORMAT_JSON] = [ |
|
179
|
|
|
'class' => JsonResponseFormatter::class, |
|
180
|
|
|
'prettyPrint' => YII_DEBUG, |
|
181
|
|
|
]; |
|
182
|
|
|
|
|
183
|
|
|
return $this->asJson($result); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Return the MetaScriptContainer |
|
188
|
|
|
* URI: /actions/seomatic/meta-container/meta-script-container?uri=/ |
|
189
|
|
|
* |
|
190
|
|
|
* @param string $uri |
|
|
|
|
|
|
191
|
|
|
* @param int $siteId |
|
|
|
|
|
|
192
|
|
|
* @param bool $asArray |
|
|
|
|
|
|
193
|
|
|
* |
|
194
|
|
|
* @return Response |
|
195
|
|
|
*/ |
|
196
|
|
|
public function actionMetaScriptContainer(string $uri, int $siteId = null, bool $asArray = false): Response |
|
197
|
|
|
{ |
|
198
|
|
|
$result = ContainerHelper::getContainerArrays( |
|
199
|
|
|
[ |
|
200
|
|
|
MetaScriptContainer::CONTAINER_TYPE, |
|
201
|
|
|
], |
|
202
|
|
|
$uri, |
|
203
|
|
|
$siteId, |
|
204
|
|
|
$asArray |
|
205
|
|
|
); |
|
206
|
|
|
// use "pretty" output in debug mode |
|
207
|
|
|
Craft::$app->response->formatters[Response::FORMAT_JSON] = [ |
|
208
|
|
|
'class' => JsonResponseFormatter::class, |
|
209
|
|
|
'prettyPrint' => YII_DEBUG, |
|
210
|
|
|
]; |
|
211
|
|
|
|
|
212
|
|
|
return $this->asJson($result); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Return the MetaJsonLdContainer |
|
217
|
|
|
* URI: /actions/seomatic/meta-container/meta-json-ld-container?uri=/ |
|
218
|
|
|
* |
|
219
|
|
|
* @param string $uri |
|
|
|
|
|
|
220
|
|
|
* @param int $siteId |
|
|
|
|
|
|
221
|
|
|
* @param bool $asArray |
|
|
|
|
|
|
222
|
|
|
* |
|
223
|
|
|
* @return Response |
|
224
|
|
|
*/ |
|
225
|
|
|
public function actionMetaJsonLdContainer(string $uri, int $siteId = null, bool $asArray = false): Response |
|
226
|
|
|
{ |
|
227
|
|
|
$result = ContainerHelper::getContainerArrays( |
|
228
|
|
|
[ |
|
229
|
|
|
MetaJsonLdContainer::CONTAINER_TYPE, |
|
230
|
|
|
], |
|
231
|
|
|
$uri, |
|
232
|
|
|
$siteId, |
|
233
|
|
|
$asArray |
|
234
|
|
|
); |
|
235
|
|
|
// use "pretty" output in debug mode |
|
236
|
|
|
Craft::$app->response->formatters[Response::FORMAT_JSON] = [ |
|
237
|
|
|
'class' => JsonResponseFormatter::class, |
|
238
|
|
|
'prettyPrint' => YII_DEBUG, |
|
239
|
|
|
]; |
|
240
|
|
|
|
|
241
|
|
|
return $this->asJson($result); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Return the MetaSiteVars "container" |
|
246
|
|
|
* URI: /actions/seomatic/meta-container/meta-site-vars-container?uri=/ |
|
247
|
|
|
* |
|
248
|
|
|
* @param string $uri |
|
|
|
|
|
|
249
|
|
|
* @param int $siteId |
|
|
|
|
|
|
250
|
|
|
* @param bool $asArray |
|
|
|
|
|
|
251
|
|
|
* |
|
252
|
|
|
* @return Response |
|
253
|
|
|
*/ |
|
254
|
|
|
public function actionMetaSiteVarsContainer(string $uri, int $siteId = null, bool $asArray = false): Response |
|
255
|
|
|
{ |
|
256
|
|
|
$result = ContainerHelper::getContainerArrays( |
|
257
|
|
|
[ |
|
258
|
|
|
MetaSiteVars::CONTAINER_TYPE, |
|
259
|
|
|
], |
|
260
|
|
|
$uri, |
|
261
|
|
|
$siteId, |
|
262
|
|
|
$asArray |
|
263
|
|
|
); |
|
264
|
|
|
// use "pretty" output in debug mode |
|
265
|
|
|
Craft::$app->response->formatters[Response::FORMAT_JSON] = [ |
|
266
|
|
|
'class' => JsonResponseFormatter::class, |
|
267
|
|
|
'prettyPrint' => YII_DEBUG, |
|
268
|
|
|
]; |
|
269
|
|
|
|
|
270
|
|
|
return $this->asJson($result); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
} |
|
274
|
|
|
|