Schema.php
8.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
namespace AIOSEO\Plugin\Common\Schema;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Builds our schema.
*
* @since 4.0.0
*/
class Schema {
/**
* The graphs that need to be generated.
*
* @since 4.2.5
*
* @var array
*/
public $graphs = [];
/**
* The context data.
*
* @since 4.0.0
*
* @var array
*/
public $context = [];
/**
* Helpers class instance.
*
* @since 4.2.7
*
* @var Helpers
*/
public $helpers = null;
/**
* The subdirectories that contain graph classes.
*
* @since 4.2.5
*
* @var array
*/
protected $graphSubDirectories = [
'Article',
'KnowledgeGraph',
'WebPage'
];
/**
* All existing WebPage graphs.
*
* @since 4.0.0
*
* @var array
*/
public $webPageGraphs = [
'WebPage',
'AboutPage',
'CheckoutPage',
'CollectionPage',
'ContactPage',
'FAQPage',
'ItemPage',
'MedicalWebPage',
'ProfilePage',
'RealEstateListing',
'SearchResultsPage'
];
/**
* Fields that can be 0 or null, which shouldn't be stripped when cleaning the data.
*
* @since 4.1.2
*
* @var array
*/
public $nullableFields = [
'price', // Needs to be 0 if free for Software Application.
'value' // Needs to be 0 if free for product shipping details.
];
/**
* List of mapped parents with properties that are allowed to contain a restricted set of HTML tags.
*
* @since 4.2.3
*
* @var array
*/
private $htmlAllowedFields = [
// FAQPage
'acceptedAnswer' => [
'text'
]
];
/**
* Class constructor.
*/
public function __construct() {
// No AJAX check since we need to be able to grab the schema output via the REST API.
if ( wp_doing_cron() ) {
return;
}
$this->helpers = new Helpers();
}
/**
* Returns the JSON schema output.
*
* @since 4.0.0
*
* @param array $graphs The graphs to output (optional - used for REST API).
* @return string The JSON schema output.
*/
public function get() {
// First, check if the schema is disabled.
if ( ! $this->helpers->isEnabled() ) {
return '';
}
$this->determineSmartGraphsAndContext();
return $this->generateSchema();
}
/**
* Generates the JSON schema after the graphs/context have been determined.
*
* @since 4.2.5
*
* @return string The JSON schema output.
*/
protected function generateSchema() {
// Now, filter the graphs.
$this->graphs = apply_filters(
'aioseo_schema_graphs',
array_unique( array_filter( array_values( $this->graphs ) ) )
);
if ( ! $this->graphs ) {
return '';
}
// Check if a WebPage graph is included. Otherwise add the default one.
$webPageGraphFound = false;
foreach ( $this->graphs as $graphName ) {
if ( in_array( $graphName, $this->webPageGraphs, true ) ) {
$webPageGraphFound = true;
break;
}
}
if ( ! $webPageGraphFound ) {
$this->graphs[] = 'WebPage';
}
// Now that we've determined the graphs, start generating their data.
$schema = [
'@context' => 'https://schema.org',
'@graph' => []
];
// By determining the length of the array after every iteration, we are able to add additional graphs during runtime.
// e.g. The Article graph may require a Person graph to be output for the author.
for ( $i = 0; $i < count( $this->graphs ); $i++ ) {
$namespace = $this->getGraphNamespace( $this->graphs[ $i ] );
if ( $namespace ) {
$schema['@graph'][] = ( new $namespace() )->get();
}
}
return aioseo()->schema->helpers->getOutput( $schema );
}
/**
* Gets the relevant namespace for the given graph.
*
* @since 4.2.5
*
* @param string $graphName The graph name.
* @return string The namespace.
*/
protected function getGraphNamespace( $graphName ) {
$namespace = "\AIOSEO\Plugin\Common\Schema\Graphs\\{$graphName}";
if ( class_exists( $namespace ) ) {
return $namespace;
}
// If we can't find it in the root dir, check if we can find it in a sub dir.
foreach ( $this->graphSubDirectories as $dirName ) {
$namespace = "\AIOSEO\Plugin\Common\Schema\Graphs\\{$dirName}\\{$graphName}";
if ( class_exists( $namespace ) ) {
return $namespace;
}
}
return '';
}
/**
* Determines the smart graphs that need to be output by default, as well as the current context for the breadcrumbs.
*
* @since 4.2.5
*
* @param bool $isValidator Whether the current call is for the validator.
* @return void
*/
protected function determineSmartGraphsAndContext( $isValidator = false ) {
$this->graphs = array_merge( $this->graphs, $this->getDefaultGraphs() );
$contextInstance = new Context();
$this->context = $contextInstance->defaults();
if ( aioseo()->helpers->isDynamicHomePage() ) {
$this->graphs[] = 'CollectionPage';
$this->context = $contextInstance->home();
return;
}
if ( is_home() || aioseo()->helpers->isWooCommerceShopPage() ) {
$this->graphs[] = 'CollectionPage';
$this->context = $contextInstance->post();
return;
}
if ( is_singular() ) {
$this->determineContextSingular( $contextInstance, $isValidator );
}
if ( is_category() || is_tag() || is_tax() ) {
$this->graphs[] = 'CollectionPage';
$this->context = $contextInstance->term();
return;
}
if ( is_author() ) {
$this->graphs[] = 'CollectionPage';
$this->graphs[] = 'PersonAuthor';
$this->context = $contextInstance->author();
}
if ( is_post_type_archive() ) {
$this->graphs[] = 'CollectionPage';
$this->context = $contextInstance->postArchive();
return;
}
if ( is_date() ) {
$this->graphs[] = 'CollectionPage';
$this->context = $contextInstance->date();
return;
}
if ( is_search() ) {
$this->graphs[] = 'SearchResultsPage';
$this->context = $contextInstance->search();
return;
}
if ( is_404() ) {
$this->context = $contextInstance->notFound();
}
}
/**
* Determines the smart graphs and context for singular pages.
*
* @since 4.2.6
*
* @param Context $contextInstance The Context class instance.
* @param bool $isValidator Whether we're getting the output for the validator.
* @return void
*/
protected function determineContextSingular( $contextInstance, $isValidator ) {
// Check if we're on a BuddyPress member page.
if ( function_exists( 'bp_is_user' ) && bp_is_user() ) {
$this->graphs[] = 'ProfilePage';
}
// If the current request is for the validator, we can't include the default graph here.
// We need to include the default graph that the validator sent.
// Don't do this if we're in Pro since we then need to get it from the post meta.
if ( ! $isValidator ) {
$this->graphs[] = $this->getDefaultPostGraph();
}
$this->context = $contextInstance->post();
}
/**
* Returns the default graph for the post type.
*
* @since 4.2.6
*
* @return string The default graph.
*/
protected function getDefaultPostGraph() {
return $this->getDefaultPostTypeGraph();
}
/**
* Returns the default graph for the current post type.
*
* @since 4.2.5
*
* @param null|WP_Post $post The post object.
* @return string The default graph.
*/
public function getDefaultPostTypeGraph( $post = null ) {
$post = $post ? $post : aioseo()->helpers->getPost();
if ( ! is_a( $post, 'WP_Post' ) ) {
return '';
}
$dynamicOptions = aioseo()->dynamicOptions->noConflict();
if ( ! $dynamicOptions->searchAppearance->postTypes->has( $post->post_type ) ) {
return '';
}
$defaultType = $dynamicOptions->searchAppearance->postTypes->{$post->post_type}->schemaType;
switch ( $defaultType ) {
case 'Article':
return $dynamicOptions->searchAppearance->postTypes->{$post->post_type}->articleType;
case 'WebPage':
return $dynamicOptions->searchAppearance->postTypes->{$post->post_type}->webPageType;
default:
return $defaultType;
}
}
/**
* Returns the default graphs that should be output on every page, regardless of its type.
*
* @since 4.2.5
*
* @return array The default graphs.
*/
protected function getDefaultGraphs() {
$siteRepresents = ucfirst( aioseo()->options->searchAppearance->global->schema->siteRepresents );
return [
'BreadcrumbList',
'Kg' . $siteRepresents,
'WebSite'
];
}
}