• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 3a107b77fc7aaeaea07df0273cad8eb4c0c863a2

30 Jan 2024 08:08AM UTC coverage: 53.141% (+0.04%) from 53.106%
3a107b77fc7aaeaea07df0273cad8eb4c0c863a2

Pull #21096

github

web-flow
Merge 2eb97856c into f06dbce37
Pull Request #21096: Links `Person or Organization` node to the `ProfilePage` Node.

7564 of 13900 branches covered (0.0%)

Branch coverage included in aggregate %.

1 of 3 new or added lines in 1 file covered. (33.33%)

56 existing lines in 1 file now uncovered.

29036 of 54973 relevant lines covered (52.82%)

40256.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.78
/src/generators/schema/person.php
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() {
6✔
44
                // Using an author piece instead.
45
                if ( $this->site_represents_current_author() ) {
6✔
46
                        return false;
2✔
47
                }
48

49
                return $this->context->site_represents === 'person';
4✔
50
        }
51

52
        /**
53
         * Returns Person Schema data.
54
         *
55
         * @return bool|array<string|string[]> Person data on success, false on failure.
56
         */
57
        public function generate() {
28✔
58
                $user_id = $this->determine_user_id();
28✔
59
                if ( ! $user_id ) {
28✔
60
                        return false;
4✔
61
                }
62

63
                return $this->build_person_data( $user_id );
24✔
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() {
20✔
72
                /**
73
                 * Filter: 'wpseo_schema_person_user_id' - Allows filtering of user ID used for person output.
74
                 *
75
                 * @param 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 );
20✔
78

79
                // It should to be an integer higher than 0.
80
                if ( \is_int( $user_id ) && $user_id > 0 ) {
20✔
81
                        return $user_id;
16✔
82
                }
83

84
                return false;
4✔
85
        }
86

87
        /**
88
         * Retrieve a list of social profile URLs for Person.
89
         *
90
         * @param string[] $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 ) {
14✔
96
                /**
97
                 * Filter: 'wpseo_schema_person_social_profiles' - Allows filtering of social profiles per user.
98
                 *
99
                 * @param string[] $social_profiles The array of social profiles to retrieve. Each should be a user meta field
100
                 *                                  key. As they are retrieved using the WordPress function `get_the_author_meta`.
101
                 * @param int      $user_id         The current user we're grabbing social profiles for.
102
                 */
103
                $social_profiles = \apply_filters( 'wpseo_schema_person_social_profiles', $this->social_profiles, $user_id );
14✔
104

105
                // We can only handle an array.
106
                if ( ! \is_array( $social_profiles ) ) {
14✔
107
                        return $same_as_urls;
2✔
108
                }
109

110
                foreach ( $social_profiles as $profile ) {
12✔
111
                        // Skip non-string values.
112
                        if ( ! \is_string( $profile ) ) {
6✔
113
                                continue;
2✔
114
                        }
115

116
                        $social_url = $this->url_social_site( $profile, $user_id );
6✔
117
                        if ( $social_url ) {
6✔
118
                                $same_as_urls[] = $social_url;
6✔
119
                        }
120
                }
121

122
                return $same_as_urls;
12✔
123
        }
124

125
        /**
126
         * Builds our array of Schema Person data for a given user ID.
127
         *
128
         * @param int  $user_id  The user ID to use.
129
         * @param bool $add_hash Wether or not the person's image url hash should be added to the image id.
130
         *
131
         * @return array<string|string[]> An array of Schema Person data.
132
         */
133
        protected function build_person_data( $user_id, $add_hash = false ) {
16✔
134
                $user_data = \get_userdata( $user_id );
16✔
135
                $data      = [
8✔
136
                        '@type' => $this->type,
16✔
137
                        '@id'   => $this->helpers->schema->id->get_user_schema_id( $user_id, $this->context ),
16✔
138
                ];
8✔
139

140
                // Safety check for the `get_userdata` WP function, which could return false.
141
                if ( $user_data === false ) {
16✔
142
                        return $data;
2✔
143
                }
144

145
                $data['name'] = $this->helpers->schema->html->smart_strip_tags( $user_data->display_name );
14✔
146
                $data         = $this->add_image( $data, $user_data, $add_hash );
14✔
147

148
                if ( ! empty( $user_data->description ) ) {
14✔
149
                        $data['description'] = $this->helpers->schema->html->smart_strip_tags( $user_data->description );
4✔
150
                }
151

152
                if ( \is_array( $this->context->schema_page_type ) && \in_array( 'ProfilePage', $this->context->schema_page_type, true ) ) {
14✔
NEW
153
                        $data['mainEntityOfPage'] = [
×
NEW
154
                                '@id' => $this->context->main_schema_id,
×
155
                        ];
156
                }
157
                $data = $this->add_same_as_urls( $data, $user_data, $user_id );
14✔
158

159
                /**
160
                 * Filter: 'wpseo_schema_person_data' - Allows filtering of schema data per user.
161
                 *
162
                 * @param array $data    The schema data we have for this person.
163
                 * @param int   $user_id The current user we're collecting schema data for.
164
                 */
165
                $data = \apply_filters( 'wpseo_schema_person_data', $data, $user_id );
14✔
166

167
                return $data;
14✔
168
        }
169

170
        /**
171
         * Returns an ImageObject for the persons avatar.
172
         *
173
         * @param array<string|string[]> $data      The Person schema.
174
         * @param WP_User                $user_data User data.
175
         * @param bool                   $add_hash  Wether or not the person's image url hash should be added to the image id.
176
         *
177
         * @return array<string|string[]> The Person schema.
178
         */
179
        protected function add_image( $data, $user_data, $add_hash = false ) {
14✔
180
                $schema_id = $this->context->site_url . Schema_IDs::PERSON_LOGO_HASH;
14✔
181

182
                $data = $this->set_image_from_options( $data, $schema_id, $add_hash, $user_data );
14✔
183
                if ( ! isset( $data['image'] ) ) {
14✔
184
                        $data = $this->set_image_from_avatar( $data, $user_data, $schema_id, $add_hash );
10✔
185
                }
186

187
                if ( \is_array( $this->type ) && \in_array( 'Organization', $this->type, true ) ) {
14✔
188
                        $data_logo    = ( $data['image']['@id'] ?? $schema_id );
14✔
189
                        $data['logo'] = [ '@id' => $data_logo ];
14✔
190
                }
191

192
                return $data;
14✔
193
        }
194

195
        /**
196
         * Generate the person image from our settings.
197
         *
198
         * @param array<string|string[]> $data      The Person schema.
199
         * @param string                 $schema_id The string used in the `@id` for the schema.
200
         * @param bool                   $add_hash  Whether or not the person's image url hash should be added to the image id.
201
         * @param WP_User                $user_data User data.
202
         *
203
         * @return array<string|string[]> The Person schema.
204
         */
205
        protected function set_image_from_options( $data, $schema_id, $add_hash = false, $user_data = null ) {
10✔
206
                if ( $this->context->site_represents !== 'person' ) {
10✔
207
                        return $data;
6✔
208
                }
209
                if ( \is_array( $this->context->person_logo_meta ) ) {
4✔
210
                        $data['image'] = $this->helpers->schema->image->generate_from_attachment_meta( $schema_id, $this->context->person_logo_meta, $data['name'], $add_hash );
4✔
211
                }
212

213
                return $data;
4✔
214
        }
215

216
        /**
217
         * Generate the person logo from gravatar.
218
         *
219
         * @param array<string|string[]> $data      The Person schema.
220
         * @param WP_User                $user_data User data.
221
         * @param string                 $schema_id The string used in the `@id` for the schema.
222
         * @param bool                   $add_hash  Wether or not the person's image url hash should be added to the image id.
223
         *
224
         * @return array<string|string[]> The Person schema.
225
         */
226
        protected function set_image_from_avatar( $data, $user_data, $schema_id, $add_hash = false ) {
10✔
227
                // If we don't have an image in our settings, fall back to an avatar, if we're allowed to.
228
                $show_avatars = \get_option( 'show_avatars' );
10✔
229
                if ( ! $show_avatars ) {
10✔
230
                        return $data;
6✔
231
                }
232

233
                $url = \get_avatar_url( $user_data->user_email );
4✔
234
                if ( empty( $url ) ) {
4✔
235
                        return $data;
2✔
236
                }
237

238
                $data['image'] = $this->helpers->schema->image->simple_image_object( $schema_id, $url, $user_data->display_name, $add_hash );
2✔
239

240
                return $data;
2✔
241
        }
242

243
        /**
244
         * Returns an author's social site URL.
245
         *
246
         * @param string    $social_site The social site to retrieve the URL for.
247
         * @param int|false $user_id     The user ID to use function outside of the loop.
248
         *
249
         * @return string
250
         */
251
        protected function url_social_site( $social_site, $user_id = false ) {
6✔
252
                $url = \get_the_author_meta( $social_site, $user_id );
6✔
253

254
                if ( ! empty( $url ) && $social_site === 'twitter' ) {
6✔
255
                        $url = 'https://twitter.com/' . $url;
4✔
256
                }
257

258
                return $url;
6✔
259
        }
260

261
        /**
262
         * Checks the site is represented by the same person as this indexable.
263
         *
264
         * @param WP_User $user_data User data.
265
         *
266
         * @return bool True when the site is represented by the same person as this indexable.
267
         */
268
        protected function site_represents_current_author( $user_data = null ) {
6✔
269
                // Can only be the case when the site represents a user.
270
                if ( $this->context->site_represents !== 'person' ) {
6✔
271
                        return false;
2✔
272
                }
273

274
                // Article post from the same user as the site represents.
275
                if (
276
                        $this->context->indexable->object_type === 'post'
4✔
277
                        && $this->helpers->schema->article->is_author_supported( $this->context->indexable->object_sub_type )
4✔
278
                        && $this->context->schema_article_type !== 'None'
4✔
279
                ) {
280
                        $user_id = ( ( ! \is_null( $user_data ) ) && ( isset( $user_data->ID ) ) ) ? $user_data->ID : $this->context->indexable->author_id;
2✔
281

282
                        return $this->context->site_user_id === $user_id;
2✔
283
                }
284

285
                // Author archive from the same user as the site represents.
286
                return $this->context->indexable->object_type === 'user' && $this->context->site_user_id === $this->context->indexable->object_id;
2✔
287
        }
288

289
        /**
290
         * Builds our SameAs array.
291
         *
292
         * @param array<string|string[]> $data      The Person schema data.
293
         * @param WP_User                $user_data The user data object.
294
         * @param int                    $user_id   The user ID to use.
295
         *
296
         * @return array<string|string[]> The Person schema data.
297
         */
298
        protected function add_same_as_urls( $data, $user_data, $user_id ) {
×
299
                $same_as_urls = [];
×
300

301
                // Add the "Website" field from WordPress' contact info.
302
                if ( ! empty( $user_data->user_url ) ) {
×
303
                        $same_as_urls[] = $user_data->user_url;
×
304
                }
305

306
                // Add the social profiles.
307
                $same_as_urls = $this->get_social_profiles( $same_as_urls, $user_id );
×
308

309
                if ( ! empty( $same_as_urls ) ) {
×
310
                        $same_as_urls   = \array_values( \array_unique( $same_as_urls ) );
×
311
                        $data['sameAs'] = $same_as_urls;
×
312
                }
313

314
                return $data;
×
315
        }
316
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc