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 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / addons / wpml-dependencies / vendor / wpml / wp / classes / Attachment.php
woocommerce-multilingual / addons / wpml-dependencies / vendor / wpml / wp / classes Last commit date
Attachment.php 1 month ago Cache.php 1 month ago Gutenberg.php 1 month ago Hooks.php 1 month ago Http.php 1 month ago Nonce.php 1 month ago Option.php 1 month ago Post.php 1 month ago PostType.php 1 month ago Resources.php 1 month ago Roles.php 4 years ago Transient.php 1 month ago Url.php 1 month ago User.php 1 month ago WP.php 1 month ago WPDB.php 1 month ago
Attachment.php
192 lines
1 <?php
2
3 namespace WPML\LIB\WP;
4
5 use WPML\FP\Logic;
6 use WPML\FP\Str;
7 use function WPML\FP\pipe;
8
9 class Attachment {
10 private static $cache = [];
11
12 private static $withOutSizeRegEx = '/-\d+[Xx]\d+\./';
13
14 private static function removeSizeFromUrl( $urlWithMaybeSize ) {
15 return Str::pregReplace( self::$withOutSizeRegEx, '.', $urlWithMaybeSize );
16 }
17
18 public static function extractSrcFromAttributes( $data ) {
19 if ( ! array_key_exists( 'attributes', $data ) || ! is_array( $data['attributes'] ) || ! array_key_exists( 'src', $data['attributes'] ) ) {
20 return '';
21 }
22
23 $src = $data['attributes']['src'];
24
25 if ( ! is_string( $src ) || strlen( $src ) === 0 ) {
26 return '';
27 }
28
29 return self::removeSizeFromUrl( $src );
30 }
31
32 public static function idFromUrlCache( $url, $urlWithoutSize = null ) {
33 if ( array_key_exists( $url, self::$cache ) ) {
34 return self::$cache[ $url ];
35 }
36
37 if ( is_null( $urlWithoutSize ) ) {
38 $urlWithoutSize = self::removeSizeFromUrl( $url );
39 }
40
41 if ( array_key_exists( $urlWithoutSize, self::$cache ) ) {
42 return self::$cache[ $urlWithoutSize ];
43 }
44
45 return null;
46 }
47
48 public static function idFromUrl( $url ) {
49 $urlWithoutSize = self::removeSizeFromUrl( $url );
50 $maybeId = self::idFromUrlCache( $url, $urlWithoutSize );
51
52 if ( ! is_null( $maybeId ) && is_numeric( $maybeId ) ) {
53 return $maybeId;
54 }
55
56 if ( $url !== $urlWithoutSize && $id = attachment_url_to_postid( $urlWithoutSize ) ) {
57 self::$cache[ $url ] = $id;
58 self::$cache[ $urlWithoutSize ] = $id;
59 return $id;
60 }
61
62 if ( $id = attachment_url_to_postid( $url ) ) {
63 self::$cache[ $url ] = $id;
64 return $id;
65 }
66
67 if ( $id = self::idByGuid( $url ) ) {
68 self::$cache[ $url ] = $id;
69 return $id;
70 }
71
72 if ( $url !== $urlWithoutSize && $id = self::idByGuid( $urlWithoutSize ) ) {
73 self::$cache[ $url ] = $id;
74 self::$cache[ $urlWithoutSize ] = $id;
75 return $id;
76 }
77
78 self::$cache[ $url ] = null;
79 return null;
80 }
81
82 public static function idByGuid( $url ) {
83 if ( array_key_exists( $url , self::$cache ) ) {
84 return self::$cache[$url];
85 }
86
87 global $wpdb;
88
89 $attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid='%s' LIMIT 1", $url ) );
90
91 return $attachment && count( $attachment )
92 ? $attachment[0]
93 : 0;
94 }
95
96 public static function addToCache( $media ) {
97 self::$cache = array_merge( self::$cache, $media );
98 }
99
100
101 public static function attachmentUrlsToPostIds( $urls ) {
102 global $wpdb;
103
104 if ( count( $urls ) === 0 ) {
105 return [];
106 }
107
108 list( $pathes, $urlsToPathes, $pathesToUrls ) = self::getPathesFromUrls( $urls );
109
110 $results = $wpdb->get_results(
111 "SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attached_file' AND meta_value IN (" . wpml_prepare_in( $pathes, '%s' ) . ")"
112 );
113
114 $results = array_map(
115 function ( $result ) {
116 return [
117 'id' => $result->post_id,
118 'value' => $result->meta_value
119 ];
120 },
121 $results
122 );
123
124 return self::mapUrlsToPostIds( $results, $urls, $pathes, $urlsToPathes, $pathesToUrls );
125 }
126
127 public static function getPathesFromUrls( $urls ) {
128 $urlsToPathes = [];
129 $pathesToUrls = [];
130 $pathes = [];
131 foreach ( $urls as $url ) {
132 $path = self::urlToPath( $url );
133 $pathes[] = $path;
134 $urlsToPathes[ $url ] = $path;
135 $pathesToUrls[ $path ] = $url;
136 }
137
138 return [ $pathes, $urlsToPathes, $pathesToUrls ];
139 }
140
141 public static function mapUrlsToPostIds( $results, $urls, $pathes, $urlsToPathes, $pathesToUrls ) {
142 $urlsToPostIds = [];
143 foreach ( $urls as $url ) {
144 $urlsToPostIds[ $url ] = null;
145 }
146
147 foreach ( $results as $result ) {
148 foreach ( $pathes as $path ) {
149 if ( $path === $result['value'] ) {
150 $url = $pathesToUrls[ $path ];
151 $urlsToPostIds[ $url ] = (int) $result['id'];
152 break;
153 }
154 }
155 }
156
157 foreach ( $urlsToPostIds as $url => $postId ) {
158 if ( ! is_null( $postId ) ) {
159 continue;
160 }
161
162 $path = strtolower( $urlsToPathes[ $url ] );
163 foreach ( $results as $result ) {
164 if ( $path === strtolower( $result['value'] ) ) {
165 $urlsToPostIds[ $url ] = (int) $result['id'];
166 break;
167 }
168 }
169 }
170
171 return $urlsToPostIds;
172 }
173
174 public static function urlToPath( $url ) {
175 $dir = wp_get_upload_dir();
176 $path = $url;
177
178 $site_url = parse_url( $dir['url'] );
179 $image_path = parse_url( $path );
180
181 if ( isset( $image_path['scheme'] ) && ( $image_path['scheme'] !== $site_url['scheme'] ) ) {
182 $path = str_replace( $image_path['scheme'], $site_url['scheme'], $path );
183 }
184
185 if ( Str::startsWith( $dir['baseurl'] . '/', $path ) ) {
186 $path = substr( $path, strlen( $dir['baseurl'] . '/' ) );
187 }
188
189 return $path;
190 }
191 }
192