-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathBootstrapCLI.php
More file actions
117 lines (107 loc) · 2.78 KB
/
Copy pathBootstrapCLI.php
File metadata and controls
117 lines (107 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* Bootstrap the CLI commands for the Accessibility Checker plugin.
*
* @since 1.15.0
*
* @package Accessibility_Checker
*/
namespace EqualizeDigital\AccessibilityChecker\WPCLI;
use EqualizeDigital\AccessibilityChecker\WPCLI\Command\CLICommandInterface;
use EqualizeDigital\AccessibilityChecker\WPCLI\Command\DeleteStats;
use EqualizeDigital\AccessibilityChecker\WPCLI\Command\GetSiteStats;
use EqualizeDigital\AccessibilityChecker\WPCLI\Command\GetStats;
use EqualizeDigital\AccessibilityChecker\WPCLI\Command\CleanupOrphanedIssues;
use Exception;
use WP_CLI;
/**
* Handles the registration of WP-CLI commands for the Accessibility Checker plugin.
*
* @since 1.15.0
*/
class BootstrapCLI {
/**
* The WP-CLI instance.
*
* This allows injecting a mock WP-CLI instance for testing.
*
* @since 1.15.0
*
* @var WP_CLI
*/
private $wp_cli;
/**
* The boot method on this class will use this array to register custom WP-CLI commands.
*
* @since 1.15.0
*
* @var CLICommandInterface[]
*/
protected array $commands = [
DeleteStats::class,
GetSiteStats::class,
GetStats::class,
CleanupOrphanedIssues::class,
];
/**
* Set up the internal wp_cli property.
*
* @since 1.15.0
*
* @param WP_CLI|null $wp_cli The WP-CLI instance.
*/
public function __construct( $wp_cli = null ) {
$this->wp_cli = $wp_cli ? $wp_cli : new WP_CLI();
}
/**
* Register the WP-CLI commands by looping through the commands array and adding each command.
*
* @since 1.15.0
*
* @return void
*/
public function register() {
// Bail if not running in WP_CLI.
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
return;
}
/**
* Filter the list of classes that hold the commands to be registered.
*
* @since 1.15.0
*
* @param CLICommandInterface[] $commands array of classes to register as commands.
*/
$commands = apply_filters( 'edac_filter_command_classes', $this->commands );
foreach ( $commands as $command ) {
// All commands must follow the interface.
if ( ! is_subclass_of( $command, CLICommandInterface::class, true ) ) {
continue;
}
try {
$this->wp_cli::add_command(
$command::get_name(),
$command,
$command::get_args()
);
// For ease of typing on cli there's a shortname too in some commands.
if ( method_exists( $command, 'get_shortname' ) ) {
$this->wp_cli::add_command(
$command::get_shortname(),
$command,
$command::get_args()
);
}
} catch ( Exception $e ) {
$this->wp_cli::warning(
sprintf(
// translators: 1: a php classname, 2: an error message that was thrown about why this failed to register.
esc_html__( 'Failed to register command %1$s because %2$s', 'accessibility-checker' ),
$command,
$e->getMessage()
)
);
}
}
}
}