PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 3.6.12
TinyPNG – JPEG, PNG & WebP image compression v3.6.12
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-cli.php
tiny-compress-images / src Last commit date
compatibility 4 months ago config 5 months ago css 5 months ago data 3 years ago images 3 years ago js 5 months ago vendor 4 months ago views 5 months ago class-tiny-bulk-optimization.php 5 months ago class-tiny-cli.php 5 months ago class-tiny-compress-client.php 5 months ago class-tiny-compress-fopen.php 5 months ago class-tiny-compress.php 5 months ago class-tiny-diagnostics.php 5 months ago class-tiny-exception.php 5 months ago class-tiny-helpers.php 5 months ago class-tiny-image-size.php 5 months ago class-tiny-image.php 5 months ago class-tiny-logger.php 5 months ago class-tiny-notices.php 5 months ago class-tiny-php.php 5 months ago class-tiny-picture.php 4 months ago class-tiny-plugin.php 4 months ago class-tiny-settings.php 5 months ago class-tiny-source-base.php 4 months ago class-tiny-source-image.php 5 months ago class-tiny-source-picture.php 5 months ago class-tiny-wp-base.php 5 months ago
class-tiny-cli.php
150 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_Cli {
22
23 /**
24 * Tinify Settings
25 *
26 * @var Tiny_Settings
27 */
28 private $tiny_settings;
29
30 public function __construct( $settings ) {
31 $this->tiny_settings = $settings;
32 }
33
34 public static function register_command( $settings ) {
35 $command_instance = new Tiny_Cli( $settings );
36 WP_CLI::add_command( 'tiny', $command_instance );
37 }
38
39 /**
40 * Optimize will process images
41 *
42 * [--attachments=<strings>]
43 * : A comma separated list of attachment IDs to process. If omitted
44 * will optimize all uncompressed attachments
45 *
46 *
47 * ## EXAMPLES
48 *
49 * optimize specific attachments
50 * wp tiny optimize --attachments=532,603,705
51 *
52 * optimize all unprocessed images
53 * wp tiny optimize
54 *
55 *
56 * @param array $args
57 * @param array $assoc_args
58 * @return void
59 */
60 public function optimize( $args, $assoc_args ) {
61 $attachments = isset( $assoc_args['attachments'] ) ?
62 array_map( 'trim', explode( ',', $assoc_args['attachments'] ) ) :
63 array();
64
65 if ( empty( $attachments ) ) {
66 $attachments = $this->get_unoptimized_attachments();
67 }
68
69 if ( empty( $attachments ) ) {
70 WP_CLI::success( 'No images found that need optimization.' );
71 return;
72 }
73
74 $total = count( $attachments );
75 WP_CLI::log( 'Optimizing ' . $total . ' images.' );
76
77 $progress = Utils\make_progress_bar( 'Optimizing images', $total );
78 $optimized = 0;
79 foreach ( $attachments as $attachment_id ) {
80 $attachment_id = intval( $attachment_id );
81
82 if ( ! $this->is_valid_attachment( $attachment_id ) ) {
83 WP_CLI::warning( 'skipping - invalid attachment: ' . $attachment_id );
84 $progress->tick();
85 continue;
86 }
87
88 try {
89 $result = $this->optimize_attachment( $attachment_id );
90 if ( isset( $result['success'] ) && $result['success'] > 0 ) {
91 ++$optimized;
92 }
93 } catch ( Exception $e ) {
94 WP_CLI::warning(
95 'skipping - error: ' .
96 $e->getMessage() .
97 ' (ID: ' .
98 $attachment_id .
99 ')'
100 );
101 }
102
103 $progress->tick();
104 }
105
106 $progress->finish();
107 WP_CLI::success( 'Done! Optimized ' . $optimized . ' of ' . $total . ' images.' );
108 }
109
110 private function get_unoptimized_attachments() {
111 $stats = Tiny_Bulk_Optimization::get_optimization_statistics( $this->tiny_settings );
112
113 if ( empty( $stats['available-for-optimization'] ) ) {
114 return array();
115 }
116
117 $ids = array();
118 foreach ( $stats['available-for-optimization'] as $item ) {
119 if ( isset( $item['ID'] ) ) {
120 $ids[] = $item['ID'];
121 }
122 }
123 return $ids;
124 }
125
126 /**
127 * Will process an attachment for optimization
128 *
129 * @return array{ success: int, failed: int }
130 */
131 private function optimize_attachment( $attachment_id ) {
132 $tiny_image = new Tiny_Image( $this->tiny_settings, $attachment_id );
133 return $tiny_image->compress();
134 }
135
136 private function is_valid_attachment( $attachment_id ) {
137 $mime_type = get_post_mime_type( $attachment_id );
138 if ( ! $mime_type || strpos( $mime_type, 'image/' ) !== 0 ) {
139 return false;
140 }
141
142 $supported_types = array( 'image/jpeg', 'image/png', 'image/webp' );
143 if ( ! in_array( $mime_type, $supported_types, true ) ) {
144 return false;
145 }
146
147 return true;
148 }
149 }
150