WP_CLI
6 years ago
abstracts
4 years ago
actions
4 years ago
data-stores
4 years ago
migration
4 years ago
schedules
6 years ago
schema
4 years ago
ActionScheduler_ActionClaim.php
6 years ago
ActionScheduler_ActionFactory.php
6 years ago
ActionScheduler_AdminView.php
6 years ago
ActionScheduler_AsyncRequest_QueueRunner.php
6 years ago
ActionScheduler_Compatibility.php
6 years ago
ActionScheduler_DataController.php
6 years ago
ActionScheduler_DateTime.php
6 years ago
ActionScheduler_Exception.php
6 years ago
ActionScheduler_FatalErrorMonitor.php
6 years ago
ActionScheduler_InvalidActionException.php
6 years ago
ActionScheduler_ListTable.php
6 years ago
ActionScheduler_LogEntry.php
6 years ago
ActionScheduler_NullLogEntry.php
6 years ago
ActionScheduler_OptionLock.php
6 years ago
ActionScheduler_QueueCleaner.php
4 years ago
ActionScheduler_QueueRunner.php
6 years ago
ActionScheduler_Versions.php
6 years ago
ActionScheduler_WPCommentCleaner.php
6 years ago
ActionScheduler_wcSystemStatus.php
4 years ago
ActionScheduler_wcSystemStatus.php
158 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class ActionScheduler_wcSystemStatus |
| 5 | */ |
| 6 | class ActionScheduler_wcSystemStatus { |
| 7 | |
| 8 | /** |
| 9 | * The active data stores |
| 10 | * |
| 11 | * @var ActionScheduler_Store |
| 12 | */ |
| 13 | protected $store; |
| 14 | |
| 15 | function __construct( $store ) { |
| 16 | $this->store = $store; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Display action data, including number of actions grouped by status and the oldest & newest action in each status. |
| 21 | * |
| 22 | * Helpful to identify issues, like a clogged queue. |
| 23 | */ |
| 24 | public function render() { |
| 25 | $action_counts = $this->store->action_counts(); |
| 26 | $status_labels = $this->store->get_status_labels(); |
| 27 | $oldest_and_newest = $this->get_oldest_and_newest( array_keys( $status_labels ) ); |
| 28 | |
| 29 | $this->get_template( $status_labels, $action_counts, $oldest_and_newest ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Get oldest and newest scheduled dates for a given set of statuses. |
| 34 | * |
| 35 | * @param array $status_keys Set of statuses to find oldest & newest action for. |
| 36 | * @return array |
| 37 | */ |
| 38 | protected function get_oldest_and_newest( $status_keys ) { |
| 39 | |
| 40 | $oldest_and_newest = array(); |
| 41 | |
| 42 | foreach ( $status_keys as $status ) { |
| 43 | $oldest_and_newest[ $status ] = array( |
| 44 | 'oldest' => '–', |
| 45 | 'newest' => '–', |
| 46 | ); |
| 47 | |
| 48 | if ( 'in-progress' === $status ) { |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | $oldest_and_newest[ $status ]['oldest'] = $this->get_action_status_date( $status, 'oldest' ); |
| 53 | $oldest_and_newest[ $status ]['newest'] = $this->get_action_status_date( $status, 'newest' ); |
| 54 | } |
| 55 | |
| 56 | return $oldest_and_newest; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get oldest or newest scheduled date for a given status. |
| 61 | * |
| 62 | * @param string $status Action status label/name string. |
| 63 | * @param string $date_type Oldest or Newest. |
| 64 | * @return DateTime |
| 65 | */ |
| 66 | protected function get_action_status_date( $status, $date_type = 'oldest' ) { |
| 67 | |
| 68 | $order = 'oldest' === $date_type ? 'ASC' : 'DESC'; |
| 69 | |
| 70 | $action = $this->store->query_actions( array( |
| 71 | 'claimed' => false, |
| 72 | 'status' => $status, |
| 73 | 'per_page' => 1, |
| 74 | 'order' => $order, |
| 75 | ) ); |
| 76 | |
| 77 | if ( ! empty( $action ) ) { |
| 78 | $date_object = $this->store->get_date( $action[0] ); |
| 79 | $action_date = $date_object->format( 'Y-m-d H:i:s O' ); |
| 80 | } else { |
| 81 | $action_date = '–'; |
| 82 | } |
| 83 | |
| 84 | return $action_date; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get oldest or newest scheduled date for a given status. |
| 89 | * |
| 90 | * @param array $status_labels Set of statuses to find oldest & newest action for. |
| 91 | * @param array $action_counts Number of actions grouped by status. |
| 92 | * @param array $oldest_and_newest Date of the oldest and newest action with each status. |
| 93 | */ |
| 94 | protected function get_template( $status_labels, $action_counts, $oldest_and_newest ) { |
| 95 | $as_version = ActionScheduler_Versions::instance()->latest_version(); |
| 96 | $as_datastore = get_class( ActionScheduler_Store::instance() ); |
| 97 | ?> |
| 98 | |
| 99 | <table class="wc_status_table widefat" cellspacing="0"> |
| 100 | <thead> |
| 101 | <tr> |
| 102 | <th colspan="5" data-export-label="Action Scheduler"><h2><?php esc_html_e( 'Action Scheduler', 'woocommerce' ); ?><?php echo wc_help_tip( esc_html__( 'This section shows details of Action Scheduler.', 'woocommerce' ) ); ?></h2></th> |
| 103 | </tr> |
| 104 | <tr> |
| 105 | <td colspan="2" data-export-label="Version"><?php esc_html_e( 'Version:', 'woocommerce' ); ?></td> |
| 106 | <td colspan="3"><?php echo esc_html( $as_version ); ?></td> |
| 107 | </tr> |
| 108 | <tr> |
| 109 | <td colspan="2" data-export-label="Data store"><?php esc_html_e( 'Data store:', 'woocommerce' ); ?></td> |
| 110 | <td colspan="3"><?php echo esc_html( $as_datastore ); ?></td> |
| 111 | </tr> |
| 112 | <tr> |
| 113 | <td><strong><?php esc_html_e( 'Action Status', 'woocommerce' ); ?></strong></td> |
| 114 | <td class="help"> </td> |
| 115 | <td><strong><?php esc_html_e( 'Count', 'woocommerce' ); ?></strong></td> |
| 116 | <td><strong><?php esc_html_e( 'Oldest Scheduled Date', 'woocommerce' ); ?></strong></td> |
| 117 | <td><strong><?php esc_html_e( 'Newest Scheduled Date', 'woocommerce' ); ?></strong></td> |
| 118 | </tr> |
| 119 | </thead> |
| 120 | <tbody> |
| 121 | <?php |
| 122 | foreach ( $action_counts as $status => $count ) { |
| 123 | // WC uses the 3rd column for export, so we need to display more data in that (hidden when viewed as part of the table) and add an empty 2nd column. |
| 124 | printf( |
| 125 | '<tr><td>%1$s</td><td> </td><td>%2$s<span style="display: none;">, Oldest: %3$s, Newest: %4$s</span></td><td>%3$s</td><td>%4$s</td></tr>', |
| 126 | esc_html( $status_labels[ $status ] ), |
| 127 | number_format_i18n( $count ), |
| 128 | $oldest_and_newest[ $status ]['oldest'], |
| 129 | $oldest_and_newest[ $status ]['newest'] |
| 130 | ); |
| 131 | } |
| 132 | ?> |
| 133 | </tbody> |
| 134 | </table> |
| 135 | |
| 136 | <?php |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * is triggered when invoking inaccessible methods in an object context. |
| 141 | * |
| 142 | * @param string $name |
| 143 | * @param array $arguments |
| 144 | * |
| 145 | * @return mixed |
| 146 | * @link https://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods |
| 147 | */ |
| 148 | public function __call( $name, $arguments ) { |
| 149 | switch ( $name ) { |
| 150 | case 'print': |
| 151 | _deprecated_function( __CLASS__ . '::print()', '2.2.4', __CLASS__ . '::render()' ); |
| 152 | return call_user_func_array( array( $this, 'render' ), $arguments ); |
| 153 | } |
| 154 | |
| 155 | return null; |
| 156 | } |
| 157 | } |
| 158 |