Action
1 month ago
ActionScheduler_WPCLI_Clean_Command.php
1 year ago
ActionScheduler_WPCLI_QueueRunner.php
1 year ago
ActionScheduler_WPCLI_Scheduler_command.php
1 year ago
Action_Command.php
1 year ago
Migration_Command.php
1 year ago
ProgressBar.php
1 year ago
System_Command.php
1 month ago
System_Command.php
283 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Action_Scheduler\WP_CLI; |
| 4 | |
| 5 | // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaping output is not necessary in WP CLI. |
| 6 | |
| 7 | use ActionScheduler_SystemInformation; |
| 8 | use WP_CLI; |
| 9 | use function \WP_CLI\Utils\get_flag_value; |
| 10 | |
| 11 | /** |
| 12 | * System info WP-CLI commands for Action Scheduler. |
| 13 | */ |
| 14 | class System_Command { |
| 15 | |
| 16 | /** |
| 17 | * Data store for querying actions |
| 18 | * |
| 19 | * @var ActionScheduler_Store |
| 20 | */ |
| 21 | protected $store; |
| 22 | |
| 23 | /** |
| 24 | * Construct. |
| 25 | */ |
| 26 | public function __construct() { |
| 27 | $this->store = \ActionScheduler::store(); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Print in-use data store class. |
| 32 | * |
| 33 | * @param array $args Positional args. |
| 34 | * @param array $assoc_args Keyed args. |
| 35 | * @return void |
| 36 | * |
| 37 | * @subcommand data-store |
| 38 | */ |
| 39 | public function datastore( array $args, array $assoc_args ) { |
| 40 | echo $this->get_current_datastore(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Print in-use runner class. |
| 45 | * |
| 46 | * @param array $args Positional args. |
| 47 | * @param array $assoc_args Keyed args. |
| 48 | * @return void |
| 49 | */ |
| 50 | public function runner( array $args, array $assoc_args ) { |
| 51 | echo $this->get_current_runner(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get system status. |
| 56 | * |
| 57 | * @param array $args Positional args. |
| 58 | * @param array $assoc_args Keyed args. |
| 59 | * @return void |
| 60 | */ |
| 61 | public function status( array $args, array $assoc_args ) { |
| 62 | /** |
| 63 | * Get runner status. |
| 64 | * |
| 65 | * @link https://github.com/woocommerce/action-scheduler-disable-default-runner |
| 66 | */ |
| 67 | $runner_enabled = has_action( 'action_scheduler_run_queue', array( \ActionScheduler::runner(), 'run' ) ); |
| 68 | |
| 69 | \WP_CLI::line( sprintf( 'Data store: %s', $this->get_current_datastore() ) ); |
| 70 | \WP_CLI::line( sprintf( 'Runner: %s%s', $this->get_current_runner(), ( $runner_enabled ? '' : ' (disabled)' ) ) ); |
| 71 | \WP_CLI::line( sprintf( 'Version: %s', $this->get_latest_version() ) ); |
| 72 | |
| 73 | $rows = array(); |
| 74 | $action_counts = $this->store->action_counts(); |
| 75 | $oldest_and_newest = $this->get_oldest_and_newest( array_keys( $action_counts ) ); |
| 76 | |
| 77 | foreach ( $action_counts as $status => $count ) { |
| 78 | $rows[] = array( |
| 79 | 'status' => $status, |
| 80 | 'count' => $count, |
| 81 | 'oldest' => $oldest_and_newest[ $status ]['oldest'], |
| 82 | 'newest' => $oldest_and_newest[ $status ]['newest'], |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'status', 'count', 'oldest', 'newest' ) ); |
| 87 | $formatter->display_items( $rows ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Display the active version, or all registered versions. |
| 92 | * |
| 93 | * ## OPTIONS |
| 94 | * |
| 95 | * [--all] |
| 96 | * : List all registered versions. |
| 97 | * |
| 98 | * @param array $args Positional args. |
| 99 | * @param array $assoc_args Keyed args. |
| 100 | * @return void |
| 101 | */ |
| 102 | public function version( array $args, array $assoc_args ) { |
| 103 | $all = (bool) get_flag_value( $assoc_args, 'all' ); |
| 104 | $latest = $this->get_latest_version(); |
| 105 | |
| 106 | if ( ! $all ) { |
| 107 | echo $latest; |
| 108 | \WP_CLI::halt( 0 ); |
| 109 | } |
| 110 | |
| 111 | $instance = \ActionScheduler_Versions::instance(); |
| 112 | $versions = $instance->get_versions(); |
| 113 | $rows = array(); |
| 114 | |
| 115 | foreach ( $versions as $version => $callback ) { |
| 116 | $active = $version === $latest; |
| 117 | |
| 118 | $rows[ $version ] = array( |
| 119 | 'version' => $version, |
| 120 | 'callback' => $callback, |
| 121 | 'active' => $active ? 'yes' : 'no', |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | uksort( $rows, 'version_compare' ); |
| 126 | |
| 127 | $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version', 'callback', 'active' ) ); |
| 128 | $formatter->display_items( $rows ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Display the current source, or all registered sources. |
| 133 | * |
| 134 | * ## OPTIONS |
| 135 | * |
| 136 | * [--all] |
| 137 | * : List all registered sources. |
| 138 | * |
| 139 | * [--fullpath] |
| 140 | * : List full path of source(s). |
| 141 | * |
| 142 | * @param array $args Positional args. |
| 143 | * @param array $assoc_args Keyed args. |
| 144 | * @uses ActionScheduler_SystemInformation::active_source_path() |
| 145 | * @uses \WP_CLI\Formatter::display_items() |
| 146 | * @uses $this->get_latest_version() |
| 147 | * @return void |
| 148 | */ |
| 149 | public function source( array $args, array $assoc_args ) { |
| 150 | $all = (bool) get_flag_value( $assoc_args, 'all' ); |
| 151 | $fullpath = (bool) get_flag_value( $assoc_args, 'fullpath' ); |
| 152 | $source = ActionScheduler_SystemInformation::active_source_path(); |
| 153 | $path = $source; |
| 154 | |
| 155 | if ( ! $fullpath ) { |
| 156 | $path = str_replace( ABSPATH, '', $path ); |
| 157 | } |
| 158 | |
| 159 | if ( ! $all ) { |
| 160 | echo $path; |
| 161 | \WP_CLI::halt( 0 ); |
| 162 | } |
| 163 | |
| 164 | $sources = ActionScheduler_SystemInformation::get_sources(); |
| 165 | |
| 166 | if ( empty( $sources ) ) { |
| 167 | WP_CLI::log( __( 'Detailed information about registered sources is not currently available.', 'action-scheduler' ) ); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | $rows = array(); |
| 172 | |
| 173 | foreach ( $sources as $check_source => $version ) { |
| 174 | $active = dirname( $check_source ) === $source; |
| 175 | $path = $check_source; |
| 176 | |
| 177 | if ( ! $fullpath ) { |
| 178 | $path = str_replace( ABSPATH, '', $path ); |
| 179 | } |
| 180 | |
| 181 | $rows[ $check_source ] = array( |
| 182 | 'source' => $path, |
| 183 | 'version' => $version, |
| 184 | 'active' => $active ? 'yes' : 'no', |
| 185 | ); |
| 186 | } |
| 187 | |
| 188 | ksort( $rows ); |
| 189 | |
| 190 | \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 ); |
| 191 | |
| 192 | $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'source', 'version', 'active' ) ); |
| 193 | $formatter->display_items( $rows ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Get current data store. |
| 198 | * |
| 199 | * @return string |
| 200 | */ |
| 201 | protected function get_current_datastore() { |
| 202 | return get_class( $this->store ); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Get latest version. |
| 207 | * |
| 208 | * @param null|\ActionScheduler_Versions $instance Versions. |
| 209 | * @return string |
| 210 | */ |
| 211 | protected function get_latest_version( $instance = null ) { |
| 212 | if ( is_null( $instance ) ) { |
| 213 | $instance = \ActionScheduler_Versions::instance(); |
| 214 | } |
| 215 | |
| 216 | return $instance->latest_version(); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Get current runner. |
| 221 | * |
| 222 | * @return string |
| 223 | */ |
| 224 | protected function get_current_runner() { |
| 225 | return get_class( \ActionScheduler::runner() ); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Get oldest and newest scheduled dates for a given set of statuses. |
| 230 | * |
| 231 | * @param array $status_keys Set of statuses to find oldest & newest action for. |
| 232 | * @return array |
| 233 | */ |
| 234 | protected function get_oldest_and_newest( $status_keys ) { |
| 235 | $oldest_and_newest = array(); |
| 236 | |
| 237 | foreach ( $status_keys as $status ) { |
| 238 | $oldest_and_newest[ $status ] = array( |
| 239 | 'oldest' => '–', |
| 240 | 'newest' => '–', |
| 241 | ); |
| 242 | |
| 243 | if ( 'in-progress' === $status ) { |
| 244 | continue; |
| 245 | } |
| 246 | |
| 247 | $oldest_and_newest[ $status ]['oldest'] = $this->get_action_status_date( $status, 'oldest' ); |
| 248 | $oldest_and_newest[ $status ]['newest'] = $this->get_action_status_date( $status, 'newest' ); |
| 249 | } |
| 250 | |
| 251 | return $oldest_and_newest; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Get oldest or newest scheduled date for a given status. |
| 256 | * |
| 257 | * @param string $status Action status label/name string. |
| 258 | * @param string $date_type Oldest or Newest. |
| 259 | * @return string |
| 260 | */ |
| 261 | protected function get_action_status_date( $status, $date_type = 'oldest' ) { |
| 262 | $order = 'oldest' === $date_type ? 'ASC' : 'DESC'; |
| 263 | |
| 264 | $args = array( |
| 265 | 'status' => $status, |
| 266 | 'per_page' => 1, |
| 267 | 'order' => $order, |
| 268 | ); |
| 269 | |
| 270 | $action = $this->store->query_actions( $args ); |
| 271 | |
| 272 | if ( ! empty( $action ) ) { |
| 273 | $date_object = $this->store->get_date( $action[0] ); |
| 274 | $action_date = $date_object->format( 'Y-m-d H:i:s O' ); |
| 275 | } else { |
| 276 | $action_date = '–'; |
| 277 | } |
| 278 | |
| 279 | return $action_date; |
| 280 | } |
| 281 | |
| 282 | } |
| 283 |