debug-bar.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.1
  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( 'wp_after_admin_bar_render', array( &$this, 'render' ) );
  30. add_action( 'wp_head', array( &$this, 'ensure_ajaxurl' ), 1 );
  31. add_filter( 'body_class', array( &$this, 'body_class' ) );
  32. add_filter( 'admin_body_class', array( &$this, 'body_class' ) );
  33. $this->requirements();
  34. $this->enqueue();
  35. $this->init_panels();
  36. }
  37. /* Are we on the wp-login.php page?
  38. * 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.
  39. */
  40. function is_wp_login() {
  41. return 'wp-login.php' == basename( $_SERVER['SCRIPT_NAME'] );
  42. }
  43. function init_ajax() {
  44. if ( ! is_super_admin() )
  45. return;
  46. $this->requirements();
  47. $this->init_panels();
  48. }
  49. function requirements() {
  50. $recs = array( 'panel', 'php', 'queries', 'request', 'wp-query', 'object-cache', 'deprecated', 'js' );
  51. foreach ( $recs as $rec )
  52. require_once "panels/class-debug-bar-$rec.php";
  53. }
  54. function enqueue() {
  55. $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '.dev' : '';
  56. wp_enqueue_style( 'debug-bar', plugins_url( "css/debug-bar$suffix.css", __FILE__ ), array(), '20111209' );
  57. wp_enqueue_script( 'debug-bar', plugins_url( "js/debug-bar$suffix.js", __FILE__ ), array( 'jquery' ), '20111209', true );
  58. do_action('debug_bar_enqueue_scripts');
  59. }
  60. function init_panels() {
  61. $classes = array(
  62. 'Debug_Bar_PHP',
  63. 'Debug_Bar_Queries',
  64. 'Debug_Bar_WP_Query',
  65. 'Debug_Bar_Deprecated',
  66. 'Debug_Bar_Request',
  67. 'Debug_Bar_Object_Cache',
  68. );
  69. foreach ( $classes as $class ) {
  70. $this->panels[] = new $class;
  71. }
  72. $this->panels = apply_filters( 'debug_bar_panels', $this->panels );
  73. }
  74. function ensure_ajaxurl() { ?>
  75. <script type="text/javascript">
  76. //<![CDATA[
  77. var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
  78. //]]>
  79. </script>
  80. <?php
  81. }
  82. // memory_get_peak_usage is PHP >= 5.2.0 only
  83. function safe_memory_get_peak_usage() {
  84. if ( function_exists( 'memory_get_peak_usage' ) ) {
  85. $usage = memory_get_peak_usage();
  86. } else {
  87. $usage = memory_get_usage();
  88. }
  89. return $usage;
  90. }
  91. function admin_bar_menu() {
  92. global $wp_admin_bar;
  93. $classes = apply_filters( 'debug_bar_classes', array() );
  94. $classes = implode( " ", $classes );
  95. /* Add the main siteadmin menu item */
  96. $wp_admin_bar->add_menu( array(
  97. 'id' => 'debug-bar',
  98. 'parent' => 'top-secondary',
  99. 'title' => apply_filters( 'debug_bar_title', __('Debug', 'debug-bar') ),
  100. 'meta' => array( 'class' => $classes ),
  101. ) );
  102. // @todo: Uncomment and finish me!
  103. // foreach ( $this->panels as $panel_key => $panel ) {
  104. // if ( ! $panel->is_visible() )
  105. // continue;
  106. //
  107. // $panel_class = get_class( $panel );
  108. //
  109. // $wp_admin_bar->add_menu( array(
  110. // 'parent' => 'debug-bar',
  111. // 'id' => "debug-bar-$panel_class",
  112. // 'title' => $panel->title(),
  113. // ) );
  114. // }
  115. }
  116. function body_class( $classes ) {
  117. if ( is_array( $classes ) )
  118. $classes[] = 'debug-bar-maximized';
  119. else
  120. $classes .= ' debug-bar-maximized ';
  121. if ( isset( $_GET['debug-bar'] ) ) {
  122. if ( is_array( $classes ) )
  123. $classes[] = 'debug-bar-visible';
  124. else
  125. $classes .= ' debug-bar-visible ';
  126. }
  127. return $classes;
  128. }
  129. function render() {
  130. global $wpdb;
  131. if ( empty( $this->panels ) )
  132. return;
  133. foreach ( $this->panels as $panel_key => $panel ) {
  134. $panel->prerender();
  135. if ( ! $panel->is_visible() )
  136. unset( $this->panels[ $panel_key ] );
  137. }
  138. ?>
  139. <div id='querylist'>
  140. <div id="debug-bar-actions">
  141. <span class="maximize">+</span>
  142. <span class="restore">&ndash;</span>
  143. <span class="close">&times;</span>
  144. </div>
  145. <div id='debug-bar-info'>
  146. <div id="debug-status">
  147. <?php //@todo: Add a links to information about WP_DEBUG, PHP version, MySQL version, and Peak Memory.
  148. $statuses = array();
  149. $statuses[] = array( 'site', php_uname( 'n' ), sprintf( __( '#%d', 'debug-bar' ), get_current_blog_id() ) );
  150. $statuses[] = array( 'php', __('PHP', 'debug-bar'), phpversion() );
  151. $db_title = empty( $wpdb->is_mysql ) ? __( 'DB', 'debug-bar' ) : 'MySQL';
  152. $statuses[] = array( 'db', $db_title, $wpdb->db_version() );
  153. $statuses[] = array( 'memory', __('Memory Usage', 'debug-bar'), sprintf( __('%s bytes', 'debug-bar'), number_format_i18n( $this->safe_memory_get_peak_usage() ) ) );
  154. if ( ! WP_DEBUG )
  155. $statuses[] = array( 'warning', __('Please Enable', 'debug-bar'), 'WP_DEBUG' );
  156. $statuses = apply_filters( 'debug_bar_statuses', $statuses );
  157. foreach ( $statuses as $status ):
  158. list( $slug, $title, $data ) = $status;
  159. ?><div id='debug-status-<?php echo esc_attr( $slug ); ?>' class='debug-status'>
  160. <div class='debug-status-title'><?php echo $title; ?></div>
  161. <?php if ( ! empty( $data ) ): ?>
  162. <div class='debug-status-data'><?php echo $data; ?></div>
  163. <?php endif; ?>
  164. </div><?php
  165. endforeach;
  166. ?>
  167. </div>
  168. </div>
  169. <div id='debug-bar-menu'>
  170. <ul id="debug-menu-links">
  171. <?php
  172. $current = ' current';
  173. foreach ( $this->panels as $panel ) :
  174. $class = get_class( $panel );
  175. ?>
  176. <li><a
  177. id="debug-menu-link-<?php echo esc_attr( $class ); ?>"
  178. class="debug-menu-link<?php echo $current; ?>"
  179. href="#debug-menu-target-<?php echo esc_attr( $class ); ?>">
  180. <?php
  181. // Not escaping html here, so panels can use html in the title.
  182. echo $panel->title();
  183. ?>
  184. </a></li>
  185. <?php
  186. $current = '';
  187. endforeach; ?>
  188. </ul>
  189. </div>
  190. <div id="debug-menu-targets"><?php
  191. $current = ' style="display: block"';
  192. foreach ( $this->panels as $panel ) :
  193. $class = get_class( $panel ); ?>
  194. <div id="debug-menu-target-<?php echo $class; ?>" class="debug-menu-target" <?php echo $current; ?>>
  195. <?php $panel->render(); ?>
  196. </div>
  197. <?php
  198. $current = '';
  199. endforeach;
  200. ?>
  201. </div>
  202. <?php do_action( 'debug_bar' ); ?>
  203. </div>
  204. <?php
  205. }
  206. }
  207. $GLOBALS['debug_bar'] = new Debug_Bar();