| @@ -47,21 +47,23 @@ | ||
| 47 | 47 | |
| 48 | 48 | /** |
| 49 | 49 | * Find an image based on its URL and generate a Schema object for it. |
| 50 | 50 | * |
| 51 | - * @param string $schema_id The `@id` to use for the returned image. | |
| 52 | - * @param string $url The image URL to base our object on. | |
| 53 | - * @param string $caption An optional caption. | |
| 51 | + * @param string $schema_id The `@id` to use for the returned image. | |
| 52 | + * @param string $url The image URL to base our object on. | |
| 53 | + * @param string $caption An optional caption. | |
| 54 | + * @param bool $add_hash Whether a hash will be added as a suffix in the @id. | |
| 55 | + * @param bool $use_link_table Whether the SEO Links table will be used to retrieve the id. | |
| 54 | 56 | * |
| 55 | 57 | * @return array Schema ImageObject array. |
| 56 | 58 | */ |
| 57 | - public function generate_from_url( $schema_id, $url, $caption = '' ) { | |
| 58 | - $attachment_id = $this->image->get_attachment_by_url( $url ); | |
| 59 | + public function generate_from_url( $schema_id, $url, $caption = '', $add_hash = false, $use_link_table = true ) { | |
| 60 | + $attachment_id = $this->image->get_attachment_by_url( $url, $use_link_table ); | |
| 59 | 61 | if ( $attachment_id > 0 ) { |
| 60 | - return $this->generate_from_attachment_id( $schema_id, $attachment_id, $caption ); | |
| 62 | + return $this->generate_from_attachment_id( $schema_id, $attachment_id, $caption, $add_hash ); | |
| 61 | 63 | } |
| 62 | 64 | |
| 63 | - return $this->simple_image_object( $schema_id, $url, $caption ); | |
| 65 | + return $this->simple_image_object( $schema_id, $url, $caption, $add_hash ); | |
| 64 | 66 | } |
| 65 | 67 | |
| 66 | 68 | /** |
| 67 | 69 | * Retrieve data about an image from the database and use it to generate a Schema object. |
| @@ -68,16 +70,21 @@ | ||
| 68 | 70 | * |
| 69 | 71 | * @param string $schema_id The `@id` to use for the returned image. |
| 70 | 72 | * @param int $attachment_id The attachment to retrieve data from. |
| 71 | 73 | * @param string $caption The caption string, if there is one. |
| 74 | + * @param bool $add_hash Whether a hash will be added as a suffix in the @id. | |
| 72 | 75 | * |
| 73 | 76 | * @return array Schema ImageObject array. |
| 74 | 77 | */ |
| 75 | - public function generate_from_attachment_id( $schema_id, $attachment_id, $caption = '' ) { | |
| 76 | - $data = $this->generate_object( $schema_id ); | |
| 78 | + public function generate_from_attachment_id( $schema_id, $attachment_id, $caption = '', $add_hash = false ) { | |
| 79 | + $data = $this->generate_object(); | |
| 80 | + $url = $this->image->get_attachment_image_url( $attachment_id, 'full' ); | |
| 77 | 81 | |
| 78 | - $data['url'] = $this->image->get_attachment_image_url( $attachment_id, 'full' ); | |
| 79 | - $data['contentUrl'] = $data['url']; | |
| 82 | + $id_suffix = ( $add_hash ) ? \md5( $url ) : ''; | |
| 83 | + | |
| 84 | + $data['@id'] = $schema_id . $id_suffix; | |
| 85 | + $data['url'] = $url; | |
| 86 | + $data['contentUrl'] = $url; | |
| 80 | 87 | $data = $this->add_image_size( $data, $attachment_id ); |
| 81 | 88 | $data = $this->add_caption( $data, $attachment_id, $caption ); |
| 82 | 89 | |
| 83 | 90 | return $data; |
| @@ -88,14 +95,18 @@ | ||
| 88 | 95 | * |
| 89 | 96 | * @param string $schema_id The `@id` to use for the returned image. |
| 90 | 97 | * @param array $attachment_meta The attachment metadata. |
| 91 | 98 | * @param string $caption The caption string, if there is one. |
| 99 | + * @param bool $add_hash Whether a hash will be added as a suffix in the @id. | |
| 92 | 100 | * |
| 93 | 101 | * @return array Schema ImageObject array. |
| 94 | 102 | */ |
| 95 | - public function generate_from_attachment_meta( $schema_id, $attachment_meta, $caption = '' ) { | |
| 96 | - $data = $this->generate_object( $schema_id ); | |
| 103 | + public function generate_from_attachment_meta( $schema_id, $attachment_meta, $caption = '', $add_hash = false ) { | |
| 104 | + $data = $this->generate_object(); | |
| 97 | 105 | |
| 106 | + $id_suffix = ( $add_hash ) ? \md5( $attachment_meta['url'] ) : ''; | |
| 107 | + | |
| 108 | + $data['@id'] = $schema_id . $id_suffix; | |
| 98 | 109 | $data['url'] = $attachment_meta['url']; |
| 99 | 110 | $data['contentUrl'] = $data['url']; |
| 100 | 111 | $data['width'] = $attachment_meta['width']; |
| 101 | 112 | $data['height'] = $attachment_meta['height']; |
| @@ -111,14 +122,18 @@ | ||
| 111 | 122 | * |
| 112 | 123 | * @param string $schema_id The `@id` to use for the returned image. |
| 113 | 124 | * @param string $url The image URL. |
| 114 | 125 | * @param string $caption A caption, if set. |
| 126 | + * @param bool $add_hash Whether a hash will be added as a suffix in the @id. | |
| 115 | 127 | * |
| 116 | 128 | * @return array Schema ImageObject array. |
| 117 | 129 | */ |
| 118 | - public function simple_image_object( $schema_id, $url, $caption = '' ) { | |
| 119 | - $data = $this->generate_object( $schema_id ); | |
| 130 | + public function simple_image_object( $schema_id, $url, $caption = '', $add_hash = false ) { | |
| 131 | + $data = $this->generate_object(); | |
| 120 | 132 | |
| 133 | + $id_suffix = ( $add_hash ) ? \md5( $url ) : ''; | |
| 134 | + | |
| 135 | + $data['@id'] = $schema_id . $id_suffix; | |
| 121 | 136 | $data['url'] = $url; |
| 122 | 137 | $data['contentUrl'] = $url; |
| 123 | 138 | |
| 124 | 139 | if ( ! empty( $caption ) ) { |
| @@ -156,16 +171,13 @@ | ||
| 156 | 171 | |
| 157 | 172 | /** |
| 158 | 173 | * Generates our bare bone ImageObject. |
| 159 | 174 | * |
| 160 | - * @param string $schema_id The `@id` to use for the returned image. | |
| 161 | - * | |
| 162 | 175 | * @return array an empty ImageObject |
| 163 | 176 | */ |
| 164 | - private function generate_object( $schema_id ) { | |
| 177 | + private function generate_object() { | |
| 165 | 178 | $data = [ |
| 166 | 179 | '@type' => 'ImageObject', |
| 167 | - '@id' => $schema_id, | |
| 168 | 180 | ]; |
| 169 | 181 | |
| 170 | 182 | $data = $this->language->add_piece_language( $data ); |
| 171 | 183 | |