| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' ); |
| 3 |
|
| 4 |
/* |
| 5 |
* Count number of attachments. |
| 6 |
* |
| 7 |
* @since 1.0 |
| 8 |
* |
| 9 |
* @return int The number of attachments. |
| 10 |
*/ |
| 11 |
function imagify_count_attachments() { |
| 12 |
$count = wp_count_attachments( get_imagify_mime_type() ); |
| 13 |
$count = get_object_vars( $count ); |
| 14 |
$count = array_sum( $count ); |
| 15 |
return (int) $count; |
| 16 |
} |
| 17 |
|
| 18 |
/* |
| 19 |
* Count number of execeed attachments (size > 5MB). |
| 20 |
* |
| 21 |
* @since 1.0 |
| 22 |
* |
| 23 |
* @return int The number of exceeded attachments. |
| 24 |
*/ |
| 25 |
function imagify_count_exceeding_attachments() { |
| 26 |
$count = 0; |
| 27 |
$query = new WP_Query( |
| 28 |
array( |
| 29 |
'post_type' => 'attachment', |
| 30 |
'post_status' => 'inherit', |
| 31 |
'post_mime_type' => get_imagify_mime_type(), |
| 32 |
'posts_per_page' => -1, |
| 33 |
'update_post_term_cache' => false, |
| 34 |
'no_found_rows' => true, |
| 35 |
'fields' => 'ids' |
| 36 |
) |
| 37 |
); |
| 38 |
$attachments = (array) $query->posts; |
| 39 |
|
| 40 |
foreach( $attachments as $attachment_id ) { |
| 41 |
$attachment = new Imagify_Attachment( $attachment_id ); |
| 42 |
|
| 43 |
// Check if the attachment extension is allowed |
| 44 |
if ( ! wp_attachment_is_image( $attachment_id ) ) { |
| 45 |
continue; |
| 46 |
} |
| 47 |
|
| 48 |
if ( $attachment->is_exceeded() ) { |
| 49 |
$count++; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return (int) $count; |
| 54 |
} |
| 55 |
|
| 56 |
/* |
| 57 |
* Count number of optimized attachments with an error. |
| 58 |
* |
| 59 |
* @since 1.0 |
| 60 |
* |
| 61 |
* @return int The number of attachments. |
| 62 |
*/ |
| 63 |
function imagify_count_error_attachments() { |
| 64 |
$query = new WP_Query( |
| 65 |
array( |
| 66 |
'post_type' => 'attachment', |
| 67 |
'post_status' => 'inherit', |
| 68 |
'post_mime_type' => get_imagify_mime_type(), |
| 69 |
'meta_key' => '_imagify_status', |
| 70 |
'meta_value' => 'error', |
| 71 |
'posts_per_page' => -1, |
| 72 |
'update_post_term_cache' => false, |
| 73 |
'no_found_rows' => true, |
| 74 |
'fields' => 'ids' |
| 75 |
) |
| 76 |
); |
| 77 |
|
| 78 |
return (int) $query->post_count; |
| 79 |
} |
| 80 |
|
| 81 |
/* |
| 82 |
* Count number of optimized attachments (by Imagify or an other tool before). |
| 83 |
* |
| 84 |
* @since 1.0 |
| 85 |
* |
| 86 |
* @return int The number of attachments. |
| 87 |
*/ |
| 88 |
function imagify_count_optimized_attachments() { |
| 89 |
$query = new WP_Query( |
| 90 |
array( |
| 91 |
'post_type' => 'attachment', |
| 92 |
'post_status' => 'inherit', |
| 93 |
'post_mime_type' => get_imagify_mime_type(), |
| 94 |
'meta_query' => array( |
| 95 |
'relation' => 'or', |
| 96 |
array( |
| 97 |
'key' => '_imagify_status', |
| 98 |
'value' => 'success', |
| 99 |
'compare' => '=' |
| 100 |
), |
| 101 |
array( |
| 102 |
'key' => '_imagify_status', |
| 103 |
'value' => 'already_optimized', |
| 104 |
'compare' => '=' |
| 105 |
) |
| 106 |
), |
| 107 |
'posts_per_page' => -1, |
| 108 |
'update_post_term_cache' => false, |
| 109 |
'no_found_rows' => true, |
| 110 |
'fields' => 'ids' |
| 111 |
) |
| 112 |
); |
| 113 |
|
| 114 |
return (int) $query->post_count; |
| 115 |
} |
| 116 |
|
| 117 |
/* |
| 118 |
* Count number of unoptimized attachments. |
| 119 |
* |
| 120 |
* @since 1.0 |
| 121 |
* |
| 122 |
* @return int The number of attachments. |
| 123 |
*/ |
| 124 |
function imagify_count_unoptimized_attachments() { |
| 125 |
$count = imagify_count_attachments() - imagify_count_optimized_attachments() - imagify_count_error_attachments(); |
| 126 |
return (int) $count; |
| 127 |
} |
| 128 |
|
| 129 |
/* |
| 130 |
* Count percent of optimized attachments. |
| 131 |
* |
| 132 |
* @since 1.0 |
| 133 |
* |
| 134 |
* @return int The percent of optimized attachments. |
| 135 |
*/ |
| 136 |
function imagify_percent_optimized_attachments() { |
| 137 |
$total_attachments = imagify_count_attachments(); |
| 138 |
$total_optimized_attachments = imagify_count_optimized_attachments(); |
| 139 |
|
| 140 |
$percent = ( 0 !== $total_attachments ) ? round( ( 100 - ( ( $total_attachments - ( $total_optimized_attachments ) ) / $total_attachments ) * 100 ) ) : 0; |
| 141 |
|
| 142 |
return $percent; |
| 143 |
} |
| 144 |
|
| 145 |
/* |
| 146 |
* Count percent, original & optimized size of all images optimized by Imagify. |
| 147 |
* |
| 148 |
* @since 1.0 |
| 149 |
* |
| 150 |
* @return array An array containing the optimization data. |
| 151 |
*/ |
| 152 |
function imagify_count_saving_data( $key = '' ) { |
| 153 |
global $wpdb; |
| 154 |
|
| 155 |
$original_size = 0; |
| 156 |
$optimized_size = 0; |
| 157 |
$query = new WP_Query( |
| 158 |
array( |
| 159 |
'post_type' => 'attachment', |
| 160 |
'post_status' => 'inherit', |
| 161 |
'post_mime_type' => get_imagify_mime_type(), |
| 162 |
'meta_key' => '_imagify_status', |
| 163 |
'meta_value' => 'success', |
| 164 |
'posts_per_page' => -1, |
| 165 |
'update_post_term_cache' => false, |
| 166 |
'no_found_rows' => true, |
| 167 |
'fields' => 'ids' |
| 168 |
) |
| 169 |
); |
| 170 |
$attachments = (array) $query->posts; |
| 171 |
|
| 172 |
foreach( $attachments as $attachment_id ) { |
| 173 |
$attachment = new Imagify_Attachment( $attachment_id ); |
| 174 |
|
| 175 |
// Check if the attachment extension is allowed |
| 176 |
if ( ! wp_attachment_is_image( $attachment_id ) ) { |
| 177 |
continue; |
| 178 |
} |
| 179 |
|
| 180 |
$stats_data = $attachment->get_stats_data(); |
| 181 |
$original_data = $attachment->get_size_data( 'full' ); |
| 182 |
|
| 183 |
// Incremente the original sizes |
| 184 |
if ( $attachment->is_optimized() ) { |
| 185 |
$original_size += ( $original_data['original_size'] ) ? $original_data['original_size'] : 0; |
| 186 |
$optimized_size += ( $original_data['optimized_size'] ) ? $original_data['optimized_size'] : 0; |
| 187 |
} |
| 188 |
|
| 189 |
$metadata = wp_get_attachment_metadata( $attachment_id ); |
| 190 |
$sizes = ( isset( $metadata['sizes'] ) ) ? (array) $metadata['sizes'] : array(); |
| 191 |
|
| 192 |
// Incremente the thumbnails sizes |
| 193 |
foreach ( $sizes as $size_key => $size_data ) { |
| 194 |
$size_data = $attachment->get_size_data( $size_key ); |
| 195 |
if ( ! empty( $size_data['success'] ) ) { |
| 196 |
$original_size += ( $size_data['original_size'] ) ? $size_data['original_size'] : 0; |
| 197 |
$optimized_size += ( $size_data['optimized_size'] ) ? $size_data['optimized_size'] : 0; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
$data = array( |
| 203 |
'count' => $query->post_count, |
| 204 |
'original_size' => (int) $original_size, |
| 205 |
'optimized_size' => (int) $optimized_size, |
| 206 |
'percent' => ( 0 !== $optimized_size ) ? ceil( ( ( $original_size - $optimized_size ) / $original_size ) * 100 ) : 0 |
| 207 |
); |
| 208 |
|
| 209 |
if ( ! empty( $key ) ) { |
| 210 |
return $data[ $key ]; |
| 211 |
} |
| 212 |
|
| 213 |
return $data; |
| 214 |
} |