Skip to content

Add WP CLI commands #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open

Conversation

tijmenbruggeman
Copy link
Collaborator

@tijmenbruggeman tijmenbruggeman commented Jul 14, 2025

This PR will add a tiny command to the WP CLI.

Example Usage
wp tiny optimize --attachments=532,603,705
This will attempt to optimize the given attachments.

wp tiny optimize
This will look for unoptimized attachments and optimize them.

Changes

  • Adds Tiny_Cli class which adds a hook to cli_init. This hook is available on WP CLI since v2.
  • Tiny_Cli will register the Tiny_Command using WP_CLI::add_command. Command arguments are parsed from PHPDoc making including documentation within the cli. (add_command)
  • It will receive/query attachments and checks if the mime type is anything the plug-in supports.
    • It reuses the bulk optimisation statistics to find unoptimised images.
  • Added unit tests for the cli commands. Lots of mocking of WordPress/Compress client was required making it quite complex.
  • Added a script to quickly run any cli command

Examples
Help wp tiny optimize --help
image

Optimizing non-existing attachment wp tiny optimize --attachments=101
image

- Only initialize CLI hooks when WP-CLI is available using defined('WP_CLI') check
- Move CLI initialization from admin_init() to init() for broader availability
- Register CLI commands using proper cli_init hook instead of immediate registration
- Improve performance by avoiding unnecessary objects when CLI is not present
- Ensure CLI commands work in all WordPress contexts (web, CLI, AJAX, etc.)

This prevents CLI-related code from running in web requests while ensuring
WP-CLI commands are properly available when needed.
- 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.
- 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.
- Add Tiny_Settings dependency to Tiny_Cli and Tiny_Command constructors
- Update method calls to include the new settings parameter
- Improve code documentation with PHPDoc comments
- Add wordpress-cli.php helper file with mock WP_CLI class
- Include WP-CLI mock in TinyTestCase for all test files
- Provides mock implementations for WP_CLI methods like log, success, warning
- Includes WP_CLI\Utils\make_progress_bar mock with MockProgressBar class
- Simplify is_valid_attachment method to use get_post_mime_type() directly
- Remove redundant post type and attachment validation checks
- Add comprehensive test for optimize command with attachment parameters
- Mock WordPress functions for proper test isolation
- Create virtual test images for file system simulation
- Add mock for Tiny_Compress with expected parameters validation
- Add test for optimize command when no attachments are specified
- Mock global wpdb object with get_results method for database operations
- Define ARRAY_A constant required for WordPress database operations
- Mock wp_get_attachment_metadata to simulate attachment metadata retrieval
- Create virtual test images and database results for realistic test scenarios
- Verify compress_file method is called when processing uncompressed attachments
- Fix parameter expectations in existing compress_file test mock
- Reorganize test sections with descriptive comments for better readability
- Group related mocking operations together (db, images, WordPress functions)
- Move wpdb mock setup before result data for logical flow
- Add clear section headers: mock db, create mock image, mock WordPress functions
- Simplify compressor mock expectations without return value
- Improve test maintainability and understanding
…dling

- Convert CLI class to use static registration method
- Add proper return value handling for optimization results
- Integrate CLI initialization into base WP class hook system
- Add is_cli() helper method to base class
- Improve optimization counting by checking success results
- Add documentation for optimize_attachment method return type
- Remove obsolete CLI initialization tests after refactoring
- Update test formatting to follow consistent style guidelines
- Add test for CLI initialization hook integration
- Fix array formatting and method call spacing
- Remove redundant test code for deprecated CLI constructor
Add development script to run WP-CLI commands in Docker containers
with configurable WordPress and PHP versions for testing CLI functionality.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant