0 ) { WP_CLI::log( sprintf( 'Converted images went from %s to %s.', size_format( $t['before'], 1 ), size_format( $t['after'], 1 ) ) ); } WP_CLI::log( sprintf( 'Old copies kept for %d images (%s).', $t['old_files'], size_format( $t['old_bytes'], 1 ) ? size_format( $t['old_bytes'], 1 ) : '0 B' ) ); } /** * Convert images. * * ## OPTIONS * * [...] * : Attachment IDs. * * [--all] * : Every image that is ready. * * @param array $args Positional arguments. * @param array $assoc_args Named arguments. */ public function convert( $args, $assoc_args ) { $this->run( 'convert', $args, $assoc_args ); } /** * Put converted images back in their original format. * * ## OPTIONS * * [...] * : Attachment IDs. * * [--all] * : Every converted image. * * @param array $args Positional arguments. * @param array $assoc_args Named arguments. */ public function undo( $args, $assoc_args ) { $this->run( 'undo', $args, $assoc_args ); } /** * Remove old copies that nothing links to. * * ## OPTIONS * * [--yes] * : Do not ask first. * * @subcommand clean-up * * @param array $args Positional arguments. * @param array $assoc_args Named arguments. */ public function clean_up( $args, $assoc_args ) { WP_CLI::confirm( 'Remove old copies that nothing links to?', $assoc_args ); Featherwell_Jobs::start( 'cleanup' ); $job = Featherwell_Jobs::step( 0 ); Featherwell_Jobs::cancel(); WP_CLI::success( Featherwell_Jobs::summary( $job ) ); } /** * @param string $type convert or undo. * @param array $args IDs. * @param array $assoc_args Options. */ private function run( $type, $args, $assoc_args ) { if ( ! empty( $assoc_args['all'] ) ) { if ( 'convert' === $type && '' === Featherwell_Settings::target() ) { WP_CLI::error( Featherwell_Library::reason_text( 'no_format' ) ); } Featherwell_Jobs::start( $type ); $job = Featherwell_Jobs::step( 0 ); Featherwell_Jobs::cancel(); WP_CLI::success( Featherwell_Jobs::summary( $job ) ); return; } if ( ! $args ) { WP_CLI::error( 'Give attachment IDs or --all.' ); } foreach ( array_map( 'intval', $args ) as $id ) { $done = 'convert' === $type ? Featherwell_Library::convert( $id ) : Featherwell_Library::undo( $id ); if ( is_wp_error( $done ) ) { WP_CLI::warning( sprintf( '%d: %s', $id, $done->get_error_message() ) ); } elseif ( 'convert' === $type ) { WP_CLI::log( sprintf( '%d: %s to %s', $id, size_format( $done['before'], 1 ), size_format( $done['after'], 1 ) ) ); } else { WP_CLI::log( sprintf( '%d: back in its original format', $id ) ); } } WP_CLI::success( 'Done.' ); } }