PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / trunk
TinyPNG – JPEG, PNG & WebP image compression vtrunk
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 day ago config 1 day ago css 5 months ago data 3 years ago images 3 years ago js 1 day ago vendor 4 months ago views 1 day ago class-tiny-apache-rewrite.php 1 day ago class-tiny-bulk-optimization.php 1 day ago class-tiny-cli.php 1 day ago class-tiny-compress-client.php 1 day ago class-tiny-compress-fopen.php 1 day ago class-tiny-compress.php 1 day ago class-tiny-conversion.php 2 months ago class-tiny-diagnostics.php 5 months ago class-tiny-exception.php 5 months ago class-tiny-helpers.php 1 day ago class-tiny-image-size.php 1 day ago class-tiny-image.php 1 day ago class-tiny-logger.php 1 day ago class-tiny-migrate.php 1 day ago class-tiny-notices.php 1 day ago class-tiny-php.php 1 day ago class-tiny-picture.php 1 day ago class-tiny-plugin.php 1 day ago class-tiny-settings.php 1 day ago class-tiny-source-base.php 2 months ago class-tiny-source-image.php 5 months ago class-tiny-source-picture.php 5 months ago class-tiny-wp-base.php 1 day ago
class-tiny-cli.php
149 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 * @param array $args
56 * @param array $assoc_args
57 * @return void
58 */
59 public function optimize( $args, $assoc_args ) {
60 $attachments = isset( $assoc_args['attachments'] ) ?
61 array_map( 'trim', explode( ',', $assoc_args['attachments'] ) ) :
62 array();
63
64 if ( empty( $attachments ) ) {
65 $attachments = $this->get_unoptimized_attachments();
66 }
67
68 if ( empty( $attachments ) ) {
69 WP_CLI::success( 'No images found that need optimization.' );
70 return;
71 }
72
73 $total = count( $attachments );
74 WP_CLI::log( 'Optimizing ' . $total . ' images.' );
75
76 $progress = Utils\make_progress_bar( 'Optimizing images', $total );
77 $optimized = 0;
78 foreach ( $attachments as $attachment_id ) {
79 $attachment_id = intval( $attachment_id );
80
81 if ( ! $this->is_valid_attachment( $attachment_id ) ) {
82 WP_CLI::warning( 'skipping - invalid attachment: ' . $attachment_id );
83 $progress->tick();
84 continue;
85 }
86
87 try {
88 $result = $this->optimize_attachment( $attachment_id );
89 if ( isset( $result['success'] ) && $result['success'] > 0 ) {
90 ++$optimized;
91 }
92 } catch ( Exception $e ) {
93 WP_CLI::warning(
94 'skipping - error: ' .
95 $e->getMessage() .
96 ' (ID: ' .
97 $attachment_id .
98 ')'
99 );
100 }
101
102 $progress->tick();
103 }
104
105 $progress->finish();
106 WP_CLI::success( 'Done! Optimized ' . $optimized . ' of ' . $total . ' images.' );
107 }
108
109 private function get_unoptimized_attachments() {
110 $stats = Tiny_Bulk_Optimization::get_optimization_statistics( $this->tiny_settings );
111
112 if ( empty( $stats['available-for-optimization'] ) ) {
113 return array();
114 }
115
116 $ids = array();
117 foreach ( $stats['available-for-optimization'] as $item ) {
118 if ( isset( $item['ID'] ) ) {
119 $ids[] = $item['ID'];
120 }
121 }
122 return $ids;
123 }
124
125 /**
126 * Will process an attachment for optimization
127 *
128 * @return array{ success: int, failed: int }
129 */
130 private function optimize_attachment( $attachment_id ) {
131 $tiny_image = new Tiny_Image( $this->tiny_settings, $attachment_id );
132 return $tiny_image->compress();
133 }
134
135 private function is_valid_attachment( $attachment_id ) {
136 $mime_type = get_post_mime_type( $attachment_id );
137 if ( ! $mime_type || strpos( $mime_type, 'image/' ) !== 0 ) {
138 return false;
139 }
140
141 $supported_types = array( 'image/jpeg', 'image/png', 'image/webp' );
142 if ( ! in_array( $mime_type, $supported_types, true ) ) {
143 return false;
144 }
145
146 return true;
147 }
148 }
149