class-schema-image.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Frontend\Schema
  6. */
  7. /**
  8. * Returns schema image data.
  9. *
  10. * @since 11.1
  11. *
  12. * @property string $schema_id The `@id` to use for the returned image.
  13. * @property array $data The ImageObject Schema array.
  14. * @property int $attachment_id The ID of the attachment used to generate the object.
  15. */
  16. class WPSEO_Schema_Image {
  17. /**
  18. * The `@id` to use for the returned image.
  19. *
  20. * @var string
  21. */
  22. private $schema_id;
  23. /**
  24. * The ImageObject Schema array.
  25. *
  26. * @var array
  27. */
  28. private $data;
  29. /**
  30. * The ID of the attachment used to generate the object.
  31. *
  32. * @var int
  33. */
  34. private $attachment_id;
  35. /**
  36. * WPSEO_Schema_Image constructor.
  37. *
  38. * @param string $schema_id The string to use in an image's `@id`.
  39. */
  40. public function __construct( $schema_id ) {
  41. $this->schema_id = $schema_id;
  42. $this->generate_object();
  43. }
  44. /**
  45. * Find an image based on its URL and generate a Schema object for it.
  46. *
  47. * @param string $url The image URL to base our object on.
  48. * @param string $caption An optional caption.
  49. *
  50. * @return array Schema ImageObject array.
  51. */
  52. public function generate_from_url( $url, $caption = '' ) {
  53. $attachment_id = WPSEO_Image_Utils::get_attachment_by_url( $url );
  54. if ( $attachment_id > 0 ) {
  55. return $this->generate_from_attachment_id( $attachment_id, $caption );
  56. }
  57. return $this->simple_image_object( $url, $caption );
  58. }
  59. /**
  60. * Retrieve data about an image from the database and use it to generate a Schema object.
  61. *
  62. * @param int $attachment_id The attachment to retrieve data from.
  63. * @param string $caption The caption string, if there is one.
  64. *
  65. * @return array Schema ImageObject array.
  66. */
  67. public function generate_from_attachment_id( $attachment_id, $caption = '' ) {
  68. $this->attachment_id = $attachment_id;
  69. $this->data['url'] = wp_get_attachment_image_url( $this->attachment_id, 'full' );
  70. $this->add_image_size();
  71. $this->add_caption( $caption );
  72. return $this->data;
  73. }
  74. /**
  75. * If we can't find $url in our database, we output a simple ImageObject.
  76. *
  77. * @param string $url The image URL.
  78. * @param string $caption A caption, if set.
  79. *
  80. * @return array $data Schema ImageObject array.
  81. */
  82. public function simple_image_object( $url, $caption = '' ) {
  83. $this->data['url'] = $url;
  84. if ( ! empty( $caption ) ) {
  85. $this->data['caption'] = $caption;
  86. }
  87. return $this->data;
  88. }
  89. /**
  90. * Retrieves an image's caption if set, or uses the alt tag if that's set.
  91. *
  92. * @param string $caption The caption string, if there is one.
  93. *
  94. * @return void
  95. */
  96. private function add_caption( $caption = '' ) {
  97. if ( ! empty( $caption ) ) {
  98. $this->data['caption'] = $caption;
  99. return;
  100. }
  101. $caption = wp_get_attachment_caption();
  102. if ( ! empty( $caption ) ) {
  103. $this->data['caption'] = $caption;
  104. return;
  105. }
  106. $caption = get_post_meta( $this->attachment_id, '_wp_attachment_image_alt', true );
  107. if ( ! empty( $caption ) ) {
  108. $this->data['caption'] = $caption;
  109. }
  110. }
  111. /**
  112. * Generates our bare bone ImageObject.
  113. *
  114. * @return void
  115. */
  116. private function generate_object() {
  117. $this->data = [
  118. '@type' => 'ImageObject',
  119. '@id' => $this->schema_id,
  120. ];
  121. }
  122. /**
  123. * Adds image's width and height.
  124. *
  125. * @return void
  126. */
  127. private function add_image_size() {
  128. $image_meta = wp_get_attachment_metadata( $this->attachment_id );
  129. if ( empty( $image_meta['width'] ) || empty( $image_meta['height'] ) ) {
  130. return;
  131. }
  132. $this->data['width'] = $image_meta['width'];
  133. $this->data['height'] = $image_meta['height'];
  134. }
  135. }