Skip to content

Commit

Permalink
Work in progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
dev4press committed Jun 21, 2023
1 parent 998d52f commit dfc5bcc
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 38 deletions.
36 changes: 26 additions & 10 deletions core/display/ErrorFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ErrorFormat {
public static function render_caller( $caller, $escape = false ) : string {
$_print = $escape ? esc_html( $caller ) : $caller;

$render = '<a href="#" class="debugpress-events-log-toggle">' . __( "Show Details", "debugpress" ) . '</a>';
$render = ' <a href="#" class="debugpress-events-log-toggle">' . __( "Show Details", "debugpress" ) . '</a>';
$render .= '<div class="debugpress-events-log-toggler"><strong>' . __( "From", "debugpress" ) . ':</strong><br/>' . $_print . '</div>';

return $render;
Expand Down Expand Up @@ -87,12 +87,7 @@ public static function doing_it_wrong( $item ) : string {
$render .= '<strong>' . __( "On line", "debugpress" ) . ":</strong> " . $item[ 'on_line' ] . '<br/>';
$render .= '<strong>' . __( "In file", "debugpress" ) . ":</strong> " . $item[ 'in_file' ] . '<br/>';

$caller = isset( $item[ 'caller' ] ) ? maybe_unserialize( $item[ 'caller' ] ) : '';
$caller = is_array( $caller ) ? join( '<br/>', $caller ) : $caller;

if ( ! empty( $caller ) ) {
$render .= ErrorFormat::render_caller( $caller );
}
$render .= ErrorFormat::process_caller( $item );

if ( $item[ 'message' ] ) {
$render .= '<div class="debugpress-error-message">' . esc_html( $item[ 'message' ] ) . '</div>';
Expand All @@ -115,6 +110,8 @@ public static function deprecated_file( $item ) : string {
$render .= sprintf( __( "<strong>%s</strong> is deprecated since version %s.", "debugpress" ), $item[ "deprecated" ], $item[ "version" ] );
}

$render .= ErrorFormat::process_caller( $item );

$render .= '</div>';

return $render;
Expand All @@ -136,10 +133,12 @@ public static function deprecated_function( $item ) : string {
$render .= sprintf( __( "<strong>%s</strong> is deprecated since version %s.", "debugpress" ), $item[ "deprecated" ], $item[ "version" ] );
}

if ( isset( $item[ 'message' ] ) && ! empty( $item[ 'message' ] ) ) {
if ( ! empty( $item[ 'message' ] ) ) {
$render .= '<em>' . $item[ 'message' ] . '</em>';
}

$render .= ErrorFormat::process_caller( $item );

$render .= '</div>';

return $render;
Expand All @@ -153,10 +152,12 @@ public static function deprecated_constructor( $item ) : string {

$render .= sprintf( __( "For <strong>%s</strong> since version %s.", "debugpress" ), $item[ "deprecated" ], $item[ "version" ] );

if ( isset( $item[ 'message' ] ) && ! empty( $item[ 'message' ] ) ) {
if ( ! empty( $item[ 'message' ] ) ) {
$render .= '<em>' . $item[ 'message' ] . '</em>';
}

$render .= ErrorFormat::process_caller( $item );

$render .= '</div>';

return $render;
Expand All @@ -176,12 +177,27 @@ public static function deprecated_argument( $item ) : string {

$render .= sprintf( __( "Argument in <strong>%s</strong> is deprecated since version %s.", "debugpress" ), $item[ "deprecated" ], $item[ "version" ] );

if ( isset( $item[ 'message' ] ) && ! empty( $item[ 'message' ] ) ) {
if ( ! empty( $item[ 'message' ] ) ) {
$render .= '<em>' . $item[ 'message' ] . '</em>';
}

$render .= ErrorFormat::process_caller( $item );

$render .= '</div>';

return $render;
}

public static function process_caller( $item ) : string {
$render = '';

$caller = isset( $item[ 'caller' ] ) ? maybe_unserialize( $item[ 'caller' ] ) : '';
$caller = is_array( $caller ) ? join( '<br/>', $caller ) : $caller;

if ( ! empty( $caller ) ) {
$render .= ErrorFormat::render_caller( $caller );
}

return $render;
}
}
24 changes: 12 additions & 12 deletions core/display/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,6 @@ public function prepare_tabs() {
$this->tabs[ 'plugins' ] = __( "Plugins", "debugpress" );
}

if ( ! empty( debugpress_tracker()->errors ) ) {
$this->tabs[ 'errors' ] = __( "Errors", "debugpress" ) . ' (' . debugpress_tracker()->counts[ 'errors' ] . ')';
}

if ( ! empty( debugpress_tracker()->deprecated ) ) {
$this->tabs[ 'deprecated' ] = __( "Deprecated", "debugpress" ) . ' (' . debugpress_tracker()->counts[ 'deprecated' ] . ')';
}

if ( ! empty( debugpress_tracker()->doingitwrong ) ) {
$this->tabs[ 'doingitwrong' ] = __( "Doing It Wrong", "debugpress" ) . ' (' . debugpress_tracker()->counts[ 'doingitwrong' ] . ')';
}

if ( debugpress_plugin()->get( 'panel_http' ) && ! empty( debugpress_tracker()->httpapi ) ) {
$this->tabs[ 'http' ] = __( "HTTP", "debugpress" ) . ' (' . count( debugpress_tracker()->httpapi ) . ')';
}
Expand All @@ -248,6 +236,18 @@ public function prepare_tabs() {
$this->tabs[ 'store' ] = __( "Store", "debugpress" ) . ' (' . count( debugpress_tracker()->logged ) . ')';
}

if ( ! empty( debugpress_tracker()->deprecated ) ) {
$this->tabs[ 'deprecated' ] = __( "Deprecated", "debugpress" ) . ' (' . debugpress_tracker()->counts[ 'deprecated' ] . ')';
}

if ( ! empty( debugpress_tracker()->doingitwrong ) ) {
$this->tabs[ 'doingitwrong' ] = __( "Doing It Wrong", "debugpress" ) . ' (' . debugpress_tracker()->counts[ 'doingitwrong' ] . ')';
}

if ( ! empty( debugpress_tracker()->errors ) ) {
$this->tabs[ 'errors' ] = __( "Errors", "debugpress" ) . ' (' . debugpress_tracker()->counts[ 'errors' ] . ')';
}

$this->tabs[ 'layout' ] = array(
'label' => __( "Layout", "debugpress" ),
'tab' => '<i class="debugpress-icon debugpress-icon-layout"></i>'
Expand Down
66 changes: 50 additions & 16 deletions core/track/Tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public function track_wrong( $function, $message, $version ) {
$wrong = $function . '()';
$in_file = $this->_strip_abspath( $backtrace[ 3 ][ 'file' ] );
$on_line = $backtrace[ 3 ][ 'line' ];
$caller = $this->_get_caller( 'doing_it_wrong_run', '_doing_it_wrong' );
$caller = $this->_get_caller( '_doing_it_wrong' );

if ( ! empty( $this->_actual_file ) ) {
$in_file = $this->_actual_file;
Expand Down Expand Up @@ -351,8 +351,14 @@ public function track_function( $function, $replacement, $version ) {

$in_file = $this->_strip_abspath( $backtrace[ $bt ][ 'file' ] );
$on_line = $backtrace[ $bt ][ 'line' ];
$caller = $this->_get_caller( '_deprecated_function', true );

$error = compact( 'deprecated', 'replacement', 'version', 'hook', 'in_file', 'on_line' );
if ( ! empty( $this->_actual_file ) ) {
$in_file = $this->_actual_file;
$on_line = $this->_actual_line;
}

$error = compact( 'deprecated', 'replacement', 'version', 'hook', 'in_file', 'on_line', 'caller' );

$this->deprecated[ 'function' ][] = $error;

Expand All @@ -368,8 +374,14 @@ public function track_file( $file, $replacement, $version, $message ) {
$deprecated = $this->_strip_abspath( $backtrace[ 3 ][ 'file' ] );
$in_file = $this->_strip_abspath( $backtrace[ 4 ][ 'file' ] );
$on_line = $backtrace[ 4 ][ 'line' ];
$caller = $this->_get_caller( '_deprecated_file', true );

if ( ! empty( $this->_actual_file ) ) {
$in_file = $this->_actual_file;
$on_line = $this->_actual_line;
}

$error = compact( 'deprecated', 'replacement', 'message', 'version', 'in_file', 'on_line', 'file' );;
$error = compact( 'deprecated', 'replacement', 'message', 'version', 'in_file', 'on_line', 'file', 'caller' );;

$this->deprecated[ 'file' ][] = $error;

Expand Down Expand Up @@ -424,7 +436,14 @@ public function track_argument( $function, $message, $version ) {
break;
}

$error = compact( 'deprecated', 'message', 'menu', 'version', 'in_file', 'on_line' );;
if ( ! empty( $this->_actual_file ) ) {
$in_file = $this->_actual_file;
$on_line = $this->_actual_line;
}

$caller = $this->_get_caller( '_deprecated_argument', true );

$error = compact( 'deprecated', 'message', 'menu', 'version', 'in_file', 'on_line', 'caller' );;

$this->deprecated[ 'argument' ][] = $error;

Expand All @@ -440,8 +459,14 @@ public function track_constructor( $class, $version ) {
$deprecated = $class;
$in_file = $this->_strip_abspath( $backtrace[ 4 ][ 'file' ] );
$on_line = $backtrace[ 4 ][ 'line' ];
$caller = $this->_get_caller( '_deprecated_constructor', true );

$error = compact( 'deprecated', 'version', 'in_file', 'on_line' );
if ( ! empty( $this->_actual_file ) ) {
$in_file = $this->_actual_file;
$on_line = $this->_actual_line;
}

$error = compact( 'deprecated', 'version', 'in_file', 'on_line', 'caller' );

$this->deprecated[ 'constructor' ][] = $error;

Expand All @@ -457,8 +482,14 @@ public function track_hook_run( $hook, $replacement, $version, $message ) {
$deprecated = $hook;
$in_file = $this->_strip_abspath( $backtrace[ 4 ][ 'file' ] );
$on_line = $backtrace[ 4 ][ 'line' ];
$caller = $this->_get_caller( '_deprecated_hook', true );

if ( ! empty( $this->_actual_file ) ) {
$in_file = $this->_actual_file;
$on_line = $this->_actual_line;
}

$error = compact( 'deprecated', 'replacement', 'message', 'version', 'in_file', 'on_line' );
$error = compact( 'deprecated', 'replacement', 'message', 'version', 'in_file', 'on_line', 'caller' );

$this->deprecated[ 'hook-run' ][] = $error;

Expand Down Expand Up @@ -613,7 +644,7 @@ private function _strip_abspath( $path ) : string {
return ltrim( str_replace( array( untrailingslashit( ABSPATH ), '\\' ), array( '', '/' ), $path ), '/' );
}

private function _get_caller( $type = '', $function_target = '' ) : array {
private function _get_caller( $function_target = '', $use_previous_call_actual = false ) : array {
$_abspath = str_replace( '\\', '/', ABSPATH );

$filters = array( 'do_action', 'apply_filter', 'do_action_ref_array', 'call_user_func_array' );
Expand All @@ -623,12 +654,20 @@ private function _get_caller( $type = '', $function_target = '' ) : array {
$this->_actual_file = '';
$this->_actual_line = 0;

$prev = null;
foreach ( $trace as $call ) {
if ( ! empty( $function_target ) && isset( $call[ 'function' ] ) ) {
if ( substr( $call[ 'function' ], 0, strlen( $function_target ) ) == $function_target ) {
if ( isset( $call[ 'file' ] ) && isset( $call[ 'line' ] ) ) {
$this->_actual_file = $this->_normalize_path( $call[ 'file' ] );
$this->_actual_line = $call[ 'line' ];
if ( $use_previous_call_actual ) {
if ( $prev && isset( $prev[ 'file' ] ) && isset( $prev[ 'line' ] ) ) {
$this->_actual_file = $this->_normalize_path( $prev[ 'file' ] );
$this->_actual_line = $prev[ 'line' ];
}
} else {
if ( isset( $call[ 'file' ] ) && isset( $call[ 'line' ] ) ) {
$this->_actual_file = $this->_normalize_path( $call[ 'file' ] );
$this->_actual_line = $call[ 'line' ];
}
}

break;
Expand All @@ -649,12 +688,6 @@ private function _get_caller( $type = '', $function_target = '' ) : array {
}
}

if ( $type == 'doing_it_wrong_run' ) {
if ( $call[ 'function' ] == 'do_action' && $call[ 'args' ][ 0 ] == 'doing_it_wrong_run' ) {
continue;
}
}

$value = isset( $call[ "class" ] ) ? "{$call["class"]}->{$call["function"]}" : $call[ "function" ];

$file = '';
Expand Down Expand Up @@ -683,6 +716,7 @@ private function _get_caller( $type = '', $function_target = '' ) : array {
}

$caller[] = $value . $file . $filter;
$prev = $call;
}

return $caller;
Expand Down
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,13 @@ Open the WordPress 'Settings' menu, there you will find 'DebugPress' panel.
= 3.2 (2023.06.22) =
* New: Support for the deprecated hook run handling
* New: Execute actions when each error has been logged
* New: Deprecated tracking now logs the caller information
* Edit: Improved caller trace cleanup for error calls
* Edit: Display more relevant error source file and log for errors
* Edit: Errors tab moved to the end of the display order
* Edit: Changed tab order for several other debugger tabs
* Fix: Fatal error tracking calling default handler directly
* Fix: Sometimes errors not getting displayed in the Error tab

= 3.1 (2023.06.14) =
* New: Identify SQL queries sources for Dev4Press plugins
Expand Down

0 comments on commit dfc5bcc

Please sign in to comment.