Skip to content

Commit e3ddfbe

Browse files
Refactor WP-CLI structure with separate command class
- Split CLI functionality into Tiny_Cli (registration) and Tiny_Cli_Commands (commands) - Make register() static to properly handle WP-CLI command registration - Separate command logic from registration logic for better architecture - Add optimize command with proper WP-CLI documentation format - Use private methods for helpers to prevent unwanted CLI command exposure This prevents WP-CLI from exposing internal methods as commands while providing a clean interface for the actual CLI functionality.
1 parent 3fc3660 commit e3ddfbe

1 file changed

Lines changed: 38 additions & 12 deletions

File tree

src/class-tiny-cli.php

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,56 @@
1919
*/
2020

2121
class Tiny_Cli {
22+
23+
public function __construct( $settings ) {
24+
25+
// 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 ) );
28+
}
29+
}
30+
}
31+
32+
class Tiny_Cli_Commands {
2233
/**
2334
* Tiny_Plugin $settings
2435
*
2536
* @var Tiny_Settings
2637
*/
2738
private $tiny_settings;
2839

29-
public function __construct( $settings ) {
30-
$this->tiny_settings = $settings;
40+
public static function register( $settings ) {
41+
$tiny_settings = $settings;
3142

32-
$this->add_hooks();
43+
WP_CLI::add_command( 'tiny', self::class );
3344
}
3445

3546
/**
36-
* Registers hooks to set up CLI support
47+
* Optimize will process images
48+
*
49+
* [--attachments=<strings>]
50+
* : A comma separated list of attachment IDs to process. If omitted
51+
* will optimize all uncompressed attachments
52+
*
53+
*
54+
* ## EXAMPLES
55+
*
56+
* optimize specific attachments
57+
* wp tiny optimize --attachments=532,603,705
58+
*
59+
* optimize all unprocessed images
60+
* wp tiny optimize
61+
*
62+
*
63+
* @param array $args
64+
* @param array $assoc_args
65+
* @return void
3766
*/
38-
public function add_hooks() {
39-
// Only add CLI hooks when WP-CLI is available
40-
if ( defined( 'WP_CLI' ) && WP_CLI ) {
41-
add_action( 'cli_init', array( $this, 'register' ) );
42-
}
67+
public function optimize( $args, $assoc_args ) {
68+
4369
}
4470

45-
public function register() {
46-
\WP_CLI::add_command( 'tiny', $this );
71+
private function compress_attachment( $id ) {
72+
4773
}
48-
}
74+
}

0 commit comments

Comments
 (0)