converter = $converter instanceof Pixelweb_Converter ? $converter : new Pixelweb_Converter(); } /** * Optimize images. * * ## OPTIONS * * [...] * : Attachment IDs. Without IDs, every pending image is processed. * * [--force] * : Re-convert even when converted files exist. * * ## EXAMPLES * * wp pixelweb optimize * wp pixelweb optimize 42 43 --force * * @param array $args IDs. * @param array $assoc Flags. */ public function optimize( $args, $assoc ) { $force = ! empty( $assoc['force'] ); if ( ! Pixelweb_Converter::webp_supported() ) { WP_CLI::error( 'This server cannot write WebP images (GD or Imagick with WebP support is required).' ); } $ids = array_map( 'absint', $args ); if ( ! $ids ) { $total = Pixelweb_Converter::count_images( 'pending' ); if ( ! $total ) { WP_CLI::success( 'Nothing to optimize.' ); return; } $progress = \WP_CLI\Utils\make_progress_bar( 'Optimizing', $total ); $counts = array(); do { $batch = Pixelweb_Converter::pending_ids( 20 ); foreach ( $batch as $id ) { $result = $this->converter->optimize_attachment( $id, null, $force, 'cli' ); $counts[ $result['status'] ] = isset( $counts[ $result['status'] ] ) ? $counts[ $result['status'] ] + 1 : 1; $progress->tick(); } if ( function_exists( 'wp_cache_flush_runtime' ) ) { wp_cache_flush_runtime(); } } while ( $batch ); $progress->finish(); $this->print_counts( $counts ); return; } $counts = array(); foreach ( $ids as $id ) { $result = $this->converter->optimize_attachment( $id, null, $force, 'cli' ); $counts[ $result['status'] ] = isset( $counts[ $result['status'] ] ) ? $counts[ $result['status'] ] + 1 : 1; WP_CLI::log( sprintf( '#%d %s %s → %s', $id, $result['status'], size_format( $result['original'] ), size_format( $result['optimized'] ) ) ); foreach ( $result['errors'] as $error ) { WP_CLI::warning( $error ); } } $this->print_counts( $counts ); } /** * Show statistics. * * ## EXAMPLES * * wp pixelweb status */ public function status() { wp_cache_delete( 'dashboard', 'pixelweb' ); $stats = Pixelweb_Stats::dashboard(); $caps = Pixelweb_Converter::capabilities(); WP_CLI::log( sprintf( 'Images: %d | Optimized: %d | Pending: %d', $stats['total_images'], $stats['optimized'], $stats['pending'] ) ); WP_CLI::log( sprintf( 'Saved: %s (%s%%)', Pixelweb_Stats::format_bytes( $stats['total_saved'] ), $stats['avg_savings'] ) ); WP_CLI::log( sprintf( 'WebP: %s | GD: %s Imagick: %s | Folder: %s', $caps['webp'] ? 'yes' : 'no', $caps['gd'] ? 'yes' : 'no', $caps['imagick'] ? 'yes' : 'no', Pixelweb_Converter::output_root() ) ); } /** * Delete every file generated by the plugin (originals are never touched). * * ## OPTIONS * * [--yes] * : Skip confirmation. * * @param array $args Unused. * @param array $assoc Flags. */ public function clean( $args, $assoc ) { WP_CLI::confirm( 'Delete all files generated by the plugin?', $assoc ); $deleted = 0; do { $ids = Pixelweb_Converter::processed_ids( 50 ); foreach ( $ids as $id ) { $deleted += $this->converter->delete_generated( $id ); } } while ( $ids ); Pixelweb_Stats::truncate(); WP_CLI::success( sprintf( '%d files deleted.', $deleted ) ); } /** * Print a summary line. * * @param array $counts Status => count. */ private function print_counts( $counts ) { $parts = array(); foreach ( $counts as $status => $n ) { $parts[] = $status . ': ' . $n; } WP_CLI::success( implode( ', ', $parts ) ); } }