PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.9
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.9
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / generators / schema / person.php

person.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.9, at src/generators/schema/person.php

313 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Generators\Schema;
4
5 use WP_User;
6 use Yoast\WP\SEO\Config\Schema_IDs;
7
8 /**
9 * Returns schema Person data.
10 */
11 class Person extends Abstract_Schema_Piece {
12
13 /**
14 * Array of the social profiles we display for a Person.
15 *
16 * @var string[]
17 */
18 private $social_profiles = [
19 'facebook',
20 'instagram',
21 'linkedin',
22 'pinterest',
23 'twitter',
24 'myspace',
25 'youtube',
26 'soundcloud',
27 'tumblr',
28 'wikipedia',
29 ];
30
31 /**
32 * The Schema type we use for this class.
33 *
34 * @var string[]
35 */
36 protected $type = [ 'Person', 'Organization' ];
37
38 /**
39 * Determine whether we should return Person schema.
40 *
41 * @return bool
42 */
43 public function is_needed() {
44 // Using an author piece instead.
45 if ( $this->site_represents_current_author() ) {
46 return false;
47 }
48
49 return $this->context->site_represents === 'person' || $this->context->indexable->object_type === 'user';
50 }
51
52 /**
53 * Returns Person Schema data.
54 *
55 * @return bool|array Person data on success, false on failure.
56 */
57 public function generate() {
58 $user_id = $this->determine_user_id();
59 if ( ! $user_id ) {
60 return false;
61 }
62
63 return $this->build_person_data( $user_id );
64 }
65
66 /**
67 * Determines a User ID for the Person data.
68 *
69 * @return bool|int User ID or false upon return.
70 */
71 protected function determine_user_id() {
72 /**
73 * Filter: 'wpseo_schema_person_user_id' - Allows filtering of user ID used for person output.
74 *
75 * @api int|bool $user_id The user ID currently determined.
76 */
77 $user_id = \apply_filters( 'wpseo_schema_person_user_id', $this->context->site_user_id );
78
79 // It should to be an integer higher than 0.
80 if ( \is_int( $user_id ) && $user_id > 0 ) {
81 return $user_id;
82 }
83
84 return false;
85 }
86
87 /**
88 * Retrieve a list of social profile URLs for Person.
89 *
90 * @param array $same_as_urls Array of SameAs URLs.
91 * @param int $user_id User ID.
92 *
93 * @return string[] A list of SameAs URLs.
94 */
95 protected function get_social_profiles( $same_as_urls, $user_id ) {
96 /**
97 * Filter: 'wpseo_schema_person_social_profiles' - Allows filtering of social profiles per user.
98 *
99 * @param int $user_id The current user we're grabbing social profiles for.
100 *
101 * @api string[] $social_profiles The array of social profiles to retrieve. Each should be a user meta field
102 * key. As they are retrieved using the WordPress function `get_the_author_meta`.
103 */
104 $social_profiles = \apply_filters( 'wpseo_schema_person_social_profiles', $this->social_profiles, $user_id );
105
106 // We can only handle an array.
107 if ( ! \is_array( $social_profiles ) ) {
108 return $same_as_urls;
109 }
110
111 foreach ( $social_profiles as $profile ) {
112 // Skip non-string values.
113 if ( ! \is_string( $profile ) ) {
114 continue;
115 }
116
117 $social_url = $this->url_social_site( $profile, $user_id );
118 if ( $social_url ) {
119 $same_as_urls[] = $social_url;
120 }
121 }
122
123 return $same_as_urls;
124 }
125
126 /**
127 * Builds our array of Schema Person data for a given user ID.
128 *
129 * @param int $user_id The user ID to use.
130 * @param bool $add_hash Wether or not the person's image url hash should be added to the image id.
131 *
132 * @return array An array of Schema Person data.
133 */
134 protected function build_person_data( $user_id, $add_hash = false ) {
135 $user_data = \get_userdata( $user_id );
136 $data = [
137 '@type' => $this->type,
138 '@id' => $this->helpers->schema->id->get_user_schema_id( $user_id, $this->context ),
139 ];
140
141 // Safety check for the `get_userdata` WP function, which could return false.
142 if ( $user_data === false ) {
143 return $data;
144 }
145
146 $data['name'] = $this->helpers->schema->html->smart_strip_tags( $user_data->display_name );
147 $data = $this->add_image( $data, $user_data, $add_hash );
148
149 if ( ! empty( $user_data->description ) ) {
150 $data['description'] = $this->helpers->schema->html->smart_strip_tags( $user_data->description );
151 }
152
153 $data = $this->add_same_as_urls( $data, $user_data, $user_id );
154
155 /**
156 * Filter: 'wpseo_schema_person_data' - Allows filtering of schema data per user.
157 *
158 * @param array $data The schema data we have for this person.
159 * @param int $user_id The current user we're collecting schema data for.
160 */
161 $data = \apply_filters( 'wpseo_schema_person_data', $data, $user_id );
162
163 return $data;
164 }
165
166 /**
167 * Returns an ImageObject for the persons avatar.
168 *
169 * @param array $data The Person schema.
170 * @param WP_User $user_data User data.
171 * @param bool $add_hash Wether or not the person's image url hash should be added to the image id.
172 *
173 * @return array The Person schema.
174 */
175 protected function add_image( $data, $user_data, $add_hash = false ) {
176 $schema_id = $this->context->site_url . Schema_IDs::PERSON_LOGO_HASH;
177
178 $data = $this->set_image_from_options( $data, $schema_id, $add_hash, $user_data );
179 if ( ! isset( $data['image'] ) ) {
180 $data = $this->set_image_from_avatar( $data, $user_data, $schema_id, $add_hash );
181 }
182
183 if ( \is_array( $this->type ) && \in_array( 'Organization', $this->type, true ) ) {
184 $data_logo = isset( $data['image']['@id'] ) ? $data['image']['@id'] : $schema_id;
185 $data['logo'] = [ '@id' => $data_logo ];
186 }
187
188 return $data;
189 }
190
191 /**
192 * Generate the person image from our settings.
193 *
194 * @param array $data The Person schema.
195 * @param string $schema_id The string used in the `@id` for the schema.
196 * @param bool $add_hash Whether or not the person's image url hash should be added to the image id.
197 * @param WP_User $user_data User data.
198 *
199 * @return array The Person schema.
200 */
201 protected function set_image_from_options( $data, $schema_id, $add_hash = false, $user_data = null ) {
202 if ( $this->context->site_represents !== 'person' ) {
203 return $data;
204 }
205 if ( \is_array( $this->context->person_logo_meta ) ) {
206 $data['image'] = $this->helpers->schema->image->generate_from_attachment_meta( $schema_id, $this->context->person_logo_meta, $data['name'], $add_hash );
207 }
208
209 return $data;
210 }
211
212 /**
213 * Generate the person logo from gravatar.
214 *
215 * @param array $data The Person schema.
216 * @param WP_User $user_data User data.
217 * @param string $schema_id The string used in the `@id` for the schema.
218 * @param bool $add_hash Wether or not the person's image url hash should be added to the image id.
219 *
220 * @return array The Person schema.
221 */
222 protected function set_image_from_avatar( $data, $user_data, $schema_id, $add_hash = false ) {
223 // If we don't have an image in our settings, fall back to an avatar, if we're allowed to.
224 $show_avatars = \get_option( 'show_avatars' );
225 if ( ! $show_avatars ) {
226 return $data;
227 }
228
229 $url = \get_avatar_url( $user_data->user_email );
230 if ( empty( $url ) ) {
231 return $data;
232 }
233
234 $data['image'] = $this->helpers->schema->image->simple_image_object( $schema_id, $url, $user_data->display_name, $add_hash );
235
236 return $data;
237 }
238
239 /**
240 * Returns an author's social site URL.
241 *
242 * @param string $social_site The social site to retrieve the URL for.
243 * @param mixed $user_id The user ID to use function outside of the loop.
244 *
245 * @return string
246 */
247 protected function url_social_site( $social_site, $user_id = false ) {
248 $url = \get_the_author_meta( $social_site, $user_id );
249
250 if ( ! empty( $url ) && $social_site === 'twitter' ) {
251 $url = 'https://twitter.com/' . $url;
252 }
253
254 return $url;
255 }
256
257 /**
258 * Checks the site is represented by the same person as this indexable.
259 *
260 * @param WP_User $user_data User data.
261 *
262 * @return bool True when the site is represented by the same person as this indexable.
263 */
264 protected function site_represents_current_author( $user_data = null ) {
265 // Can only be the case when the site represents a user.
266 if ( $this->context->site_represents !== 'person' ) {
267 return false;
268 }
269
270 // Article post from the same user as the site represents.
271 if (
272 $this->context->indexable->object_type === 'post'
273 && $this->helpers->schema->article->is_author_supported( $this->context->indexable->object_sub_type )
274 && $this->context->schema_article_type !== 'None'
275 ) {
276 $user_id = ( ( ! \is_null( $user_data ) ) && ( isset( $user_data->ID ) ) ) ? $user_data->ID : $this->context->indexable->author_id;
277
278 return $this->context->site_user_id === $user_id;
279 }
280
281 // Author archive from the same user as the site represents.
282 return $this->context->indexable->object_type === 'user' && $this->context->site_user_id === $this->context->indexable->object_id;
283 }
284
285 /**
286 * Builds our SameAs array.
287 *
288 * @param array $data The Person schema data.
289 * @param WP_User $user_data The user data object.
290 * @param int $user_id The user ID to use.
291 *
292 * @return array The Person schema data.
293 */
294 protected function add_same_as_urls( $data, $user_data, $user_id ) {
295 $same_as_urls = [];
296
297 // Add the "Website" field from WordPress' contact info.
298 if ( ! empty( $user_data->user_url ) ) {
299 $same_as_urls[] = $user_data->user_url;
300 }
301
302 // Add the social profiles.
303 $same_as_urls = $this->get_social_profiles( $same_as_urls, $user_id );
304
305 if ( ! empty( $same_as_urls ) ) {
306 $same_as_urls = \array_values( \array_unique( $same_as_urls ) );
307 $data['sameAs'] = $same_as_urls;
308 }
309
310 return $data;
311 }
312 }
313