class-json-ld.php
6.47 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
<?php
/**
* @package WPSEO\Frontend
*/
/**
* Class WPSEO_JSON_LD
*
* Outputs schema code specific for Google's JSON LD stuff
*
* @since 1.8
*/
class WPSEO_JSON_LD {
/**
* @var array Holds the plugins options.
*/
public $options = array();
/**
* @var array Holds the social profiles for the entity
*/
private $profiles = array();
/**
* @var array Holds the data to put out
*/
private $data = array();
/**
* Class constructor
*/
public function __construct() {
$this->options = WPSEO_Options::get_options( array( 'wpseo', 'wpseo_social' ) );
add_action( 'wpseo_head', array( $this, 'json_ld' ), 90 );
add_action( 'wpseo_json_ld', array( $this, 'website' ), 10 );
add_action( 'wpseo_json_ld', array( $this, 'organization_or_person' ), 20 );
}
/**
* JSON LD output function that the functions for specific code can hook into
*
* @since 1.8
*/
public function json_ld() {
do_action( 'wpseo_json_ld' );
}
/**
* Outputs code to allow Google to recognize social profiles for use in the Knowledge graph
*
* @since 1.8
*/
public function organization_or_person() {
if ( '' === $this->options['company_or_person'] ) {
return;
}
$this->prepare_organization_person_markup();
switch ( $this->options['company_or_person'] ) {
case 'company':
$this->organization();
break;
case 'person':
$this->person();
break;
}
$this->output( $this->options['company_or_person'] );
}
/**
* Outputs code to allow recognition of the internal search engine
*
* @since 1.5.7
*
* @link https://developers.google.com/structured-data/site-name
*/
public function website() {
$this->data = array(
'@context' => 'http://schema.org',
'@type' => 'WebSite',
'@id' => '#website',
'url' => $this->get_home_url(),
'name' => $this->get_website_name(),
);
$this->add_alternate_name();
$this->internal_search_section();
$this->output( 'website' );
}
/**
* Outputs the JSON LD code in a valid JSON+LD wrapper
*
* @since 1.8
*
* @param string $context The context of the output, useful for filtering.
*/
private function output( $context ) {
/**
* Filter: 'wpseo_json_ld_output' - Allows filtering of the JSON+LD output
*
* @api array $output The output array, before its JSON encoded
*
* @param string $context The context of the output, useful to determine whether to filter or not.
*/
$this->data = apply_filters( 'wpseo_json_ld_output', $this->data, $context );
if ( is_array( $this->data ) && ! empty( $this->data ) ) {
$json_data = wp_json_encode( $this->data );
echo "<script type='application/ld+json'>", $json_data, '</script>', "\n";
}
// Empty the $data array so we don't output it twice.
$this->data = array();
}
/**
* Schema for Organization
*/
private function organization() {
if ( '' !== $this->options['company_name'] ) {
$this->data['@type'] = 'Organization';
$this->data['@id'] = '#organization';
$this->data['name'] = $this->options['company_name'];
$this->data['logo'] = $this->options['company_logo'];
return;
}
$this->data = false;
}
/**
* Schema for Person
*/
private function person() {
if ( '' !== $this->options['person_name'] ) {
$this->data['@type'] = 'Person';
$this->data['@id'] = '#person';
$this->data['name'] = $this->options['person_name'];
return;
}
$this->data = false;
}
/**
* Prepares the organization or person markup
*/
private function prepare_organization_person_markup() {
$this->fetch_social_profiles();
$this->data = array(
'@context' => 'http://schema.org',
'@type' => '',
'url' => WPSEO_Frontend::get_instance()->canonical( false, true ),
'sameAs' => $this->profiles,
);
}
/**
* Retrieve the social profiles to display in the organization output.
*
* @since 1.8
*
* @link https://developers.google.com/webmasters/structured-data/customize/social-profiles
*/
private function fetch_social_profiles() {
$social_profiles = array(
'facebook_site',
'instagram_url',
'linkedin_url',
'plus-publisher',
'myspace_url',
'youtube_url',
'pinterest_url',
);
foreach ( $social_profiles as $profile ) {
if ( $this->options[ $profile ] !== '' ) {
$this->profiles[] = $this->options[ $profile ];
}
}
if ( ! empty( $this->options['twitter_site'] ) ) {
$this->profiles[] = 'https://twitter.com/' . $this->options['twitter_site'];
}
}
/**
* Retrieves the home URL
*
* @return string
*/
private function get_home_url() {
/**
* Filter: 'wpseo_json_home_url' - Allows filtering of the home URL for Yoast SEO's JSON+LD output
*
* @api unsigned string
*/
return apply_filters( 'wpseo_json_home_url', WPSEO_Utils::home_url() );
}
/**
* Returns an alternate name if one was specified in the Yoast SEO settings
*/
private function add_alternate_name() {
if ( '' !== $this->options['alternate_website_name'] ) {
$this->data['alternateName'] = $this->options['alternate_website_name'];
}
}
/**
* Adds the internal search JSON LD code if it's not disabled
*
* @link https://developers.google.com/structured-data/slsb-overview
*/
private function internal_search_section() {
/**
* Filter: 'disable_wpseo_json_ld_search' - Allow disabling of the json+ld output
*
* @api bool $display_search Whether or not to display json+ld search on the frontend
*/
if ( ! apply_filters( 'disable_wpseo_json_ld_search', false ) ) {
/**
* Filter: 'wpseo_json_ld_search_url' - Allows filtering of the search URL for Yoast SEO
*
* @api string $search_url The search URL for this site with a `{search_term_string}` variable.
*/
$search_url = apply_filters( 'wpseo_json_ld_search_url', $this->get_home_url() . '?s={search_term_string}' );
$this->data['potentialAction'] = array(
'@type' => 'SearchAction',
'target' => $search_url,
'query-input' => 'required name=search_term_string',
);
}
}
/**
* Returns the website name either from Yoast SEO's options or from the site settings
*
* @since 2.1
*
* @return string
*/
private function get_website_name() {
if ( '' !== $this->options['website_name'] ) {
return $this->options['website_name'];
}
return get_bloginfo( 'name' );
}
/**
* Renders internal search schema markup
*
* @deprecated 2.1
* @deprecated use WPSEO_JSON_LD::website()
*/
public function internal_search() {
_deprecated_function( __METHOD__, 'WPSEO 2.1', 'WPSEO_JSON_LD::website()' );
$this->website();
}
}