Skip to content

Commit ad3c5ab

Browse files
committed
Add WPAdminCommand
1 parent d5a5d9e commit ad3c5ab

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

command.php

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use TypistTech\ImageOptimizeCommand\CLI\Commands\FindCommand;
1111
use TypistTech\ImageOptimizeCommand\CLI\Commands\ResetCommand;
1212
use TypistTech\ImageOptimizeCommand\CLI\Commands\RestoreCommand;
13+
use TypistTech\ImageOptimizeCommand\CLI\Commands\WPAdminCommand;
1314
use WP_CLI;
1415

1516
if (! class_exists('WP_CLI')) {
@@ -25,3 +26,4 @@
2526
WP_CLI::add_command('image-optimize reset', ResetCommand::class);
2627

2728
WP_CLI::add_command('image-optimize find', FindCommand::class);
29+
WP_CLI::add_command('image-optimize wp-admin', WPAdminCommand::class);

src/CLI/Commands/CommandNamespace.php

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
*
2626
* # Find and optimize images under a given directory.
2727
* $ wp image-optimize find /path/to/my/directory
28+
*
29+
* # Find and optimize images under wp-admin.
30+
* $ wp image-optimize wp-admin
2831
*/
2932
class CommandNamespace extends BaseCommandNamespace
3033
{

src/CLI/Commands/WPAdminCommand.php

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

0 commit comments

Comments
 (0)