/vendor/plugin-update-checker/debug-bar-panel.php

https://gitlab.com/axminenko/rocks-tools · PHP · 135 lines · 111 code · 20 blank · 4 comment · 22 complexity · b46694799ee3ed7ef32192749e4fd4c0 MD5 · raw file

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