PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 4.4.1
Admin Columns v4.4.1
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 / Media.php
codepress-admin-columns / classes / Helper Last commit date
Select 4 years ago Arrays.php 4 years ago Date.php 4 years ago File.php 4 years ago Html.php 4 years ago Icon.php 4 years ago Image.php 4 years ago Media.php 4 years ago Menu.php 4 years ago Network.php 4 years ago Post.php 4 years ago Strings.php 4 years ago Taxonomy.php 4 years ago User.php 4 years ago
Media.php
92 lines
1 <?php
2
3 namespace AC\Helper;
4
5 class Media {
6
7 /**
8 * @param string $image_url
9 * @param bool $check_cropped_versions Checks for cropped version of the image. e.g. file-name-320x60.jpg
10 *
11 * @return false|int
12 */
13 public function get_attachment_id_by_url( $image_url, $check_cropped_versions = false ) {
14 if ( ! $image_url ) {
15 return false;
16 }
17
18 $upload_dir = wp_get_upload_dir();
19
20 // Is image in upload folder?
21 if ( false === strpos( $image_url, $upload_dir['baseurl'] ) ) {
22 return false;
23 }
24
25 $file_with_relative_path = ltrim( str_replace( $upload_dir['baseurl'], '', $image_url ), '/' );
26
27 $image_id = false;
28
29 $images = get_posts( [
30 'post_type' => 'attachment',
31 'fields' => 'ids',
32 'meta_query' => [
33 [
34 'key' => '_wp_attached_file',
35 'value' => $file_with_relative_path,
36 ],
37 ],
38 'posts_per_page' => 1,
39 ] );
40
41 if ( $images ) {
42 $image_id = $images[0];
43 }
44
45 // Maybe it's a cropped version of an image. e.g. file-name-320x60.jpg
46 if ( ! $image_id && $check_cropped_versions ) {
47
48 $relative_upload_dir = dirname( $file_with_relative_path );
49
50 $image_ids = get_posts( [
51 'post_type' => 'attachment',
52 'fields' => 'ids',
53 'meta_query' => [
54 [
55 'key' => '_wp_attachment_metadata',
56 'value' => serialize( basename( $image_url ) ),
57 'compare' => 'LIKE',
58 ],
59 [
60 'key' => '_wp_attached_file',
61 'value' => $relative_upload_dir,
62 'compare' => 'LIKE',
63 ],
64 ],
65 'posts_per_page' => 1,
66 ] );
67
68 if ( $image_ids ) {
69
70 if ( 1 === count( $image_ids ) ) {
71
72 // Direct match
73 $image_id = $image_ids[0];
74 } else {
75
76 // Make sure the found image is in the same folder as the original
77 foreach ( $image_ids as $_image_id ) {
78 if ( 0 === strpos( get_post_meta( $_image_id, '_wp_attached_file', true ), $relative_upload_dir ) ) {
79 $image_id = $_image_id;
80
81 }
82 }
83
84 }
85 }
86
87 }
88
89 return (int) $image_id;
90 }
91
92 }