Image
2 days ago
Select
2 days ago
Arrays.php
2 days ago
AvailableTranslations.php
2 days ago
Creatable.php
2 days ago
Dashicon.php
2 days ago
Date.php
2 days ago
Html.php
2 days ago
Icon.php
2 days ago
Image.php
2 days ago
LocalFile.php
2 days ago
Mbstring.php
2 days ago
MediaIdResolver.php
2 days ago
Menu.php
2 days ago
Network.php
2 days ago
Post.php
2 days ago
Strings.php
2 days ago
Taxonomy.php
2 days ago
User.php
2 days ago
UserRoles.php
2 days ago
WpDateFormat.php
2 days ago
MediaIdResolver.php
97 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Helper; |
| 6 | |
| 7 | class MediaIdResolver extends Creatable |
| 8 | { |
| 9 | public function get_attachment_id_by_url(string $image_url, bool $check_cropped_versions = false): ?int |
| 10 | { |
| 11 | if (! $image_url) { |
| 12 | return null; |
| 13 | } |
| 14 | |
| 15 | $upload_dir = wp_get_upload_dir(); |
| 16 | |
| 17 | // Normalize the scheme (http vs https) on both URLs so a valid local upload isn't rejected on a scheme mismatch. |
| 18 | $image_url = set_url_scheme($image_url); |
| 19 | $baseurl = set_url_scheme($upload_dir['baseurl']); |
| 20 | |
| 21 | // Is image in upload folder? |
| 22 | if (false === strpos($image_url, $baseurl)) { |
| 23 | return null; |
| 24 | } |
| 25 | |
| 26 | $file_with_relative_path = ltrim(str_replace($baseurl, '', $image_url), '/'); |
| 27 | |
| 28 | $images = get_posts([ |
| 29 | 'post_type' => 'attachment', |
| 30 | 'fields' => 'ids', |
| 31 | 'meta_query' => [ |
| 32 | [ |
| 33 | 'key' => '_wp_attached_file', |
| 34 | 'value' => $file_with_relative_path, |
| 35 | ], |
| 36 | ], |
| 37 | 'posts_per_page' => 1, |
| 38 | ]); |
| 39 | |
| 40 | if ($images) { |
| 41 | return (int)$images[0]; |
| 42 | } |
| 43 | |
| 44 | // Maybe it's a cropped version of an image. e.g. file-name-320x60.jpg |
| 45 | if ($check_cropped_versions) { |
| 46 | return $this->get_cropped_attachment_id_by_url($image_url, $file_with_relative_path); |
| 47 | } |
| 48 | |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | private function get_cropped_attachment_id_by_url(string $image_url, string $file_with_relative_path): ?int |
| 53 | { |
| 54 | $relative_upload_dir = dirname($file_with_relative_path); |
| 55 | |
| 56 | $image_ids = get_posts([ |
| 57 | 'post_type' => 'attachment', |
| 58 | 'fields' => 'ids', |
| 59 | 'meta_query' => [ |
| 60 | [ |
| 61 | 'key' => '_wp_attachment_metadata', |
| 62 | 'value' => serialize(basename($image_url)), |
| 63 | 'compare' => 'LIKE', |
| 64 | ], |
| 65 | [ |
| 66 | 'key' => '_wp_attached_file', |
| 67 | 'value' => $relative_upload_dir, |
| 68 | 'compare' => 'LIKE', |
| 69 | ], |
| 70 | ], |
| 71 | 'posts_per_page' => 1, |
| 72 | ]); |
| 73 | |
| 74 | if (! $image_ids) { |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | // Direct match |
| 79 | if (1 === count($image_ids)) { |
| 80 | return (int)$image_ids[0]; |
| 81 | } |
| 82 | |
| 83 | // Make sure the found image is in the same folder as the original |
| 84 | $image_id = null; |
| 85 | |
| 86 | foreach ($image_ids as $_image_id) { |
| 87 | if (0 === strpos((string) get_post_meta($_image_id, '_wp_attached_file', true), $relative_upload_dir)) { |
| 88 | $image_id = $_image_id; |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return $image_id === null ? null : (int)$image_id; |
| 94 | } |
| 95 | |
| 96 | } |
| 97 |