class-debug-bar-wp-query.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. class Debug_Bar_WP_Query extends Debug_Bar_Panel {
  3. function init() {
  4. $this->title( __('WP Query', 'debug-bar') );
  5. }
  6. function prerender() {
  7. $this->set_visible( defined('SAVEQUERIES') && SAVEQUERIES );
  8. }
  9. function render() {
  10. global $template, $wp_query;
  11. $queried_object = get_queried_object();
  12. if ( $queried_object && isset( $queried_object->post_type ) )
  13. $post_type_object = get_post_type_object( $queried_object->post_type );
  14. echo "<div id='debug-bar-wp-query'>";
  15. echo '<h2><span>Queried Object ID:</span>' . get_queried_object_id() . "</h2>\n";
  16. // Determine the query type. Follows the template loader order.
  17. $type = '';
  18. if ( is_404() )
  19. $type = '404';
  20. elseif ( is_search() )
  21. $type = 'Search';
  22. elseif ( is_tax() )
  23. $type = 'Taxonomy';
  24. elseif ( is_front_page() )
  25. $type = 'Front Page';
  26. elseif ( is_home() )
  27. $type = 'Home';
  28. elseif ( is_attachment() )
  29. $type = 'Attachment';
  30. elseif ( is_single() )
  31. $type = 'Single';
  32. elseif ( is_page() )
  33. $type = 'Page';
  34. elseif ( is_category() )
  35. $type = 'Category';
  36. elseif ( is_tag() )
  37. $type = 'Tag';
  38. elseif ( is_author() )
  39. $type = 'Author';
  40. elseif ( is_date() )
  41. $type = 'Date';
  42. elseif ( is_archive() )
  43. $type = 'Archive';
  44. elseif ( is_paged() )
  45. $type = 'Paged';
  46. if ( !empty($type) )
  47. echo '<h2><span>Query Type:</span>' . $type . "</h2>\n";
  48. if ( !empty($template) )
  49. echo '<h2><span>Query Template:</span>' . basename($template) . "</h2>\n";
  50. $show_on_front = get_option( 'show_on_front' );
  51. $page_on_front = get_option( 'page_on_front' );
  52. $page_for_posts = get_option( 'page_for_posts' );
  53. echo '<h2><span>Show on Front:</span>' . $show_on_front . "</h2>\n";
  54. if ( 'page' == $show_on_front ) {
  55. echo '<h2><span>Page for Posts:</span>' . $page_for_posts . "</h2>\n";
  56. echo '<h2><span>Page on Front:</span>' . $page_on_front . "</h2>\n";
  57. }
  58. if ( isset( $post_type_object ) )
  59. echo '<h2><span>Post Type:</span>' . $post_type_object->labels->singular_name . "</h2>\n";
  60. echo '<div class="clear"></div>';
  61. if ( empty($wp_query->query) )
  62. $query = 'None';
  63. else
  64. $query = http_build_query( $wp_query->query );
  65. echo '<h3>Query Arguments:</h3>';
  66. echo '<p>' . esc_html( $query ) . '</p>';
  67. if ( ! empty($wp_query->request) ) {
  68. echo '<h3>Query SQL:</h3>';
  69. echo '<p>' . esc_html( $wp_query->request ) . '</p>';
  70. }
  71. if ( ! is_null( $queried_object ) ) {
  72. echo '<h3>Queried Object:</h3>';
  73. echo '<ol class="debug-bar-wp-query-list">';
  74. $this->_recursive_print_kv($queried_object);
  75. echo '</ol>';
  76. }
  77. echo '</div>';
  78. }
  79. protected function _recursive_print_kv( $kv_array ) {
  80. foreach ( $kv_array as $key => $value ) {
  81. if( is_object( $value ) || is_array( $value ) ) {
  82. printf( '<li>%s => <ol>', $key );
  83. $this->_recursive_print_kv( $value );
  84. echo '</ol></li>';
  85. } else {
  86. echo "<li>{$key} => {$value}</li>";
  87. }
  88. }
  89. }
  90. }