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

283 lines 7.0 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|webp))#';
201 $urls = [];
202
203 if ( $string === null ) {
204 return $urls;
205 }
206
207 if ( preg_match_all( $pattern, $string, $matches ) ) {
208 if ( isset( $matches[0] ) && ! empty( $matches[0] ) ) {
209 $urls = $matches[0];
210 }
211 }
212
213 return $urls;
214 }
215
216 /**
217 * Get the urls to all images in an object or array.
218 *
219 * @param mixed $data
220 * @return array
221 */
222 static public function get_image_urls_from_data( $data ) {
223 $urls = [];
224
225 foreach ( $data as $key => $val ) {
226 if ( in_array( $key, self::$data_blacklist ) ) {
227 continue;
228 }
229
230 $val = maybe_unserialize( $val );
231
232 if ( is_object( $val ) || is_array( $val ) ) {
233 $urls = array_merge( $urls, self::get_image_urls_from_data( $val ) );
234 } elseif ( JsonHelper::is_string_json( $val ) ) {
235 $val = wp_unslash( $val );
236 $urls = array_merge( $urls, self::get_image_urls_from_string( $val ) );
237 } else {
238 $urls = array_merge( $urls, self::get_image_urls_from_string( $val ) );
239 }
240 }
241
242 return $urls;
243 }
244
245 /**
246 * Get the info for a media library image from a url.
247 *
248 * @param string $url
249 * @return array
250 */
251 static public function get_image_info_from_url( $url ) {
252 $info = [
253 'url' => $url,
254 'full_size_url' => $url,
255 'file_name' => null,
256 'width' => null,
257 'height' => null,
258 'ext' => null,
259 ];
260
261 preg_match_all( '/(-(\d+)x(\d+))\.(jpg|jpeg|png|gif|svg)/', $url, $matches );
262
263 if ( isset( $matches[1] ) && ! empty( $matches[1] ) ) {
264 $info['full_size_url'] = str_replace( $matches[1], '', $url );
265 }
266
267 $parts = explode( '/', $info['full_size_url'] );
268 $info['file_name'] = array_pop( $parts );
269
270 if ( isset( $matches[2] ) && ! empty( $matches[2] ) ) {
271 $info['width'] = $matches[2][0];
272 }
273 if ( isset( $matches[3] ) && ! empty( $matches[3] ) ) {
274 $info['height'] = $matches[3][0];
275 }
276 if ( isset( $matches[4] ) && ! empty( $matches[4] ) ) {
277 $info['ext'] = $matches[4][0];
278 }
279
280 return $info;
281 }
282 }
283