PluginProbe
Assistant – Every Day Productivity Apps / 1.3.4
Assistant – Every Day Productivity Apps v1.3.4
1.5.5 trunk 0.1.0 0.2.0 0.2.1 0.2.2 0.3.0 0.3.1 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.6.0 0.7.0 0.7.0.2 0.7.0.3 0.7.0.4 0.7.0.5 0.7.0.6 0.7.0.7 0.7.0.8 0.7.0.9 0.7.1 1.0 All 71 releases
assistant / backend / src / Helpers / MediaPathHelper.php

MediaPathHelper.php in Assistant – Every Day Productivity Apps 1.3.4, at backend/src/Helpers/MediaPathHelper.php

266 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FL\Assistant\Helpers;
4
5 use FL\Assistant\Helpers\JsonHelper;
6
7 class MediaPathHelper {
8
9 /**
10 * Replaces the imported attachment in an object or array.
11 *
12 * @param object|array|string $data
13 * @param array $imported
14 * @return object|array|string
15 */
16 static public function replace_imported_attachment_urls_in_data( $data, $imported ) {
17 if ( is_object( $data ) || is_array( $data ) ) {
18 foreach ( $data as $key => $val ) {
19 if ( is_object( $val ) || is_array( $val ) ) {
20 $new_val = self::replace_imported_attachment_urls_in_data( $val, $imported );
21 } else {
22 $new_val = self::replace_imported_attachment_urls_in_string( $val, $imported );
23 }
24
25 if ( is_object( $data ) ) {
26 $data->$key = $new_val;
27 } else {
28 $data[ $key ] = $new_val;
29 }
30 }
31 } else {
32 $data = self::replace_imported_attachment_urls_in_string( $data, $imported );
33 }
34
35 return $data;
36 }
37
38 /**
39 * Replaces the imported attachment in a string.
40 *
41 * @param string $string
42 * @param array $imported
43 * @return string
44 */
45 static public function replace_imported_attachment_urls_in_string( $string, $imported ) {
46 $urls = self::get_image_urls_from_string( $string );
47
48 foreach ( $urls as $url ) {
49 $url_info = self::get_image_info_from_url( $url );
50
51 if ( ! isset( $imported[ $url_info['file_name'] ] ) ) {
52 continue;
53 }
54
55 $import_data = (array) $imported[ $url_info['file_name'] ];
56 $sizes = self::get_ordered_image_sizes( (array) $import_data['sizes'] );
57 $new_url = null;
58
59 if ( $url_info['width'] && $url_info['height'] ) {
60 foreach ( $sizes as $size => $size_data ) {
61 $size_data = (array) $size_data;
62 if ( $url_info['width'] <= $size_data['width'] && $url_info['height'] <= $size_data['height'] ) {
63 if ( isset( $size_data['url'] ) ) {
64 $new_url = $size_data['url'];
65 break;
66 } elseif ( isset( $import_data['id'] ) ) {
67 $size_src = wp_get_attachment_image_src( $import_data['id'], $size );
68 if ( $size_src ) {
69 $new_url = $size_src[0];
70 break;
71 }
72 }
73 }
74 }
75 }
76
77 if ( ! $new_url ) {
78 if ( isset( $import_data['url'] ) ) {
79 $new_url = $import_data['url'];
80 } elseif ( isset( $import_data['id'] ) ) {
81 $new_url = wp_get_attachment_url( $import_data['id'] );
82 }
83 }
84
85 if ( $new_url ) {
86 $string = str_replace( $url, $new_url, $string );
87 }
88 }
89
90 return $string;
91 }
92
93 /**
94 * Returns an array or ordered image sizes.
95 *
96 * @param array $sizes
97 * @return array
98 */
99 static public function get_ordered_image_sizes( $sizes ) {
100 $keys = [];
101 $ordered = [];
102
103 foreach ( $sizes as $size => $data ) {
104 if ( 'thumb' === $size || 'thumbnail' === $size ) {
105 continue;
106 }
107 $data = (array) $data;
108 $key = $data['width'] + $data['height'];
109 $keys[ $key ] = $size;
110 }
111
112 ksort( $keys );
113
114 foreach ( $keys as $key ) {
115 $ordered[ $key ] = $sizes[ $key ];
116 }
117
118 return $ordered;
119 }
120
121 /**
122 * Get the server paths to all images in a string.
123 *
124 * @param string $string
125 * @return array
126 */
127 static public function get_image_paths_from_string( $string ) {
128 $urls = self::get_image_urls_from_string( $string );
129 $paths = self::get_image_paths_from_urls( $urls );
130 return $paths;
131 }
132
133 /**
134 * Get the server paths to all images in an array or object.
135 *
136 * @param mixed $data
137 * @return array
138 */
139 static public function get_image_paths_from_data( $data ) {
140 $urls = self::get_image_urls_from_data( $data );
141 $paths = self::get_image_paths_from_urls( $urls );
142 return $paths;
143 }
144
145 /**
146 * Get the server paths to all images in an array of urls.
147 *
148 * @param array $urls
149 * @return array
150 */
151 static public function get_image_paths_from_urls( $urls = [] ) {
152 $paths = [];
153
154 foreach ( $urls as $url ) {
155 $path = self::get_image_path_from_url( $url );
156 if ( $path ) {
157 $paths[] = $path;
158 }
159 }
160
161 return $paths;
162 }
163
164 /**
165 * Converts a media library URL to the server path of the full size image.
166 *
167 * @param string $url
168 * @return string
169 */
170 static public function get_image_path_from_url( $url ) {
171 $upload_dir = wp_get_upload_dir();
172 $base_url = preg_replace( '/https?:\/\//', '', $upload_dir['baseurl'] );
173 $url_info = self::get_image_info_from_url( $url );
174 $url = preg_replace( '/https?:\/\//', '', $url_info['full_size_url'] );
175 $path = null;
176
177 if ( stristr( $url, $base_url ) ) {
178 $path = $upload_dir['basedir'] . str_ireplace( $base_url, '', $url );
179 }
180
181 return $path;
182 }
183
184 /**
185 * Get the urls to all images in a string.
186 *
187 * @param string $string
188 * @return array
189 */
190 static public function get_image_urls_from_string( $string ) {
191 $pattern = '#https?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/)\.(jpg|jpeg|png|gif|svg))#';
192 $urls = [];
193
194 if ( preg_match_all( $pattern, $string, $matches ) ) {
195 if ( isset( $matches[0] ) && ! empty( $matches[0] ) ) {
196 $urls = $matches[0];
197 }
198 }
199
200 return $urls;
201 }
202
203 /**
204 * Get the urls to all images in an object or array.
205 *
206 * @param mixed $data
207 * @return array
208 */
209 static public function get_image_urls_from_data( $data ) {
210 $urls = [];
211
212 foreach ( $data as $key => $val ) {
213 $val = maybe_unserialize( $val );
214
215 if ( is_object( $val ) || is_array( $val ) ) {
216 $urls = array_merge( $urls, self::get_image_urls_from_data( $val ) );
217 } elseif ( JsonHelper::is_string_json( $val ) ) {
218 $val = wp_unslash( $val );
219 $urls = array_merge( $urls, self::get_image_urls_from_string( $val ) );
220 } else {
221 $urls = array_merge( $urls, self::get_image_urls_from_string( $val ) );
222 }
223 }
224
225 return $urls;
226 }
227
228 /**
229 * Get the info for a media library image from a url.
230 *
231 * @param string $url
232 * @return array
233 */
234 static public function get_image_info_from_url( $url ) {
235 $info = [
236 'url' => $url,
237 'full_size_url' => $url,
238 'file_name' => null,
239 'width' => null,
240 'height' => null,
241 'ext' => null,
242 ];
243
244 preg_match_all( '/(-(\d+)x(\d+))\.(jpg|jpeg|png|gif|svg)/', $url, $matches );
245
246 if ( isset( $matches[1] ) && ! empty( $matches[1] ) ) {
247 $info['full_size_url'] = str_replace( $matches[1], '', $url );
248 }
249
250 $parts = explode( '/', $info['full_size_url'] );
251 $info['file_name'] = array_pop( $parts );
252
253 if ( isset( $matches[2] ) && ! empty( $matches[2] ) ) {
254 $info['width'] = $matches[2][0];
255 }
256 if ( isset( $matches[3] ) && ! empty( $matches[3] ) ) {
257 $info['height'] = $matches[3][0];
258 }
259 if ( isset( $matches[4] ) && ! empty( $matches[4] ) ) {
260 $info['ext'] = $matches[4][0];
261 }
262
263 return $info;
264 }
265 }
266