| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || 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 |
static $count; |
| 15 |
|
| 16 |
/** |
| 17 |
* Filter the number of attachments. |
| 18 |
* 3rd party will be able to override the result. |
| 19 |
* |
| 20 |
* @since 1.5 |
| 21 |
* |
| 22 |
* @param int|bool $pre_count Default is false. Provide an integer. |
| 23 |
*/ |
| 24 |
$pre_count = apply_filters( 'imagify_count_attachments', false ); |
| 25 |
|
| 26 |
if ( false !== $pre_count ) { |
| 27 |
return (int) $pre_count; |
| 28 |
} |
| 29 |
|
| 30 |
if ( isset( $count ) ) { |
| 31 |
return $count; |
| 32 |
} |
| 33 |
|
| 34 |
$mime_types = Imagify_DB::get_mime_types(); |
| 35 |
$statuses = Imagify_DB::get_post_statuses(); |
| 36 |
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause(); |
| 37 |
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause(); |
| 38 |
$count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok. |
| 39 |
" |
| 40 |
SELECT COUNT( p.ID ) |
| 41 |
FROM $wpdb->posts AS p |
| 42 |
$nodata_join |
| 43 |
WHERE p.post_mime_type IN ( $mime_types ) |
| 44 |
AND p.post_type = 'attachment' |
| 45 |
AND p.post_status IN ( $statuses ) |
| 46 |
$nodata_where" |
| 47 |
); |
| 48 |
|
| 49 |
if ( $count > imagify_get_unoptimized_attachment_limit() ) { |
| 50 |
set_transient( 'imagify_large_library', 1 ); |
| 51 |
} elseif ( get_transient( 'imagify_large_library' ) ) { |
| 52 |
// In case the number is decreasing under our limit. |
| 53 |
delete_transient( 'imagify_large_library' ); |
| 54 |
} |
| 55 |
|
| 56 |
return $count; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Count number of optimized attachments with an error. |
| 61 |
* |
| 62 |
* @since 1.0 |
| 63 |
* @author Jonathan Buttigieg |
| 64 |
* |
| 65 |
* @return int The number of attachments. |
| 66 |
*/ |
| 67 |
function imagify_count_error_attachments() { |
| 68 |
global $wpdb; |
| 69 |
static $count; |
| 70 |
|
| 71 |
/** |
| 72 |
* Filter the number of optimized attachments with an error. |
| 73 |
* 3rd party will be able to override the result. |
| 74 |
* |
| 75 |
* @since 1.5 |
| 76 |
* |
| 77 |
* @param int|bool $pre_count Default is false. Provide an integer. |
| 78 |
*/ |
| 79 |
$pre_count = apply_filters( 'imagify_count_error_attachments', false ); |
| 80 |
|
| 81 |
if ( false !== $pre_count ) { |
| 82 |
return (int) $pre_count; |
| 83 |
} |
| 84 |
|
| 85 |
if ( isset( $count ) ) { |
| 86 |
return $count; |
| 87 |
} |
| 88 |
|
| 89 |
$mime_types = Imagify_DB::get_mime_types(); |
| 90 |
$statuses = Imagify_DB::get_post_statuses(); |
| 91 |
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause(); |
| 92 |
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause(); |
| 93 |
$count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok. |
| 94 |
" |
| 95 |
SELECT COUNT( DISTINCT p.ID ) |
| 96 |
FROM $wpdb->posts AS p |
| 97 |
$nodata_join |
| 98 |
INNER JOIN $wpdb->postmeta AS mt1 |
| 99 |
ON ( p.ID = mt1.post_id AND mt1.meta_key = '_imagify_status' ) |
| 100 |
WHERE p.post_mime_type IN ( $mime_types ) |
| 101 |
AND p.post_type = 'attachment' |
| 102 |
AND p.post_status IN ( $statuses ) |
| 103 |
AND mt1.meta_value = 'error' |
| 104 |
$nodata_where" |
| 105 |
); |
| 106 |
|
| 107 |
return $count; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Count number of optimized attachments (by Imagify or an other tool before). |
| 112 |
* |
| 113 |
* @since 1.0 |
| 114 |
* @author Jonathan Buttigieg |
| 115 |
* |
| 116 |
* @return int The number of attachments. |
| 117 |
*/ |
| 118 |
function imagify_count_optimized_attachments() { |
| 119 |
global $wpdb; |
| 120 |
static $count; |
| 121 |
|
| 122 |
/** |
| 123 |
* Filter the number of optimized attachments. |
| 124 |
* 3rd party will be able to override the result. |
| 125 |
* |
| 126 |
* @since 1.5 |
| 127 |
* |
| 128 |
* @param int|bool $pre_count Default is false. Provide an integer. |
| 129 |
*/ |
| 130 |
$pre_count = apply_filters( 'imagify_count_optimized_attachments', false ); |
| 131 |
|
| 132 |
if ( false !== $pre_count ) { |
| 133 |
return (int) $pre_count; |
| 134 |
} |
| 135 |
|
| 136 |
if ( isset( $count ) ) { |
| 137 |
return $count; |
| 138 |
} |
| 139 |
|
| 140 |
$mime_types = Imagify_DB::get_mime_types(); |
| 141 |
$statuses = Imagify_DB::get_post_statuses(); |
| 142 |
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause(); |
| 143 |
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause(); |
| 144 |
$count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok. |
| 145 |
" |
| 146 |
SELECT COUNT( DISTINCT p.ID ) |
| 147 |
FROM $wpdb->posts AS p |
| 148 |
$nodata_join |
| 149 |
INNER JOIN $wpdb->postmeta AS mt1 |
| 150 |
ON ( p.ID = mt1.post_id AND mt1.meta_key = '_imagify_status' ) |
| 151 |
WHERE p.post_mime_type IN ( $mime_types ) |
| 152 |
AND p.post_type = 'attachment' |
| 153 |
AND p.post_status IN ( $statuses ) |
| 154 |
AND mt1.meta_value IN ( 'success', 'already_optimized' ) |
| 155 |
$nodata_where" |
| 156 |
); |
| 157 |
|
| 158 |
return $count; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Count number of unoptimized attachments. |
| 163 |
* |
| 164 |
* @since 1.0 |
| 165 |
* @author Jonathan Buttigieg |
| 166 |
* |
| 167 |
* @return int The number of attachments. |
| 168 |
*/ |
| 169 |
function imagify_count_unoptimized_attachments() { |
| 170 |
/** |
| 171 |
* Filter the number of unoptimized attachments. |
| 172 |
* 3rd party will be able to override the result. |
| 173 |
* |
| 174 |
* @since 1.5 |
| 175 |
* |
| 176 |
* @param int|bool $pre_count Default is false. Provide an integer. |
| 177 |
*/ |
| 178 |
$pre_count = apply_filters( 'imagify_count_unoptimized_attachments', false ); |
| 179 |
|
| 180 |
if ( false !== $pre_count ) { |
| 181 |
return (int) $pre_count; |
| 182 |
} |
| 183 |
|
| 184 |
return imagify_count_attachments() - imagify_count_optimized_attachments() - imagify_count_error_attachments(); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Count percent of optimized attachments. |
| 189 |
* |
| 190 |
* @since 1.0 |
| 191 |
* @author Jonathan Buttigieg |
| 192 |
* |
| 193 |
* @return int The percent of optimized attachments. |
| 194 |
*/ |
| 195 |
function imagify_percent_optimized_attachments() { |
| 196 |
/** |
| 197 |
* Filter the percent of optimized attachments. |
| 198 |
* 3rd party will be able to override the result. |
| 199 |
* |
| 200 |
* @since 1.5 |
| 201 |
* |
| 202 |
* @param int|bool $percent Default is false. Provide an integer. |
| 203 |
*/ |
| 204 |
$percent = apply_filters( 'imagify_percent_optimized_attachments', false ); |
| 205 |
|
| 206 |
if ( false !== $percent ) { |
| 207 |
return (int) $percent; |
| 208 |
} |
| 209 |
|
| 210 |
$total_attachments = imagify_count_attachments(); |
| 211 |
$total_optimized_attachments = imagify_count_optimized_attachments(); |
| 212 |
|
| 213 |
if ( ! $total_attachments || ! $total_optimized_attachments ) { |
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
return min( round( 100 * $total_optimized_attachments / $total_attachments ), 100 ); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Count percent, original & optimized size of all images optimized by Imagify. |
| 222 |
* |
| 223 |
* @since 1.0 |
| 224 |
* @since 1.6.7 Revamped to handle huge libraries. |
| 225 |
* @author Jonathan Buttigieg |
| 226 |
* |
| 227 |
* @param string $key What data to return. Choices are between 'count', 'original_size', 'optimized_size', and 'percent'. If left empty, the whole array is returned. |
| 228 |
* @return array|int An array containing the optimization data. A single data if $key is provided. |
| 229 |
*/ |
| 230 |
function imagify_count_saving_data( $key = '' ) { |
| 231 |
global $wpdb; |
| 232 |
|
| 233 |
/** |
| 234 |
* Filter the query to get all optimized attachments. |
| 235 |
* 3rd party will be able to override the result. |
| 236 |
* |
| 237 |
* @since 1.5 |
| 238 |
* @since 1.6.7 This filter should return an array containing the following keys: 'count', 'original_size', and 'optimized_size'. |
| 239 |
* |
| 240 |
* @param bool|array $attachments An array containing the keys ('count', 'original_size', and 'optimized_size'), or an array of attachments (back compat', deprecated), or false. |
| 241 |
*/ |
| 242 |
$attachments = apply_filters( 'imagify_count_saving_data', false ); |
| 243 |
|
| 244 |
$original_size = 0; |
| 245 |
$optimized_size = 0; |
| 246 |
$count = 0; |
| 247 |
|
| 248 |
if ( is_array( $attachments ) ) { |
| 249 |
/** |
| 250 |
* Bypass. |
| 251 |
*/ |
| 252 |
if ( isset( $attachments['count'], $attachments['original_size'], $attachments['optimized_size'] ) ) { |
| 253 |
/** |
| 254 |
* We have the results we need. |
| 255 |
*/ |
| 256 |
$attachments['percent'] = $attachments['optimized_size'] && $attachments['original_size'] ? ceil( ( ( $attachments['original_size'] - $attachments['optimized_size'] ) / $attachments['original_size'] ) * 100 ) : 0; |
| 257 |
|
| 258 |
return $attachments; |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Back compat'. |
| 263 |
* The following shouldn't be used. Sites with a huge library won't like it. |
| 264 |
*/ |
| 265 |
$attachments = array_map( 'maybe_unserialize', (array) $attachments ); |
| 266 |
|
| 267 |
if ( $attachments ) { |
| 268 |
foreach ( $attachments as $attachment_data ) { |
| 269 |
if ( ! $attachment_data ) { |
| 270 |
continue; |
| 271 |
} |
| 272 |
|
| 273 |
++$count; |
| 274 |
$original_data = $attachment_data['sizes']['full']; |
| 275 |
|
| 276 |
// Increment the original sizes. |
| 277 |
$original_size += $original_data['original_size'] ? $original_data['original_size'] : 0; |
| 278 |
$optimized_size += $original_data['optimized_size'] ? $original_data['optimized_size'] : 0; |
| 279 |
|
| 280 |
unset( $attachment_data['sizes']['full'] ); |
| 281 |
|
| 282 |
// Increment the thumbnails sizes. |
| 283 |
if ( $attachment_data['sizes'] ) { |
| 284 |
foreach ( $attachment_data['sizes'] as $size_data ) { |
| 285 |
if ( ! empty( $size_data['success'] ) ) { |
| 286 |
$original_size += $size_data['original_size'] ? $size_data['original_size'] : 0; |
| 287 |
$optimized_size += $size_data['optimized_size'] ? $size_data['optimized_size'] : 0; |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
} else { |
| 294 |
/** |
| 295 |
* Filter the chunk size of the requests fetching the data. |
| 296 |
* 15,000 seems to be a good balance between memory used, speed, and number of DB hits. |
| 297 |
* |
| 298 |
* @param int $limit The maximum number of elements per chunk. |
| 299 |
*/ |
| 300 |
$limit = apply_filters( 'imagify_count_saving_data_limit', 15000 ); |
| 301 |
$limit = absint( $limit ); |
| 302 |
|
| 303 |
$mime_types = Imagify_DB::get_mime_types(); |
| 304 |
$statuses = Imagify_DB::get_post_statuses(); |
| 305 |
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause(); |
| 306 |
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause(); |
| 307 |
$attachment_ids = $wpdb->get_col( // WPCS: unprepared SQL ok. |
| 308 |
" |
| 309 |
SELECT p.ID |
| 310 |
FROM $wpdb->posts AS p |
| 311 |
$nodata_join |
| 312 |
INNER JOIN $wpdb->postmeta AS mt1 |
| 313 |
ON ( p.ID = mt1.post_id AND mt1.meta_key = '_imagify_status' ) |
| 314 |
WHERE p.post_mime_type IN ( $mime_types ) |
| 315 |
AND p.post_type = 'attachment' |
| 316 |
AND p.post_status IN ( $statuses ) |
| 317 |
AND mt1.meta_value = 'success' |
| 318 |
$nodata_where |
| 319 |
ORDER BY CAST( p.ID AS UNSIGNED )" |
| 320 |
); |
| 321 |
$wpdb->flush(); |
| 322 |
|
| 323 |
$attachment_ids = array_map( 'absint', array_unique( $attachment_ids ) ); |
| 324 |
$attachment_ids = array_chunk( $attachment_ids, $limit ); |
| 325 |
|
| 326 |
while ( $attachment_ids ) { |
| 327 |
$limit_ids = array_shift( $attachment_ids ); |
| 328 |
$limit_ids = implode( ',', $limit_ids ); |
| 329 |
|
| 330 |
$attachments = $wpdb->get_col( // WPCS: unprepared SQL ok. |
| 331 |
" |
| 332 |
SELECT meta_value |
| 333 |
FROM $wpdb->postmeta |
| 334 |
WHERE post_id IN ( $limit_ids ) |
| 335 |
AND meta_key = '_imagify_data'" |
| 336 |
); |
| 337 |
$wpdb->flush(); |
| 338 |
|
| 339 |
unset( $limit_ids ); |
| 340 |
|
| 341 |
if ( ! $attachments ) { |
| 342 |
// Uh?! |
| 343 |
continue; |
| 344 |
} |
| 345 |
|
| 346 |
$attachments = array_map( 'maybe_unserialize', $attachments ); |
| 347 |
|
| 348 |
foreach ( $attachments as $attachment_data ) { |
| 349 |
if ( ! $attachment_data ) { |
| 350 |
continue; |
| 351 |
} |
| 352 |
|
| 353 |
if ( empty( $attachment_data['sizes']['full']['success'] ) ) { |
| 354 |
/** |
| 355 |
* - Case where this attachment has multiple '_imagify_status' metas, and is fetched (in the above query) as a "success" while the '_imagify_data' says otherwise. |
| 356 |
* - Case where this meta has no "full" entry. |
| 357 |
* Don't ask how it's possible, I don't know ¯\(°_o)/¯ |
| 358 |
*/ |
| 359 |
continue; |
| 360 |
} |
| 361 |
|
| 362 |
$original_data = $attachment_data['sizes']['full']; |
| 363 |
|
| 364 |
++$count; |
| 365 |
|
| 366 |
// Increment the original sizes. |
| 367 |
$original_size += ! empty( $original_data['original_size'] ) ? $original_data['original_size'] : 0; |
| 368 |
$optimized_size += ! empty( $original_data['optimized_size'] ) ? $original_data['optimized_size'] : 0; |
| 369 |
|
| 370 |
unset( $attachment_data['sizes']['full'], $original_data ); |
| 371 |
|
| 372 |
// Increment the thumbnails sizes. |
| 373 |
if ( $attachment_data['sizes'] ) { |
| 374 |
foreach ( $attachment_data['sizes'] as $size_data ) { |
| 375 |
if ( ! empty( $size_data['success'] ) ) { |
| 376 |
$original_size += ! empty( $size_data['original_size'] ) ? $size_data['original_size'] : 0; |
| 377 |
$optimized_size += ! empty( $size_data['optimized_size'] ) ? $size_data['optimized_size'] : 0; |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
unset( $size_data ); |
| 383 |
} |
| 384 |
|
| 385 |
unset( $attachments, $attachment_data ); |
| 386 |
} // End while(). |
| 387 |
} // End if(). |
| 388 |
|
| 389 |
$data = array( |
| 390 |
'count' => $count, |
| 391 |
'original_size' => $original_size, |
| 392 |
'optimized_size' => $optimized_size, |
| 393 |
'percent' => $original_size && $optimized_size ? ceil( ( ( $original_size - $optimized_size ) / $original_size ) * 100 ) : 0, |
| 394 |
); |
| 395 |
|
| 396 |
if ( ! empty( $key ) ) { |
| 397 |
return isset( $data[ $key ] ) ? $data[ $key ] : 0; |
| 398 |
} |
| 399 |
|
| 400 |
return $data; |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Returns the estimated total size of the images not optimized. |
| 405 |
* |
| 406 |
* We estimate the total size of the images in the library by getting the latest 250 images and their thumbnails |
| 407 |
* add up their filesizes, and doing some maths to get the total size. |
| 408 |
* |
| 409 |
* @since 1.6 |
| 410 |
* @author Remy Perona |
| 411 |
* |
| 412 |
* @return int The current estimated total size of images not optimized. |
| 413 |
*/ |
| 414 |
function imagify_calculate_total_size_images_library() { |
| 415 |
global $wpdb; |
| 416 |
|
| 417 |
$mime_types = Imagify_DB::get_mime_types(); |
| 418 |
$statuses = Imagify_DB::get_post_statuses(); |
| 419 |
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause(); |
| 420 |
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause(); |
| 421 |
$image_ids = $wpdb->get_col( // WPCS: unprepared SQL ok. |
| 422 |
" |
| 423 |
SELECT p.ID |
| 424 |
FROM $wpdb->posts AS p |
| 425 |
$nodata_join |
| 426 |
WHERE p.post_mime_type IN ( $mime_types ) |
| 427 |
AND p.post_type = 'attachment' |
| 428 |
AND p.post_status IN ( $statuses ) |
| 429 |
$nodata_where |
| 430 |
LIMIT 250 |
| 431 |
" ); |
| 432 |
|
| 433 |
if ( ! $image_ids ) { |
| 434 |
return 0; |
| 435 |
} |
| 436 |
|
| 437 |
$count_latest_images = count( $image_ids ); |
| 438 |
$count_total_images = imagify_count_attachments(); |
| 439 |
|
| 440 |
return imagify_calculate_total_image_size( $image_ids, $count_latest_images, $count_total_images ); |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* Returns the estimated average size of the images uploaded per month. |
| 445 |
* |
| 446 |
* We estimate the average size of the images uploaded in the library per month by getting the latest 250 images and their thumbnails |
| 447 |
* for the 3 latest months, add up their filesizes, and doing some maths to get the total average size. |
| 448 |
* |
| 449 |
* @since 1.6 |
| 450 |
* @since 1.7 Use wpdb instead of WP_Query. |
| 451 |
* @author Remy Perona |
| 452 |
* |
| 453 |
* @return int The current estimated average size of images uploaded per month. |
| 454 |
*/ |
| 455 |
function imagify_calculate_average_size_images_per_month() { |
| 456 |
global $wpdb; |
| 457 |
|
| 458 |
$mime_types = Imagify_DB::get_mime_types(); |
| 459 |
$statuses = Imagify_DB::get_post_statuses(); |
| 460 |
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause( "$wpdb->posts.ID" ); |
| 461 |
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause(); |
| 462 |
$limit = ' LIMIT 0, 250'; |
| 463 |
$query = " |
| 464 |
SELECT $wpdb->posts.ID |
| 465 |
FROM $wpdb->posts |
| 466 |
$nodata_join |
| 467 |
WHERE $wpdb->posts.post_mime_type IN ( $mime_types ) |
| 468 |
AND $wpdb->posts.post_type = 'attachment' |
| 469 |
AND $wpdb->posts.post_status IN ( $statuses ) |
| 470 |
$nodata_where |
| 471 |
%date_query%"; |
| 472 |
|
| 473 |
// Queries per month. |
| 474 |
$date_query = new WP_Date_Query( array( |
| 475 |
array( |
| 476 |
'before' => 'now', |
| 477 |
'after' => '1 month ago', |
| 478 |
), |
| 479 |
) ); |
| 480 |
|
| 481 |
$partial_images_uploaded_last_month = $wpdb->get_col( str_replace( '%date_query%', $date_query->get_sql(), $query . $limit ) ); // WPCS: unprepared SQL ok. |
| 482 |
|
| 483 |
$date_query = new WP_Date_Query( array( |
| 484 |
array( |
| 485 |
'before' => '1 month ago', |
| 486 |
'after' => '2 months ago', |
| 487 |
), |
| 488 |
) ); |
| 489 |
|
| 490 |
$partial_images_uploaded_two_months_ago = $wpdb->get_col( str_replace( '%date_query%', $date_query->get_sql(), $query . $limit ) ); // WPCS: unprepared SQL ok. |
| 491 |
|
| 492 |
$date_query = new WP_Date_Query( array( |
| 493 |
array( |
| 494 |
'before' => '2 month ago', |
| 495 |
'after' => '3 months ago', |
| 496 |
), |
| 497 |
) ); |
| 498 |
|
| 499 |
$partial_images_uploaded_three_months_ago = $wpdb->get_col( str_replace( '%date_query%', $date_query->get_sql(), $query . $limit ) ); // WPCS: unprepared SQL ok. |
| 500 |
|
| 501 |
// Total for the 3 months. |
| 502 |
$partial_images_uploaded_id = array_merge( $partial_images_uploaded_last_month, $partial_images_uploaded_two_months_ago, $partial_images_uploaded_three_months_ago ); |
| 503 |
|
| 504 |
if ( ! $partial_images_uploaded_id ) { |
| 505 |
return 0; |
| 506 |
} |
| 507 |
|
| 508 |
// Total for the 3 months, without the "250" limit. |
| 509 |
$date_query = new WP_Date_Query( array( |
| 510 |
array( |
| 511 |
'before' => 'now', |
| 512 |
'after' => '3 month ago', |
| 513 |
), |
| 514 |
) ); |
| 515 |
|
| 516 |
$images_uploaded_id = $wpdb->get_col( str_replace( '%date_query%', $date_query->get_sql(), $query ) ); // WPCS: unprepared SQL ok. |
| 517 |
|
| 518 |
if ( ! $images_uploaded_id ) { |
| 519 |
return 0; |
| 520 |
} |
| 521 |
|
| 522 |
// Number of image attachments uploaded for the 3 latest months, limited to 250 per month. |
| 523 |
$partial_total_images_uploaded = count( $partial_images_uploaded_id ); |
| 524 |
// Total number of image attachments uploaded for the 3 latest months. |
| 525 |
$total_images_uploaded = count( $images_uploaded_id ); |
| 526 |
|
| 527 |
return imagify_calculate_total_image_size( $partial_images_uploaded_id, $partial_total_images_uploaded, $total_images_uploaded ) / 3; |
| 528 |
} |
| 529 |
|
| 530 |
/** |
| 531 |
* Returns the estimated total size of images. |
| 532 |
* |
| 533 |
* @since 1.6 |
| 534 |
* @author Remy Perona |
| 535 |
* |
| 536 |
* @param array $image_ids Array of image IDs. |
| 537 |
* @param int $partial_total_images The number of image attachments we're doing the calculation with. |
| 538 |
* @param int $total_images The total number of image attachments. |
| 539 |
* @return int The estimated total size of images. |
| 540 |
*/ |
| 541 |
function imagify_calculate_total_image_size( $image_ids, $partial_total_images, $total_images ) { |
| 542 |
global $wpdb; |
| 543 |
|
| 544 |
$image_ids = array_filter( array_map( 'absint', $image_ids ) ); |
| 545 |
|
| 546 |
if ( ! $image_ids ) { |
| 547 |
return 0; |
| 548 |
} |
| 549 |
|
| 550 |
$results = Imagify_DB::get_metas( array( |
| 551 |
// Get attachments filename. |
| 552 |
'filenames' => '_wp_attached_file', |
| 553 |
// Get attachments data. |
| 554 |
'data' => '_wp_attachment_metadata', |
| 555 |
// Get Imagify data. |
| 556 |
'imagify_data' => '_imagify_data', |
| 557 |
// Get attachments status. |
| 558 |
'statuses' => '_imagify_status', |
| 559 |
), $image_ids ); |
| 560 |
|
| 561 |
// Number of image attachments we're doing the calculation with. In case array_filter() removed results. |
| 562 |
$partial_total_images = count( $image_ids ); |
| 563 |
// Total size of unoptimized size. |
| 564 |
$partial_size_images = 0; |
| 565 |
// Total number of thumbnails. |
| 566 |
$partial_total_intermediate_images = 0; |
| 567 |
|
| 568 |
$filesystem = imagify_get_filesystem(); |
| 569 |
$is_active_for_network = imagify_is_active_for_network(); |
| 570 |
$disallowed_sizes = get_imagify_option( 'disallowed-sizes' ); |
| 571 |
|
| 572 |
foreach ( $image_ids as $i => $image_id ) { |
| 573 |
$attachment_status = isset( $results['statuses'][ $image_id ] ) ? $results['statuses'][ $image_id ] : false; |
| 574 |
|
| 575 |
if ( 'success' === $attachment_status ) { |
| 576 |
/** |
| 577 |
* The image files have been optimized. |
| 578 |
*/ |
| 579 |
// Original size. |
| 580 |
$partial_size_images += isset( $results['imagify_data'][ $image_id ]['stats']['original_size'] ) ? $results['imagify_data'][ $image_id ]['stats']['original_size'] : 0; |
| 581 |
// Number of thumbnails. |
| 582 |
$partial_total_intermediate_images += count( $results['imagify_data'][ $image_id ]['sizes'] ); |
| 583 |
unset( |
| 584 |
$image_ids[ $i ], |
| 585 |
$results['filenames'][ $image_id ], |
| 586 |
$results['data'][ $image_id ], |
| 587 |
$results['imagify_data'][ $image_id ], |
| 588 |
$results['statuses'][ $image_id ] |
| 589 |
); |
| 590 |
continue; |
| 591 |
} |
| 592 |
|
| 593 |
/** |
| 594 |
* The image files are not optimized. |
| 595 |
*/ |
| 596 |
// Create an array containing all this attachment files. |
| 597 |
$files = array( |
| 598 |
'full' => get_imagify_attached_file( $results['filenames'][ $image_id ] ), |
| 599 |
); |
| 600 |
|
| 601 |
$sizes = isset( $results['data'][ $image_id ]['sizes'] ) ? $results['data'][ $image_id ]['sizes'] : array(); |
| 602 |
|
| 603 |
if ( $sizes && is_array( $sizes ) ) { |
| 604 |
if ( ! $is_active_for_network ) { |
| 605 |
$sizes = array_diff_key( $sizes, $disallowed_sizes ); |
| 606 |
} |
| 607 |
|
| 608 |
if ( $sizes ) { |
| 609 |
$full_dirname = $filesystem->dir_path( $files['full'] ); |
| 610 |
|
| 611 |
foreach ( $sizes as $size_key => $size_data ) { |
| 612 |
$files[ $size_key ] = $full_dirname . '/' . $size_data['file']; |
| 613 |
} |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
/** |
| 618 |
* Allow to provide all files size and the number of thumbnails. |
| 619 |
* |
| 620 |
* @since 1.6.7 |
| 621 |
* @author Grégory Viguier |
| 622 |
* |
| 623 |
* @param bool $size_and_count False by default. |
| 624 |
* @param int $image_id The attachment ID. |
| 625 |
* @param array $files An array of file paths with thumbnail sizes as keys. |
| 626 |
* @param array $image_ids An array of all attachment IDs. |
| 627 |
* @return bool|array False by default. Provide an array with the keys 'filesize' (containing the total filesize) and 'thumbnails' (containing the number of thumbnails). |
| 628 |
*/ |
| 629 |
$size_and_count = apply_filters( 'imagify_total_attachment_filesize', false, $image_id, $files, $image_ids ); |
| 630 |
|
| 631 |
if ( is_array( $size_and_count ) ) { |
| 632 |
$partial_size_images += $size_and_count['filesize']; |
| 633 |
$partial_total_intermediate_images += $size_and_count['thumbnails']; |
| 634 |
} else { |
| 635 |
foreach ( $files as $file ) { |
| 636 |
if ( $filesystem->exists( $file ) ) { |
| 637 |
$partial_size_images += $filesystem->size( $file ); |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
unset( $files['full'] ); |
| 642 |
$partial_total_intermediate_images += count( $files ); |
| 643 |
} |
| 644 |
|
| 645 |
unset( |
| 646 |
$image_ids[ $i ], |
| 647 |
$results['filenames'][ $image_id ], |
| 648 |
$results['data'][ $image_id ], |
| 649 |
$results['imagify_data'][ $image_id ], |
| 650 |
$results['statuses'][ $image_id ] |
| 651 |
); |
| 652 |
} // End foreach(). |
| 653 |
|
| 654 |
// Number of thumbnails per attachment = Number of thumbnails / Number of attachments. |
| 655 |
$intermediate_images_per_image = $partial_total_intermediate_images / $partial_total_images; |
| 656 |
/** |
| 657 |
* Note: Number of attachments ($partial_total_images) === Number of full sizes. |
| 658 |
* Average image size = Size of the images / ( Number of full sizes + Number of thumbnails ). |
| 659 |
* Average image size = Size of the images / Number of images. |
| 660 |
*/ |
| 661 |
$average_size_images = $partial_size_images / ( $partial_total_images + $partial_total_intermediate_images ); |
| 662 |
/** |
| 663 |
* Note: Total number of attachments ($total_images) === Total number of full sizes. |
| 664 |
* Total images size = Average image size * ( Total number of full sizes + ( Number of thumbnails per attachment * Total number of attachments ) ). |
| 665 |
* Total images size = Average image size * ( Total number of full sizes + Total number of thumbnails ). |
| 666 |
*/ |
| 667 |
$total_size_images = $average_size_images * ( $total_images + ( $intermediate_images_per_image * $total_images ) ); |
| 668 |
|
| 669 |
return $total_size_images; |
| 670 |
} |
| 671 |
|
| 672 |
/** |
| 673 |
* Get all generic stats to be used in the bulk optimization page. |
| 674 |
* |
| 675 |
* @since 1.7.1 |
| 676 |
* @author Grégory Viguier |
| 677 |
* |
| 678 |
* @param array $types The folder types. If a folder type is "library", the context should be suffixed after a pipe character. They are passed as array keys. |
| 679 |
* @param array $args { |
| 680 |
* Optional. An array of arguments. |
| 681 |
* |
| 682 |
* @type bool $fullset True to return the full set of data. False to return only the main data. |
| 683 |
* @type bool $formatting Some of the data is returned formatted. |
| 684 |
* } |
| 685 |
* @return array |
| 686 |
*/ |
| 687 |
function imagify_get_bulk_stats( $types, $args = array() ) { |
| 688 |
$types = $types && is_array( $types ) ? $types : array(); |
| 689 |
$args = array_merge( array( |
| 690 |
'fullset' => false, |
| 691 |
'formatting' => true, |
| 692 |
), (array) $args ); |
| 693 |
|
| 694 |
$data = array( |
| 695 |
// Global chart. |
| 696 |
'total_attachments' => 0, |
| 697 |
'unoptimized_attachments' => 0, |
| 698 |
'optimized_attachments' => 0, |
| 699 |
'errors_attachments' => 0, |
| 700 |
// Stats block. |
| 701 |
'already_optimized_attachments' => 0, |
| 702 |
'original_human' => 0, |
| 703 |
'optimized_human' => 0, |
| 704 |
); |
| 705 |
|
| 706 |
if ( isset( $types['library|wp'] ) ) { |
| 707 |
/** |
| 708 |
* Library. |
| 709 |
*/ |
| 710 |
$saving_data = imagify_count_saving_data(); |
| 711 |
|
| 712 |
// Global chart. |
| 713 |
$data['total_attachments'] += imagify_count_attachments(); |
| 714 |
$data['unoptimized_attachments'] += imagify_count_unoptimized_attachments(); |
| 715 |
$data['optimized_attachments'] += imagify_count_optimized_attachments(); |
| 716 |
$data['errors_attachments'] += imagify_count_error_attachments(); |
| 717 |
// Stats block. |
| 718 |
$data['already_optimized_attachments'] += $saving_data['count']; |
| 719 |
$data['original_human'] += $saving_data['original_size']; |
| 720 |
$data['optimized_human'] += $saving_data['optimized_size']; |
| 721 |
} |
| 722 |
|
| 723 |
if ( isset( $types['custom-folders|custom-folders'] ) ) { |
| 724 |
/** |
| 725 |
* Custom folders. |
| 726 |
*/ |
| 727 |
// Global chart. |
| 728 |
$data['total_attachments'] += Imagify_Files_Stats::count_all_files(); |
| 729 |
$data['unoptimized_attachments'] += Imagify_Files_Stats::count_no_status_files(); |
| 730 |
$data['optimized_attachments'] += Imagify_Files_Stats::count_optimized_files(); |
| 731 |
$data['errors_attachments'] += Imagify_Files_Stats::count_error_files(); |
| 732 |
// Stats block. |
| 733 |
$data['already_optimized_attachments'] += Imagify_Files_Stats::count_success_files(); |
| 734 |
$data['original_human'] += Imagify_Files_Stats::get_original_size(); |
| 735 |
$data['optimized_human'] += Imagify_Files_Stats::get_optimized_size(); |
| 736 |
} |
| 737 |
|
| 738 |
/** |
| 739 |
* Full set of data. |
| 740 |
*/ |
| 741 |
if ( $args['fullset'] ) { |
| 742 |
// User account. |
| 743 |
$views = Imagify_Views::get_instance(); |
| 744 |
|
| 745 |
$data['unconsumed_quota'] = $views->get_quota_percent(); |
| 746 |
$data['quota_class'] = $views->get_quota_class(); |
| 747 |
$data['quota_icon'] = $views->get_quota_icon(); |
| 748 |
} |
| 749 |
|
| 750 |
/** |
| 751 |
* Filter the generic stats used in the bulk optimization page. |
| 752 |
* |
| 753 |
* @since 1.7.1 |
| 754 |
* @author Grégory Viguier |
| 755 |
* |
| 756 |
* @param array $data The data. |
| 757 |
* @param array $types The folder types. They are passed as array keys. |
| 758 |
* @param array $args { |
| 759 |
* Optional. An array of arguments. |
| 760 |
* |
| 761 |
* @type bool $fullset True to return the full set of data. False to return only the main data. |
| 762 |
* @type bool $formatting Some of the data is returned formatted. |
| 763 |
* } |
| 764 |
*/ |
| 765 |
$data = apply_filters( 'imagify_bulk_stats', $data, $types, $args ); |
| 766 |
|
| 767 |
/** |
| 768 |
* Percentages. |
| 769 |
*/ |
| 770 |
if ( $data['total_attachments'] && $data['optimized_attachments'] ) { |
| 771 |
$data['optimized_attachments_percent'] = round( 100 * $data['optimized_attachments'] / $data['total_attachments'] ); |
| 772 |
} else { |
| 773 |
$data['optimized_attachments_percent'] = 0; |
| 774 |
} |
| 775 |
|
| 776 |
if ( $data['original_human'] && $data['optimized_human'] ) { |
| 777 |
$data['optimized_percent'] = ceil( 100 - ( 100 * $data['optimized_human'] / $data['original_human'] ) ); |
| 778 |
} else { |
| 779 |
$data['optimized_percent'] = 0; |
| 780 |
} |
| 781 |
|
| 782 |
/** |
| 783 |
* Formating. |
| 784 |
*/ |
| 785 |
if ( $args['formatting'] ) { |
| 786 |
$data['already_optimized_attachments'] = number_format_i18n( $data['already_optimized_attachments'] ); |
| 787 |
$data['original_human'] = imagify_size_format( $data['original_human'], 1 ); |
| 788 |
$data['optimized_human'] = imagify_size_format( $data['optimized_human'], 1 ); |
| 789 |
} |
| 790 |
|
| 791 |
return $data; |
| 792 |
} |
| 793 |
|