Skip to content

Commit 45722c2

Browse files
committed
optimize and add "all" associative argument
1 parent 094741b commit 45722c2

File tree

1 file changed

+55
-32
lines changed

1 file changed

+55
-32
lines changed

classes/WP_CLI/System_Command.php

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -98,42 +98,44 @@ public function status( array $args, array $assoc_args ) {
9898
* @return void
9999
*/
100100
public function version( array $args, array $assoc_args ) {
101-
$all = (bool) get_flag_value( $assoc_args, 'all' );
102-
$instance = \ActionScheduler_Versions::instance();
103-
$latest = $this->get_latest_version( $instance );
104-
105-
if ( $all ) {
106-
$versions = $instance->get_versions();
107-
108-
$rows = array();
101+
$all = (bool) get_flag_value( $assoc_args, 'all' );
102+
$latest = $this->get_latest_version( $instance );
109103

110-
foreach ( $versions as $version => $callback ) {
111-
$active = 'no';
112-
113-
if ( $version === $latest ) {
114-
$active = 'yes';
115-
}
116-
117-
$rows[ $version ] = array(
118-
'version' => $version,
119-
'callback' => $callback,
120-
'active' => $active,
121-
);
122-
}
104+
if ( ! $all ) {
105+
echo $latest;
106+
\WP_CLI::halt( 0 );
107+
}
123108

124-
uksort( $rows, 'version_compare' );
109+
$instance = \ActionScheduler_Versions::instance();
110+
$versions = $instance->get_versions();
111+
$rows = array();
125112

126-
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version', 'callback', 'active' ) );
127-
$formatter->display_items( $rows );
113+
foreach ( $versions as $version => $callback ) {
114+
$active = $version === $latest;
128115

129-
return;
116+
$rows[ $version ] = array(
117+
'version' => $version,
118+
'callback' => $callback,
119+
'active' => $active ? 'yes' : 'no',
120+
);
130121
}
131122

132-
echo $latest;
123+
uksort( $rows, 'version_compare' );
124+
125+
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version', 'callback', 'active' ) );
126+
$formatter->display_items( $rows );
133127
}
134128

135129
/**
136-
* Get all system sources.
130+
* Display the current source, or all registered sources.
131+
*
132+
* ## OPTIONS
133+
*
134+
* [--all]
135+
* : List all registered sources.
136+
*
137+
* [--fullpath]
138+
* : List full path of source(s).
137139
*
138140
* @param array $args Positional args.
139141
* @param array $assoc_args Keyed args.
@@ -142,22 +144,43 @@ public function version( array $args, array $assoc_args ) {
142144
* @uses $this->get_latest_version()
143145
* @return void
144146
*/
145-
public function sources( array $args, array $assoc_args ) {
147+
public function source( array $args, array $assoc_args ) {
148+
$all = (bool) get_flag_value( $assoc_args, 'all' );
149+
$fullpath = (bool) get_flag_value( $assoc_args, 'fullpath' );
150+
$source = dirname( __DIR__, 2 );
151+
$path = $source;
152+
153+
if ( ! $fullpath ) {
154+
$path = str_replace( ABSPATH, '', $path );
155+
}
156+
157+
if ( ! $all ) {
158+
echo $path;
159+
\WP_CLI::halt( 0 );
160+
}
161+
146162
$instance = \ActionScheduler_Versions::instance();
147163
$sources = $instance->get_sources();
164+
$rows = array();
165+
166+
foreach ( $sources as $check_source => $version ) {
167+
$active = dirname( $check_source ) === $source;
168+
$path = $source;
148169

149-
$rows = array();
170+
if ( ! $fullpath ) {
171+
$path = str_replace( ABSPATH, '', $path );
172+
}
150173

151-
foreach ( $sources as $source => $version ) {
152174
$rows[ $source ] = array(
153-
'source' => str_replace( ABSPATH, '', $source ),
175+
'source' => $path,
154176
'version' => $version,
177+
'active' => $active ? 'yes' : 'no',
155178
);
156179
}
157180

158181
ksort( $rows );
159182

160-
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'source', 'version' ) );
183+
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'source', 'version', 'active' ) );
161184
$formatter->display_items( $rows );
162185
}
163186

0 commit comments

Comments
 (0)