Schema.php
6.11 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
<?php
namespace AIOSEO\Plugin\Common\Schema;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Builds our schema.
*
* @since 4.0.0
*/
class Schema {
/**
* The included graphs.
*
* @since 4.0.0
*
* @var array
*/
public $graphs = [];
/**
* The context data.
*
* @since 4.0.0
*
* @var array
*/
public $context = [];
/**
* All existing WebPage graphs.
*
* @since 4.0.0
*
* @var array
*/
protected $webPageGraphs = [
'WebPage',
'AboutPage',
'CheckoutPage',
'CollectionPage',
'ContactPage',
'FAQPage',
'ItemPage',
'MedicalWebPage',
'ProfilePage',
'QAPage',
'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.
];
/**
* Returns the JSON schema for the requested page.
*
* @since 4.0.0
*
* @return string The JSON schema.
*/
public function get() {
// First, let's check if it's disabled.
if (
apply_filters( 'aioseo_schema_disable', false ) ||
( in_array( 'enableSchemaMarkup', aioseo()->internalOptions->deprecatedOptions, true ) && ! aioseo()->options->deprecated->searchAppearance->global->schema->enableSchemaMarkup )
) {
return '';
}
$this->init();
if ( ! $this->graphs ) {
return '';
}
$schema = [
'@context' => 'https://schema.org',
'@graph' => []
];
$graphs = apply_filters( 'aioseo_schema_graphs', array_unique( array_filter( $this->graphs ) ) );
foreach ( $graphs as $graph ) {
if ( class_exists( "\AIOSEO\Plugin\Common\Schema\Graphs\\$graph" ) ) {
$namespace = "\AIOSEO\Plugin\Common\Schema\Graphs\\$graph";
//if graph is actually a fully qualified class name
if ( class_exists( $graph ) ) {
$namespace = $graph;
}
$schema['@graph'][] = array_filter( ( new $namespace )->get() );
}
}
$schema['@graph'] = apply_filters( 'aioseo_schema_output', $schema['@graph'] );
$schema['@graph'] = array_values( $this->cleanData( $schema['@graph'] ) );
return isset( $_GET['aioseo-dev'] ) ? wp_json_encode( $schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) : wp_json_encode( $schema );
}
/**
* Determines the context and graphs for the requested page.
*
* This can't run in the constructor since the queried object needs to be available first.
*
* @since 4.0.0
*
* @return void
*/
protected function init() {
$context = new Context();
$this->graphs = [
'WebSite',
ucfirst( aioseo()->options->searchAppearance->global->schema->siteRepresents ),
'BreadcrumbList'
];
if ( is_front_page() ) {
$this->graphs[] = 'posts' === get_option( 'show_on_front' ) ? 'CollectionPage' : 'WebPage';
$this->context = $context->home();
return;
}
if ( is_home() || aioseo()->helpers->isWooCommerceShopPage() ) {
$this->graphs[] = 'CollectionPage';
$this->context = $context->post();
return;
}
if ( is_singular() ) {
$this->context = $context->post();
// Check if we're on a BuddyPress member page.
if ( function_exists( 'bp_is_user' ) && bp_is_user() ) {
array_push( $this->graphs, 'ProfilePage', 'PersonAuthor' );
return;
}
$post = aioseo()->helpers->getPost();
if ( $post && 'page' !== $post->post_type ) {
$this->graphs[] = 'PersonAuthor';
}
$postGraphs = $this->getPostGraphs( $post );
if ( is_array( $postGraphs ) ) {
$this->graphs = array_merge( $this->graphs, $postGraphs );
return;
}
$this->graphs[] = $postGraphs;
}
if ( is_category() || is_tag() || is_tax() ) {
$this->graphs[] = 'CollectionPage';
$this->context = $context->term();
return;
}
if ( is_author() ) {
array_push( $this->graphs, 'CollectionPage', 'PersonAuthor' );
$this->context = $context->author();
return;
}
if ( is_post_type_archive() ) {
$this->graphs[] = 'CollectionPage';
$this->context = $context->postArchive();
return;
}
if ( is_date() ) {
$this->graphs[] = 'CollectionPage';
$this->context = $context->date();
return;
}
if ( is_search() ) {
$this->graphs[] = 'SearchResultsPage';
$this->context = $context->search();
return;
}
if ( is_404() ) {
$this->context = $context->notFound();
}
}
/**
* Returns the graph names that are set for the post.
*
* @since 4.0.0
*
* @param WP_Post The post object.
* @return string|array The graph name(s).
*/
public function getPostGraphs( $post = null ) {
$post = is_object( $post ) ? $post : aioseo()->helpers->getPost();
$dynamicOptions = aioseo()->dynamicOptions->noConflict();
if ( ! $dynamicOptions->searchAppearance->postTypes->has( $post->post_type ) ) {
return 'WebPage';
}
$schemaType = $dynamicOptions->searchAppearance->postTypes->{$post->post_type}->schemaType;
switch ( $schemaType ) {
case 'WebPage':
return ucfirst( $dynamicOptions->searchAppearance->postTypes->{$post->post_type}->webPageType );
case 'Article':
return [ 'WebPage', ucfirst( $dynamicOptions->searchAppearance->postTypes->{$post->post_type}->articleType ) ];
case 'none':
return '';
default:
// This fixes a bug from WPForms Form Pages.
if ( 'default' === $schemaType ) {
return 'WebPage';
}
// Check if the schema type isn't already WebPage or one of its child graphs.
if ( in_array( $schemaType, $this->webPageGraphs, true ) ) {
return ucfirst( $schemaType );
}
return [ 'WebPage', ucfirst( $schemaType ) ];
}
}
/**
* Strips HTML and removes all blank properties in each of our graphs.
*
* @since 4.0.13
*
* @param array $data The graph data.
* @return array The cleaned graph data.
*/
protected function cleanData( $data ) {
foreach ( $data as $k => &$v ) {
if ( is_array( $v ) ) {
$v = $this->cleanData( $v );
} else {
$v = is_int( $v ) ? $v : trim( wp_strip_all_tags( $v ) );
}
if ( empty( $v ) && ! in_array( $k, $this->nullableFields, true ) ) {
unset( $data[ $k ] );
} else {
$data[ $k ] = $v;
}
}
return $data;
}
}