-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathGetSiteStats.php
More file actions
139 lines (126 loc) · 3.27 KB
/
Copy pathGetSiteStats.php
File metadata and controls
139 lines (126 loc) · 3.27 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* Get stats for the entire site.
*
* @since 1.15.0
*
* @package Accessibility_Checker
*/
namespace EqualizeDigital\AccessibilityChecker\WPCLI\Command;
use EDAC\Admin\Scans_Stats;
use WP_CLI;
use WP_CLI\ExitException;
/**
* Get stats for the entire site.
*
* @since 1.15.0
*
* @package AccessibilityCheckerCLI
*/
class GetSiteStats implements CLICommandInterface {
/**
* The WP-CLI instance.
*
* This lets a mock be passed in for testing.
*
* @var mixed|WP_CLI
*/
private $wp_cli;
/**
* GetStats constructor.
*
* @param mixed|WP_CLI $wp_cli The WP-CLI instance.
*/
public function __construct( $wp_cli = null ) {
$this->wp_cli = $wp_cli ?? new WP_CLI();
}
/**
* Get the name of the command.
*
* @since 1.15.0
*
* @return string
*/
public static function get_name(): string {
return 'accessibility-checker get-site-stats';
}
/**
* Get the short name of the command.
*
* @since 1.33.0
*
* @return string
*/
public static function get_shortname(): string {
return 'edac get-site-stats';
}
/**
* Get the arguments for the command
*
* @since 1.15.0
*
* @return array
*/
public static function get_args(): array {
return [
'synopsis' => [
[
'type' => 'assoc',
'name' => 'stat',
'description' => esc_html__( 'Keys to show in the results. Defaults to all keys.', 'accessibility-checker' ),
'optional' => true,
'default' => null,
'repeating' => true,
],
[
'type' => 'flag',
'name' => 'clear-cache',
'description' => esc_html__( 'Clear the cache before retrieving the stats (can be intensive).', 'accessibility-checker' ),
'repeating' => false,
'optional' => true,
],
],
];
}
/**
* Gets the accessibility-checker stats for the whole site. Use the --clear-cache flag to clear the cache before retrieving the stats.
*
* @since 1.15.0
*
* @param array $options The positional argument, none is this case.
* @param array $arguments The associative arguments, the stat keys in this case.
*
* @return void
* @throws ExitException If the post ID does not exist, or the class we need isn't available.
*/
public function __invoke( array $options = [], array $arguments = [] ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if ( ! empty( $arguments['clear-cache'] ) ) {
// Clear the cache.
( new Scans_Stats() )->clear_cache();
}
$all_stats = ( new Scans_Stats() )->summary();
if ( ! empty( $arguments['stat'] ) ) {
$items_to_return = [];
$requested_stats = explode( ',', $arguments['stat'] );
foreach ( $requested_stats as $key ) {
$stats_key = trim( $key );
if ( ! isset( $all_stats[ $stats_key ] ) ) {
$this->wp_cli::error(
sprintf(
// translators: 1: a stat key that was requested but not found.
esc_html__( 'Stat key: %1$s not found in stats.', 'accessibility-checker' ),
$stats_key
)
);
return;
}
$items_to_return[ $stats_key ] = $all_stats[ $stats_key ];
}
}
if ( isset( $items_to_return ) ) {
$this->wp_cli::success( wp_json_encode( $items_to_return, JSON_PRETTY_PRINT ) );
return;
}
$this->wp_cli::success( wp_json_encode( $all_stats, JSON_PRETTY_PRINT ) );
}
}