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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions bin/run-wpcli
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

# Usage: bin/run-wpcli [WORDPRESS_VERSION] [PHP_VERSION] [COMMAND]
# Example: bin/run-wpcli 67 82 "tiny optimize --attachments=123,456"
# Example: bin/run-wpcli 67 82 "tiny optimize"


CONFIG_FILE="config/wp-version.conf"
WORDPRESS_VERSION_ENV="${1:-$WORDPRESS_VERSION}"
PHP_VERSION_ENV="${2:-$PHP_VERSION}"

WORDPRESS_PORT="80${WORDPRESS_VERSION_ENV}"
WP_IMAGE_KEY="${WORDPRESS_VERSION_ENV}_${PHP_VERSION_ENV}"
WP_IMAGE=$(grep "^${WP_IMAGE_KEY}=" "$CONFIG_FILE" | cut -d'=' -f2)

COMMAND=${3:-"tiny --help"}

export WP_IMAGE
export COMPOSE_PROJECT_NAME="tinify_${WORDPRESS_VERSION_ENV}_${PHP_VERSION_ENV}"
export WORDPRESS_PORT

docker compose -f config/docker-compose.yml run --rm wpcli wp $COMMAND
151 changes: 151 additions & 0 deletions src/class-tiny-cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php
/*
* Tiny Compress Images - WordPress plugin.
* Copyright (C) 2015-2018 Tinify B.V.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

class Tiny_Cli {
public static function register_command( $settings ) {
$command_instance = new Tiny_Command( $settings );
WP_CLI::add_command( 'tiny', $command_instance );
}
}

class Tiny_Command {

/**
* Tinify Settings
*
* @var Tiny_Settings
*/
private $tiny_settings;

public function __construct( $settings ) {
$this->tiny_settings = $settings;
}

/**
* Optimize will process images
*
* [--attachments=<strings>]
* : A comma separated list of attachment IDs to process. If omitted
* will optimize all uncompressed attachments
*
*
* ## EXAMPLES
*
* optimize specific attachments
* wp tiny optimize --attachments=532,603,705
*
* optimize all unprocessed images
* wp tiny optimize
*
*
* @param array $args
* @param array $assoc_args
* @return void
*/
public function optimize( $args, $assoc_args ) {
$attachments = isset( $assoc_args['attachments'] ) ?
array_map( 'trim', explode( ',', $assoc_args['attachments'] ) ) :
array();

if ( empty( $attachments ) ) {
$attachments = $this->get_unoptimized_attachments();
}

if ( empty( $attachments ) ) {
WP_CLI::success( 'No images found that need optimization.' );
return;
}

$total = count( $attachments );
WP_CLI::log( 'Optimizing ' . $total . ' images.' );

$progress = WP_CLI\Utils\make_progress_bar( 'Optimizing images', $total );
$optimized = 0;
foreach ( $attachments as $attachment_id ) {
$attachment_id = intval( $attachment_id );

if ( ! $this->is_valid_attachment( $attachment_id ) ) {
WP_CLI::warning( 'skipping - invalid attachment: ' . $attachment_id );
$progress->tick();
continue;
}

try {
$result = $this->optimize_attachment( $attachment_id );
if ( isset( $result['success'] ) && $result['success'] > 0 ) {
$optimized++;
}
} catch ( Exception $e ) {
WP_CLI::warning(
'skipping - error: ' .
$e->getMessage() .
' (ID: ' .
$attachment_id .
')'
);
}

$progress->tick();
}

$progress->finish();
WP_CLI::success( 'Done! Optimized ' . $optimized . ' of ' . $total . ' images.' );
}

private function get_unoptimized_attachments() {
$stats = Tiny_Bulk_Optimization::get_optimization_statistics( $this->tiny_settings );

if ( empty( $stats['available-for-optimization'] ) ) {
return array();
}

$ids = array();
foreach ( $stats['available-for-optimization'] as $item ) {
if ( isset( $item['ID'] ) ) {
$ids[] = $item['ID'];
}
}
return $ids;
}

/**
* Will process an attachment for optimization
*
* @return array{ success: int, failed: int }
*/
private function optimize_attachment( $attachment_id ) {
$tiny_image = new Tiny_Image( $this->tiny_settings, $attachment_id );
return $tiny_image->compress();
}

private function is_valid_attachment( $attachment_id ) {
$mime_type = get_post_mime_type( $attachment_id );
if ( ! $mime_type || strpos( $mime_type, 'image/' ) !== 0 ) {
return false;
}

$supported_types = array( 'image/jpeg', 'image/png', 'image/webp' );
if ( ! in_array( $mime_type, $supported_types, true ) ) {
return false;
}

return true;
}
}
4 changes: 4 additions & 0 deletions src/class-tiny-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public function init() {
}
}

public function cli_init() {
Tiny_CLI::register_command( $this->settings );
}

public function ajax_init() {
add_filter( 'wp_ajax_tiny_async_optimize_upload_new_media',
$this->get_method( 'compress_on_upload' )
Expand Down
7 changes: 7 additions & 0 deletions src/class-tiny-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public function xmlrpc_init() {
}
}

public function cli_init() {
try {
$this->init_compressor();
} catch ( Tiny_Exception $e ) {
}
}

public function ajax_init() {
try {
$this->init_compressor();
Expand Down
11 changes: 11 additions & 0 deletions src/class-tiny-wp-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ protected function doing_ajax_request() {
return defined( 'DOING_AJAX' ) && DOING_AJAX;
}

protected function is_cli() {
return defined( 'WP_CLI' ) && WP_CLI;
}

protected static function get_prefixed_name( $name ) {
return self::PREFIX . $name;
}
Expand All @@ -65,6 +69,10 @@ public function __construct() {
add_action( 'admin_init', $this->get_method( 'admin_init' ) );
add_action( 'admin_menu', $this->get_method( 'admin_menu' ) );
}

if ( self::is_cli() ) {
add_action( 'cli_init', $this->get_method( 'cli_init' ) );
}
}

protected function get_method( $name ) {
Expand Down Expand Up @@ -100,4 +108,7 @@ public function admin_menu() {

public function rest_init() {
}

public function cli_init() {
}
}
29 changes: 29 additions & 0 deletions test/helpers/wordpress-cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* Mock WP_CLI to be used in php unit tests
*/
class WP_CLI
{
public static function log($message) {}

public static function success($message) {}

public static function warning($message) {}

public static function add_command($name, $command) {}
}

namespace WP_CLI\Utils {
class MockProgressBar
{
public function tick() {}

public function finish() {}
}

function make_progress_bar($label, $count)
{
return new MockProgressBar();
}
}
Loading