Skip to content

Instantly share code, notes, and snippets.

@scribu
Created October 7, 2012 22:52
Show Gist options
  • Save scribu/3849859 to your computer and use it in GitHub Desktop.
Save scribu/3849859 to your computer and use it in GitHub Desktop.
Log WP_Query calls
<?php
/**
* Wrapper around WP_Query; logs various actions.
*
* After writing it, I realized that it's better to just step through the code using Xdebug.
*/
class WP_Query_Log {
private $original;
function __construct( $wp_query ) {
$this->original = $wp_query;
}
function __call( $method, $args ) {
if ( 0 !== strpos( $method, 'is_' ) )
self::log_call( $method, $args );
return call_user_func_array( array( $this->original, $method ), $args );
}
function &__get( $key ) {
self::log( "Getting '$key'" );
return $this->original->$key;
}
function __set( $key, $value ) {
self::log( "Setting '$key' to " . self::arg_to_str( $value ) );
return $this->original->$key = $value;
}
private static function log_call( $method, $args ) {
$arg_str = array_map( array( __CLASS__, 'arg_to_str' ), $args );
$arg_str = empty( $args ) ? '' : ' ' . implode( ', ', $arg_str ) . ' ';
echo self::log( "Calling $method($arg_str)" );
}
private static function arg_to_str( $arg ) {
if ( is_array( $arg ) && empty( $arg ) )
return 'array()';
return var_export( $arg, true );
}
private static function log( $str ) {
echo html( 'pre', $str );
}
}
add_action( 'setup_theme', function() {
global $wp_query;
$wp_query = new WP_Query_Log( $wp_query );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment