class-debug-bar-queries.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. class Debug_Bar_Queries extends Debug_Bar_Panel {
  3. function init() {
  4. $this->title( __('Queries', 'debug-bar') );
  5. }
  6. function prerender() {
  7. $this->set_visible( defined('SAVEQUERIES') && SAVEQUERIES || ! empty($GLOBALS['EZSQL_ERROR']) );
  8. }
  9. function debug_bar_classes( $classes ) {
  10. if ( ! empty($GLOBALS['EZSQL_ERROR']) )
  11. $classes[] = 'debug-bar-php-warning-summary';
  12. return $classes;
  13. }
  14. function render() {
  15. global $wpdb, $EZSQL_ERROR;
  16. $out = '';
  17. $total_time = 0;
  18. if ( !empty($wpdb->queries) ) {
  19. $show_many = isset($_GET['debug_queries']);
  20. if ( $wpdb->num_queries > 500 && !$show_many )
  21. $out .= "<p>" . sprintf( __('There are too many queries to show easily! <a href="%s">Show them anyway</a>', 'debug-bar'), esc_url( add_query_arg( 'debug_queries', 'true' ) ) ) . "</p>";
  22. $out .= '<ol class="wpd-queries">';
  23. $counter = 0;
  24. foreach ( $wpdb->queries as $q ) {
  25. list($query, $elapsed, $debug) = $q;
  26. $total_time += $elapsed;
  27. if ( ++$counter > 500 && ! $show_many )
  28. continue;
  29. $debug = explode( ', ', $debug );
  30. $debug = array_diff( $debug, array( 'require_once', 'require', 'include_once', 'include' ) );
  31. $debug = implode( ', ', $debug );
  32. $debug = str_replace( array( 'do_action, call_user_func_array' ), array( 'do_action' ), $debug );
  33. $query = nl2br(esc_html($query));
  34. $out .= "<li>$query<br/><div class='qdebug'>$debug <span>#{$counter} (" . number_format(sprintf('%0.1f', $elapsed * 1000), 1, '.', ',') . "ms)</span></div></li>\n";
  35. }
  36. $out .= '</ol>';
  37. } else {
  38. if ( $wpdb->num_queries == 0 )
  39. $out .= "<p><strong>" . __('There are no queries on this page.', 'debug-bar') . "</strong></p>";
  40. else
  41. $out .= "<p><strong>" . __('SAVEQUERIES must be defined to show the query log.', 'debug-bar') . "</strong></p>";
  42. }
  43. if ( ! empty($EZSQL_ERROR) ) {
  44. $out .= '<h3>' . __( 'Database Errors', 'debug-bar' ) . '</h3>';
  45. $out .= '<ol class="wpd-queries">';
  46. foreach ( $EZSQL_ERROR as $e ) {
  47. $query = nl2br(esc_html($e['query']));
  48. $out .= "<li>$query<br/><div class='qdebug'>{$e['error_str']}</div></li>\n";
  49. }
  50. $out .= '</ol>';
  51. }
  52. $heading = '';
  53. if ( $wpdb->num_queries )
  54. $heading .= '<h2><span>Total Queries:</span>' . number_format( $wpdb->num_queries ) . "</h2>\n";
  55. if ( $total_time )
  56. $heading .= '<h2><span>Total query time:</span>' . number_format(sprintf('%0.1f', $total_time * 1000), 1) . " ms</h2>\n";
  57. if ( ! empty($EZSQL_ERROR) )
  58. $heading .= '<h2><span>Total DB Errors:</span>' . number_format( count($EZSQL_ERROR) ) . "</h2>\n";
  59. $out = $heading . $out;
  60. echo $out;
  61. }
  62. }