From cff0e266a4a2c0508e96ff1ce116510b77b62f95 Mon Sep 17 00:00:00 2001 From: westi Date: Sat, 28 Jan 2012 11:56:50 +0000 Subject: [PATCH] Refactor WPDB::get_caller() into wp_debug_backtrace_summary() and improve the functionality to provide enhanced context and a standardised default pretty format. Fixes #19589 git-svn-id: http://svn.automattic.com/wordpress/trunk@19773 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/functions.php | 40 +++++++++++++++++++++++++++++++++++++++ wp-includes/wp-db.php | 11 +---------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 747356d23..f41da9466 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -3658,3 +3658,43 @@ function wp_allowed_protocols() { return $protocols; } + +/** + * Return a comma seperated string of functions that have been called to get to the current point in code. + * @link http://core.trac.wordpress.org/ticket/19589 + * @since 3.4 + * + * @param string $ignore_class A class to ignore all function calls within - useful when you want to just give info about the calllee + * @param string $skip_frames A number of stack frames to skip - useful for unwinding back to the source of the issue + * @param bool $pretty Whether or not you want a comma seperated string or raw array returned + * @return string|array Either a string containing a reversed comma seperated trace or an array of individual calls. + */ +function wp_debug_backtrace_summary( $ignore_class = null, $skip_frames = 0, $pretty = true ) { + $trace = debug_backtrace( false ); + $caller = array(); + $check_class = ! is_null( $ignore_class ); + $skip_frames++; // skip this function + + foreach ( $trace as $call ) { + if ( $skip_frames > 0 ) { + $skip_frames--; + } elseif ( isset( $call['class'] ) ) { + if ( $check_class && $ignore_class == $call['class'] ) + continue; // Filter out calls + + $caller[] = "{$call['class']}{$call['type']}{$call['function']}"; + } else { + if ( in_array( $call['function'], array( 'do_action', 'apply_filters' ) ) ) { + $caller[] = "{$call['function']}('{$call['args'][0]}')"; + } elseif ( in_array( $call['function'], array( 'include', 'include_once', 'require', 'require_once' ) ) ) { + $caller[] = $call['function'] . "('" . str_replace( array( WP_CONTENT_DIR, ABSPATH ) , '', $call['args'][0] ) . "')"; + } else { + $caller[] = $call['function']; + } + } + } + if ( $pretty ) + return join( ', ', array_reverse( $caller ) ); + else + return $caller; +} \ No newline at end of file diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php index 816ec1115..2347c912d 100644 --- a/wp-includes/wp-db.php +++ b/wp-includes/wp-db.php @@ -1545,16 +1545,7 @@ class wpdb { * @return string The name of the calling function */ function get_caller() { - $trace = array_reverse( debug_backtrace() ); - $caller = array(); - - foreach ( $trace as $call ) { - if ( isset( $call['class'] ) && __CLASS__ == $call['class'] ) - continue; // Filter out wpdb calls. - $caller[] = isset( $call['class'] ) ? "{$call['class']}->{$call['function']}" : $call['function']; - } - - return join( ', ', $caller ); + return wp_debug_backtrace_summary( __CLASS__ ); } /**