|
18 | 18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 | 19 | */ |
20 | 20 |
|
21 | | -class Tiny_Cli |
22 | | -{ |
| 21 | +class Tiny_Cli { |
| 22 | + |
23 | 23 | /** |
24 | 24 | * Tinify Settings |
25 | 25 | * |
26 | 26 | * @var Tiny_Settings |
27 | 27 | */ |
28 | 28 | private $tiny_settings; |
29 | 29 |
|
30 | | - public function __construct( $settings ) |
31 | | - { |
| 30 | + public function __construct( $settings ) { |
32 | 31 | $this->tiny_settings = $settings; |
33 | 32 |
|
34 | 33 | // Only add CLI hooks when WP-CLI is available |
35 | | - if (defined('WP_CLI') && WP_CLI) { |
36 | | - add_action('cli_init', array($this, 'register_command')); |
| 34 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 35 | + add_action( 'cli_init', array( $this, 'register_command' ) ); |
37 | 36 | } |
38 | 37 | } |
39 | 38 |
|
40 | 39 | public function register_command() { |
41 | 40 | $command_instance = new Tiny_Command( $this->tiny_settings ); |
42 | | - WP_CLI::add_command('tiny', $command_instance); |
| 41 | + WP_CLI::add_command( 'tiny', $command_instance ); |
43 | 42 | } |
44 | 43 | } |
45 | 44 |
|
46 | | -class Tiny_Command |
47 | | -{ |
| 45 | +class Tiny_Command { |
| 46 | + |
48 | 47 | /** |
49 | 48 | * Tinify Settings |
50 | 49 | * |
@@ -77,90 +76,86 @@ public function __construct( $settings ) { |
77 | 76 | * @param array $assoc_args |
78 | 77 | * @return void |
79 | 78 | */ |
80 | | - public function optimize($args, $assoc_args) |
81 | | - { |
82 | | - $attachments = isset($assoc_args['attachments']) ? array_map('trim', explode(',', $assoc_args['attachments'])) : array(); |
| 79 | + public function optimize( $args, $assoc_args ) { |
| 80 | + $attachments = isset( $assoc_args['attachments'] ) ? array_map( 'trim', explode( ',', $assoc_args['attachments'] ) ) : array(); |
83 | 81 |
|
84 | | - if (empty($attachments)) { |
| 82 | + if ( empty( $attachments ) ) { |
85 | 83 | $attachments = $this->get_unoptimized_attachments(); |
86 | 84 | } |
87 | 85 |
|
88 | | - if (empty($attachments)) { |
89 | | - WP_CLI::success('No images found that need optimization.'); |
| 86 | + if ( empty( $attachments ) ) { |
| 87 | + WP_CLI::success( 'No images found that need optimization.' ); |
90 | 88 | return; |
91 | 89 | } |
92 | 90 |
|
93 | | - $total = count($attachments); |
94 | | - WP_CLI::log('Optimizing ' . $total . ' images.'); |
| 91 | + $total = count( $attachments ); |
| 92 | + WP_CLI::log( 'Optimizing ' . $total . ' images.' ); |
95 | 93 |
|
96 | | - $progress = WP_CLI\Utils\make_progress_bar('Optimizing images', $total); |
| 94 | + $progress = WP_CLI\Utils\make_progress_bar( 'Optimizing images', $total ); |
97 | 95 | $optimized = 0; |
98 | | - foreach ($attachments as $attachment_id) { |
99 | | - $attachment_id = intval($attachment_id); |
| 96 | + foreach ( $attachments as $attachment_id ) { |
| 97 | + $attachment_id = intval( $attachment_id ); |
100 | 98 |
|
101 | | - if (! $this->is_valid_attachment($attachment_id)) { |
102 | | - WP_CLI::warning('skipping - invalid attachment: ' . $attachment_id); |
| 99 | + if ( ! $this->is_valid_attachment( $attachment_id ) ) { |
| 100 | + WP_CLI::warning( 'skipping - invalid attachment: ' . $attachment_id ); |
103 | 101 | $progress->tick(); |
104 | 102 | continue; |
105 | 103 | } |
106 | 104 |
|
107 | 105 | try { |
108 | | - $this->optimize_attachment($attachment_id); |
| 106 | + $this->optimize_attachment( $attachment_id ); |
109 | 107 | $optimized++; |
110 | | - } catch (Exception $e) { |
111 | | - WP_CLI::warning('skipping - error: ' . $e->getMessage() . ' (ID: ' . $attachment_id . ')'); |
| 108 | + } catch ( Exception $e ) { |
| 109 | + WP_CLI::warning( 'skipping - error: ' . $e->getMessage() . ' (ID: ' . $attachment_id . ')' ); |
112 | 110 | } |
113 | 111 |
|
114 | 112 | $progress->tick(); |
115 | 113 | } |
116 | 114 |
|
117 | 115 | $progress->finish(); |
118 | | - WP_CLI::success('Done! Optimized ' . $optimized . ' of ' . $total . ' images.'); |
| 116 | + WP_CLI::success( 'Done! Optimized ' . $optimized . ' of ' . $total . ' images.' ); |
119 | 117 | } |
120 | 118 |
|
121 | | - private function get_unoptimized_attachments() |
122 | | - { |
123 | | - $stats = Tiny_Bulk_Optimization::get_optimization_statistics($this->tiny_settings); |
| 119 | + private function get_unoptimized_attachments() { |
| 120 | + $stats = Tiny_Bulk_Optimization::get_optimization_statistics( $this->tiny_settings ); |
124 | 121 |
|
125 | | - if (empty($stats['available-for-optimization'])) { |
| 122 | + if ( empty( $stats['available-for-optimization'] ) ) { |
126 | 123 | return array(); |
127 | 124 | } |
128 | 125 |
|
129 | 126 | $ids = array(); |
130 | | - foreach ($stats['available-for-optimization'] as $item) { |
131 | | - if (isset($item['ID'])) { |
| 127 | + foreach ( $stats['available-for-optimization'] as $item ) { |
| 128 | + if ( isset( $item['ID'] ) ) { |
132 | 129 | $ids[] = $item['ID']; |
133 | 130 | } |
134 | 131 | } |
135 | 132 | return $ids; |
136 | 133 | } |
137 | 134 |
|
138 | | - private function optimize_attachment($attachment_id) |
139 | | - { |
140 | | - $tiny_image = new Tiny_Image($this->tiny_settings, $attachment_id); |
| 135 | + private function optimize_attachment( $attachment_id ) { |
| 136 | + $tiny_image = new Tiny_Image( $this->tiny_settings, $attachment_id ); |
141 | 137 | $tiny_image->compress(); |
142 | 138 | } |
143 | 139 |
|
144 | | - private function is_valid_attachment($attachment_id) |
145 | | - { |
146 | | - $attachment = get_post($attachment_id); |
147 | | - if (! $attachment) { |
| 140 | + private function is_valid_attachment( $attachment_id ) { |
| 141 | + $attachment = get_post( $attachment_id ); |
| 142 | + if ( ! $attachment ) { |
148 | 143 | return false; |
149 | 144 | } |
150 | 145 |
|
151 | | - if ($attachment->post_type !== 'attachment') { |
| 146 | + if ( $attachment->post_type !== 'attachment' ) { |
152 | 147 | return false; |
153 | 148 | } |
154 | 149 |
|
155 | 150 | // Check if it's an image |
156 | 151 | $mime_type = $attachment->post_mime_type; |
157 | | - if (! $mime_type || strpos($mime_type, 'image/') !== 0) { |
| 152 | + if ( ! $mime_type || strpos( $mime_type, 'image/' ) !== 0 ) { |
158 | 153 | return false; |
159 | 154 | } |
160 | 155 |
|
161 | 156 | // Check if it's a supported format |
162 | | - $supported_types = array('image/jpeg', 'image/png', 'image/webp'); |
163 | | - if (! in_array($mime_type, $supported_types, true)) { |
| 157 | + $supported_types = array( 'image/jpeg', 'image/png', 'image/webp' ); |
| 158 | + if ( ! in_array( $mime_type, $supported_types, true ) ) { |
164 | 159 | return false; |
165 | 160 | } |
166 | 161 |
|
|
0 commit comments