Panel.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. if ( !class_exists('Puc_v4p11_DebugBar_Panel', false) && class_exists('Debug_Bar_Panel', false) ):
  3. class Puc_v4p11_DebugBar_Panel extends Debug_Bar_Panel {
  4. /** @var Puc_v4p11_UpdateChecker */
  5. protected $updateChecker;
  6. private $responseBox = '<div class="puc-ajax-response" style="display: none;"></div>';
  7. public function __construct($updateChecker) {
  8. $this->updateChecker = $updateChecker;
  9. $title = sprintf(
  10. '<span class="puc-debug-menu-link-%s">PUC (%s)</span>',
  11. esc_attr($this->updateChecker->getUniqueName('uid')),
  12. $this->updateChecker->slug
  13. );
  14. parent::__construct($title);
  15. }
  16. public function render() {
  17. printf(
  18. '<div class="puc-debug-bar-panel-v4" id="%1$s" data-slug="%2$s" data-uid="%3$s" data-nonce="%4$s">',
  19. esc_attr($this->updateChecker->getUniqueName('debug-bar-panel')),
  20. esc_attr($this->updateChecker->slug),
  21. esc_attr($this->updateChecker->getUniqueName('uid')),
  22. esc_attr(wp_create_nonce('puc-ajax'))
  23. );
  24. $this->displayConfiguration();
  25. $this->displayStatus();
  26. $this->displayCurrentUpdate();
  27. echo '</div>';
  28. }
  29. private function displayConfiguration() {
  30. echo '<h3>Configuration</h3>';
  31. echo '<table class="puc-debug-data">';
  32. $this->displayConfigHeader();
  33. $this->row('Slug', htmlentities($this->updateChecker->slug));
  34. $this->row('DB option', htmlentities($this->updateChecker->optionName));
  35. $requestInfoButton = $this->getMetadataButton();
  36. $this->row('Metadata URL', htmlentities($this->updateChecker->metadataUrl) . ' ' . $requestInfoButton . $this->responseBox);
  37. $scheduler = $this->updateChecker->scheduler;
  38. if ( $scheduler->checkPeriod > 0 ) {
  39. $this->row('Automatic checks', 'Every ' . $scheduler->checkPeriod . ' hours');
  40. } else {
  41. $this->row('Automatic checks', 'Disabled');
  42. }
  43. if ( isset($scheduler->throttleRedundantChecks) ) {
  44. if ( $scheduler->throttleRedundantChecks && ($scheduler->checkPeriod > 0) ) {
  45. $this->row(
  46. 'Throttling',
  47. sprintf(
  48. 'Enabled. If an update is already available, check for updates every %1$d hours instead of every %2$d hours.',
  49. $scheduler->throttledCheckPeriod,
  50. $scheduler->checkPeriod
  51. )
  52. );
  53. } else {
  54. $this->row('Throttling', 'Disabled');
  55. }
  56. }
  57. $this->updateChecker->onDisplayConfiguration($this);
  58. echo '</table>';
  59. }
  60. protected function displayConfigHeader() {
  61. //Do nothing. This should be implemented in subclasses.
  62. }
  63. protected function getMetadataButton() {
  64. return '';
  65. }
  66. private function displayStatus() {
  67. echo '<h3>Status</h3>';
  68. echo '<table class="puc-debug-data">';
  69. $state = $this->updateChecker->getUpdateState();
  70. $checkNowButton = '';
  71. if ( function_exists('get_submit_button') ) {
  72. $checkNowButton = get_submit_button(
  73. 'Check Now',
  74. 'secondary',
  75. 'puc-check-now-button',
  76. false,
  77. array('id' => $this->updateChecker->getUniqueName('check-now-button'))
  78. );
  79. }
  80. if ( $state->getLastCheck() > 0 ) {
  81. $this->row('Last check', $this->formatTimeWithDelta($state->getLastCheck()) . ' ' . $checkNowButton . $this->responseBox);
  82. } else {
  83. $this->row('Last check', 'Never');
  84. }
  85. $nextCheck = wp_next_scheduled($this->updateChecker->scheduler->getCronHookName());
  86. $this->row('Next automatic check', $this->formatTimeWithDelta($nextCheck));
  87. if ( $state->getCheckedVersion() !== '' ) {
  88. $this->row('Checked version', htmlentities($state->getCheckedVersion()));
  89. $this->row('Cached update', $state->getUpdate());
  90. }
  91. $this->row('Update checker class', htmlentities(get_class($this->updateChecker)));
  92. echo '</table>';
  93. }
  94. private function displayCurrentUpdate() {
  95. $update = $this->updateChecker->getUpdate();
  96. if ( $update !== null ) {
  97. echo '<h3>An Update Is Available</h3>';
  98. echo '<table class="puc-debug-data">';
  99. $fields = $this->getUpdateFields();
  100. foreach($fields as $field) {
  101. if ( property_exists($update, $field) ) {
  102. $this->row(ucwords(str_replace('_', ' ', $field)), htmlentities($update->$field));
  103. }
  104. }
  105. echo '</table>';
  106. } else {
  107. echo '<h3>No updates currently available</h3>';
  108. }
  109. }
  110. protected function getUpdateFields() {
  111. return array('version', 'download_url', 'slug',);
  112. }
  113. private function formatTimeWithDelta($unixTime) {
  114. if ( empty($unixTime) ) {
  115. return 'Never';
  116. }
  117. $delta = time() - $unixTime;
  118. $result = human_time_diff(time(), $unixTime);
  119. if ( $delta < 0 ) {
  120. $result = 'after ' . $result;
  121. } else {
  122. $result = $result . ' ago';
  123. }
  124. $result .= ' (' . $this->formatTimestamp($unixTime) . ')';
  125. return $result;
  126. }
  127. private function formatTimestamp($unixTime) {
  128. return gmdate('Y-m-d H:i:s', $unixTime + (get_option('gmt_offset') * 3600));
  129. }
  130. public function row($name, $value) {
  131. if ( is_object($value) || is_array($value) ) {
  132. $value = '<pre>' . htmlentities(print_r($value, true)) . '</pre>';
  133. } else if ($value === null) {
  134. $value = '<code>null</code>';
  135. }
  136. printf('<tr><th scope="row">%1$s</th> <td>%2$s</td></tr>', $name, $value);
  137. }
  138. }
  139. endif;