PluginProbe
Assistant – Every Day Productivity Apps / 1.4.5
Assistant – Every Day Productivity Apps v1.4.5
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.4.5, at backend/src/Helpers/MediaPathHelper.php

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