From 1e3195ffc8a2cddb15a15076403d2423da1b16ab Mon Sep 17 00:00:00 2001 From: Andrey Savchenko Date: Mon, 12 Mar 2018 22:33:07 +0200 Subject: [PATCH] Implemented includes shortening in backtrace. --- src/Formatter/Backtrace_Formatter.php | 42 +++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/Formatter/Backtrace_Formatter.php b/src/Formatter/Backtrace_Formatter.php index f41602c..c6a20c9 100644 --- a/src/Formatter/Backtrace_Formatter.php +++ b/src/Formatter/Backtrace_Formatter.php @@ -13,12 +13,30 @@ class Backtrace_Formatter { 'wp-load.php', 'wp-config.php', 'admin.php', + 'template-loader.php', 'locate_template', 'load_template', '_wp_get_current_user', 'WP_Hook', ]; + /** @var array $truncate_paths Paths to truncate from includes. */ + protected $truncate_paths = []; + + /** + * Set up object properties. + */ + public function __construct() { + + $this->truncate_paths = [ + wp_normalize_path( ABSPATH ), + wp_normalize_path( WP_CONTENT_DIR ), + 'wp-admin/', + 'themes/', + 'plugins/', + ]; + } + /** * @param array|string $backtrace Backtrace input as produced by WP. * @@ -30,9 +48,10 @@ class Backtrace_Formatter { $backtrace = explode( ', ', $backtrace ); } - // TODO shorten includes. - $backtrace = array_filter( $backtrace, [ $this, 'filter' ] ); + $backtrace = array_map( [ $this, 'shorten_include' ], $backtrace ); + + // TODO compact redunant function sequences, such as transient into option calls. return $backtrace; } @@ -52,4 +71,23 @@ class Backtrace_Formatter { return true; } + + /** + * @param string $item Backtrace item. + * + * @return string + */ + protected function shorten_include( $item ) { + + if ( 0 === strpos( $item, 'include' ) || 0 === strpos( $item, 'require' ) ) { + + $path = substr( $item, strpos( $item, '(\'' ) + 2, - 2 ); + $path = wp_normalize_path( $path ); + $path = ltrim( str_replace( $this->truncate_paths, '', $path ), '/' ); + + return $path; + } + + return $item; + } }