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