-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathclass-tiny-bulk-optimization.php
More file actions
156 lines (144 loc) · 5.64 KB
/
class-tiny-bulk-optimization.php
File metadata and controls
156 lines (144 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/*
* Tiny Compress Images - WordPress plugin.
* Copyright (C) 2015-2018 Tinify B.V.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
class Tiny_Bulk_Optimization {
// Page the retrieved database results for memory purposes
// in case the media library is extremely big.
const PAGING_SIZE = 25000;
public static function get_optimization_statistics( $settings, $result = null ) {
$stats = array();
$stats['uploaded-images'] = 0;
$stats['optimized-image-sizes'] = 0;
$stats['available-unoptimized-sizes'] = 0;
$stats['optimized-library-size'] = 0;
$stats['unoptimized-library-size'] = 0;
$stats['estimated_credit_use'] = 0;
$stats['available-for-optimization'] = array();
if ( is_null( $result ) ) {
$last_image_id = null;
do {
$result = self::wpdb_retrieve_images_and_metadata( $last_image_id );
$stats = self::populate_optimization_statistics( $settings, $result, $stats );
$last_image = end( $result );
if ( isset( $last_image['ID'] ) ) {
$last_image_id = $last_image['ID'];
}
} while ( sizeof( $result ) == self::PAGING_SIZE );
} else {
$stats = self::populate_optimization_statistics( $settings, $result, $stats );
}
unset( $result );
if ( 0 != $stats['unoptimized-library-size'] ) {
$stats['display-percentage'] = round(
100 -
( $stats['optimized-library-size'] / $stats['unoptimized-library-size'] * 100 ),
1
);
} else {
$stats['display-percentage'] = 0;
}
return $stats;
}
private static function wpdb_retrieve_images_and_metadata( $start_id ) {
global $wpdb;
// Retrieve posts that have "_wp_attachment_metadata" image metadata
// and optionally contain "tiny_compress_images" metadata.
$sql_start_id = ( $start_id ? " $wpdb->posts.ID < $start_id AND " : '' );
$query =
"SELECT
$wpdb->posts.ID,
$wpdb->posts.post_title,
$wpdb->postmeta.meta_value,
wp_postmeta_tiny.meta_value AS tiny_meta_value
FROM $wpdb->posts
INNER JOIN $wpdb->postmeta
ON $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = '_wp_attachment_metadata'
INNER JOIN $wpdb->postmeta AS wp_postmeta_file
ON $wpdb->posts.ID = wp_postmeta_file.post_id
AND wp_postmeta_file.meta_key = '_wp_attached_file'
LEFT JOIN $wpdb->postmeta AS wp_postmeta_tiny
ON $wpdb->posts.ID = wp_postmeta_tiny.post_id
AND wp_postmeta_tiny.meta_key = '" . Tiny_Config::META_KEY . "'
WHERE
$sql_start_id
$wpdb->posts.post_type = 'attachment'
AND (
$wpdb->posts.post_mime_type = 'image/jpeg' OR
$wpdb->posts.post_mime_type = 'image/png' OR
$wpdb->posts.post_mime_type = 'image/webp'
)
ORDER BY ID DESC
LIMIT " . self::PAGING_SIZE;
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Query is built from internal IDs and constants only.
return $wpdb->get_results( $query, ARRAY_A );
}
private static function populate_optimization_statistics( $settings, $result, $stats ) {
$active_sizes = $settings->get_sizes();
$active_tinify_sizes = $settings->get_active_tinify_sizes();
$conversion_enabled = $settings->get_conversion_enabled();
for ( $i = 0; $i < sizeof( $result ); $i++ ) {
$wp_metadata = unserialize( (string) $result[ $i ]['meta_value'] );
$tiny_metadata = isset( $result[ $i ]['tiny_meta_value'] ) ?
unserialize( (string) $result[ $i ]['tiny_meta_value'] ) :
array();
if ( ! is_array( $tiny_metadata ) ) {
$tiny_metadata = array();
}
$tiny_image = new Tiny_Image(
$settings,
$result[ $i ]['ID'],
$wp_metadata,
$tiny_metadata,
$active_sizes,
$active_tinify_sizes
);
$image_stats = $tiny_image->get_statistics( $active_sizes, $active_tinify_sizes );
++$stats['uploaded-images'];
$stats['estimated_credit_use'] += $image_stats['available_uncompressed_sizes'];
if ( $conversion_enabled ) {
$stats['available-unoptimized-sizes'] +=
$image_stats['available_unconverted_sizes'];
$stats['optimized-image-sizes'] +=
$image_stats['image_sizes_converted'];
$stats['estimated_credit_use'] +=
$image_stats['available_unconverted_sizes'];
} else {
$stats['available-unoptimized-sizes'] +=
$image_stats['available_uncompressed_sizes'];
$stats['optimized-image-sizes'] +=
$image_stats['image_sizes_compressed'];
}
$stats['optimized-library-size'] += $image_stats['compressed_total_size'];
$stats['unoptimized-library-size'] += $image_stats['initial_total_size'];
$has_conversions = $image_stats['available_unconverted_sizes'] > 0;
$has_compressions = $image_stats['available_uncompressed_sizes'] > 0;
$has_optimizations = $has_compressions || (
$conversion_enabled && $has_conversions
);
if ( $has_optimizations ) {
$stats['available-for-optimization'][] = array(
'ID' => $result[ $i ]['ID'],
'post_title' => $result[ $i ]['post_title'],
);
}
}// End for().
return $stats;
}
}