PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.9.13
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.9.13
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 1.9.13, at inc/functions/attachments.php

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