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\controllers; |
13
|
|
|
|
14
|
|
|
use craft\web\Controller; |
15
|
|
|
use nystudio107\seomatic\helpers\Schema as SchemaHelper; |
16
|
|
|
use nystudio107\seomatic\Seomatic; |
17
|
|
|
use yii\web\Response; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author nystudio107 |
21
|
|
|
* @package Seomatic |
22
|
|
|
* @since 3.0.0 |
23
|
|
|
*/ |
24
|
|
|
class JsonLdController extends Controller |
25
|
|
|
{ |
26
|
|
|
// Properties |
27
|
|
|
// ========================================================================= |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
|
|
protected $allowAnonymous = [ |
33
|
|
|
'get-type', |
34
|
|
|
'get-decomposed-type', |
35
|
|
|
'get-type-array', |
36
|
|
|
'get-type-menu', |
37
|
|
|
'get-single-type-menu', |
38
|
|
|
'get-type-tree', |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
// Public Methods |
42
|
|
|
// ========================================================================= |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
|
|
public function beforeAction($action) |
48
|
|
|
{ |
49
|
|
|
if (!Seomatic::$settings->enableJsonLdEndpoint) { |
50
|
|
|
$this->allowAnonymous = false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return parent::beforeAction($action); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the fully composed schema type |
58
|
|
|
* |
59
|
|
|
* @param string $schemaType |
60
|
|
|
* |
61
|
|
|
* @return Response |
62
|
|
|
*/ |
63
|
|
|
public function actionGetType($schemaType): Response |
64
|
|
|
{ |
65
|
|
|
return $this->asJson(SchemaHelper::getSchemaType($schemaType)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the fully composed schema type |
70
|
|
|
* |
71
|
|
|
* @param string $schemaType |
72
|
|
|
* @return Response |
73
|
|
|
* @throws \Exception |
74
|
|
|
*/ |
75
|
|
|
public function actionGetTypeInfo($schemaType): Response |
76
|
|
|
{ |
77
|
|
|
return $this->asJson([ |
78
|
|
|
'schema' => SchemaHelper::getSchemaType($schemaType), |
79
|
|
|
'meta' => SchemaHelper::getTypeMetaInfo($schemaType), |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the decomposed schema type |
85
|
|
|
* |
86
|
|
|
* @param string $schemaType |
87
|
|
|
* |
88
|
|
|
* @return Response |
89
|
|
|
*/ |
90
|
|
|
public function actionGetDecomposedType($schemaType): Response |
91
|
|
|
{ |
92
|
|
|
return $this->asJson(SchemaHelper::getDecomposedSchemaType($schemaType)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get an array of schema types |
97
|
|
|
* |
98
|
|
|
* @param string $path |
99
|
|
|
* |
100
|
|
|
* @return Response |
101
|
|
|
*/ |
102
|
|
|
public function actionGetTypeArray($path): Response |
103
|
|
|
{ |
104
|
|
|
try { |
105
|
|
|
$schemaArray = SchemaHelper::getSchemaArray($path); |
106
|
|
|
} catch (\Exception $e) { |
107
|
|
|
$schemaArray = []; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->asJson($schemaArray); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get a flattened menu of schema types as an array |
115
|
|
|
* |
116
|
|
|
* @param string $path |
117
|
|
|
* |
118
|
|
|
* @return Response |
119
|
|
|
*/ |
120
|
|
|
public function actionGetTypeMenu($path): Response |
121
|
|
|
{ |
122
|
|
|
return $this->asJson(SchemaHelper::getTypeMenu($path)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Return a single menu of schemas starting at $path |
127
|
|
|
* |
128
|
|
|
* @param string $path |
129
|
|
|
* |
130
|
|
|
* @return Response |
131
|
|
|
*/ |
132
|
|
|
public function actionGetSingleTypeMenu($path): Response |
133
|
|
|
{ |
134
|
|
|
return $this->asJson(SchemaHelper::getSingleTypeMenu($path)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Return a full nested tree of Schema.org types, pre-formatted for the treeselect component |
139
|
|
|
* |
140
|
|
|
* @return Response |
141
|
|
|
*/ |
142
|
|
|
public function actionGetTypeTree(): Response |
143
|
|
|
{ |
144
|
|
|
return $this->asJson(SchemaHelper::getTypeTree()); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|