Action
1 year 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 year ago
index.php
3 years ago
System_Command.php
140 lines
| 1 | <?php |
| 2 | namespace Action_Scheduler\WP_CLI; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaping output is not necessary in WP CLI. |
| 5 | use ActionScheduler_SystemInformation; |
| 6 | use WP_CLI; |
| 7 | use function \WP_CLI\Utils\get_flag_value; |
| 8 | class System_Command { |
| 9 | protected $store; |
| 10 | public function __construct() { |
| 11 | $this->store = \ActionScheduler::store(); |
| 12 | } |
| 13 | public function datastore( array $args, array $assoc_args ) { |
| 14 | echo $this->get_current_datastore(); |
| 15 | } |
| 16 | public function runner( array $args, array $assoc_args ) { |
| 17 | echo $this->get_current_runner(); |
| 18 | } |
| 19 | public function status( array $args, array $assoc_args ) { |
| 20 | $runner_enabled = has_action( 'action_scheduler_run_queue', array( \ActionScheduler::runner(), 'run' ) ); |
| 21 | \WP_CLI::line( sprintf( 'Data store: %s', $this->get_current_datastore() ) ); |
| 22 | \WP_CLI::line( sprintf( 'Runner: %s%s', $this->get_current_runner(), ( $runner_enabled ? '' : ' (disabled)' ) ) ); |
| 23 | \WP_CLI::line( sprintf( 'Version: %s', $this->get_latest_version() ) ); |
| 24 | $rows = array(); |
| 25 | $action_counts = $this->store->action_counts(); |
| 26 | $oldest_and_newest = $this->get_oldest_and_newest( array_keys( $action_counts ) ); |
| 27 | foreach ( $action_counts as $status => $count ) { |
| 28 | $rows[] = array( |
| 29 | 'status' => $status, |
| 30 | 'count' => $count, |
| 31 | 'oldest' => $oldest_and_newest[ $status ]['oldest'], |
| 32 | 'newest' => $oldest_and_newest[ $status ]['newest'], |
| 33 | ); |
| 34 | } |
| 35 | $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'status', 'count', 'oldest', 'newest' ) ); |
| 36 | $formatter->display_items( $rows ); |
| 37 | } |
| 38 | public function version( array $args, array $assoc_args ) { |
| 39 | $all = (bool) get_flag_value( $assoc_args, 'all' ); |
| 40 | $latest = $this->get_latest_version(); |
| 41 | if ( ! $all ) { |
| 42 | echo $latest; |
| 43 | \WP_CLI::halt( 0 ); |
| 44 | } |
| 45 | $instance = \ActionScheduler_Versions::instance(); |
| 46 | $versions = $instance->get_versions(); |
| 47 | $rows = array(); |
| 48 | foreach ( $versions as $version => $callback ) { |
| 49 | $active = $version === $latest; |
| 50 | $rows[ $version ] = array( |
| 51 | 'version' => $version, |
| 52 | 'callback' => $callback, |
| 53 | 'active' => $active ? 'yes' : 'no', |
| 54 | ); |
| 55 | } |
| 56 | uksort( $rows, 'version_compare' ); |
| 57 | $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version', 'callback', 'active' ) ); |
| 58 | $formatter->display_items( $rows ); |
| 59 | } |
| 60 | public function source( array $args, array $assoc_args ) { |
| 61 | $all = (bool) get_flag_value( $assoc_args, 'all' ); |
| 62 | $fullpath = (bool) get_flag_value( $assoc_args, 'fullpath' ); |
| 63 | $source = ActionScheduler_SystemInformation::active_source_path(); |
| 64 | $path = $source; |
| 65 | if ( ! $fullpath ) { |
| 66 | $path = str_replace( ABSPATH, '', $path ); |
| 67 | } |
| 68 | if ( ! $all ) { |
| 69 | echo $path; |
| 70 | \WP_CLI::halt( 0 ); |
| 71 | } |
| 72 | $sources = ActionScheduler_SystemInformation::get_sources(); |
| 73 | if ( empty( $sources ) ) { |
| 74 | WP_CLI::log( __( 'Detailed information about registered sources is not currently available.', 'action-scheduler' ) ); |
| 75 | return; |
| 76 | } |
| 77 | $rows = array(); |
| 78 | foreach ( $sources as $check_source => $version ) { |
| 79 | $active = dirname( $check_source ) === $source; |
| 80 | $path = $check_source; |
| 81 | if ( ! $fullpath ) { |
| 82 | $path = str_replace( ABSPATH, '', $path ); |
| 83 | } |
| 84 | $rows[ $check_source ] = array( |
| 85 | 'source' => $path, |
| 86 | 'version' => $version, |
| 87 | 'active' => $active ? 'yes' : 'no', |
| 88 | ); |
| 89 | } |
| 90 | ksort( $rows ); |
| 91 | \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 ); |
| 92 | $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'source', 'version', 'active' ) ); |
| 93 | $formatter->display_items( $rows ); |
| 94 | } |
| 95 | protected function get_current_datastore() { |
| 96 | return get_class( $this->store ); |
| 97 | } |
| 98 | protected function get_latest_version( $instance = null ) { |
| 99 | if ( is_null( $instance ) ) { |
| 100 | $instance = \ActionScheduler_Versions::instance(); |
| 101 | } |
| 102 | return $instance->latest_version(); |
| 103 | } |
| 104 | protected function get_current_runner() { |
| 105 | return get_class( \ActionScheduler::runner() ); |
| 106 | } |
| 107 | protected function get_oldest_and_newest( $status_keys ) { |
| 108 | $oldest_and_newest = array(); |
| 109 | foreach ( $status_keys as $status ) { |
| 110 | $oldest_and_newest[ $status ] = array( |
| 111 | 'oldest' => '–', |
| 112 | 'newest' => '–', |
| 113 | ); |
| 114 | if ( 'in-progress' === $status ) { |
| 115 | continue; |
| 116 | } |
| 117 | $oldest_and_newest[ $status ]['oldest'] = $this->get_action_status_date( $status, 'oldest' ); |
| 118 | $oldest_and_newest[ $status ]['newest'] = $this->get_action_status_date( $status, 'newest' ); |
| 119 | } |
| 120 | return $oldest_and_newest; |
| 121 | } |
| 122 | protected function get_action_status_date( $status, $date_type = 'oldest' ) { |
| 123 | $order = 'oldest' === $date_type ? 'ASC' : 'DESC'; |
| 124 | $args = array( |
| 125 | 'claimed' => false, |
| 126 | 'status' => $status, |
| 127 | 'per_page' => 1, |
| 128 | 'order' => $order, |
| 129 | ); |
| 130 | $action = $this->store->query_actions( $args ); |
| 131 | if ( ! empty( $action ) ) { |
| 132 | $date_object = $this->store->get_date( $action[0] ); |
| 133 | $action_date = $date_object->format( 'Y-m-d H:i:s O' ); |
| 134 | } else { |
| 135 | $action_date = '–'; |
| 136 | } |
| 137 | return $action_date; |
| 138 | } |
| 139 | } |
| 140 |