/wp-content/plugins/woocommerce/packages/action-scheduler/classes/ActionScheduler_wcSystemStatus.php

https://gitlab.com/campus-academy/krowkaramel · PHP · 166 lines · 95 code · 21 blank · 50 comment · 4 complexity · ca61b9d15669f4d1e9829ad78e82f287 MD5 · raw file

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