• Resolved Václav Greif

    (@vasikgreif)


    Hi, thanks for an amazing plugin! I checked the code and I think it should be pretty straightforward to add basic WP CLI bulk convert support:

    $config = Config::loadConfigAndFix();
    	$arr = BulkConvert::getList($config);
    	foreach ( $arr as $group ) {
    		foreach ( $group['files'] as $file ) {
    			Convert::convertFile(trailingslashit($group['root']). $file);
    		}
    	}

    Is there anything wrong with this? I’ll be happy to do this properly and submit a pull request if interested.

    Thanks!
    Václav

    • This topic was modified 3 years, 5 months ago by Václav Greif.
Viewing 7 replies - 1 through 7 (of 7 total)
  • I second this, that would be awsome! ??

    Thread Starter Václav Greif

    (@vasikgreif)

    Here’s a complete version that warks well for me and includes a simple progress bar:

    functions.php

    if ( class_exists( 'WP_CLI' ) ) {
    	require_once 'class.webp-express-cli.php';
    	\WP_CLI::add_command('webp_express', '\WebPExpress\BackgroundConvert');
    	//\WebPExpress\CliInit::init();
    }

    class.webp-express-cli.php

    <?php
    
    namespace WebPExpress;
    
    class BackgroundConvert extends \WP_CLI_Command
    {
    	public function bgconvert($args, $assoc_args)
    	{
    		$config = Config::loadConfigAndFix();
    		$arr = BulkConvert::getList($config);
    		foreach ( $arr as $group ) {
    			$count = count($group['files']);
    			$progress = \WP_CLI\Utils\make_progress_bar( sprintf('Converting %s files', $count), $count );
    			$files = array_reverse($group['files']);
    			foreach ( $files as $key =>  $file ) {
    				$path = trailingslashit($group['root']). $file;
    				\WP_CLI::log(sprintf('%s/%s : %s', $progress->current(), $count, $path));
    				$progress->tick();
    				Convert::convertFile($path);
    			}
    			$progress->finish();
    		}
    	}
    }
    • This reply was modified 3 years, 5 months ago by Václav Greif.
    Plugin Author rosell.dk

    (@roselldk)

    Thanks, guys.
    I’ll add WP CLI support in the next release

    Thread Starter Václav Greif

    (@vasikgreif)

    Very cool, thanks!

    Plugin Author rosell.dk

    (@roselldk)

    Notice that one may run into permission problems when mixing between converting with WP CLI and the admin UI. The webp files and folders get created with www-data group through web ui. But through the CLI, they get created with the user that runs the CLI. After running CLI, Bulk convert and the other mechanisms for converting may fail (when trying to create a webp file in a folder not accessible for www-data)

    Is there a workaround for this? (for the FAQ)

    • This reply was modified 3 years, 5 months ago by rosell.dk.
    • This reply was modified 3 years, 5 months ago by rosell.dk.
    • This reply was modified 3 years, 5 months ago by rosell.dk.
    Plugin Author rosell.dk

    (@roselldk)

    I can see that same thing happens with permissions with wp media regenerate. So the issue should be familiar for users of WP CLI

    Plugin Author rosell.dk

    (@roselldk)

    Published! (0.20.0).

    You can use the –help option to learn about the options:
    wp webp-express --help. Displays the available commands
    wp webp-express convert --help. Displays the available options for the “convert” command.

    A few examples:
    wp webp-express convert: Creates webp images for all unconverted images
    wp webp-express convert --reconvert: Also convert images that are already converted
    wp webp-express convert themes: Only images in the themes folder
    wp webp-express convert --only-png: Only the PNG images
    wp webp-express convert --quality=50: Use quality 50 (instead of what was entered in settings screen)
    wp webp-express convert --quality=50: Use quality 50 (instead of what was entered in settings screen)

    wp webp-express flushwebp: Remove all webp images
    wp webp-express flushwebp --only-png: Remove all webp images that are conversions of PNG images

    I’m considering adding commands for viewing status, viewing conversion stats, generating the .htaccess files and modifying the settings. Please let me know if you need any of these or perhaps something else.

    • This reply was modified 3 years, 5 months ago by rosell.dk.
    • This reply was modified 3 years, 5 months ago by rosell.dk.
    • This reply was modified 3 years, 5 months ago by rosell.dk.
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘WP CLI Support for bulk convert’ is closed to new replies.