| @@ -508,12 +508,11 @@ | ||
| 508 | 508 | * Attachment ID behind a configured icon URL, or 0 when it is not ours. |
| 509 | 509 | * |
| 510 | 510 | * attachment_url_to_postid() matches _wp_attached_file, which holds the |
| 511 | 511 | * ORIGINAL upload path, so the URL of a generated derivative |
| 512 | - * (`logo-512x512.png`) returns 0 — and that is exactly what the media | |
| 513 | - * picker hands back when the user chooses a size. Attachment_Lookup falls | |
| 514 | - * back to the original behind it; the fallback started here and moved | |
| 515 | - * there when every other image lookup turned out to need it (#847). | |
| 512 | + * (`logo-512.png`) returns 0 — and that is exactly what the media picker | |
| 513 | + * hands back when the user chooses a size. Strip the dimension suffix and | |
| 514 | + * try the original once. | |
| 516 | 515 | * |
| 517 | 516 | * Shared with SEO_Manager's site-icon filter so both sides of the feature |
| 518 | 517 | * agree on which attachment a configured URL means. |
| 519 | 518 | * |
| @@ -522,9 +521,21 @@ | ||
| 522 | 521 | * @param string $url Configured icon URL. |
| 523 | 522 | * @return int Attachment ID, or 0. |
| 524 | 523 | */ |
| 525 | 524 | public static function icon_attachment_id(string $url): int { |
| 526 | - return Attachment_Lookup::id_from_url($url); | |
| 525 | + $attachment_id = (int) attachment_url_to_postid($url); | |
| 526 | + | |
| 527 | + if ($attachment_id) { | |
| 528 | + return $attachment_id; | |
| 529 | + } | |
| 530 | + | |
| 531 | + $original = preg_replace('/-\d+x\d+(?=\.[a-zA-Z0-9]+$)/', '', $url); | |
| 532 | + | |
| 533 | + if (is_string($original) && $original !== $url) { | |
| 534 | + return (int) attachment_url_to_postid($original); | |
| 535 | + } | |
| 536 | + | |
| 537 | + return 0; | |
| 527 | 538 | } |
| 528 | 539 | |
| 529 | 540 | /** |
| 530 | 541 | * Which ICON_SIZES derivatives this attachment still needs. |
| @@ -1421,16 +1432,13 @@ | ||
| 1421 | 1432 | } |
| 1422 | 1433 | |
| 1423 | 1434 | // Additional logo analysis for local images |
| 1424 | 1435 | if (!empty($logo_url) && filter_var($logo_url, FILTER_VALIDATE_URL)) { |
| 1425 | - $attachment_id = Attachment_Lookup::id_from_url($logo_url); | |
| 1436 | + $attachment_id = attachment_url_to_postid($logo_url); | |
| 1426 | 1437 | if ($attachment_id) { |
| 1427 | 1438 | $image_meta = wp_get_attachment_metadata($attachment_id); |
| 1428 | - // The configured file's own size — a logo picked at a generated | |
| 1429 | - // size is not as large as the upload behind it. | |
| 1430 | - $logo_file = Attachment_Lookup::describe($attachment_id, $logo_url); | |
| 1431 | - $width = $logo_file['width']; | |
| 1432 | - $height = $logo_file['height']; | |
| 1439 | + $width = isset($image_meta['width']) ? (int) $image_meta['width'] : 0; | |
| 1440 | + $height = isset($image_meta['height']) ? (int) $image_meta['height'] : 0; | |
| 1433 | 1441 | |
| 1434 | 1442 | // SVG logos store 0x0 metadata — no dimension/ratio analysis |
| 1435 | 1443 | // is possible (and dividing by 0 is fatal). |
| 1436 | 1444 | if ($image_meta && $width > 0 && $height > 0) { |
| @@ -4253,20 +4261,18 @@ | ||
| 4253 | 4261 | return $optimization; |
| 4254 | 4262 | } |
| 4255 | 4263 | |
| 4256 | 4264 | // Check if it's a local image |
| 4257 | - $attachment_id = Attachment_Lookup::id_from_url($value); | |
| 4265 | + $attachment_id = attachment_url_to_postid($value); | |
| 4258 | 4266 | if ($attachment_id) { |
| 4259 | 4267 | $image_meta = wp_get_attachment_metadata($attachment_id); |
| 4260 | 4268 | |
| 4261 | 4269 | if ($image_meta && isset($image_meta['width'], $image_meta['height'])) { |
| 4262 | - // Check recommended size, against the configured file itself | |
| 4263 | - // rather than the upload it may have been generated from. | |
| 4270 | + // Check recommended size | |
| 4264 | 4271 | if (isset($config['recommended_size'])) { |
| 4265 | 4272 | [$rec_width, $rec_height] = explode('x', $config['recommended_size']); |
| 4266 | - $image_file = Attachment_Lookup::describe($attachment_id, $value); | |
| 4267 | 4273 | |
| 4268 | - if ($image_file['width'] !== (int) $rec_width || $image_file['height'] !== (int) $rec_height) { | |
| 4274 | + if ((int) $image_meta['width'] !== (int) $rec_width || (int) $image_meta['height'] !== (int) $rec_height) { | |
| 4269 | 4275 | $optimization['suggestions'][] = "Consider using {$config['recommended_size']} size for optimal {$element}"; |
| 4270 | 4276 | } |
| 4271 | 4277 | } |
| 4272 | 4278 | |