PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 3.8.0
TinyPNG – JPEG, PNG & WebP image compression v3.8.0
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 2 months ago config 2 months ago css 7 months ago data 4 years ago images 4 years ago js 1 week ago vendor 6 months ago views 1 week ago class-tiny-apache-rewrite.php 2 months ago class-tiny-bulk-optimization.php 2 months ago class-tiny-cli.php 2 months ago class-tiny-compress-client.php 1 week ago class-tiny-compress-fopen.php 2 months ago class-tiny-compress.php 1 week ago class-tiny-conversion.php 4 months ago class-tiny-diagnostics.php 7 months ago class-tiny-exception.php 7 months ago class-tiny-helpers.php 2 months ago class-tiny-image-size.php 1 week ago class-tiny-image.php 1 week ago class-tiny-logger.php 2 months ago class-tiny-migrate.php 2 months ago class-tiny-notices.php 2 months ago class-tiny-php.php 2 months ago class-tiny-picture.php 2 months ago class-tiny-plugin.php 1 week ago class-tiny-settings.php 1 week ago class-tiny-source-base.php 1 week ago class-tiny-source-image.php 7 months ago class-tiny-source-picture.php 7 months ago class-tiny-wp-base.php 2 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 $result_count = count( $result );
46 } while ( self::PAGING_SIZE == $result_count );
47 } else {
48 $stats = self::populate_optimization_statistics( $settings, $result, $stats );
49 }
50 unset( $result );
51
52 if ( 0 != $stats['unoptimized-library-size'] ) {
53 $stats['display-percentage'] = round(
54 100 -
55 ( $stats['optimized-library-size'] / $stats['unoptimized-library-size'] * 100 ),
56 1
57 );
58 } else {
59 $stats['display-percentage'] = 0;
60 }
61 return $stats;
62 }
63
64 private static function wpdb_retrieve_images_and_metadata( $start_id ) {
65 global $wpdb;
66
67 // Retrieve posts that have "_wp_attachment_metadata" image metadata
68 // and optionally contain "tiny_compress_images" metadata.
69 $sql_start_id = ( $start_id ? " $wpdb->posts.ID < $start_id AND " : '' );
70 $query =
71 "SELECT
72 $wpdb->posts.ID,
73 $wpdb->posts.post_title,
74 $wpdb->postmeta.meta_value,
75 wp_postmeta_file.meta_value AS unique_attachment_name,
76 wp_postmeta_tiny.meta_value AS tiny_meta_value
77 FROM $wpdb->posts
78 LEFT JOIN $wpdb->postmeta
79 ON $wpdb->posts.ID = $wpdb->postmeta.post_id
80 LEFT JOIN $wpdb->postmeta AS wp_postmeta_file
81 ON $wpdb->posts.ID = wp_postmeta_file.post_id
82 AND wp_postmeta_file.meta_key = '_wp_attached_file'
83 LEFT JOIN $wpdb->postmeta AS wp_postmeta_tiny
84 ON $wpdb->posts.ID = wp_postmeta_tiny.post_id
85 AND wp_postmeta_tiny.meta_key = '" . Tiny_Config::META_KEY . "'
86 WHERE
87 $sql_start_id
88 $wpdb->posts.post_type = 'attachment'
89 AND (
90 $wpdb->posts.post_mime_type = 'image/jpeg' OR
91 $wpdb->posts.post_mime_type = 'image/png' OR
92 $wpdb->posts.post_mime_type = 'image/webp'
93 )
94 AND $wpdb->postmeta.meta_key = '_wp_attachment_metadata'
95 GROUP BY unique_attachment_name
96 ORDER BY ID DESC
97 LIMIT " . self::PAGING_SIZE;
98
99 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Query is built from internal IDs and constants only.
100 return $wpdb->get_results( $query, ARRAY_A );
101 }
102
103 private static function populate_optimization_statistics( $settings, $result, $stats ) {
104 $active_sizes = $settings->get_sizes();
105 $active_tinify_sizes = $settings->get_active_tinify_sizes();
106 $conversion_enabled = $settings->get_conversion_enabled();
107
108 $result_count = count( $result );
109 for ( $i = 0; $i < $result_count; $i++ ) {
110 $wp_metadata = maybe_unserialize( (string) $result[ $i ]['meta_value'] );
111 $tiny_metadata = isset( $result[ $i ]['tiny_meta_value'] ) ?
112 maybe_unserialize( (string) $result[ $i ]['tiny_meta_value'] ) :
113 array();
114 if ( ! is_array( $tiny_metadata ) ) {
115 $tiny_metadata = array();
116 }
117 $tiny_image = new Tiny_Image(
118 $settings,
119 $result[ $i ]['ID'],
120 $wp_metadata,
121 $tiny_metadata,
122 $active_sizes,
123 $active_tinify_sizes
124 );
125 $image_stats = $tiny_image->get_statistics( $active_sizes, $active_tinify_sizes );
126
127 ++$stats['uploaded-images'];
128 $stats['estimated_credit_use'] += $image_stats['available_uncompressed_sizes'];
129 $stats['available-unoptimized-sizes'] += $image_stats['available_unoptimized_sizes'];
130 $stats['optimized-image-sizes'] += $image_stats['image_sizes_optimized'];
131 $stats['optimized-library-size'] += $image_stats['compressed_total_size'];
132 $stats['unoptimized-library-size'] += $image_stats['initial_total_size'];
133
134 if ( $conversion_enabled ) {
135 $stats['estimated_credit_use'] += $image_stats['available_unconverted_sizes'];
136 }
137
138 $has_conversions = $image_stats['available_unconverted_sizes'] > 0;
139 $has_compressions = $image_stats['available_uncompressed_sizes'] > 0;
140 $has_optimizations = $has_compressions || (
141 $conversion_enabled && $has_conversions
142 );
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