Нет описания

ActionScheduler_wcSystemStatus.php 4.9KB

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