PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 7.1.4
Admin Columns v7.1.4
7.1.4 7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Helper / MediaIdResolver.php
codepress-admin-columns / classes / Helper Last commit date
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