PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 3.6.4
TinyPNG – JPEG, PNG & WebP image compression v3.6.4
3.8.0 3.7.0 3.6.14 trunk 1.0.0 1.1.0 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.4.0 1.5.0 1.6.0 1.7.0 1.7.1 1.7.2 2.0.0 2.0.1 2.0.2 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.0.0 3.0.1 3.1.0 3.2.0 3.2.1 3.3 3.4 3.4.1 3.4.2 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.5.2 3.6.0 3.6.1 3.6.10 3.6.11 3.6.12 3.6.13 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9
tiny-compress-images / src / class-tiny-bulk-optimization.php
tiny-compress-images / src Last commit date
compatibility 1 year ago config 4 years ago css 10 months ago data 4 years ago images 4 years ago js 9 months ago vendor 1 year ago views 10 months ago class-tiny-bulk-optimization.php 1 year ago class-tiny-cli.php 10 months ago class-tiny-compress-client.php 9 months ago class-tiny-compress-fopen.php 9 months ago class-tiny-compress.php 9 months ago class-tiny-exception.php 4 years ago class-tiny-helpers.php 1 year ago class-tiny-image-size.php 10 months ago class-tiny-image.php 9 months ago class-tiny-notices.php 11 months ago class-tiny-php.php 4 years ago class-tiny-picture.php 9 months ago class-tiny-plugin.php 9 months ago class-tiny-settings.php 9 months ago class-tiny-wp-base.php 11 months ago
class-tiny-bulk-optimization.php
154 lines
1 <?php
2 /*
3 * Tiny Compress Images - WordPress plugin.
4 * Copyright (C) 2015-2018 Tinify B.V.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 class Tiny_Bulk_Optimization {
22 // Page the retrieved database results for memory purposes
23 // in case the media library is extremely big.
24 const PAGING_SIZE = 25000;
25
26 public static function get_optimization_statistics( $settings, $result = null ) {
27 $stats = array();
28 $stats['uploaded-images'] = 0;
29 $stats['optimized-image-sizes'] = 0;
30 $stats['available-unoptimized-sizes'] = 0;
31 $stats['optimized-library-size'] = 0;
32 $stats['unoptimized-library-size'] = 0;
33 $stats['estimated_credit_use'] = 0;
34 $stats['available-for-optimization'] = array();
35
36 if ( is_null( $result ) ) {
37 $last_image_id = null;
38 do {
39 $result = self::wpdb_retrieve_images_and_metadata( $last_image_id );
40 $stats = self::populate_optimization_statistics( $settings, $result, $stats );
41 $last_image = end( $result );
42 if ( isset( $last_image['ID'] ) ) {
43 $last_image_id = $last_image['ID'];
44 }
45 } while ( sizeof( $result ) == self::PAGING_SIZE );
46 } else {
47 $stats = self::populate_optimization_statistics( $settings, $result, $stats );
48 }
49 unset( $result );
50
51 if ( 0 != $stats['unoptimized-library-size'] ) {
52 $stats['display-percentage'] = round(
53 100 -
54 ( $stats['optimized-library-size'] / $stats['unoptimized-library-size'] * 100 ), 1
55 );
56 } else {
57 $stats['display-percentage'] = 0;
58 }
59 return $stats;
60 }
61
62 private static function wpdb_retrieve_images_and_metadata( $start_id ) {
63 global $wpdb;
64
65 // Retrieve posts that have "_wp_attachment_metadata" image metadata
66 // and optionally contain "tiny_compress_images" metadata.
67 $sql_start_id = ( $start_id ? " $wpdb->posts.ID < $start_id AND " : '' );
68 $query =
69 "SELECT
70 $wpdb->posts.ID,
71 $wpdb->posts.post_title,
72 $wpdb->postmeta.meta_value,
73 wp_postmeta_file.meta_value AS unique_attachment_name,
74 wp_postmeta_tiny.meta_value AS tiny_meta_value
75 FROM $wpdb->posts
76 LEFT JOIN $wpdb->postmeta
77 ON $wpdb->posts.ID = $wpdb->postmeta.post_id
78 LEFT JOIN $wpdb->postmeta AS wp_postmeta_file
79 ON $wpdb->posts.ID = wp_postmeta_file.post_id
80 AND wp_postmeta_file.meta_key = '_wp_attached_file'
81 LEFT JOIN $wpdb->postmeta AS wp_postmeta_tiny
82 ON $wpdb->posts.ID = wp_postmeta_tiny.post_id
83 AND wp_postmeta_tiny.meta_key = '" . Tiny_Config::META_KEY . "'
84 WHERE
85 $sql_start_id
86 $wpdb->posts.post_type = 'attachment'
87 AND (
88 $wpdb->posts.post_mime_type = 'image/jpeg' OR
89 $wpdb->posts.post_mime_type = 'image/png' OR
90 $wpdb->posts.post_mime_type = 'image/webp'
91 )
92 AND $wpdb->postmeta.meta_key = '_wp_attachment_metadata'
93 GROUP BY unique_attachment_name
94 ORDER BY ID DESC
95 LIMIT " . self::PAGING_SIZE;
96
97 return $wpdb->get_results( $query, ARRAY_A ); // WPCS: unprepared SQL OK.
98 }
99
100 private static function populate_optimization_statistics( $settings, $result, $stats ) {
101 $active_sizes = $settings->get_sizes();
102 $active_tinify_sizes = $settings->get_active_tinify_sizes();
103 $conversion_enabled = $settings->get_conversion_enabled();
104
105 for ( $i = 0; $i < sizeof( $result ); $i++ ) {
106 $wp_metadata = unserialize( (string) $result[ $i ]['meta_value'] );
107 $tiny_metadata = isset( $result[ $i ]['tiny_meta_value'] ) ?
108 unserialize( (string) $result[ $i ]['tiny_meta_value'] ) :
109 array();
110 if ( ! is_array( $tiny_metadata ) ) {
111 $tiny_metadata = array();
112 }
113 $tiny_image = new Tiny_Image(
114 $settings,
115 $result[ $i ]['ID'],
116 $wp_metadata,
117 $tiny_metadata,
118 $active_sizes,
119 $active_tinify_sizes
120 );
121 $image_stats = $tiny_image->get_statistics( $active_sizes, $active_tinify_sizes );
122
123 $stats['uploaded-images']++;
124 $stats['estimated_credit_use'] += $image_stats['available_uncompressed_sizes'];
125 if ( $conversion_enabled ) {
126 $stats['available-unoptimized-sizes'] +=
127 $image_stats['available_unconverted_sizes'];
128 $stats['optimized-image-sizes'] +=
129 $image_stats['image_sizes_converted'];
130 $stats['estimated_credit_use'] += $image_stats['available_unconverted_sizes'];
131 } else {
132 $stats['available-unoptimized-sizes'] +=
133 $image_stats['available_uncompressed_sizes'];
134 $stats['optimized-image-sizes'] +=
135 $image_stats['image_sizes_compressed'];
136 }
137 $stats['optimized-library-size'] += $image_stats['compressed_total_size'];
138 $stats['unoptimized-library-size'] += $image_stats['initial_total_size'];
139
140 $has_conversions = $image_stats['available_unconverted_sizes'] > 0;
141 $has_compressions = $image_stats['available_uncompressed_sizes'] > 0;
142 $has_optimizations = $has_compressions || ($conversion_enabled && $has_conversions);
143 if ( $has_optimizations ) {
144 $stats['available-for-optimization'][] = array(
145 'ID' => $result[ $i ]['ID'],
146 'post_title' => $result[ $i ]['post_title'],
147 );
148 }
149 }// End for().
150
151 return $stats;
152 }
153 }
154