Skip to content

Instantly share code, notes, and snippets.

@JDGrimes
Created April 4, 2014 21:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JDGrimes/9983284 to your computer and use it in GitHub Desktop.
Save JDGrimes/9983284 to your computer and use it in GitHub Desktop.
My development tools
<?php
/**
* Plugin Name: J.D.'s Dev Tools
* Version: 0.9.9-beta-5
* Description: My development tools.
* Author: J.D. Grimes
* Author URI: http://codesymphony.co/
*/
/**
* Dump a variable to the error log.
*
* You can pass as many variables as you want!
*
* @since 0.9.9-beta-1
*
* @param mixed $var
* @param mixed ...
*/
function var_log() {
ob_start();
call_user_func_array( 'var_dump', func_get_args() );
error_log( ob_get_clean() );
}
/**
* Dump a variable into the error log, and return it.
*
* Useful when you don't want to change you code structure just for debugging. Also
* useful with add_filter().
*
* @since 0.9.9-beta-3
*
* @param mixed $var The variable to log.
*
* @return mixed The variable passed in.
*/
function var_log_thru( $var ) {
var_log( $var );
return $var;
}
/**
* Dump a backtrace into the error log.
*
* @since 0.9.9-beta-5
*
* @param int $option {@see debug_print_backtrace() Backtrace options}
* @param int $limit {@see debug_pring_backtrace() Backtrace stack limit}
*/
function log_backtrace( $option = 0, $limit = 0 ) {
ob_start();
debug_print_backtrace( $option, $limit );
error_log( ob_get_clean() );
}
/**
* Log a backtrace and return the first argument passed.
*
* Useful for logging a backtrace when a filter is called.
*
* @param mixed $var A var to return.
*
* @return mixed $var
*/
function log_backtrace_thru( $var = null ) {
log_backtrace();
return $var;
}
/**
* Find the file in which a function is defined.
*
* @since 0.9.9-beta-6
*
* @param string $function The name of the function to find.
*
* @return string|bool The file and line number on which the function is defined.
*/
function find_function_definition( $function ) {
try {
$reflection = new ReflectionFunction( $function );
} catch ( ReflectionException $e ) {
return false;
}
if ( $reflection->isInternal() ) {
return false;
}
return $reflection->getFileName() . ':' . $reflection->getStartLine();
}
/**
* Find the file in which a class is defined.
*
* @since 0.9.9-beta-6
*
* @param string $class The name of the class to find.
*
* @return string|bool The file and line number on which the class is defined.
*/
function find_class_definition( $class ) {
try {
$reflection = new ReflectionClass( $class );
} catch ( ReflectionException $e ) {
return false;
}
if ( $reflection->isInternal() ) {
return false;
}
return $reflection->getFileName() . ':' . $reflection->getStartLine();
}
/**
* Find the definition location of a callable.
*
* @param callable A callable to find the definition location of.
*
* @return string|bool The path to the file the callable is defined in, or false.
*/
function find_callable_definition( $callable ) {
if ( is_string( $callable ) ) {
if ( strpos( $callable, '::' ) ) {
return find_method_definition( $callable );
} else {
return find_function_definition( $callable );
}
} elseif ( is_array( $callable ) ) {
return call_user_func_array( 'find_method_definition', $callable );
}
return false;
}
/**
* Find the difinition location of a class method.
*
* @param string|object $class A :: string or an instance of the class.
* @param string $method The method to get, if $class is an object instance.
*
* @return string|bool The path to the method definition, or false on failure.
*/
function find_method_definition( $class, $method = null ) {
try {
if ( is_string( $class ) && ! isset( $method ) ) {
$reflection = new ReflectionMethod( $class );
} else {
$reflection = new ReflectionMethod( $class, $method );
}
} catch ( ReflectionException $e ) {
return false;
}
if ( $reflection->isInternal() ) {
return false;
}
return $reflection->getFileName() . ':' . $reflection->getStartLine();
}
/**
* Find the location where a shortcode callback is defined.
*
* @param string $shortcode The shortcode name.
*
* @return string|bool The path to the callback function, or false on failure.
*/
function find_shortcode_definition( $shortcode ) {
global $shortcode_tags;
if ( ! isset( $shortcode_tags[ $shortcode ] ) ) {
return false;
}
return find_callable_definition( $shortcode_tags[ $shortcode ] );
}
if ( function_exists( 'add_action' ) && ini_get( 'xdebug.default_enable' ) != '1' ) {
add_action( 'wordpoints_debug_message', 'log_backtrace', 10, 0 );
add_action( 'doing_it_wrong_run', 'log_backtrace', 10, 0 );
add_action( 'deprecated_function_run', 'log_backtrace', 10, 0 );
add_action( 'deprecated_file_included', 'log_backtrace', 10, 0 );
add_action( 'deprecated_argument_run', 'log_backtrace', 10, 0 );
}
// end of file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment