| 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 |
/** |
| 16 |
* Constructor method for ActionScheduler_wcSystemStatus. |
| 17 |
* |
| 18 |
* @param ActionScheduler_Store $store Active store object. |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public function __construct( $store ) { |
| 23 |
$this->store = $store; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Display action data, including number of actions grouped by status and the oldest & newest action in each status. |
| 28 |
* |
| 29 |
* Helpful to identify issues, like a clogged queue. |
| 30 |
*/ |
| 31 |
public function render() { |
| 32 |
$action_counts = $this->store->action_counts(); |
| 33 |
$status_labels = $this->store->get_status_labels(); |
| 34 |
$oldest_and_newest = $this->get_oldest_and_newest( array_keys( $status_labels ) ); |
| 35 |
|
| 36 |
$this->get_template( $status_labels, $action_counts, $oldest_and_newest ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Get oldest and newest scheduled dates for a given set of statuses. |
| 41 |
* |
| 42 |
* @param array $status_keys Set of statuses to find oldest & newest action for. |
| 43 |
* @return array |
| 44 |
*/ |
| 45 |
protected function get_oldest_and_newest( $status_keys ) { |
| 46 |
|
| 47 |
$oldest_and_newest = array(); |
| 48 |
|
| 49 |
foreach ( $status_keys as $status ) { |
| 50 |
$oldest_and_newest[ $status ] = array( |
| 51 |
'oldest' => '–', |
| 52 |
'newest' => '–', |
| 53 |
); |
| 54 |
|
| 55 |
if ( 'in-progress' === $status ) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
$oldest_and_newest[ $status ]['oldest'] = $this->get_action_status_date( $status, 'oldest' ); |
| 60 |
$oldest_and_newest[ $status ]['newest'] = $this->get_action_status_date( $status, 'newest' ); |
| 61 |
} |
| 62 |
|
| 63 |
return $oldest_and_newest; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Get oldest or newest scheduled date for a given status. |
| 68 |
* |
| 69 |
* @param string $status Action status label/name string. |
| 70 |
* @param string $date_type Oldest or Newest. |
| 71 |
* @return DateTime |
| 72 |
*/ |
| 73 |
protected function get_action_status_date( $status, $date_type = 'oldest' ) { |
| 74 |
|
| 75 |
$order = 'oldest' === $date_type ? 'ASC' : 'DESC'; |
| 76 |
|
| 77 |
$action = $this->store->query_actions( |
| 78 |
array( |
| 79 |
'status' => $status, |
| 80 |
'per_page' => 1, |
| 81 |
'order' => $order, |
| 82 |
) |
| 83 |
); |
| 84 |
|
| 85 |
if ( ! empty( $action ) ) { |
| 86 |
$date_object = $this->store->get_date( $action[0] ); |
| 87 |
$action_date = $date_object->format( 'Y-m-d H:i:s O' ); |
| 88 |
} else { |
| 89 |
$action_date = '–'; |
| 90 |
} |
| 91 |
|
| 92 |
return $action_date; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get oldest or newest scheduled date for a given status. |
| 97 |
* |
| 98 |
* @param array $status_labels Set of statuses to find oldest & newest action for. |
| 99 |
* @param array $action_counts Number of actions grouped by status. |
| 100 |
* @param array $oldest_and_newest Date of the oldest and newest action with each status. |
| 101 |
*/ |
| 102 |
protected function get_template( $status_labels, $action_counts, $oldest_and_newest ) { |
| 103 |
$as_version = ActionScheduler_Versions::instance()->latest_version(); |
| 104 |
$as_datastore = get_class( ActionScheduler_Store::instance() ); |
| 105 |
?> |
| 106 |
|
| 107 |
<table class="wc_status_table widefat" cellspacing="0"> |
| 108 |
<thead> |
| 109 |
<tr> |
| 110 |
<th colspan="5" data-export-label="Action Scheduler"><h2><?php esc_html_e( 'Action Scheduler', 'action-scheduler' ); ?><?php echo wc_help_tip( esc_html__( 'This section shows details of Action Scheduler.', 'action-scheduler' ) ); ?></h2></th> |
| 111 |
</tr> |
| 112 |
<tr> |
| 113 |
<td colspan="2" data-export-label="Version"><?php esc_html_e( 'Version:', 'action-scheduler' ); ?></td> |
| 114 |
<td colspan="3"><?php echo esc_html( $as_version ); ?></td> |
| 115 |
</tr> |
| 116 |
<tr> |
| 117 |
<td colspan="2" data-export-label="Data store"><?php esc_html_e( 'Data store:', 'action-scheduler' ); ?></td> |
| 118 |
<td colspan="3"><?php echo esc_html( $as_datastore ); ?></td> |
| 119 |
</tr> |
| 120 |
<tr> |
| 121 |
<td><strong><?php esc_html_e( 'Action Status', 'action-scheduler' ); ?></strong></td> |
| 122 |
<td class="help"> </td> |
| 123 |
<td><strong><?php esc_html_e( 'Count', 'action-scheduler' ); ?></strong></td> |
| 124 |
<td><strong><?php esc_html_e( 'Oldest Scheduled Date', 'action-scheduler' ); ?></strong></td> |
| 125 |
<td><strong><?php esc_html_e( 'Newest Scheduled Date', 'action-scheduler' ); ?></strong></td> |
| 126 |
</tr> |
| 127 |
</thead> |
| 128 |
<tbody> |
| 129 |
<?php |
| 130 |
foreach ( $action_counts as $status => $count ) { |
| 131 |
// 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. |
| 132 |
printf( |
| 133 |
'<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>', |
| 134 |
esc_html( $status_labels[ $status ] ), |
| 135 |
esc_html( number_format_i18n( $count ) ), |
| 136 |
esc_html( $oldest_and_newest[ $status ]['oldest'] ), |
| 137 |
esc_html( $oldest_and_newest[ $status ]['newest'] ) |
| 138 |
); |
| 139 |
} |
| 140 |
?> |
| 141 |
</tbody> |
| 142 |
</table> |
| 143 |
|
| 144 |
<?php |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Is triggered when invoking inaccessible methods in an object context. |
| 149 |
* |
| 150 |
* @param string $name Name of method called. |
| 151 |
* @param array $arguments Parameters to invoke the method with. |
| 152 |
* |
| 153 |
* @return mixed |
| 154 |
* @link https://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods |
| 155 |
*/ |
| 156 |
public function __call( $name, $arguments ) { |
| 157 |
switch ( $name ) { |
| 158 |
case 'print': |
| 159 |
_deprecated_function( __CLASS__ . '::print()', '2.2.4', __CLASS__ . '::render()' ); |
| 160 |
return call_user_func_array( array( $this, 'render' ), $arguments ); |
| 161 |
} |
| 162 |
|
| 163 |
return null; |
| 164 |
} |
| 165 |
} |
| 166 |
|