Extension.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. if ( !class_exists('Puc_v4p11_DebugBar_Extension', false) ):
  3. class Puc_v4p11_DebugBar_Extension {
  4. const RESPONSE_BODY_LENGTH_LIMIT = 4000;
  5. /** @var Puc_v4p11_UpdateChecker */
  6. protected $updateChecker;
  7. protected $panelClass = 'Puc_v4p11_DebugBar_Panel';
  8. public function __construct($updateChecker, $panelClass = null) {
  9. $this->updateChecker = $updateChecker;
  10. if ( isset($panelClass) ) {
  11. $this->panelClass = $panelClass;
  12. }
  13. if ( version_compare(PHP_VERSION, '5.3', '>=') && (strpos($this->panelClass, '\\') === false) ) {
  14. $this->panelClass = __NAMESPACE__ . '\\' . $this->panelClass;
  15. }
  16. add_filter('debug_bar_panels', array($this, 'addDebugBarPanel'));
  17. add_action('debug_bar_enqueue_scripts', array($this, 'enqueuePanelDependencies'));
  18. add_action('wp_ajax_puc_v4_debug_check_now', array($this, 'ajaxCheckNow'));
  19. }
  20. /**
  21. * Register the PUC Debug Bar panel.
  22. *
  23. * @param array $panels
  24. * @return array
  25. */
  26. public function addDebugBarPanel($panels) {
  27. if ( $this->updateChecker->userCanInstallUpdates() ) {
  28. $panels[] = new $this->panelClass($this->updateChecker);
  29. }
  30. return $panels;
  31. }
  32. /**
  33. * Enqueue our Debug Bar scripts and styles.
  34. */
  35. public function enqueuePanelDependencies() {
  36. wp_enqueue_style(
  37. 'puc-debug-bar-style-v4',
  38. $this->getLibraryUrl("/css/puc-debug-bar.css"),
  39. array('debug-bar'),
  40. '20171124'
  41. );
  42. wp_enqueue_script(
  43. 'puc-debug-bar-js-v4',
  44. $this->getLibraryUrl("/js/debug-bar.js"),
  45. array('jquery'),
  46. '20201209'
  47. );
  48. }
  49. /**
  50. * Run an update check and output the result. Useful for making sure that
  51. * the update checking process works as expected.
  52. */
  53. public function ajaxCheckNow() {
  54. if ( $_POST['uid'] !== $this->updateChecker->getUniqueName('uid') ) {
  55. return;
  56. }
  57. $this->preAjaxRequest();
  58. $update = $this->updateChecker->checkForUpdates();
  59. if ( $update !== null ) {
  60. echo "An update is available:";
  61. echo '<pre>', htmlentities(print_r($update, true)), '</pre>';
  62. } else {
  63. echo 'No updates found.';
  64. }
  65. $errors = $this->updateChecker->getLastRequestApiErrors();
  66. if ( !empty($errors) ) {
  67. printf('<p>The update checker encountered %d API error%s.</p>', count($errors), (count($errors) > 1) ? 's' : '');
  68. foreach (array_values($errors) as $num => $item) {
  69. $wpError = $item['error'];
  70. /** @var WP_Error $wpError */
  71. printf('<h4>%d) %s</h4>', $num + 1, esc_html($wpError->get_error_message()));
  72. echo '<dl>';
  73. printf('<dt>Error code:</dt><dd><code>%s</code></dd>', esc_html($wpError->get_error_code()));
  74. if ( isset($item['url']) ) {
  75. printf('<dt>Requested URL:</dt><dd><code>%s</code></dd>', esc_html($item['url']));
  76. }
  77. if ( isset($item['httpResponse']) ) {
  78. if ( is_wp_error($item['httpResponse']) ) {
  79. $httpError = $item['httpResponse'];
  80. /** @var WP_Error $httpError */
  81. printf(
  82. '<dt>WordPress HTTP API error:</dt><dd>%s (<code>%s</code>)</dd>',
  83. esc_html($httpError->get_error_message()),
  84. esc_html($httpError->get_error_code())
  85. );
  86. } else {
  87. //Status code.
  88. printf(
  89. '<dt>HTTP status:</dt><dd><code>%d %s</code></dd>',
  90. wp_remote_retrieve_response_code($item['httpResponse']),
  91. wp_remote_retrieve_response_message($item['httpResponse'])
  92. );
  93. //Headers.
  94. echo '<dt>Response headers:</dt><dd><pre>';
  95. foreach (wp_remote_retrieve_headers($item['httpResponse']) as $name => $value) {
  96. printf("%s: %s\n", esc_html($name), esc_html($value));
  97. }
  98. echo '</pre></dd>';
  99. //Body.
  100. $body = wp_remote_retrieve_body($item['httpResponse']);
  101. if ( $body === '' ) {
  102. $body = '(Empty response.)';
  103. } else if ( strlen($body) > self::RESPONSE_BODY_LENGTH_LIMIT ) {
  104. $length = strlen($body);
  105. $body = substr($body, 0, self::RESPONSE_BODY_LENGTH_LIMIT)
  106. . sprintf("\n(Long string truncated. Total length: %d bytes.)", $length);
  107. }
  108. printf('<dt>Response body:</dt><dd><pre>%s</pre></dd>', esc_html($body));
  109. }
  110. }
  111. echo '<dl>';
  112. }
  113. }
  114. exit;
  115. }
  116. /**
  117. * Check access permissions and enable error display (for debugging).
  118. */
  119. protected function preAjaxRequest() {
  120. if ( !$this->updateChecker->userCanInstallUpdates() ) {
  121. die('Access denied');
  122. }
  123. check_ajax_referer('puc-ajax');
  124. error_reporting(E_ALL);
  125. @ini_set('display_errors', 'On');
  126. }
  127. /**
  128. * Remove hooks that were added by this extension.
  129. */
  130. public function removeHooks() {
  131. remove_filter('debug_bar_panels', array($this, 'addDebugBarPanel'));
  132. remove_action('debug_bar_enqueue_scripts', array($this, 'enqueuePanelDependencies'));
  133. remove_action('wp_ajax_puc_v4_debug_check_now', array($this, 'ajaxCheckNow'));
  134. }
  135. /**
  136. * @param string $filePath
  137. * @return string
  138. */
  139. private function getLibraryUrl($filePath) {
  140. $absolutePath = realpath(dirname(__FILE__) . '/../../../' . ltrim($filePath, '/'));
  141. //Where is the library located inside the WordPress directory structure?
  142. $absolutePath = Puc_v4p11_Factory::normalizePath($absolutePath);
  143. $pluginDir = Puc_v4p11_Factory::normalizePath(WP_PLUGIN_DIR);
  144. $muPluginDir = Puc_v4p11_Factory::normalizePath(WPMU_PLUGIN_DIR);
  145. $themeDir = Puc_v4p11_Factory::normalizePath(get_theme_root());
  146. if ( (strpos($absolutePath, $pluginDir) === 0) || (strpos($absolutePath, $muPluginDir) === 0) ) {
  147. //It's part of a plugin.
  148. return plugins_url(basename($absolutePath), $absolutePath);
  149. } else if ( strpos($absolutePath, $themeDir) === 0 ) {
  150. //It's part of a theme.
  151. $relativePath = substr($absolutePath, strlen($themeDir) + 1);
  152. $template = substr($relativePath, 0, strpos($relativePath, '/'));
  153. $baseUrl = get_theme_root_uri($template);
  154. if ( !empty($baseUrl) && $relativePath ) {
  155. return $baseUrl . '/' . $relativePath;
  156. }
  157. }
  158. return '';
  159. }
  160. }
  161. endif;