engines()->detect(); $rows = array( array( 'engine' => 'gd_webp', 'value' => $detect['gd_webp'] ? 'yes' : 'no' ), array( 'engine' => 'gd_avif', 'value' => $detect['gd_avif'] ? 'yes' : 'no' ), array( 'engine' => 'imagick', 'value' => $detect['imagick'] ? 'yes' : 'no' ), array( 'engine' => 'imagick_webp', 'value' => $detect['imagick_webp'] ? 'yes' : 'no' ), array( 'engine' => 'imagick_avif', 'value' => $detect['imagick_avif'] ? 'yes' : 'no' ), array( 'engine' => 'cli_cwebp', 'value' => $detect['cli_cwebp'] ? $detect['cli_cwebp'] : '-' ), array( 'engine' => 'cli_avifenc', 'value' => $detect['cli_avifenc'] ? $detect['cli_avifenc'] : '-' ), ); $formatter = new WP_CLI\Formatter( $assoc_args, array( 'engine', 'value' ) ); $formatter->display_items( $rows ); $stats = $plugin->converter()->stats(); WP_CLI::line( sprintf( 'Variants: %d | Original size: %s | Next-gen size: %s | Saved: %s', (int) $stats['variants'], ADEE_Converter::human_bytes( $stats['src_bytes'] ), ADEE_Converter::human_bytes( $stats['out_bytes'] ), ADEE_Converter::human_bytes( max( 0, (int) $stats['src_bytes'] - (int) $stats['out_bytes'] ) ) ) ); } /** * Convert attachments to WebP/AVIF. * * ## OPTIONS * * [...] * : Attachment IDs or the word "all". * * [--limit=] * : Max attachments when converting "all". Default 0 (no limit). * * [--format=] * : Restrict to webp or avif. Default: both formats from settings. * * [--skip-existing] * : Skip attachments that already have variants. * * ## EXAMPLES * * wp adee convert all --limit=100 --skip-existing * wp adee convert 123 124 125 * * @param array $args Positional args. * @param array $assoc_args Associative args. */ public function convert( $args, $assoc_args ) { $assoc = wp_parse_args( $assoc_args, array( 'limit' => 0, 'format' => '', ) ); $target = isset( $args[0] ) ? strtolower( (string) $args[0] ) : 'all'; if ( 'all' === $target ) { $ids = ADEE_Bulk::eligible_ids(); if ( ! empty( $assoc['limit'] ) ) { $ids = array_slice( $ids, 0, (int) $assoc['limit'] ); } } else { $ids = array_map( 'intval', $args ); } if ( empty( $ids ) ) { WP_CLI::warning( 'No attachments to convert.' ); return; } $formats = array(); if ( in_array( $assoc['format'], array( 'webp', 'avif' ), true ) ) { $formats = array( $assoc['format'] ); } $skip_existing = ! empty( $assoc['skip-existing'] ) || ! empty( $assoc_args['skip_existing'] ); $converter = ADEE_Plugin::get_instance()->converter(); $progress = \WP_CLI\Utils\make_progress_bar( 'Converting', count( $ids ) ); $ok = 0; $skipped = 0; $errors = 0; foreach ( $ids as $id ) { $res = $converter->convert_attachment( (int) $id, array( 'formats' => $formats, 'skip_existing' => $skip_existing, ) ); if ( 'ok' === $res['status'] ) { $ok++; } elseif ( 'skipped' === $res['status'] ) { $skipped++; } else { $errors++; WP_CLI::debug( '#' . $id . ': ' . ( isset( $res['reason'] ) ? $res['reason'] : 'unknown' ) ); } $progress->tick(); } $progress->finish(); WP_CLI::success( sprintf( 'Converted: %d, skipped: %d, errors: %d.', $ok, $skipped, $errors ) ); } /** * Delete variants and restore originals. * * ## OPTIONS * * [...] * : Attachment IDs or the word "all". * * [--limit=] * : Max attachments per run when using "all". Default 200. * * [--yes] * : Confirm restore-all without prompting. * * ## EXAMPLES * * wp adee restore all --yes * wp adee restore 123 * * @param array $args Positional args. * @param array $assoc_args Associative args. */ public function restore( $args, $assoc_args ) { $assoc = wp_parse_args( $assoc_args, array( 'limit' => 200 ) ); $target = isset( $args[0] ) ? strtolower( (string) $args[0] ) : ''; $converter = ADEE_Plugin::get_instance()->converter(); if ( 'all' === $target ) { if ( empty( $assoc['yes'] ) ) { WP_CLI::confirm( 'This will DELETE all WebP/AVIF variant files. Continue?', $assoc_args ); } $count = $converter->restore_all( (int) $assoc['limit'] ); WP_CLI::success( sprintf( 'Restored %d attachments. Run again if more remain.', $count ) ); return; } if ( empty( $args ) ) { WP_CLI::error( 'Provide attachment IDs or "all".' ); } $restored = 0; foreach ( array_map( 'intval', $args ) as $id ) { if ( $converter->restore_attachment( $id ) ) { $restored++; } } WP_CLI::success( sprintf( 'Restored %d of %d attachments.', $restored, count( $args ) ) ); } }