Skip to content

Commit ec8a527

Browse files
committed
Add PluginsCommand
1 parent ad3c5ab commit ec8a527

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

command.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use TypistTech\ImageOptimizeCommand\CLI\Commands\BatchCommand;
99
use TypistTech\ImageOptimizeCommand\CLI\Commands\CommandNamespace;
1010
use TypistTech\ImageOptimizeCommand\CLI\Commands\FindCommand;
11+
use TypistTech\ImageOptimizeCommand\CLI\Commands\PluginsCommand;
1112
use TypistTech\ImageOptimizeCommand\CLI\Commands\ResetCommand;
1213
use TypistTech\ImageOptimizeCommand\CLI\Commands\RestoreCommand;
1314
use TypistTech\ImageOptimizeCommand\CLI\Commands\WPAdminCommand;
@@ -27,3 +28,4 @@
2728

2829
WP_CLI::add_command('image-optimize find', FindCommand::class);
2930
WP_CLI::add_command('image-optimize wp-admin', WPAdminCommand::class);
31+
WP_CLI::add_command('image-optimize plugins', PluginsCommand::class);

src/CLI/Commands/CommandNamespace.php

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
* # Find and optimize images under a given directory.
2727
* $ wp image-optimize find /path/to/my/directory
2828
*
29+
* # Find and optimize images under wp-content/plugins.
30+
* $ wp image-optimize plugins
31+
*
2932
* # Find and optimize images under wp-admin.
3033
* $ wp image-optimize wp-admin
3134
*/

src/CLI/Commands/PluginsCommand.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace TypistTech\ImageOptimizeCommand\CLI\Commands;
5+
6+
use WP_CLI;
7+
8+
class PluginsCommand
9+
{
10+
/**
11+
* Find and optimize images under wp-content/plugins.
12+
*
13+
* ## OPTIONS
14+
*
15+
* [--extensions=<extensions>]
16+
* : File types to optimize, separated by commas.
17+
* Default: gif,jpeg,jpg,png
18+
*
19+
* ## EXAMPLES
20+
*
21+
* # Find and optimize images under wp-content/plugins
22+
* $ wp image-optimize plugins
23+
*
24+
* # Find and optimize SVGs,PNGs under wp-content/plugins
25+
* $ wp image-optimize plugins --extensions=svg,png
26+
*/
27+
public function __invoke($_, $assocArgs): void
28+
{
29+
if (! defined('WP_PLUGIN_DIR')) {
30+
WP_CLI::error("Constant 'WP_PLUGIN_DIR' not defined. Is WordPress loaded?");
31+
}
32+
33+
$directory = constant('WP_PLUGIN_DIR');
34+
35+
$findCommand = new FindCommand();
36+
$findCommand([$directory], $assocArgs);
37+
}
38+
}

0 commit comments

Comments
 (0)