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