query( 'SET SESSION query_cache_type = 0;' );
$debug_queries = '';
if ( $wpdb->queries ) {
$x = 0;
$total_time = timer_stop( FALSE, 22 );
$total_query_time = 0;
$class = '';
$debug_queries .= '
' . "\n";
foreach ( $wpdb->queries as $q ) {
if ( $x % 2 != 0 )
$class = '';
else
$class = ' class="alt"';
$q[0] = trim( ereg_replace( '[[:space:]]+', ' ', $q[0]) );
$total_query_time += $q[1];
$debug_queries .= '';
$debug_queries .= '- ' . __( 'Time:' ) . ' ' . $q[1] . '
';
if ( isset($q[1]) )
$debug_queries .= '- ' . __( 'Query:' ) . ' ' . htmlentities( $q[0] ) . '
';
if ( isset($q[2]) )
$debug_queries .= '- ' . __( 'Call from:' ) . ' ' . htmlentities( $q[2] ) . '
';
$debug_queries .= '
' . "\n";
$x++;
}
$debug_queries .= '
' . "\n\n";
}
$php_time = $total_time - $total_query_time;
// Create the percentages
$mysqlper = number_format_i18n( $total_query_time / $total_time * 100, 2 );
$phpper = number_format_i18n( $php_time / $total_time * 100, 2 );
$debug_queries .= '' . "\n";
$debug_queries .= '- ' . __( 'Total query time:' ) . ' ' . number_format_i18n( $total_query_time, 5 ) . __( 's for' ) . ' ' . count($wpdb->queries) . ' ' . __( 'queries.' ) . '
';
if ( count($wpdb->queries) != get_num_queries() ) {
$debug_queries .= '- ' . __( 'Total num_query time:' ) . ' ' . timer_stop() . ' ' . __( 'for' ) . ' ' . get_num_queries() . ' ' . __( 'num_queries.' ) . '
' . "\n";
$debug_queries .= '- ' . __( '» Different values in num_query and query? - please set the constant' ) . '
define(\'SAVEQUERIES\', true);' . __( 'in your' ) . ' wp-config.php ' . "\n";
}
if ( $total_query_time == 0 )
$debug_queries .= '- ' . __( '» Query time is null (0)? - please set the constant' ) . '
SAVEQUERIES' . ' ' . __( 'at' ) . ' TRUE ' . __( 'in your' ) . ' wp-config.php ' . "\n";
$debug_queries .= '- ' . __( 'Page generated in' ). ' ' . number_format_i18n( $total_time, 5 ) . __( 's, ' ) . $phpper . __( '% PHP' ) . ', ' . $mysqlper . __( '% MySQL' ) . '
' . "\n";
$debug_queries .= '
' . "\n";
return $debug_queries;
}
// echo in html-comment
function html_comment_queries() {
if ( ! current_user_can( 'DebugQueries' ) )
return;
$echo = '';
$echo .= "\n\n" . __( '' . "\n\n";
echo $echo;
}
// echo in frontend
function the_queries() {
if ( ! current_user_can( 'DebugQueries' ) )
return;
$echo = '';
$echo .= '' . "\n";
$echo .= '
' . "\n";
$echo .= '
' . __( '» Deactivate after analysis!' ). '
' . "\n";
$echo .= $this->get_queries();
$echo .= '
' . "\n\n";
echo $echo;
}
// add user rights
function activate() {
global $wp_roles;
$wp_roles->add_cap( 'administrator', 'DebugQueries' );
}
// delete user rights
function deactivate() {
global $wp_roles;
$wp_roles->remove_cap( 'administrator', 'DebugQueries' );
}
// function for WP < 2.8
function plugins_url( $path = '', $plugin = '' ) {
if ( function_exists( 'is_ssl' ) )
$scheme = ( is_ssl() ? 'https' : 'http' );
else
$scheme = 'http';
$url = WP_PLUGIN_URL;
if ( 0 === strpos( $url, 'http' ) ) {
if ( function_exists( 'is_ssl' ) && is_ssl() )
$url = str_replace( 'http://', "{$scheme}://", $url );
}
if ( ! empty( $plugin ) && is_string( $plugin ) ) {
$folder = dirname( plugin_basename( $plugin ) );
if ( '.' != $folder)
$url .= '/' . ltrim($folder, '/' );
}
if ( ! empty( $path ) && FALSE === is_string( $path ) && strpos( $path, '..' ) )
$url .= '/' . ltrim( $path, '/' );
return apply_filters( 'plugins_url', $url, $path, $plugin );
}
// infos in frontend, add css to head
function wp_head() {
global $wp_version;
if ( ! current_user_can( 'DebugQueries' ) )
return NULL;
if ( version_compare( $wp_version, '2.8dev', '>' ) )
$style = plugins_url( 'css/style-frontend.css', __FILE__ );
else
$style = $this->plugins_url( 'css/style-frontend.css', __FILE__ );
$return = '';
echo $return;
}
}
$debug_queries = new Debug_Queries();
}
?>