PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 3.0.1
TinyPNG – JPEG, PNG & WebP image compression v3.0.1
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
config 8 years ago css 8 years ago data 10 years ago images 9 years ago js 8 years ago vendor 8 years ago views 8 years ago class-tiny-bulk-optimization.php 8 years ago class-tiny-compress-client.php 8 years ago class-tiny-compress-fopen.php 8 years ago class-tiny-compress.php 8 years ago class-tiny-exception.php 8 years ago class-tiny-image-size.php 8 years ago class-tiny-image.php 8 years ago class-tiny-notices.php 8 years ago class-tiny-php.php 8 years ago class-tiny-plugin.php 8 years ago class-tiny-settings.php 8 years ago class-tiny-wp-base.php 8 years ago
class-tiny-bulk-optimization.php
129 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-unoptimised-sizes'] = 0;
31 $stats['optimized-library-size'] = 0;
32 $stats['unoptimized-library-size'] = 0;
33 $stats['available-for-optimization'] = array();
34
35 if ( is_null( $result ) ) {
36 $last_image_id = null;
37 do {
38 $result = self::wpdb_retrieve_images_and_metadata( $last_image_id );
39 $stats = self::populate_optimization_statistics( $settings, $result, $stats );
40 $last_image = end( $result );
41 $last_image_id = $last_image['ID'];
42 } while ( sizeof( $result ) == self::PAGING_SIZE );
43 } else {
44 $stats = self::populate_optimization_statistics( $settings, $result, $stats );
45 }
46 unset( $result );
47
48 if ( 0 != $stats['unoptimized-library-size'] ) {
49 $stats['display-percentage'] = round(
50 100 -
51 ( $stats['optimized-library-size'] / $stats['unoptimized-library-size'] * 100 ), 1
52 );
53 } else {
54 $stats['display-percentage'] = 0;
55 }
56 return $stats;
57 }
58
59 private static function wpdb_retrieve_images_and_metadata( $start_id ) {
60 global $wpdb;
61
62 // Retrieve posts that have "_wp_attachment_metadata" image metadata
63 // and optionally contain "tiny_compress_images" metadata.
64 $sql_start_id = ( $start_id ? " $wpdb->posts.ID < $start_id AND " : '' );
65 $query =
66 "SELECT
67 $wpdb->posts.ID,
68 $wpdb->posts.post_title,
69 $wpdb->postmeta.meta_value,
70 wp_postmeta_file.meta_value AS unique_attachment_name,
71 wp_postmeta_tiny.meta_value AS tiny_meta_value
72 FROM $wpdb->posts
73 LEFT JOIN $wpdb->postmeta
74 ON $wpdb->posts.ID = $wpdb->postmeta.post_id
75 LEFT JOIN $wpdb->postmeta AS wp_postmeta_file
76 ON $wpdb->posts.ID = wp_postmeta_file.post_id
77 AND wp_postmeta_file.meta_key = '_wp_attached_file'
78 LEFT JOIN $wpdb->postmeta AS wp_postmeta_tiny
79 ON $wpdb->posts.ID = wp_postmeta_tiny.post_id
80 AND wp_postmeta_tiny.meta_key = '" . Tiny_Config::META_KEY . "'
81 WHERE
82 $sql_start_id
83 $wpdb->posts.post_type = 'attachment'
84 AND (
85 $wpdb->posts.post_mime_type = 'image/jpeg' OR
86 $wpdb->posts.post_mime_type = 'image/png'
87 )
88 AND $wpdb->postmeta.meta_key = '_wp_attachment_metadata'
89 GROUP BY unique_attachment_name
90 ORDER BY ID DESC
91 LIMIT " . self::PAGING_SIZE;
92
93 return $wpdb->get_results( $query, ARRAY_A ); // WPCS: unprepared SQL OK.
94 }
95
96 private static function populate_optimization_statistics( $settings, $result, $stats ) {
97 $active_sizes = $settings->get_sizes();
98 $active_tinify_sizes = $settings->get_active_tinify_sizes();
99 for ( $i = 0; $i < sizeof( $result ); $i++ ) {
100 $wp_metadata = unserialize( $result[ $i ]['meta_value'] );
101 $tiny_metadata = unserialize( $result[ $i ]['tiny_meta_value'] );
102 if ( ! is_array( $tiny_metadata ) ) {
103 $tiny_metadata = array();
104 }
105 $tiny_image = new Tiny_Image(
106 $settings,
107 $result[ $i ]['ID'],
108 $wp_metadata,
109 $tiny_metadata,
110 $active_sizes,
111 $active_tinify_sizes
112 );
113 $image_stats = $tiny_image->get_statistics( $active_sizes, $active_tinify_sizes );
114 $stats['uploaded-images']++;
115 $stats['available-unoptimised-sizes'] += $image_stats['available_unoptimized_sizes'];
116 $stats['optimized-image-sizes'] += $image_stats['image_sizes_optimized'];
117 $stats['optimized-library-size'] += $image_stats['optimized_total_size'];
118 $stats['unoptimized-library-size'] += $image_stats['initial_total_size'];
119 if ( $image_stats['available_unoptimized_sizes'] > 0 ) {
120 $stats['available-for-optimization'][] = array(
121 'ID' => $result[ $i ]['ID'],
122 'post_title' => $result[ $i ]['post_title'],
123 );
124 }
125 }
126 return $stats;
127 }
128 }
129