Skip to content

Commit 517d0c9

Browse files
Format
1 parent 871ef66 commit 517d0c9

2 files changed

Lines changed: 39 additions & 44 deletions

File tree

src/class-tiny-cli.php

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,32 @@
1818
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919
*/
2020

21-
class Tiny_Cli
22-
{
21+
class Tiny_Cli {
22+
2323
/**
2424
* Tinify Settings
2525
*
2626
* @var Tiny_Settings
2727
*/
2828
private $tiny_settings;
2929

30-
public function __construct( $settings )
31-
{
30+
public function __construct( $settings ) {
3231
$this->tiny_settings = $settings;
3332

3433
// 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' ) );
3736
}
3837
}
3938

4039
public function register_command() {
4140
$command_instance = new Tiny_Command( $this->tiny_settings );
42-
WP_CLI::add_command('tiny', $command_instance);
41+
WP_CLI::add_command( 'tiny', $command_instance );
4342
}
4443
}
4544

46-
class Tiny_Command
47-
{
45+
class Tiny_Command {
46+
4847
/**
4948
* Tinify Settings
5049
*
@@ -77,90 +76,86 @@ public function __construct( $settings ) {
7776
* @param array $assoc_args
7877
* @return void
7978
*/
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();
8381

84-
if (empty($attachments)) {
82+
if ( empty( $attachments ) ) {
8583
$attachments = $this->get_unoptimized_attachments();
8684
}
8785

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.' );
9088
return;
9189
}
9290

93-
$total = count($attachments);
94-
WP_CLI::log('Optimizing ' . $total . ' images.');
91+
$total = count( $attachments );
92+
WP_CLI::log( 'Optimizing ' . $total . ' images.' );
9593

96-
$progress = WP_CLI\Utils\make_progress_bar('Optimizing images', $total);
94+
$progress = WP_CLI\Utils\make_progress_bar( 'Optimizing images', $total );
9795
$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 );
10098

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 );
103101
$progress->tick();
104102
continue;
105103
}
106104

107105
try {
108-
$this->optimize_attachment($attachment_id);
106+
$this->optimize_attachment( $attachment_id );
109107
$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 . ')' );
112110
}
113111

114112
$progress->tick();
115113
}
116114

117115
$progress->finish();
118-
WP_CLI::success('Done! Optimized ' . $optimized . ' of ' . $total . ' images.');
116+
WP_CLI::success( 'Done! Optimized ' . $optimized . ' of ' . $total . ' images.' );
119117
}
120118

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 );
124121

125-
if (empty($stats['available-for-optimization'])) {
122+
if ( empty( $stats['available-for-optimization'] ) ) {
126123
return array();
127124
}
128125

129126
$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'] ) ) {
132129
$ids[] = $item['ID'];
133130
}
134131
}
135132
return $ids;
136133
}
137134

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 );
141137
$tiny_image->compress();
142138
}
143139

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 ) {
148143
return false;
149144
}
150145

151-
if ($attachment->post_type !== 'attachment') {
146+
if ( $attachment->post_type !== 'attachment' ) {
152147
return false;
153148
}
154149

155150
// Check if it's an image
156151
$mime_type = $attachment->post_mime_type;
157-
if (! $mime_type || strpos($mime_type, 'image/') !== 0) {
152+
if ( ! $mime_type || strpos( $mime_type, 'image/' ) !== 0 ) {
158153
return false;
159154
}
160155

161156
// 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 ) ) {
164159
return false;
165160
}
166161

src/class-tiny-plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function init() {
6767
);
6868

6969
new Tiny_CLI( $this->settings );
70-
70+
7171
if ( $this->settings->get_conversion_enabled() ) {
7272
new Tiny_Picture( ABSPATH, array( get_site_url() ) );
7373
}

0 commit comments

Comments
 (0)