PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.2.9
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.2.9
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / functions / attachments.php

attachments.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.2.9, at inc/functions/attachments.php

376 lines 9.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || exit;
3
4 /**
5 * Get all mime types which could be optimized by Imagify.
6 *
7 * @since 1.7
8 * @since 1.8 Added $type parameter.
9 *
10 * @param string $type One of 'image', 'not-image'. Any other value will return all mime types.
11 * @return array The mime types.
12 */
13 function imagify_get_mime_types( $type = null ) {
14 $mimes = [];
15
16 if ( 'not-image' !== $type ) {
17 $mimes = [
18 'jpg|jpeg|jpe' => 'image/jpeg',
19 'png' => 'image/png',
20 'gif' => 'image/gif',
21 'webp' => 'image/webp',
22 ];
23 }
24
25 if ( 'image' !== $type ) {
26 $mimes['pdf'] = 'application/pdf';
27 }
28
29 return $mimes;
30 }
31
32 /**
33 * Tell if an attachment has a supported mime type.
34 * Was previously Imagify_AS3CF::is_mime_type_supported() since 1.6.6.
35 *
36 * @since 1.6.8
37 * @author Grégory Viguier
38 *
39 * @param int $attachment_id The attachment ID.
40 * @return bool
41 */
42 function imagify_is_attachment_mime_type_supported( $attachment_id ) {
43 static $is = [ false ];
44
45 $attachment_id = absint( $attachment_id );
46
47 if ( isset( $is[ $attachment_id ] ) ) {
48 return $is[ $attachment_id ];
49 }
50
51 $mime_types = imagify_get_mime_types();
52 $mime_types = array_flip( $mime_types );
53 $mime_type = (string) get_post_mime_type( $attachment_id );
54
55 $is[ $attachment_id ] = isset( $mime_types[ $mime_type ] );
56
57 return $is[ $attachment_id ];
58 }
59
60 /**
61 * Get post statuses related to attachments.
62 *
63 * @since 1.7
64 * @author Grégory Viguier
65 *
66 * @return array
67 */
68 function imagify_get_post_statuses() {
69 static $statuses;
70
71 if ( isset( $statuses ) ) {
72 return $statuses;
73 }
74
75 $statuses = [
76 'inherit' => 'inherit',
77 'private' => 'private',
78 ];
79
80 $custom_statuses = get_post_stati( [ 'public' => true ] );
81 unset( $custom_statuses['publish'] );
82
83 if ( $custom_statuses ) {
84 $statuses = array_merge( $statuses, $custom_statuses );
85 }
86
87 /**
88 * Filter the post statuses Imagify is allowed to optimize.
89 *
90 * @since 1.7
91 * @author Grégory Viguier
92 *
93 * @param array $statuses An array of post statuses. Kays and values are set.
94 */
95 $statuses = apply_filters( 'imagify_post_statuses', $statuses );
96
97 return $statuses;
98 }
99
100 /**
101 * Tell if the site has attachments (only the ones Imagify would optimize) without the required WP metadata.
102 *
103 * @param bool $reset Reset the static method to null when set to true, defaulted to false.
104 * @since 1.7
105 * @author Grégory Viguier
106 *
107 * @return bool
108 */
109 function imagify_has_attachments_without_required_metadata( $reset = false ) {
110 global $wpdb;
111 static $has;
112
113 if ( $reset ) {
114 $has = null;
115 }
116
117 if ( isset( $has ) ) {
118 return $has;
119 }
120
121 $mime_types = Imagify_DB::get_mime_types();
122 $statuses = Imagify_DB::get_post_statuses();
123 $exist_data = Imagify_DB::get_required_wp_metadata_exist_clause(
124 'p.ID',
125 false
126 );
127 $has = (bool) $wpdb->get_var( // WPCS: unprepared SQL ok.
128 "
129 SELECT p.ID
130 FROM $wpdb->posts AS p
131 WHERE p.post_mime_type IN ( $mime_types )
132 AND p.post_type = 'attachment'
133 AND p.post_status IN ( $statuses )
134 $exist_data
135 LIMIT 1"
136 );
137
138 return $has;
139 }
140
141 /**
142 * Get the path to the backups directory.
143 *
144 * @since 1.6.8
145 * @author Grégory Viguier
146 *
147 * @param bool $bypass_error True to return the path even if there is an error. This is used when we want to display this path in a message for example.
148 * @return string|bool Path to the backups directory. False on failure.
149 */
150 function get_imagify_backup_dir_path( $bypass_error = false ) {
151 static $backup_dir;
152
153 if ( isset( $backup_dir ) ) {
154 return $backup_dir;
155 }
156
157 $upload_basedir = get_imagify_upload_basedir( $bypass_error );
158
159 if ( ! $upload_basedir ) {
160 return false;
161 }
162
163 $backup_dir = $upload_basedir . 'backup/';
164
165 /**
166 * Filter the backup directory path.
167 *
168 * @since 1.0
169 *
170 * @param string $backup_dir The backup directory path.
171 */
172 $backup_dir = apply_filters( 'imagify_backup_directory', $backup_dir );
173 $backup_dir = imagify_get_filesystem()->normalize_dir_path( $backup_dir );
174
175 return $backup_dir;
176 }
177
178 /**
179 * Tell if the folder containing the backups is writable.
180 *
181 * @since 1.6.8
182 * @author Grégory Viguier
183 *
184 * @return bool
185 */
186 function imagify_backup_dir_is_writable() {
187 return imagify_get_filesystem()->make_dir( get_imagify_backup_dir_path() );
188 }
189
190 /**
191 * Get the backup path of a specific attachement.
192 *
193 * @since 1.0
194 *
195 * @param string $file_path The file path.
196 * @return string|bool The backup path. False on failure.
197 */
198 function get_imagify_attachment_backup_path( $file_path ) {
199 $file_path = wp_normalize_path( (string) $file_path );
200 $upload_basedir = get_imagify_upload_basedir();
201 $backup_dir = get_imagify_backup_dir_path();
202
203 if ( ! $file_path || ! $upload_basedir ) {
204 return false;
205 }
206
207 return preg_replace( '@^' . preg_quote( $upload_basedir, '@' ) . '@', $backup_dir, $file_path );
208 }
209
210 /**
211 * Retrieve file path for an attachment based on filename.
212 *
213 * @since 1.4.5
214 *
215 * @param int $file_path The file path.
216 * @return string|false The file path to where the attached file should be, false otherwise.
217 */
218 function get_imagify_attached_file( $file_path ) {
219 $file_path = wp_normalize_path( (string) $file_path );
220 $upload_basedir = get_imagify_upload_basedir();
221
222 if ( ! $file_path || ! $upload_basedir ) {
223 return false;
224 }
225
226 // The file path is absolute.
227 if ( strpos( $file_path, '/' ) === 0 || preg_match( '|^.:\\\|', $file_path ) ) {
228 return false;
229 }
230
231 // Prepend upload dir.
232 return $upload_basedir . $file_path;
233 }
234
235 /**
236 * Retrieve the URL for an attachment based on file path.
237 *
238 * @since 1.4.5
239 *
240 * @param string $file_path A relative or absolute file path.
241 * @return string|bool File URL, otherwise false.
242 */
243 function get_imagify_attachment_url( $file_path ) {
244 $file_path = wp_normalize_path( (string) $file_path );
245 $upload_basedir = get_imagify_upload_basedir();
246
247 if ( ! $file_path || ! $upload_basedir ) {
248 return false;
249 }
250
251 $upload_baseurl = get_imagify_upload_baseurl();
252
253 // Check that the upload base exists in the (absolute) file location.
254 if ( 0 === strpos( $file_path, $upload_basedir ) ) {
255 // Replace file location with url location.
256 return preg_replace( '@^' . preg_quote( $upload_basedir, '@' ) . '@', $upload_baseurl, $file_path );
257 }
258
259 if ( false !== strpos( '/' . $file_path, '/wp-content/uploads/' ) ) {
260 // Get the directory name relative to the basedir (back compat for pre-2.7 uploads).
261 return trailingslashit( $upload_baseurl . _wp_get_attachment_relative_path( $file_path ) ) . imagify_get_filesystem()->file_name( $file_path );
262 }
263
264 // It's a newly-uploaded file, therefore $file is relative to the basedir.
265 return $upload_baseurl . $file_path;
266 }
267
268 /**
269 * Get size information for all currently registered thumbnail sizes.
270 *
271 * @since 1.5.10
272 * @since 1.6.10 For consistency, revamped the function like WP does with wp_generate_attachment_metadata().
273 * Removed the filter, added crop value to each size.
274 * @author Grégory Viguier
275 *
276 * @return array {
277 * Data for the currently registered thumbnail sizes.
278 * Size names are used as array keys.
279 *
280 * @type int $width The image width.
281 * @type int $height The image height.
282 * @type bool $crop True to crop, false to resize.
283 * @type string $name The size name.
284 * }
285 */
286 function get_imagify_thumbnail_sizes() {
287 // All image size names.
288 $intermediate_image_sizes = get_intermediate_image_sizes();
289 $intermediate_image_sizes = array_flip( $intermediate_image_sizes );
290 // Additional image size attributes.
291 $additional_image_sizes = wp_get_additional_image_sizes();
292
293 // Create the full array with sizes and crop info.
294 foreach ( $intermediate_image_sizes as $size_name => $s ) {
295 $intermediate_image_sizes[ $size_name ] = [
296 'width' => '',
297 'height' => '',
298 'crop' => false,
299 'name' => $size_name,
300 ];
301
302 if ( isset( $additional_image_sizes[ $size_name ]['width'] ) ) {
303 // For theme-added sizes.
304 $intermediate_image_sizes[ $size_name ]['width'] = (int) $additional_image_sizes[ $size_name ]['width'];
305 } else {
306 // For default sizes set in options.
307 $intermediate_image_sizes[ $size_name ]['width'] = (int) get_option( "{$size_name}_size_w" );
308 }
309
310 if ( isset( $additional_image_sizes[ $size_name ]['height'] ) ) {
311 // For theme-added sizes.
312 $intermediate_image_sizes[ $size_name ]['height'] = (int) $additional_image_sizes[ $size_name ]['height'];
313 } else {
314 // For default sizes set in options.
315 $intermediate_image_sizes[ $size_name ]['height'] = (int) get_option( "{$size_name}_size_h" );
316 }
317
318 if ( isset( $additional_image_sizes[ $size_name ]['crop'] ) ) {
319 // For theme-added sizes.
320 $intermediate_image_sizes[ $size_name ]['crop'] = (int) $additional_image_sizes[ $size_name ]['crop'];
321 } else {
322 // For default sizes set in options.
323 $intermediate_image_sizes[ $size_name ]['crop'] = (int) get_option( "{$size_name}_crop" );
324 }
325 }
326
327 return $intermediate_image_sizes;
328 }
329
330 /**
331 * A simple helper to get the upload basedir.
332 *
333 * @since 1.6.7
334 * @since 1.6.8 Added the $bypass_error parameter.
335 * @author Grégory Viguier
336 *
337 * @param bool $bypass_error True to return the path even if there is an error. This is used when we want to display this path in a message for example.
338 * @return string|bool The path. False on failure.
339 */
340 function get_imagify_upload_basedir( $bypass_error = false ) {
341 return imagify_get_filesystem()->get_upload_basedir( $bypass_error );
342 }
343
344 /**
345 * A simple helper to get the upload baseurl.
346 *
347 * @since 1.6.7
348 * @author Grégory Viguier
349 *
350 * @return string|bool The URL. False on failure.
351 */
352 function get_imagify_upload_baseurl() {
353 return imagify_get_filesystem()->get_upload_baseurl();
354 }
355
356 /**
357 * Get the maximal number of unoptimized attachments to fetch.
358 *
359 * @since 1.6.14
360 * @author Grégory Viguier
361 *
362 * @return int
363 */
364 function imagify_get_unoptimized_attachment_limit() {
365 /**
366 * Filter the unoptimized attachments limit query.
367 *
368 * @since 1.4.4
369 *
370 * @param int $limit The limit (-1 for unlimited).
371 */
372 $limit = (int) apply_filters( 'imagify_unoptimized_attachment_limit', 10000 );
373
374 return -1 === $limit ? PHP_INT_MAX : abs( $limit );
375 }
376