| @@ -90,14 +90,14 @@ | ||
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | $images = []; |
| 93 | 93 | |
| 94 | - foreach (self::candidates($post_id) as $candidate) { | |
| 94 | + foreach (self::candidates($post_id) as $url) { | |
| 95 | 95 | if (count($images) >= $limit - 1) { |
| 96 | 96 | break; |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - $url = self::usable($candidate['url']); | |
| 99 | + $url = self::usable($url); | |
| 100 | 100 | |
| 101 | 101 | if ('' === $url) { |
| 102 | 102 | continue; |
| 103 | 103 | } |
| @@ -108,9 +108,9 @@ | ||
| 108 | 108 | continue; |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | $seen[$print] = true; |
| 112 | - $images[] = self::describe($url, $post_id, $candidate['attachment_id']); | |
| 112 | + $images[] = self::describe($url, $post_id); | |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | /** |
| 116 | 116 | * Filter the secondary Open Graph images for a page. |
| @@ -123,20 +123,16 @@ | ||
| 123 | 123 | return (array) apply_filters('thinkrank_og_extra_images', $images, $post_id); |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | /** |
| 127 | - * Candidate images, in the order they should be offered. | |
| 127 | + * Candidate URLs, in the order they should be offered. | |
| 128 | 128 | * |
| 129 | - * Each carries the attachment ID it is believed to come from, where there | |
| 130 | - * is one to be had without asking the database: the featured image's own, | |
| 131 | - * or the `wp-image-{ID}` the editor wrote on a body image (#847). | |
| 132 | - * | |
| 133 | 129 | * @since 2.7.0 |
| 134 | 130 | * @param int $post_id Post being rendered. |
| 135 | - * @return array<int, array{url:string, attachment_id:int}> | |
| 131 | + * @return array<int, string> | |
| 136 | 132 | */ |
| 137 | 133 | private static function candidates(int $post_id): array { |
| 138 | - $candidates = []; | |
| 134 | + $urls = []; | |
| 139 | 135 | |
| 140 | 136 | // The featured image. Usually this IS the primary and the fingerprint |
| 141 | 137 | // check drops it; it matters when a per-post social override displaced |
| 142 | 138 | // it, which is exactly the case where a second image is worth offering. |
| @@ -145,24 +141,21 @@ | ||
| 145 | 141 | if ($thumbnail_id) { |
| 146 | 142 | $src = wp_get_attachment_image_src($thumbnail_id, 'large'); |
| 147 | 143 | |
| 148 | 144 | if (is_array($src) && !empty($src[0])) { |
| 149 | - $candidates[] = [ | |
| 150 | - 'url' => (string) $src[0], | |
| 151 | - 'attachment_id' => $thumbnail_id, | |
| 152 | - ]; | |
| 145 | + $urls[] = (string) $src[0]; | |
| 153 | 146 | } |
| 154 | 147 | } |
| 155 | 148 | |
| 156 | - foreach (self::from_content($post_id) as $candidate) { | |
| 157 | - $candidates[] = $candidate; | |
| 149 | + foreach (self::from_content($post_id) as $url) { | |
| 150 | + $urls[] = $url; | |
| 158 | 151 | } |
| 159 | 152 | |
| 160 | - return $candidates; | |
| 153 | + return $urls; | |
| 161 | 154 | } |
| 162 | 155 | |
| 163 | 156 | /** |
| 164 | - * Images in the post body, in document order. | |
| 157 | + * Image URLs in the post body, in document order. | |
| 165 | 158 | * |
| 166 | 159 | * Reads the stored content rather than running it through `the_content`. |
| 167 | 160 | * This is called while the document head is being written, and rendering |
| 168 | 161 | * the body there would run every shortcode and block on the page for the |
| @@ -169,9 +162,9 @@ | ||
| 169 | 162 | * sake of a few `src` attributes. |
| 170 | 163 | * |
| 171 | 164 | * @since 2.7.0 |
| 172 | 165 | * @param int $post_id Post being rendered. |
| 173 | - * @return array<int, array{url:string, attachment_id:int}> | |
| 166 | + * @return array<int, string> | |
| 174 | 167 | */ |
| 175 | 168 | private static function from_content(int $post_id): array { |
| 176 | 169 | $content = (string) get_post_field('post_content', $post_id); |
| 177 | 170 | |
| @@ -182,20 +175,17 @@ | ||
| 182 | 175 | if (!preg_match_all('/<img\b[^>]*>/i', $content, $tags)) { |
| 183 | 176 | return []; |
| 184 | 177 | } |
| 185 | 178 | |
| 186 | - $images = []; | |
| 179 | + $urls = []; | |
| 187 | 180 | |
| 188 | 181 | foreach ($tags[0] as $tag) { |
| 189 | 182 | if (preg_match('/\bsrc\s*=\s*["\']([^"\']+)["\']/i', $tag, $match)) { |
| 190 | - $images[] = [ | |
| 191 | - 'url' => $match[1], | |
| 192 | - 'attachment_id' => Attachment_Lookup::hint_from_markup($tag), | |
| 193 | - ]; | |
| 183 | + $urls[] = $match[1]; | |
| 194 | 184 | } |
| 195 | 185 | } |
| 196 | 186 | |
| 197 | - return $images; | |
| 187 | + return $urls; | |
| 198 | 188 | } |
| 199 | 189 | |
| 200 | 190 | /** |
| 201 | 191 | * A URL Open Graph can actually use, or '' to skip it. |
| @@ -231,33 +221,34 @@ | ||
| 231 | 221 | * to lay out a card before it has fetched the file. |
| 232 | 222 | * |
| 233 | 223 | * @since 2.7.0 |
| 234 | 224 | * |
| 235 | - * @param string $url Image URL. | |
| 236 | - * @param int $post_id Post being rendered, for the alt fallback. | |
| 237 | - * @param int $attachment_id Attachment the URL is believed to come from, | |
| 238 | - * or 0. Checked before it is trusted. | |
| 225 | + * @param string $url Image URL. | |
| 226 | + * @param int $post_id Post being rendered, for the alt fallback. | |
| 239 | 227 | * @return array<string, mixed> |
| 240 | 228 | */ |
| 241 | - private static function describe(string $url, int $post_id, int $attachment_id = 0): array { | |
| 229 | + private static function describe(string $url, int $post_id): array { | |
| 242 | 230 | $image = ['url' => $url]; |
| 243 | 231 | |
| 244 | - $attachment_id = Attachment_Lookup::id_from_url($url, $attachment_id); | |
| 232 | + $attachment_id = (int) attachment_url_to_postid($url); | |
| 245 | 233 | |
| 246 | 234 | if ($attachment_id) { |
| 247 | - // The file this URL names, which for a body image is usually a | |
| 248 | - // generated size rather than the original upload. | |
| 249 | - $file = Attachment_Lookup::describe($attachment_id, $url); | |
| 235 | + $meta = wp_get_attachment_metadata($attachment_id); | |
| 250 | 236 | |
| 251 | 237 | // Vector uploads report 0x0; publishing that as a dimension is |
| 252 | 238 | // invalid, so the companions are omitted rather than zeroed. |
| 253 | - if ($file['width'] > 0 && $file['height'] > 0) { | |
| 254 | - $image['width'] = $file['width']; | |
| 255 | - $image['height'] = $file['height']; | |
| 239 | + $width = is_array($meta) && isset($meta['width']) ? (int) $meta['width'] : 0; | |
| 240 | + $height = is_array($meta) && isset($meta['height']) ? (int) $meta['height'] : 0; | |
| 241 | + | |
| 242 | + if ($width > 0 && $height > 0) { | |
| 243 | + $image['width'] = $width; | |
| 244 | + $image['height'] = $height; | |
| 256 | 245 | } |
| 257 | 246 | |
| 258 | - if ('' !== $file['type']) { | |
| 259 | - $image['type'] = $file['type']; | |
| 247 | + $type = (string) get_post_mime_type($attachment_id); | |
| 248 | + | |
| 249 | + if ('' !== $type) { | |
| 250 | + $image['type'] = $type; | |
| 260 | 251 | } |
| 261 | 252 | |
| 262 | 253 | $alt = trim((string) get_post_meta($attachment_id, '_wp_attachment_image_alt', true)); |
| 263 | 254 | |