| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | -defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); | |
| 2 | +defined( 'ABSPATH' ) || exit; | |
| 3 | 3 | |
| 4 | 4 | /** |
| 5 | 5 | * Get all mime types which could be optimized by Imagify. |
| 6 | 6 | * |
| @@ -10,16 +10,17 @@ | ||
| 10 | 10 | * @param string $type One of 'image', 'not-image'. Any other value will return all mime types. |
| 11 | 11 | * @return array The mime types. |
| 12 | 12 | */ |
| 13 | 13 | function imagify_get_mime_types( $type = null ) { |
| 14 | - $mimes = array(); | |
| 14 | + $mimes = []; | |
| 15 | 15 | |
| 16 | 16 | if ( 'not-image' !== $type ) { |
| 17 | - $mimes = array( | |
| 17 | + $mimes = [ | |
| 18 | 18 | 'jpg|jpeg|jpe' => 'image/jpeg', |
| 19 | 19 | 'png' => 'image/png', |
| 20 | 20 | 'gif' => 'image/gif', |
| 21 | - ); | |
| 21 | + 'webp' => 'image/webp', | |
| 22 | + ]; | |
| 22 | 23 | } |
| 23 | 24 | |
| 24 | 25 | if ( 'image' !== $type ) { |
| 25 | 26 | $mimes['pdf'] = 'application/pdf'; |
| @@ -24,9 +25,25 @@ | ||
| 24 | 25 | if ( 'image' !== $type ) { |
| 25 | 26 | $mimes['pdf'] = 'application/pdf'; |
| 26 | 27 | } |
| 27 | 28 | |
| 28 | - return $mimes; | |
| 29 | + /** | |
| 30 | + * Filter the mime types which could be optimized by Imagify. | |
| 31 | + * | |
| 32 | + * @since 2.3.1 | |
| 33 | + * | |
| 34 | + * @param array $mimes The mime types, as extension => mime type pairs. | |
| 35 | + */ | |
| 36 | + $mimes = wpm_apply_filters_typed( 'array', 'imagify_get_mime_types', $mimes ); | |
| 37 | + | |
| 38 | + // Keep only well-formed extension => mime type pairs. | |
| 39 | + return array_filter( | |
| 40 | + $mimes, | |
| 41 | + function ( $mime, $ext ) { | |
| 42 | + return is_string( $ext ) && '' !== $ext && is_string( $mime ) && preg_match( '@^[a-z-]+/[a-z0-9.+-]+$@', $mime ); | |
| 43 | + }, | |
| 44 | + ARRAY_FILTER_USE_BOTH | |
| 45 | + ); | |
| 29 | 46 | } |
| 30 | 47 | |
| 31 | 48 | /** |
| 32 | 49 | * Tell if an attachment has a supported mime type. |
| @@ -38,9 +55,9 @@ | ||
| 38 | 55 | * @param int $attachment_id The attachment ID. |
| 39 | 56 | * @return bool |
| 40 | 57 | */ |
| 41 | 58 | function imagify_is_attachment_mime_type_supported( $attachment_id ) { |
| 42 | - static $is = array( false ); | |
| 59 | + static $is = [ false ]; | |
| 43 | 60 | |
| 44 | 61 | $attachment_id = absint( $attachment_id ); |
| 45 | 62 | |
| 46 | 63 | if ( isset( $is[ $attachment_id ] ) ) { |
| @@ -70,14 +87,14 @@ | ||
| 70 | 87 | if ( isset( $statuses ) ) { |
| 71 | 88 | return $statuses; |
| 72 | 89 | } |
| 73 | 90 | |
| 74 | - $statuses = array( | |
| 91 | + $statuses = [ | |
| 75 | 92 | 'inherit' => 'inherit', |
| 76 | 93 | 'private' => 'private', |
| 77 | - ); | |
| 94 | + ]; | |
| 78 | 95 | |
| 79 | - $custom_statuses = get_post_stati( array( 'public' => true ) ); | |
| 96 | + $custom_statuses = get_post_stati( [ 'public' => true ] ); | |
| 80 | 97 | unset( $custom_statuses['publish'] ); |
| 81 | 98 | |
| 82 | 99 | if ( $custom_statuses ) { |
| 83 | 100 | $statuses = array_merge( $statuses, $custom_statuses ); |
| @@ -98,37 +115,40 @@ | ||
| 98 | 115 | |
| 99 | 116 | /** |
| 100 | 117 | * Tell if the site has attachments (only the ones Imagify would optimize) without the required WP metadata. |
| 101 | 118 | * |
| 119 | + * @param bool $reset Reset the static method to null when set to true, defaulted to false. | |
| 102 | 120 | * @since 1.7 |
| 103 | 121 | * @author Grégory Viguier |
| 104 | 122 | * |
| 105 | 123 | * @return bool |
| 106 | 124 | */ |
| 107 | -function imagify_has_attachments_without_required_metadata() { | |
| 125 | +function imagify_has_attachments_without_required_metadata( $reset = false ) { | |
| 108 | 126 | global $wpdb; |
| 109 | 127 | static $has; |
| 110 | 128 | |
| 129 | + if ( $reset ) { | |
| 130 | + $has = null; | |
| 131 | + } | |
| 132 | + | |
| 111 | 133 | if ( isset( $has ) ) { |
| 112 | 134 | return $has; |
| 113 | 135 | } |
| 114 | 136 | |
| 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. | |
| 137 | + $mime_types = Imagify_DB::get_mime_types(); | |
| 138 | + $statuses = Imagify_DB::get_post_statuses(); | |
| 139 | + $exist_data = Imagify_DB::get_required_wp_metadata_exist_clause( | |
| 140 | + 'p.ID', | |
| 141 | + false | |
| 142 | + ); | |
| 143 | + $has = (bool) $wpdb->get_var( // WPCS: unprepared SQL ok. | |
| 123 | 144 | " |
| 124 | 145 | SELECT p.ID |
| 125 | 146 | FROM $wpdb->posts AS p |
| 126 | - $nodata_join | |
| 127 | 147 | WHERE p.post_mime_type IN ( $mime_types ) |
| 128 | 148 | AND p.post_type = 'attachment' |
| 129 | 149 | AND p.post_status IN ( $statuses ) |
| 130 | - $nodata_where | |
| 150 | + $exist_data | |
| 131 | 151 | LIMIT 1" |
| 132 | 152 | ); |
| 133 | 153 | |
| 134 | 154 | return $has; |
| @@ -283,18 +303,18 @@ | ||
| 283 | 303 | // All image size names. |
| 284 | 304 | $intermediate_image_sizes = get_intermediate_image_sizes(); |
| 285 | 305 | $intermediate_image_sizes = array_flip( $intermediate_image_sizes ); |
| 286 | 306 | // Additional image size attributes. |
| 287 | - $additional_image_sizes = wp_get_additional_image_sizes(); | |
| 307 | + $additional_image_sizes = wp_get_additional_image_sizes(); | |
| 288 | 308 | |
| 289 | 309 | // Create the full array with sizes and crop info. |
| 290 | 310 | foreach ( $intermediate_image_sizes as $size_name => $s ) { |
| 291 | - $intermediate_image_sizes[ $size_name ] = array( | |
| 311 | + $intermediate_image_sizes[ $size_name ] = [ | |
| 292 | 312 | 'width' => '', |
| 293 | 313 | 'height' => '', |
| 294 | 314 | 'crop' => false, |
| 295 | 315 | 'name' => $size_name, |
| 296 | - ); | |
| 316 | + ]; | |
| 297 | 317 | |
| 298 | 318 | if ( isset( $additional_image_sizes[ $size_name ]['width'] ) ) { |
| 299 | 319 | // For theme-added sizes. |
| 300 | 320 | $intermediate_image_sizes[ $size_name ]['width'] = (int) $additional_image_sizes[ $size_name ]['width']; |