debug-bar.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /*
  3. Plugin Name: Debug Bar
  4. Plugin URI: http://wordpress.org/extend/plugins/debug-bar/
  5. Description: Adds a debug menu to the admin bar that shows query, cache, and other helpful debugging information.
  6. Author: wordpressdotorg
  7. Version: 0.8.2
  8. Author URI: http://wordpress.org/
  9. */
  10. /***
  11. * Debug Functions
  12. *
  13. * When logged in as a super admin, these functions will run to provide
  14. * debugging information when specific super admin menu items are selected.
  15. *
  16. * They are not used when a regular user is logged in.
  17. */
  18. class Debug_Bar {
  19. var $panels = array();
  20. function Debug_Bar() {
  21. if ( defined('DOING_AJAX') && DOING_AJAX )
  22. add_action( 'admin_init', array( &$this, 'init_ajax' ) );
  23. add_action( 'admin_bar_init', array( &$this, 'init' ) );
  24. }
  25. function init() {
  26. if ( ! is_super_admin() || ! is_admin_bar_showing() || $this->is_wp_login() )
  27. return;
  28. add_action( 'admin_bar_menu', array( &$this, 'admin_bar_menu' ), 1000 );
  29. add_action( 'admin_footer', array( &$this, 'render' ), 1000 );
  30. add_action( 'wp_footer', array( &$this, 'render' ), 1000 );
  31. add_action( 'wp_head', array( &$this, 'ensure_ajaxurl' ), 1 );
  32. add_filter( 'body_class', array( &$this, 'body_class' ) );
  33. add_filter( 'admin_body_class', array( &$this, 'body_class' ) );
  34. $this->requirements();
  35. $this->enqueue();
  36. $this->init_panels();
  37. }
  38. /* Are we on the wp-login.php page?
  39. * We can get here while logged in and break the page as the admin bar isn't shown and otherthings the js relies on aren't available.
  40. */
  41. function is_wp_login() {
  42. return 'wp-login.php' == basename( $_SERVER['SCRIPT_NAME'] );
  43. }
  44. function init_ajax() {
  45. if ( ! is_super_admin() )
  46. return;
  47. $this->requirements();
  48. $this->init_panels();
  49. }
  50. function requirements() {
  51. $path = plugin_dir_path( __FILE__ );
  52. require_once( $path . '/compat.php' );
  53. $recs = array( 'panel', 'php', 'queries', 'request', 'wp-query', 'object-cache', 'deprecated', 'js' );
  54. foreach ( $recs as $rec )
  55. require_once "$path/panels/class-debug-bar-$rec.php";
  56. }
  57. function enqueue() {
  58. $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '.dev' : '';
  59. wp_enqueue_style( 'debug-bar', plugins_url( "css/debug-bar$suffix.css", __FILE__ ), array(), '20120317' );
  60. wp_enqueue_script( 'debug-bar', plugins_url( "js/debug-bar$suffix.js", __FILE__ ), array( 'jquery' ), '20121228.2', true );
  61. do_action('debug_bar_enqueue_scripts');
  62. }
  63. function init_panels() {
  64. $classes = array(
  65. 'Debug_Bar_PHP',
  66. 'Debug_Bar_Queries',
  67. 'Debug_Bar_WP_Query',
  68. 'Debug_Bar_Deprecated',
  69. 'Debug_Bar_Request',
  70. 'Debug_Bar_Object_Cache',
  71. 'Debug_Bar_JS',
  72. );
  73. foreach ( $classes as $class ) {
  74. $this->panels[] = new $class;
  75. }
  76. $this->panels = apply_filters( 'debug_bar_panels', $this->panels );
  77. }
  78. function ensure_ajaxurl() { ?>
  79. <script type="text/javascript">
  80. //<![CDATA[
  81. var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
  82. //]]>
  83. </script>
  84. <?php
  85. }
  86. // memory_get_peak_usage is PHP >= 5.2.0 only
  87. function safe_memory_get_peak_usage() {
  88. if ( function_exists( 'memory_get_peak_usage' ) ) {
  89. $usage = memory_get_peak_usage();
  90. } else {
  91. $usage = memory_get_usage();
  92. }
  93. return $usage;
  94. }
  95. function admin_bar_menu() {
  96. global $wp_admin_bar;
  97. $classes = apply_filters( 'debug_bar_classes', array() );
  98. $classes = implode( " ", $classes );
  99. /* Add the main siteadmin menu item */
  100. $wp_admin_bar->add_menu( array(
  101. 'id' => 'debug-bar',
  102. 'parent' => 'top-secondary',
  103. 'title' => apply_filters( 'debug_bar_title', __('Debug', 'debug-bar') ),
  104. 'meta' => array( 'class' => $classes ),
  105. ) );
  106. // @todo: Uncomment and finish me!
  107. // foreach ( $this->panels as $panel_key => $panel ) {
  108. // if ( ! $panel->is_visible() )
  109. // continue;
  110. //
  111. // $panel_class = get_class( $panel );
  112. //
  113. // $wp_admin_bar->add_menu( array(
  114. // 'parent' => 'debug-bar',
  115. // 'id' => "debug-bar-$panel_class",
  116. // 'title' => $panel->title(),
  117. // ) );
  118. // }
  119. }
  120. function body_class( $classes ) {
  121. if ( is_array( $classes ) )
  122. $classes[] = 'debug-bar-maximized';
  123. else
  124. $classes .= ' debug-bar-maximized ';
  125. if ( isset( $_GET['debug-bar'] ) ) {
  126. if ( is_array( $classes ) )
  127. $classes[] = 'debug-bar-visible';
  128. else
  129. $classes .= ' debug-bar-visible ';
  130. }
  131. return $classes;
  132. }
  133. function render() {
  134. global $wpdb;
  135. if ( empty( $this->panels ) )
  136. return;
  137. foreach ( $this->panels as $panel_key => $panel ) {
  138. $panel->prerender();
  139. if ( ! $panel->is_visible() )
  140. unset( $this->panels[ $panel_key ] );
  141. }
  142. ?>
  143. <div id='querylist'>
  144. <div id="debug-bar-actions">
  145. <span class="maximize">+</span>
  146. <span class="restore">&ndash;</span>
  147. <span class="close">&times;</span>
  148. </div>
  149. <div id='debug-bar-info'>
  150. <div id="debug-status">
  151. <?php //@todo: Add a links to information about WP_DEBUG, PHP version, MySQL version, and Peak Memory.
  152. $statuses = array();
  153. $statuses[] = array( 'site', php_uname( 'n' ), sprintf( __( '#%d', 'debug-bar' ), get_current_blog_id() ) );
  154. $statuses[] = array( 'php', __('PHP', 'debug-bar'), phpversion() );
  155. $db_title = empty( $wpdb->is_mysql ) ? __( 'DB', 'debug-bar' ) : 'MySQL';
  156. $statuses[] = array( 'db', $db_title, $wpdb->db_version() );
  157. $statuses[] = array( 'memory', __('Memory Usage', 'debug-bar'), sprintf( __('%s bytes', 'debug-bar'), number_format_i18n( $this->safe_memory_get_peak_usage() ) ) );
  158. if ( ! WP_DEBUG )
  159. $statuses[] = array( 'warning', __('Please Enable', 'debug-bar'), 'WP_DEBUG' );
  160. $statuses = apply_filters( 'debug_bar_statuses', $statuses );
  161. foreach ( $statuses as $status ):
  162. list( $slug, $title, $data ) = $status;
  163. ?><div id='debug-status-<?php echo esc_attr( $slug ); ?>' class='debug-status'>
  164. <div class='debug-status-title'><?php echo $title; ?></div>
  165. <?php if ( ! empty( $data ) ): ?>
  166. <div class='debug-status-data'><?php echo $data; ?></div>
  167. <?php endif; ?>
  168. </div><?php
  169. endforeach;
  170. ?>
  171. </div>
  172. </div>
  173. <div id='debug-bar-menu'>
  174. <ul id="debug-menu-links">
  175. <?php
  176. $current = ' current';
  177. foreach ( $this->panels as $panel ) :
  178. $class = get_class( $panel );
  179. ?>
  180. <li><a
  181. id="debug-menu-link-<?php echo esc_attr( $class ); ?>"
  182. class="debug-menu-link<?php echo $current; ?>"
  183. href="#debug-menu-target-<?php echo esc_attr( $class ); ?>">
  184. <?php
  185. // Not escaping html here, so panels can use html in the title.
  186. echo $panel->title();
  187. ?>
  188. </a></li>
  189. <?php
  190. $current = '';
  191. endforeach; ?>
  192. </ul>
  193. </div>
  194. <div id="debug-menu-targets"><?php
  195. $current = ' style="display: block"';
  196. foreach ( $this->panels as $panel ) :
  197. $class = get_class( $panel ); ?>
  198. <div id="debug-menu-target-<?php echo $class; ?>" class="debug-menu-target" <?php echo $current; ?>>
  199. <?php $panel->render(); ?>
  200. </div>
  201. <?php
  202. $current = '';
  203. endforeach;
  204. ?>
  205. </div>
  206. <?php do_action( 'debug_bar' ); ?>
  207. </div>
  208. <?php
  209. }
  210. }
  211. $GLOBALS['debug_bar'] = new Debug_Bar();