Skip to content

Commit

Permalink
Merge branch 'trunk' into help-source-name
Browse files Browse the repository at this point in the history
  • Loading branch information
crstauf committed Dec 10, 2024
2 parents 3f36d93 + 903e424 commit ea09e08
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 21 deletions.
27 changes: 27 additions & 0 deletions classes/ActionScheduler_Versions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class ActionScheduler_Versions {
*/
private $versions = array();

/**
* Registered sources.
*
* @var array<string, string>
*/
private $sources = array();

/**
* Register version's callback.
*
Expand All @@ -28,7 +35,13 @@ public function register( $version_string, $initialization_callback ) {
if ( isset( $this->versions[ $version_string ] ) ) {
return false;
}

// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
$backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS );
$source = $backtrace[0]['file'];

$this->versions[ $version_string ] = $initialization_callback;
$this->sources[ $source ] = $version_string;
return true;
}

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

/**
* Get registered sources.
*
* @return array<string, string>
*/
public function get_sources() {
return $this->sources;
}

/**
* Get latest version registered.
*/
Expand Down Expand Up @@ -87,6 +109,11 @@ public static function initialize_latest_version() {
call_user_func( $self->latest_version_callback() );
}

/**
* Get directory of active source.
*
* @return string
*/
public function active_source() {
$file = __FILE__;
$dir = __DIR__;
Expand Down
96 changes: 75 additions & 21 deletions classes/WP_CLI/System_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,38 +98,92 @@ public function status( array $args, array $assoc_args ) {
* @return void
*/
public function version( array $args, array $assoc_args ) {
$all = (bool) get_flag_value( $assoc_args, 'all' );
$all = (bool) get_flag_value( $assoc_args, 'all' );
$latest = $this->get_latest_version( $instance );

if ( ! $all ) {
echo $latest;
\WP_CLI::halt( 0 );
}

$instance = \ActionScheduler_Versions::instance();
$latest = $this->get_latest_version( $instance );
$versions = $instance->get_versions();
$rows = array();

if ( $all ) {
$versions = $instance->get_versions();
foreach ( $versions as $version => $callback ) {
$active = $version === $latest;

$rows = array();
$rows[ $version ] = array(
'version' => $version,
'callback' => $callback,
'active' => $active ? 'yes' : 'no',
);
}

foreach ( $versions as $version => $callback ) {
$active = 'no';
uksort( $rows, 'version_compare' );

if ( $version === $latest ) {
$active = 'yes';
}
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version', 'callback', 'active' ) );
$formatter->display_items( $rows );
}

$rows[ $version ] = array(
'version' => $version,
'callback' => $callback,
'active' => $active,
);
}
/**
* Display the current source, or all registered sources.
*
* ## OPTIONS
*
* [--all]
* : List all registered sources.
*
* [--fullpath]
* : List full path of source(s).
*
* @param array $args Positional args.
* @param array $assoc_args Keyed args.
* @uses \ActionScheduler_Versions::get_sources()
* @uses \WP_CLI\Formatter::display_items()
* @uses $this->get_latest_version()
* @return void
*/
public function source( array $args, array $assoc_args ) {
$all = (bool) get_flag_value( $assoc_args, 'all' );
$fullpath = (bool) get_flag_value( $assoc_args, 'fullpath' );
$versions = \ActionScheduler_Versions::instance();
$source = $versions->active_source();
$path = $source;

if ( ! $fullpath ) {
$path = str_replace( ABSPATH, '', $path );
}

uksort( $rows, 'version_compare' );
if ( ! $all ) {
echo $path;
\WP_CLI::halt( 0 );
}

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

return;
foreach ( $sources as $check_source => $version ) {
$active = dirname( $check_source ) === $source;
$path = $check_source;

if ( ! $fullpath ) {
$path = str_replace( ABSPATH, '', $path );
}

$rows[ $check_source ] = array(
'source' => $path,
'version' => $version,
'active' => $active ? 'yes' : 'no',
);
}

echo $latest;
ksort( $rows );

\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 );

$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'source', 'version', 'active' ) );
$formatter->display_items( $rows );
}

/**
Expand Down

0 comments on commit ea09e08

Please sign in to comment.