-
Notifications
You must be signed in to change notification settings - Fork 37
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
tijmenbruggeman
wants to merge
16
commits into
tinify:master
Choose a base branch
from
wcreateweb:tiny-cli
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
cli_init
. This hook is available on WP CLI since v2.Examples

Help
wp tiny optimize --help
Optimizing non-existing attachment

wp tiny optimize --attachments=101