| 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 |
|