Skip to content

Commit 93ce905

Browse files
Implement WP-CLI optimize command with PHP 5.6 compatibility
- Add complete optimize method with progress tracking and error handling - Support specific attachment IDs via --attachments flag or process all unoptimized - Implement PHP 5.6 compatible array syntax and manual array_column replacement - Add robust attachment validation for supported image formats - Use instance-based command registration without global variables - Provide detailed logging and user feedback during optimization process This enables bulk image optimization via WP-CLI while maintaining compatibility with older PHP environments.
1 parent e3ddfbe commit 93ce905

1 file changed

Lines changed: 111 additions & 10 deletions

File tree

src/class-tiny-cli.php

Lines changed: 111 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,42 @@
1818
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919
*/
2020

21-
class Tiny_Cli {
21+
class Tiny_Cli
22+
{
23+
/**
24+
* Tiny_Plugin $settings
25+
*
26+
* @var Tiny_Settings
27+
*/
28+
private $tiny_settings;
2229

23-
public function __construct( $settings ) {
30+
public function __construct($settings)
31+
{
32+
$this->tiny_settings = $settings;
2433

2534
// Only add CLI hooks when WP-CLI is available
26-
if ( defined( 'WP_CLI' ) && WP_CLI ) {
27-
add_action( 'cli_init', Tiny_Cli_Commands::register( $settings ) );
35+
if (defined('WP_CLI') && WP_CLI) {
36+
add_action('cli_init', array($this, 'register_command'));
2837
}
2938
}
39+
40+
public function register_command() {
41+
$command_instance = new Tiny_Command( $this->tiny_settings );
42+
WP_CLI::add_command('tiny', $command_instance);
43+
}
3044
}
3145

32-
class Tiny_Cli_Commands {
46+
class Tiny_Command
47+
{
3348
/**
3449
* Tiny_Plugin $settings
3550
*
3651
* @var Tiny_Settings
3752
*/
3853
private $tiny_settings;
3954

40-
public static function register( $settings ) {
55+
public function __construct( $settings ) {
4156
$tiny_settings = $settings;
42-
43-
WP_CLI::add_command( 'tiny', self::class );
4457
}
4558

4659
/**
@@ -64,11 +77,99 @@ public static function register( $settings ) {
6477
* @param array $assoc_args
6578
* @return void
6679
*/
67-
public function optimize( $args, $assoc_args ) {
80+
public function optimize($args, $assoc_args)
81+
{
82+
if (! $this->tiny_settings) {
83+
WP_CLI::error('TinyPNG settings not available.');
84+
return;
85+
}
86+
87+
$attachments = isset($assoc_args['attachments']) ? array_map('trim', explode(',', $assoc_args['attachments'])) : array();
88+
89+
if (empty($attachments)) {
90+
$attachments = $this->get_unoptimized_attachments();
91+
}
92+
93+
if (empty($attachments)) {
94+
WP_CLI::success('No images found that need optimization.');
95+
return;
96+
}
97+
98+
$total = count($attachments);
99+
WP_CLI::log('Optimizing ' . $total . ' images.');
100+
101+
$progress = WP_CLI\Utils\make_progress_bar('Optimizing images', $total);
102+
$optimized = 0;
103+
foreach ($attachments as $attachment_id) {
104+
$attachment_id = intval($attachment_id);
68105

106+
if (! $this->is_valid_attachment($attachment_id)) {
107+
WP_CLI::warning('skipping - invalid attachment: ' . $attachment_id);
108+
$progress->tick();
109+
continue;
110+
}
111+
112+
try {
113+
$this->optimize_attachment($attachment_id);
114+
$optimized++;
115+
} catch (Exception $e) {
116+
WP_CLI::warning('skipping - error: ' . $e->getMessage() . ' (ID: ' . $attachment_id . ')');
117+
}
118+
119+
$progress->tick();
120+
}
121+
122+
$progress->finish();
123+
WP_CLI::success('Done! Optimized ' . $optimized . ' of ' . $total . ' images.');
124+
}
125+
126+
private function get_unoptimized_attachments()
127+
{
128+
$stats = Tiny_Bulk_Optimization::get_optimization_statistics($this->tiny_settings);
129+
130+
if (empty($stats['available-for-optimization'])) {
131+
return array();
132+
}
133+
134+
// PHP 5.6 compatible alternative to array_column
135+
$ids = array();
136+
foreach ($stats['available-for-optimization'] as $item) {
137+
if (isset($item['ID'])) {
138+
$ids[] = $item['ID'];
139+
}
140+
}
141+
return $ids;
69142
}
70143

71-
private function compress_attachment( $id ) {
144+
private function optimize_attachment($attachment_id)
145+
{
146+
$tiny_image = new Tiny_Image($this->tiny_settings, $attachment_id);
147+
$tiny_image->compress();
148+
}
149+
150+
private function is_valid_attachment($attachment_id)
151+
{
152+
$attachment = get_post($attachment_id);
153+
if (! $attachment) {
154+
return false;
155+
}
156+
157+
if ($attachment->post_type !== 'attachment') {
158+
return false;
159+
}
160+
161+
// Check if it's an image
162+
$mime_type = $attachment->post_mime_type;
163+
if (! $mime_type || strpos($mime_type, 'image/') !== 0) {
164+
return false;
165+
}
166+
167+
// Check if it's a supported format
168+
$supported_types = array('image/jpeg', 'image/png', 'image/webp');
169+
if (! in_array($mime_type, $supported_types, true)) {
170+
return false;
171+
}
72172

173+
return true;
73174
}
74175
}

0 commit comments

Comments
 (0)