PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.4.7
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.4.7
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.7, at inc/functions/admin-stats.php

161 lines 4.8 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 global $wpdb;
13
14 $count = $wpdb->get_var(
15 "SELECT COUNT($wpdb->posts.ID)
16 FROM $wpdb->posts
17 WHERE post_type = 'attachment'
18 AND post_status != 'trash'
19 AND ($wpdb->posts.post_mime_type = 'image/jpeg' OR $wpdb->posts.post_mime_type = 'image/png' OR $wpdb->posts.post_mime_type = 'image/gif')"
20 );
21
22 if ( $count > apply_filters( 'imagify_unoptimized_attachment_limit', 10000 ) ) {
23 set_transient( IMAGIFY_SLUG . '_large_library', 1 );
24 }
25
26 return (int) $count;
27 }
28
29 /*
30 * Count number of optimized attachments with an error.
31 *
32 * @since 1.0
33 *
34 * @return int The number of attachments.
35 */
36 function imagify_count_error_attachments() {
37 global $wpdb;
38
39 $count = $wpdb->get_var(
40 "SELECT COUNT($wpdb->posts.ID)
41 FROM $wpdb->posts
42 INNER JOIN $wpdb->postmeta
43 ON $wpdb->posts.ID = $wpdb->postmeta.post_id
44 WHERE ($wpdb->posts.post_mime_type = 'image/jpeg' OR $wpdb->posts.post_mime_type = 'image/png' OR $wpdb->posts.post_mime_type = 'image/gif')
45 AND ( ( $wpdb->postmeta.meta_key = '_imagify_status' AND CAST($wpdb->postmeta.meta_value AS CHAR) = 'error' )
46 )
47 AND $wpdb->posts.post_type = 'attachment'
48 AND $wpdb->posts.post_status = 'inherit'"
49 );
50
51 return (int) $count;
52 }
53
54 /*
55 * Count number of optimized attachments (by Imagify or an other tool before).
56 *
57 * @since 1.0
58 *
59 * @return int The number of attachments.
60 */
61 function imagify_count_optimized_attachments() {
62 global $wpdb;
63
64 $count = $wpdb->get_var(
65 "SELECT COUNT($wpdb->posts.ID)
66 FROM $wpdb->posts
67 INNER JOIN $wpdb->postmeta
68 ON $wpdb->posts.ID = $wpdb->postmeta.post_id
69 WHERE ($wpdb->posts.post_mime_type = 'image/jpeg' OR $wpdb->posts.post_mime_type = 'image/png' OR $wpdb->posts.post_mime_type = 'image/gif')
70 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' ) )
71 AND $wpdb->posts.post_type = 'attachment'
72 AND $wpdb->posts.post_status = 'inherit'"
73 );
74
75 return (int) $count;
76 }
77
78 /*
79 * Count number of unoptimized attachments.
80 *
81 * @since 1.0
82 *
83 * @return int The number of attachments.
84 */
85 function imagify_count_unoptimized_attachments() {
86 $count = imagify_count_attachments() - imagify_count_optimized_attachments() - imagify_count_error_attachments();
87 return (int) $count;
88 }
89
90 /*
91 * Count percent of optimized attachments.
92 *
93 * @since 1.0
94 *
95 * @return int The percent of optimized attachments.
96 */
97 function imagify_percent_optimized_attachments() {
98 $total_attachments = imagify_count_attachments();
99 $total_optimized_attachments = imagify_count_optimized_attachments();
100
101 $percent = ( 0 !== $total_attachments ) ? round( ( 100 - ( ( $total_attachments - ( $total_optimized_attachments ) ) / $total_attachments ) * 100 ) ) : 0;
102
103 return $percent;
104 }
105
106 /*
107 * Count percent, original & optimized size of all images optimized by Imagify.
108 *
109 * @since 1.0
110 *
111 * @return array An array containing the optimization data.
112 */
113 function imagify_count_saving_data( $key = '' ) {
114 global $wpdb;
115
116 $attachments = $wpdb->get_col(
117 "SELECT pm1.meta_value
118 FROM $wpdb->postmeta as pm1
119 INNER JOIN $wpdb->postmeta as pm2
120 ON pm1.post_id = pm2.post_id
121 WHERE pm1.meta_key= '_imagify_data'
122 AND ( pm2.meta_key= '_imagify_status' AND pm2.meta_value= 'success' )"
123 );
124
125 $attachments = array_map( 'maybe_unserialize', (array) $attachments );
126 $original_size = 0;
127 $optimized_size = 0;
128 $count = 0;
129
130 foreach( $attachments as $attachment_data ) {
131 $stats_data = $attachment_data['stats'];
132 $original_data = $attachment_data['sizes']['full'];
133
134 // Incremente the original sizes
135 $original_size += ( $original_data['original_size'] ) ? $original_data['original_size'] : 0;
136 $optimized_size += ( $original_data['optimized_size'] ) ? $original_data['optimized_size'] : 0;
137
138 unset( $attachment_data['sizes']['full'] );
139
140 // Incremente the thumbnails sizes
141 foreach ( $attachment_data['sizes'] as $size_key => $size_data ) {
142 if ( ! empty( $size_data['success'] ) ) {
143 $original_size += ( $size_data['original_size'] ) ? $size_data['original_size'] : 0;
144 $optimized_size += ( $size_data['optimized_size'] ) ? $size_data['optimized_size'] : 0;
145 }
146 }
147 }
148
149 $data = array(
150 'count' => count( $attachments ),
151 'original_size' => (int) $original_size,
152 'optimized_size' => (int) $optimized_size,
153 'percent' => ( 0 !== $optimized_size ) ? ceil( ( ( $original_size - $optimized_size ) / $original_size ) * 100 ) : 0
154 );
155
156 if ( ! empty( $key ) ) {
157 return $data[ $key ];
158 }
159
160 return $data;
161 }