PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.4.4
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.4.4
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 / admin-stats.php

admin-stats.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.4.4, at inc/functions/admin-stats.php

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