| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' ); |
| 3 |
|
| 4 |
/* |
| 5 |
* Count number of attachments. |
| 6 |
* |
| 7 |
* @since 1.0 |
| 8 |
* @author Jonathan Buttigieg |
| 9 |
* |
| 10 |
* @return int The number of attachments. |
| 11 |
*/ |
| 12 |
function imagify_count_attachments() { |
| 13 |
global $wpdb; |
| 14 |
|
| 15 |
/** |
| 16 |
* Filter the number of attachments |
| 17 |
* 3rd party will be able to override the result |
| 18 |
* |
| 19 |
* @since 1.5 |
| 20 |
*/ |
| 21 |
$count = apply_filters( 'imagify_count_attachments', false ); |
| 22 |
|
| 23 |
if ( false !== $count ) { |
| 24 |
return $count; |
| 25 |
} |
| 26 |
|
| 27 |
static $count; |
| 28 |
|
| 29 |
if ( ! $count ) { |
| 30 |
$count = $wpdb->get_var( |
| 31 |
"SELECT COUNT($wpdb->posts.ID) |
| 32 |
FROM $wpdb->posts |
| 33 |
WHERE post_type = 'attachment' |
| 34 |
AND post_status != 'trash' |
| 35 |
AND ($wpdb->posts.post_mime_type = 'image/jpeg' OR $wpdb->posts.post_mime_type = 'image/png' OR $wpdb->posts.post_mime_type = 'image/gif')" |
| 36 |
); |
| 37 |
|
| 38 |
if ( $count > apply_filters( 'imagify_unoptimized_attachment_limit', 10000 ) ) { |
| 39 |
set_transient( 'imagify_large_library', 1 ); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
return (int) $count; |
| 44 |
} |
| 45 |
|
| 46 |
/* |
| 47 |
* Count number of optimized attachments with an error. |
| 48 |
* |
| 49 |
* @since 1.0 |
| 50 |
* @author Jonathan Buttigieg |
| 51 |
* |
| 52 |
* @return int The number of attachments. |
| 53 |
*/ |
| 54 |
function imagify_count_error_attachments() { |
| 55 |
global $wpdb; |
| 56 |
|
| 57 |
/** |
| 58 |
* Filter the number of optimized attachments with an error |
| 59 |
* 3rd party will be able to override the result |
| 60 |
* |
| 61 |
* @since 1.5 |
| 62 |
*/ |
| 63 |
$count = apply_filters( 'imagify_count_error_attachments', false ); |
| 64 |
|
| 65 |
if ( false !== $count ) { |
| 66 |
return $count; |
| 67 |
} |
| 68 |
|
| 69 |
static $count; |
| 70 |
|
| 71 |
if ( ! $count ) { |
| 72 |
$count = $wpdb->get_var( |
| 73 |
"SELECT COUNT($wpdb->posts.ID) |
| 74 |
FROM $wpdb->posts |
| 75 |
INNER JOIN $wpdb->postmeta |
| 76 |
ON $wpdb->posts.ID = $wpdb->postmeta.post_id |
| 77 |
WHERE ($wpdb->posts.post_mime_type = 'image/jpeg' OR $wpdb->posts.post_mime_type = 'image/png' OR $wpdb->posts.post_mime_type = 'image/gif') |
| 78 |
AND ( ( $wpdb->postmeta.meta_key = '_imagify_status' AND CAST($wpdb->postmeta.meta_value AS CHAR) = 'error' ) |
| 79 |
) |
| 80 |
AND $wpdb->posts.post_type = 'attachment' |
| 81 |
AND $wpdb->posts.post_status = 'inherit'" |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
return (int) $count; |
| 86 |
} |
| 87 |
|
| 88 |
/* |
| 89 |
* Count number of optimized attachments (by Imagify or an other tool before). |
| 90 |
* |
| 91 |
* @since 1.0 |
| 92 |
* @author Jonathan Buttigieg |
| 93 |
* |
| 94 |
* @return int The number of attachments. |
| 95 |
*/ |
| 96 |
function imagify_count_optimized_attachments() { |
| 97 |
global $wpdb; |
| 98 |
|
| 99 |
/** |
| 100 |
* Filter the number of optimized attachments |
| 101 |
* 3rd party will be able to override the result |
| 102 |
* |
| 103 |
* @since 1.5 |
| 104 |
*/ |
| 105 |
$count = apply_filters( 'imagify_count_optimized_attachments', false ); |
| 106 |
|
| 107 |
if ( false !== $count ) { |
| 108 |
return $count; |
| 109 |
} |
| 110 |
|
| 111 |
static $count; |
| 112 |
|
| 113 |
if ( ! $count ) { |
| 114 |
$count = $wpdb->get_var( |
| 115 |
"SELECT COUNT($wpdb->posts.ID) |
| 116 |
FROM $wpdb->posts |
| 117 |
INNER JOIN $wpdb->postmeta |
| 118 |
ON $wpdb->posts.ID = $wpdb->postmeta.post_id |
| 119 |
WHERE ($wpdb->posts.post_mime_type = 'image/jpeg' OR $wpdb->posts.post_mime_type = 'image/png' OR $wpdb->posts.post_mime_type = 'image/gif') |
| 120 |
AND ( ( $wpdb->postmeta.meta_key = '_imagify_status' AND CAST($wpdb->postmeta.meta_value AS CHAR) = 'success' ) OR ( $wpdb->postmeta.meta_key = '_imagify_status' AND CAST($wpdb->postmeta.meta_value AS CHAR) = 'already_optimized' ) ) |
| 121 |
AND $wpdb->posts.post_type = 'attachment' |
| 122 |
AND $wpdb->posts.post_status = 'inherit'" |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
return (int) $count; |
| 127 |
} |
| 128 |
|
| 129 |
/* |
| 130 |
* Count number of unoptimized attachments. |
| 131 |
* |
| 132 |
* @since 1.0 |
| 133 |
* @author Jonathan Buttigieg |
| 134 |
* |
| 135 |
* @return int The number of attachments. |
| 136 |
*/ |
| 137 |
function imagify_count_unoptimized_attachments() { |
| 138 |
/** |
| 139 |
* Filter the number of unoptimized attachments |
| 140 |
* 3rd party will be able to override the result |
| 141 |
* |
| 142 |
* @since 1.5 |
| 143 |
*/ |
| 144 |
$count = apply_filters( 'imagify_count_unoptimized_attachments', false ); |
| 145 |
|
| 146 |
if ( false !== $count ) { |
| 147 |
return $count; |
| 148 |
} |
| 149 |
|
| 150 |
static $count; |
| 151 |
|
| 152 |
if ( ! $count ) { |
| 153 |
$count = imagify_count_attachments() - imagify_count_optimized_attachments() - imagify_count_error_attachments(); |
| 154 |
} |
| 155 |
|
| 156 |
return (int) $count; |
| 157 |
} |
| 158 |
|
| 159 |
/* |
| 160 |
* Count percent of optimized attachments. |
| 161 |
* |
| 162 |
* @since 1.0 |
| 163 |
* @author Jonathan Buttigieg |
| 164 |
* |
| 165 |
* @return int The percent of optimized attachments. |
| 166 |
*/ |
| 167 |
function imagify_percent_optimized_attachments() { |
| 168 |
/** |
| 169 |
* Filter the percent of optimized attachments |
| 170 |
* 3rd party will be able to override the result |
| 171 |
* |
| 172 |
* @since 1.5 |
| 173 |
*/ |
| 174 |
$percent = apply_filters( 'imagify_percent_optimized_attachments', false ); |
| 175 |
|
| 176 |
if ( false === $percent ) { |
| 177 |
$total_attachments = imagify_count_attachments(); |
| 178 |
$total_optimized_attachments = imagify_count_optimized_attachments(); |
| 179 |
|
| 180 |
$percent = ( 0 !== $total_attachments ) ? round( ( 100 - ( ( $total_attachments - ( $total_optimized_attachments ) ) / $total_attachments ) * 100 ) ) : 0; |
| 181 |
} |
| 182 |
|
| 183 |
return $percent; |
| 184 |
} |
| 185 |
|
| 186 |
/* |
| 187 |
* Count percent, original & optimized size of all images optimized by Imagify. |
| 188 |
* |
| 189 |
* @since 1.0 |
| 190 |
* @author Jonathan Buttigieg |
| 191 |
* |
| 192 |
* @return array An array containing the optimization data. |
| 193 |
*/ |
| 194 |
function imagify_count_saving_data( $key = '' ) { |
| 195 |
global $wpdb; |
| 196 |
|
| 197 |
/** |
| 198 |
* Filter the query to get all optimized attachments |
| 199 |
* 3rd party will be able to override the result |
| 200 |
* |
| 201 |
* @since 1.5 |
| 202 |
*/ |
| 203 |
$attachments = apply_filters( 'imagify_count_saving_data', false ); |
| 204 |
|
| 205 |
if ( false === $attachments ) { |
| 206 |
$attachments = $wpdb->get_col( |
| 207 |
"SELECT pm1.meta_value |
| 208 |
FROM $wpdb->postmeta as pm1 |
| 209 |
INNER JOIN $wpdb->postmeta as pm2 |
| 210 |
ON pm1.post_id = pm2.post_id |
| 211 |
WHERE pm1.meta_key= '_imagify_data' |
| 212 |
AND ( pm2.meta_key= '_imagify_status' AND pm2.meta_value= 'success' )" |
| 213 |
); |
| 214 |
} |
| 215 |
|
| 216 |
$attachments = array_map( 'maybe_unserialize', (array) $attachments ); |
| 217 |
|
| 218 |
$original_size = 0; |
| 219 |
$optimized_size = 0; |
| 220 |
$count = 0; |
| 221 |
|
| 222 |
foreach( $attachments as $attachment_data ) { |
| 223 |
$stats_data = $attachment_data['stats']; |
| 224 |
$original_data = $attachment_data['sizes']['full']; |
| 225 |
|
| 226 |
// Incremente the original sizes |
| 227 |
$original_size += ( $original_data['original_size'] ) ? $original_data['original_size'] : 0; |
| 228 |
$optimized_size += ( $original_data['optimized_size'] ) ? $original_data['optimized_size'] : 0; |
| 229 |
|
| 230 |
unset( $attachment_data['sizes']['full'] ); |
| 231 |
|
| 232 |
// Incremente the thumbnails sizes |
| 233 |
foreach ( $attachment_data['sizes'] as $size_key => $size_data ) { |
| 234 |
if ( ! empty( $size_data['success'] ) ) { |
| 235 |
$original_size += ( $size_data['original_size'] ) ? $size_data['original_size'] : 0; |
| 236 |
$optimized_size += ( $size_data['optimized_size'] ) ? $size_data['optimized_size'] : 0; |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
$data = array( |
| 242 |
'count' => count( $attachments ), |
| 243 |
'original_size' => (int) $original_size, |
| 244 |
'optimized_size' => (int) $optimized_size, |
| 245 |
'percent' => ( 0 !== $optimized_size ) ? ceil( ( ( $original_size - $optimized_size ) / $original_size ) * 100 ) : 0 |
| 246 |
); |
| 247 |
|
| 248 |
if ( ! empty( $key ) ) { |
| 249 |
return $data[ $key ]; |
| 250 |
} |
| 251 |
|
| 252 |
return $data; |
| 253 |
} |