class-schema-main-image.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Frontend\Schema
  6. */
  7. /**
  8. * Returns ImageObject schema data.
  9. *
  10. * @since 11.5
  11. */
  12. class WPSEO_Schema_MainImage implements WPSEO_Graph_Piece {
  13. /**
  14. * A value object with context variables.
  15. *
  16. * @var WPSEO_Schema_Context
  17. */
  18. private $context;
  19. /**
  20. * WPSEO_Schema_WebPage constructor.
  21. *
  22. * @codeCoverageIgnore
  23. *
  24. * @param WPSEO_Schema_Context $context A value object with context variables.
  25. */
  26. public function __construct( WPSEO_Schema_Context $context ) {
  27. $this->context = $context;
  28. }
  29. /**
  30. * Determines whether or not a piece should be added to the graph.
  31. *
  32. * @return bool
  33. */
  34. public function is_needed() {
  35. if ( is_singular() ) {
  36. return true;
  37. }
  38. return false;
  39. }
  40. /**
  41. * Adds a main image for the current URL to the schema if there is one.
  42. *
  43. * This can be either the featured image, or fall back to the first image in the content of the page.
  44. *
  45. * @return false|array $data Image Schema.
  46. */
  47. public function generate() {
  48. $image_id = $this->context->canonical . WPSEO_Schema_IDs::PRIMARY_IMAGE_HASH;
  49. $image_schema = $this->get_featured_image( $this->context->id, $image_id );
  50. if ( $image_schema === null ) {
  51. $image_schema = $this->get_first_content_image( $this->context->id, $image_id );
  52. }
  53. if ( $image_schema === null ) {
  54. return false;
  55. }
  56. $this->context->has_image = true;
  57. return $image_schema;
  58. }
  59. /**
  60. * Gets the image schema for the web page based on the featured image.
  61. *
  62. * @param int $post_id The post id.
  63. * @param string $image_id The image schema id.
  64. *
  65. * @return array|null The image schema object or null if there is no featured image.
  66. */
  67. private function get_featured_image( $post_id, $image_id ) {
  68. if ( ! has_post_thumbnail( $post_id ) ) {
  69. return null;
  70. }
  71. return $this->generate_image_schema_from_attachment_id( $image_id );
  72. }
  73. /**
  74. * Gets the image schema for the web page based on the first content image image.
  75. *
  76. * @param int $post_id The post id.
  77. * @param string $image_id The image schema id.
  78. *
  79. * @return array|null The image schema object or null if there is no image in the content.
  80. */
  81. private function get_first_content_image( $post_id, $image_id ) {
  82. $image_url = $this->get_first_usable_content_image_for_post( $post_id );
  83. if ( $image_url === null ) {
  84. return null;
  85. }
  86. return $this->generate_image_schema_from_url( $image_id, $image_url );
  87. }
  88. /**
  89. * Gets the post's first usable content image. Null if none is available.
  90. *
  91. * @codeCoverageIgnore
  92. *
  93. * @param int $post_id The post id.
  94. *
  95. * @return string|null The image URL or null if there is no image.
  96. */
  97. protected function get_first_usable_content_image_for_post( $post_id ) {
  98. return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $post_id );
  99. }
  100. /**
  101. * Generates image schema from the attachment id.
  102. *
  103. * @codeCoverageIgnore
  104. *
  105. * @param string $image_id The image schema id.
  106. *
  107. * @return array Schema ImageObject array.
  108. */
  109. protected function generate_image_schema_from_attachment_id( $image_id ) {
  110. $schema_image = new WPSEO_Schema_Image( $image_id );
  111. return $schema_image->generate_from_attachment_id( get_post_thumbnail_id() );
  112. }
  113. /**
  114. * Generates image schema from the url.
  115. *
  116. * @codeCoverageIgnore
  117. *
  118. * @param string $image_id The image schema id.
  119. * @param string $image_url The image URL.
  120. *
  121. * @return array Schema ImageObject array.
  122. */
  123. protected function generate_image_schema_from_url( $image_id, $image_url ) {
  124. $schema_image = new WPSEO_Schema_Image( $image_id );
  125. return $schema_image->generate_from_url( $image_url );
  126. }
  127. }