From 1eae586b46a9e11a21e9e3dfe0dba380ba4ee3b7 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 17 Jul 2024 20:53:13 -0400 Subject: [PATCH 1/9] track sources and add sources command --- classes/ActionScheduler_Versions.php | 11 ++++++- .../ActionScheduler_WPCLI_System_Command.php | 32 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/classes/ActionScheduler_Versions.php b/classes/ActionScheduler_Versions.php index 915c2e632..58dcd166f 100644 --- a/classes/ActionScheduler_Versions.php +++ b/classes/ActionScheduler_Versions.php @@ -10,12 +10,18 @@ class ActionScheduler_Versions { private static $instance = NULL; private $versions = array(); + private $sources = array(); public function register( $version_string, $initialization_callback ) { if ( isset($this->versions[$version_string]) ) { return FALSE; } + + $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); + $source = $backtrace[0]['file']; + $this->versions[$version_string] = $initialization_callback; + $this->sources[$source] = $version_string; return TRUE; } @@ -23,6 +29,10 @@ public function get_versions() { return $this->versions; } + public function get_sources() { + return $this->sources; + } + public function latest_version() { $keys = array_keys($this->versions); if ( empty($keys) ) { @@ -59,4 +69,3 @@ public static function initialize_latest_version() { call_user_func($self->latest_version_callback()); } } - \ No newline at end of file diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php index 59e6c35d5..2279c3021 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php +++ b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php @@ -51,6 +51,7 @@ public function runner( array $args, array $assoc_args ) { * @return void */ public function status( array $args, array $assoc_args ) { + $format = get_flag_value( $this->assoc_args, 'group', '' ); /** * Get runner status. * @@ -110,7 +111,7 @@ public function version( array $args, array $assoc_args ) { ); } - ksort( $rows, SORT_NUMERIC ); + uksort( $rows, 'version_compare' ); $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version', 'callback' ) ); $formatter->display_items( $rows ); @@ -121,6 +122,35 @@ public function version( array $args, array $assoc_args ) { echo $this->get_latest_version( $instance ); } + /** + * Get all system sources. + * + * @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 sources( array $args, array $assoc_args ) { + $instance = \ActionScheduler_Versions::instance(); + $sources = $instance->get_sources(); + + $rows = array(); + + foreach ( $sources as $source => $version ) { + $rows[ $source ] = array( + 'source' => str_replace( ABSPATH, '', $source ), + 'version' => $version, + ); + } + + ksort( $rows ); + + $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'source', 'version' ) ); + $formatter->display_items( $rows ); + } + /** * Get current data store. * From bdf3720281a09c8d1cb866183b8f04ef4872c295 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 17 Jul 2024 21:02:09 -0400 Subject: [PATCH 2/9] cleanup --- classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php | 1 - 1 file changed, 1 deletion(-) diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php index 2279c3021..94d68d099 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php +++ b/classes/WP_CLI/ActionScheduler_WPCLI_System_Command.php @@ -51,7 +51,6 @@ public function runner( array $args, array $assoc_args ) { * @return void */ public function status( array $args, array $assoc_args ) { - $format = get_flag_value( $this->assoc_args, 'group', '' ); /** * Get runner status. * From 712987d98bf6146370d9e171c3e84c9f19f15530 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Wed, 14 Aug 2024 12:44:15 -0400 Subject: [PATCH 3/9] save --- classes/WP_CLI/Action/Cancel_Command.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/WP_CLI/Action/Cancel_Command.php b/classes/WP_CLI/Action/Cancel_Command.php index 0f1a161e2..a9802e780 100644 --- a/classes/WP_CLI/Action/Cancel_Command.php +++ b/classes/WP_CLI/Action/Cancel_Command.php @@ -110,8 +110,8 @@ protected function print_error( \Exception $e, $multiple ) { \WP_CLI::error( sprintf( /* translators: %1$s: singular or plural %2$s: refers to the exception error message. */ - __( 'There was an error cancelling the scheduled %1$s: %2$s', 'action-scheduler' ), - $multiple ? 'actions' : 'action', + __( 'There was an error cancelling the %1$s: %2$s', 'action-scheduler' ), + $multiple ? __( 'scheduled actions', 'action-scheduler' ) : __( 'scheduled action', 'action-scheduler' ), $e->getMessage() ) ); From 45722c2724acc9c70a5db826786359e5326e4c6d Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Mon, 2 Dec 2024 16:02:04 -0500 Subject: [PATCH 4/9] optimize and add "all" associative argument --- classes/WP_CLI/System_Command.php | 87 +++++++++++++++++++------------ 1 file changed, 55 insertions(+), 32 deletions(-) diff --git a/classes/WP_CLI/System_Command.php b/classes/WP_CLI/System_Command.php index 44b33ce64..e539795e3 100644 --- a/classes/WP_CLI/System_Command.php +++ b/classes/WP_CLI/System_Command.php @@ -98,42 +98,44 @@ 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' ); - $instance = \ActionScheduler_Versions::instance(); - $latest = $this->get_latest_version( $instance ); - - if ( $all ) { - $versions = $instance->get_versions(); - - $rows = array(); + $all = (bool) get_flag_value( $assoc_args, 'all' ); + $latest = $this->get_latest_version( $instance ); - foreach ( $versions as $version => $callback ) { - $active = 'no'; - - if ( $version === $latest ) { - $active = 'yes'; - } - - $rows[ $version ] = array( - 'version' => $version, - 'callback' => $callback, - 'active' => $active, - ); - } + if ( ! $all ) { + echo $latest; + \WP_CLI::halt( 0 ); + } - uksort( $rows, 'version_compare' ); + $instance = \ActionScheduler_Versions::instance(); + $versions = $instance->get_versions(); + $rows = array(); - $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version', 'callback', 'active' ) ); - $formatter->display_items( $rows ); + foreach ( $versions as $version => $callback ) { + $active = $version === $latest; - return; + $rows[ $version ] = array( + 'version' => $version, + 'callback' => $callback, + 'active' => $active ? 'yes' : 'no', + ); } - echo $latest; + uksort( $rows, 'version_compare' ); + + $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version', 'callback', 'active' ) ); + $formatter->display_items( $rows ); } /** - * Get all system sources. + * 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. @@ -142,22 +144,43 @@ public function version( array $args, array $assoc_args ) { * @uses $this->get_latest_version() * @return void */ - public function sources( array $args, array $assoc_args ) { + public function source( array $args, array $assoc_args ) { + $all = (bool) get_flag_value( $assoc_args, 'all' ); + $fullpath = (bool) get_flag_value( $assoc_args, 'fullpath' ); + $source = dirname( __DIR__, 2 ); + $path = $source; + + if ( ! $fullpath ) { + $path = str_replace( ABSPATH, '', $path ); + } + + if ( ! $all ) { + echo $path; + \WP_CLI::halt( 0 ); + } + $instance = \ActionScheduler_Versions::instance(); $sources = $instance->get_sources(); + $rows = array(); + + foreach ( $sources as $check_source => $version ) { + $active = dirname( $check_source ) === $source; + $path = $source; - $rows = array(); + if ( ! $fullpath ) { + $path = str_replace( ABSPATH, '', $path ); + } - foreach ( $sources as $source => $version ) { $rows[ $source ] = array( - 'source' => str_replace( ABSPATH, '', $source ), + 'source' => $path, 'version' => $version, + 'active' => $active ? 'yes' : 'no', ); } ksort( $rows ); - $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'source', 'version' ) ); + $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'source', 'version', 'active' ) ); $formatter->display_items( $rows ); } From 5650cee55254db0c073c3bca81c358cf7003def9 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Fri, 6 Dec 2024 13:57:59 -0500 Subject: [PATCH 5/9] fix variable assignment and usage --- classes/WP_CLI/System_Command.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/WP_CLI/System_Command.php b/classes/WP_CLI/System_Command.php index e539795e3..d61a2e500 100644 --- a/classes/WP_CLI/System_Command.php +++ b/classes/WP_CLI/System_Command.php @@ -165,13 +165,13 @@ public function source( array $args, array $assoc_args ) { foreach ( $sources as $check_source => $version ) { $active = dirname( $check_source ) === $source; - $path = $source; + $path = $check_source; if ( ! $fullpath ) { $path = str_replace( ABSPATH, '', $path ); } - $rows[ $source ] = array( + $rows[ $check_source ] = array( 'source' => $path, 'version' => $version, 'active' => $active ? 'yes' : 'no', From b3b93efc679a49efb52a688c0b7bedf2f1964b33 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Mon, 9 Dec 2024 14:08:09 -0500 Subject: [PATCH 6/9] add phpdoc blocks --- classes/ActionScheduler_Versions.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/classes/ActionScheduler_Versions.php b/classes/ActionScheduler_Versions.php index ccc74e0a1..8f4cbc64c 100644 --- a/classes/ActionScheduler_Versions.php +++ b/classes/ActionScheduler_Versions.php @@ -17,7 +17,13 @@ class ActionScheduler_Versions { * @var array */ private $versions = array(); - private $sources = array(); + + /** + * Registered sources. + * + * @var array + */ + private $sources = array(); /** * Register version's callback. @@ -45,6 +51,11 @@ public function get_versions() { return $this->versions; } + /** + * Get registered sources. + * + * @return array + */ public function get_sources() { return $this->sources; } @@ -97,6 +108,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() { return trailingslashit( dirname( __DIR__ ) ); } From 2409dddfac5fa98392654d0921daf6b5fd4b0565 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Mon, 9 Dec 2024 14:08:28 -0500 Subject: [PATCH 7/9] use new method and add notice --- classes/WP_CLI/System_Command.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/classes/WP_CLI/System_Command.php b/classes/WP_CLI/System_Command.php index d61a2e500..9b2102faa 100644 --- a/classes/WP_CLI/System_Command.php +++ b/classes/WP_CLI/System_Command.php @@ -137,6 +137,9 @@ public function version( array $args, array $assoc_args ) { * [--fullpath] * : List full path of source(s). * + * [--skip-notice] + * : Skip notice about registered sources. + * * @param array $args Positional args. * @param array $assoc_args Keyed args. * @uses \ActionScheduler_Versions::get_sources() @@ -147,7 +150,9 @@ public function version( array $args, array $assoc_args ) { public function source( array $args, array $assoc_args ) { $all = (bool) get_flag_value( $assoc_args, 'all' ); $fullpath = (bool) get_flag_value( $assoc_args, 'fullpath' ); - $source = dirname( __DIR__, 2 ); + $skipnote = (bool) get_flag_value( $assoc_args, 'skip-notice' ); + $versions = \ActionScheduler_Versions::instance(); + $source = $versions->active_source(); $path = $source; if ( ! $fullpath ) { @@ -159,9 +164,8 @@ public function source( array $args, array $assoc_args ) { \WP_CLI::halt( 0 ); } - $instance = \ActionScheduler_Versions::instance(); - $sources = $instance->get_sources(); - $rows = array(); + $sources = $versions->get_sources(); + $rows = array(); foreach ( $sources as $check_source => $version ) { $active = dirname( $check_source ) === $source; @@ -180,6 +184,10 @@ public function source( array $args, array $assoc_args ) { ksort( $rows ); + if ( ! $skipnote ) { + \WP_CLI::line( 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 ); } From fd96702256f401ca626e08525f0c1da374a526a5 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Mon, 9 Dec 2024 15:59:06 -0500 Subject: [PATCH 8/9] change to use log helper --- classes/WP_CLI/System_Command.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/classes/WP_CLI/System_Command.php b/classes/WP_CLI/System_Command.php index 9b2102faa..fe79269df 100644 --- a/classes/WP_CLI/System_Command.php +++ b/classes/WP_CLI/System_Command.php @@ -137,9 +137,6 @@ public function version( array $args, array $assoc_args ) { * [--fullpath] * : List full path of source(s). * - * [--skip-notice] - * : Skip notice about registered sources. - * * @param array $args Positional args. * @param array $assoc_args Keyed args. * @uses \ActionScheduler_Versions::get_sources() @@ -150,7 +147,6 @@ public function version( array $args, array $assoc_args ) { public function source( array $args, array $assoc_args ) { $all = (bool) get_flag_value( $assoc_args, 'all' ); $fullpath = (bool) get_flag_value( $assoc_args, 'fullpath' ); - $skipnote = (bool) get_flag_value( $assoc_args, 'skip-notice' ); $versions = \ActionScheduler_Versions::instance(); $source = $versions->active_source(); $path = $source; @@ -184,9 +180,7 @@ public function source( array $args, array $assoc_args ) { ksort( $rows ); - if ( ! $skipnote ) { - \WP_CLI::line( 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 ); - } + \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 ); From 6a71ea70879275dae75ec41162ee732c89cd3857 Mon Sep 17 00:00:00 2001 From: barryhughes <3594411+barryhughes@users.noreply.github.com> Date: Mon, 9 Dec 2024 15:54:10 -0800 Subject: [PATCH 9/9] PHPCS fixes. --- classes/ActionScheduler_Versions.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/classes/ActionScheduler_Versions.php b/classes/ActionScheduler_Versions.php index 8f4cbc64c..7841afd18 100644 --- a/classes/ActionScheduler_Versions.php +++ b/classes/ActionScheduler_Versions.php @@ -36,11 +36,12 @@ public function register( $version_string, $initialization_callback ) { 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; + $this->sources[ $source ] = $version_string; return true; }