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