class-schema-author.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Frontend\Schema
  6. */
  7. /**
  8. * Returns schema Person data.
  9. *
  10. * @since 10.2
  11. *
  12. * @property WPSEO_Schema_Context $context A value object with context variables.
  13. */
  14. class WPSEO_Schema_Author extends WPSEO_Schema_Person implements WPSEO_Graph_Piece {
  15. /**
  16. * A value object with context variables.
  17. *
  18. * @var WPSEO_Schema_Context
  19. */
  20. private $context;
  21. /**
  22. * The Schema type we use for this class.
  23. *
  24. * @var string[]
  25. */
  26. protected $type = [ 'Person' ];
  27. /**
  28. * WPSEO_Schema_Author constructor.
  29. *
  30. * @param WPSEO_Schema_Context $context A value object with context variables.
  31. */
  32. public function __construct( WPSEO_Schema_Context $context ) {
  33. parent::__construct( $context );
  34. $this->context = $context;
  35. $this->image_hash = WPSEO_Schema_IDs::AUTHOR_LOGO_HASH;
  36. }
  37. /**
  38. * Determine whether we should return Person schema.
  39. *
  40. * @return bool
  41. */
  42. public function is_needed() {
  43. if ( is_author() ) {
  44. return true;
  45. }
  46. if ( $this->is_post_author() ) {
  47. $post = get_post( $this->context->id );
  48. // If the author is the user the site represents, no need for an extra author block.
  49. if ( (int) $post->post_author === $this->context->site_user_id ) {
  50. return false;
  51. }
  52. return true;
  53. }
  54. return false;
  55. }
  56. /**
  57. * Returns Person Schema data.
  58. *
  59. * @return bool|array Person data on success, false on failure.
  60. */
  61. public function generate() {
  62. $user_id = $this->determine_user_id();
  63. if ( ! $user_id ) {
  64. return false;
  65. }
  66. $data = $this->build_person_data( $user_id );
  67. // If this is an author page, the Person object is the main object, so we set it as such here.
  68. if ( is_author() ) {
  69. $data['mainEntityOfPage'] = [
  70. '@id' => $this->context->canonical . WPSEO_Schema_IDs::WEBPAGE_HASH,
  71. ];
  72. }
  73. return $data;
  74. }
  75. /**
  76. * Determine whether the current URL is worthy of Article schema.
  77. *
  78. * @return bool
  79. */
  80. protected function is_post_author() {
  81. if ( is_singular() && WPSEO_Schema_Article::is_article_post_type() ) {
  82. return true;
  83. }
  84. return false;
  85. }
  86. /**
  87. * Determines a User ID for the Person data.
  88. *
  89. * @return bool|int User ID or false upon return.
  90. */
  91. protected function determine_user_id() {
  92. switch ( true ) {
  93. case is_author():
  94. $user_id = get_queried_object_id();
  95. break;
  96. default:
  97. $post = get_post( $this->context->id );
  98. $user_id = (int) $post->post_author;
  99. break;
  100. }
  101. /**
  102. * Filter: 'wpseo_schema_person_user_id' - Allows filtering of user ID used for person output.
  103. *
  104. * @api int|bool $user_id The user ID currently determined.
  105. */
  106. return apply_filters( 'wpseo_schema_person_user_id', $user_id );
  107. }
  108. /**
  109. * An author should not have an image from options, this only applies to persons.
  110. *
  111. * @param array $data The Person schema.
  112. * @param string $schema_id The string used in the `@id` for the schema.
  113. *
  114. * @return array The Person schema.
  115. */
  116. private function set_image_from_options( $data, $schema_id ) {
  117. return $data;
  118. }
  119. /**
  120. * Gets the Schema type we use for this class.
  121. *
  122. * @return string[] The schema type.
  123. */
  124. public static function get_type() {
  125. return self::$type;
  126. }
  127. }