Skip to content

Commit 6aaeacc

Browse files
committed
Merge branch 'trunk' into fix-forum-17945942
2 parents eb438cb + 903e424 commit 6aaeacc

File tree

2 files changed

+102
-21
lines changed

2 files changed

+102
-21
lines changed

classes/ActionScheduler_Versions.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ class ActionScheduler_Versions {
1818
*/
1919
private $versions = array();
2020

21+
/**
22+
* Registered sources.
23+
*
24+
* @var array<string, string>
25+
*/
26+
private $sources = array();
27+
2128
/**
2229
* Register version's callback.
2330
*
@@ -28,7 +35,13 @@ public function register( $version_string, $initialization_callback ) {
2835
if ( isset( $this->versions[ $version_string ] ) ) {
2936
return false;
3037
}
38+
39+
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
40+
$backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS );
41+
$source = $backtrace[0]['file'];
42+
3143
$this->versions[ $version_string ] = $initialization_callback;
44+
$this->sources[ $source ] = $version_string;
3245
return true;
3346
}
3447

@@ -39,6 +52,15 @@ public function get_versions() {
3952
return $this->versions;
4053
}
4154

55+
/**
56+
* Get registered sources.
57+
*
58+
* @return array<string, string>
59+
*/
60+
public function get_sources() {
61+
return $this->sources;
62+
}
63+
4264
/**
4365
* Get latest version registered.
4466
*/
@@ -87,6 +109,11 @@ public static function initialize_latest_version() {
87109
call_user_func( $self->latest_version_callback() );
88110
}
89111

112+
/**
113+
* Get directory of active source.
114+
*
115+
* @return string
116+
*/
90117
public function active_source() {
91118
return trailingslashit( dirname( __DIR__ ) );
92119
}

classes/WP_CLI/System_Command.php

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -98,38 +98,92 @@ 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' );
101+
$all = (bool) get_flag_value( $assoc_args, 'all' );
102+
$latest = $this->get_latest_version( $instance );
103+
104+
if ( ! $all ) {
105+
echo $latest;
106+
\WP_CLI::halt( 0 );
107+
}
108+
102109
$instance = \ActionScheduler_Versions::instance();
103-
$latest = $this->get_latest_version( $instance );
110+
$versions = $instance->get_versions();
111+
$rows = array();
104112

105-
if ( $all ) {
106-
$versions = $instance->get_versions();
113+
foreach ( $versions as $version => $callback ) {
114+
$active = $version === $latest;
107115

108-
$rows = array();
116+
$rows[ $version ] = array(
117+
'version' => $version,
118+
'callback' => $callback,
119+
'active' => $active ? 'yes' : 'no',
120+
);
121+
}
109122

110-
foreach ( $versions as $version => $callback ) {
111-
$active = 'no';
123+
uksort( $rows, 'version_compare' );
112124

113-
if ( $version === $latest ) {
114-
$active = 'yes';
115-
}
125+
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version', 'callback', 'active' ) );
126+
$formatter->display_items( $rows );
127+
}
116128

117-
$rows[ $version ] = array(
118-
'version' => $version,
119-
'callback' => $callback,
120-
'active' => $active,
121-
);
122-
}
129+
/**
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).
139+
*
140+
* @param array $args Positional args.
141+
* @param array $assoc_args Keyed args.
142+
* @uses \ActionScheduler_Versions::get_sources()
143+
* @uses \WP_CLI\Formatter::display_items()
144+
* @uses $this->get_latest_version()
145+
* @return void
146+
*/
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+
$versions = \ActionScheduler_Versions::instance();
151+
$source = $versions->active_source();
152+
$path = $source;
153+
154+
if ( ! $fullpath ) {
155+
$path = str_replace( ABSPATH, '', $path );
156+
}
123157

124-
uksort( $rows, 'version_compare' );
158+
if ( ! $all ) {
159+
echo $path;
160+
\WP_CLI::halt( 0 );
161+
}
125162

126-
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version', 'callback', 'active' ) );
127-
$formatter->display_items( $rows );
163+
$sources = $versions->get_sources();
164+
$rows = array();
128165

129-
return;
166+
foreach ( $sources as $check_source => $version ) {
167+
$active = dirname( $check_source ) === $source;
168+
$path = $check_source;
169+
170+
if ( ! $fullpath ) {
171+
$path = str_replace( ABSPATH, '', $path );
172+
}
173+
174+
$rows[ $check_source ] = array(
175+
'source' => $path,
176+
'version' => $version,
177+
'active' => $active ? 'yes' : 'no',
178+
);
130179
}
131180

132-
echo $latest;
181+
ksort( $rows );
182+
183+
\WP_CLI::log( PHP_EOL . 'Please note there can only be one unique registered instance of Action Scheduler per ' . PHP_EOL . 'version number, so this list may not include all the currently present copies of ' . PHP_EOL . 'Action Scheduler.' . PHP_EOL );
184+
185+
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'source', 'version', 'active' ) );
186+
$formatter->display_items( $rows );
133187
}
134188

135189
/**

0 commit comments

Comments
 (0)