PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.5.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.5.1
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 / author.php

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

114 lines 2.8 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 Yoast\WP\SEO\Config\Schema_IDs;
6
7 /**
8 * Returns schema Author data.
9 */
10 class Author extends Person {
11
12 /**
13 * Determine whether we should return Person schema.
14 *
15 * @return bool
16 */
17 public function is_needed() {
18 if ( $this->context->indexable->object_type === 'user' ) {
19 return true;
20 }
21
22 if (
23 $this->context->indexable->object_type === 'post'
24 && $this->helpers->schema->article->is_author_supported( $this->context->indexable->object_sub_type )
25 && $this->context->schema_article_type !== 'None'
26 ) {
27 return true;
28 }
29
30 return false;
31 }
32
33 /**
34 * Returns Person Schema data.
35 *
36 * @return bool|array Person data on success, false on failure.
37 */
38 public function generate() {
39 $user_id = $this->determine_user_id();
40 if ( ! $user_id ) {
41 return false;
42 }
43
44 $data = $this->build_person_data( $user_id );
45
46 if ( $this->site_represents_current_author() === false ) {
47 $data['@type'] = [ 'Person' ];
48 unset( $data['logo'] );
49 }
50
51 // If this is an author page, the Person object is the main object, so we set it as such here.
52 if ( $this->context->indexable->object_type === 'user' ) {
53 $data['mainEntityOfPage'] = [
54 '@id' => $this->context->canonical . Schema_IDs::WEBPAGE_HASH,
55 ];
56 }
57
58 // If this is a post and the author archives are enabled, set the author archive url as the author url.
59 if ( $this->context->indexable->object_type === 'post' ) {
60 if ( $this->helpers->options->get( 'disable-author' ) !== true ) {
61 $data['url'] = $this->helpers->user->get_the_author_posts_url( $user_id );
62 }
63 }
64
65 return $data;
66 }
67
68 /**
69 * Determines a User ID for the Person data.
70 *
71 * @return bool|int User ID or false upon return.
72 */
73 protected function determine_user_id() {
74 $user_id = 0;
75
76 if ( $this->context->indexable->object_type === 'post' ) {
77 $user_id = (int) $this->context->post->post_author;
78 }
79
80 if ( $this->context->indexable->object_type === 'user' ) {
81 $user_id = $this->context->indexable->object_id;
82 }
83
84 /**
85 * Filter: 'wpseo_schema_person_user_id' - Allows filtering of user ID used for person output.
86 *
87 * @api int|bool $user_id The user ID currently determined.
88 */
89 $user_id = \apply_filters( 'wpseo_schema_person_user_id', $user_id );
90
91 if ( \is_int( $user_id ) && $user_id > 0 ) {
92 return $user_id;
93 }
94
95 return false;
96 }
97
98 /**
99 * An author should not have an image from options, this only applies to persons.
100 *
101 * @param array $data The Person schema.
102 * @param string $schema_id The string used in the `@id` for the schema.
103 *
104 * @return array The Person schema.
105 */
106 protected function set_image_from_options( $data, $schema_id ) {
107 if ( $this->site_represents_current_author() ) {
108 return parent::set_image_from_options( $data, $schema_id );
109 }
110
111 return $data;
112 }
113 }
114