Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions debug-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,31 @@
require_once 'vendor/autoload.php';

/**
* Shows error messages rather than the critical errors screen.
* Checks if a request is likely from Rest API.
*
* @return boolean
*/
function pinkcrab_is_rest() {
return defined( 'REST_API_VERSION' ) && strpos( $_SERVER['REQUEST_URI'], '/wp-json/' ) !== false;
}

/**
* Shows a custom error message in place of the WSOD.
*
* Will show a styled view of the error when accessed via the browser.
* Will show a simple error message when accessed via AJAX or Rest.
*
* @param string $message The error message.
* @param array $error The error array.
*/
add_filter(
'wp_php_error_args',
function ( $message, $error ) {
include 'views/wp-error.php';
if ( wp_doing_ajax() || pinkcrab_is_rest() ) {
include 'views/ajax-error.php';
} else {
include 'views/wp-error.php';
}
},
2,
10
Expand All @@ -32,7 +51,7 @@ function ( $message, $error ) {
* @return void
*/
function adump( ...$data ) {
if ( ! wp_doing_ajax() ) {
if ( ! wp_doing_ajax() && ! pinkcrab_is_rest() ) {
echo '<pre>';
}foreach ( $data as $item ) {
if ( is_null( $item ) ) {
Expand All @@ -45,7 +64,7 @@ function adump( ...$data ) {
print_r( $item );
}
}
if ( ! wp_doing_ajax() ) {
if ( ! wp_doing_ajax() && ! pinkcrab_is_rest() ) {
echo '</pre>';
}
}
Expand All @@ -58,18 +77,19 @@ function adump( ...$data ) {
* @return void
*/
function adie( ...$data ) {
if ( ! wp_doing_ajax() ) {
if ( ! wp_doing_ajax() && ! pinkcrab_is_rest() ) {
echo '<pre>';
}
adump( $data );
if ( ! wp_doing_ajax() ) {
if ( ! wp_doing_ajax() && ! pinkcrab_is_rest() ) {
echo '</pre>';
}
die();
}

/**
* Shows all the enqueued scripts and styles in header, if set in url.
* ?show_enqueued
*/
if ( ! empty( $_GET['show_enqueued'] ) ) {
add_action(
Expand Down Expand Up @@ -104,20 +124,27 @@ function ( $e ) use ( $wp_scripts ) {
* So all of defined hooks if in url.
* ?show_hooks=hook,hook2
*/
if( ! empty( $_GET['show_hooks'] ) ) {
if ( ! empty( $_GET['show_hooks'] ) ) {
add_action(
'wp_head',
function () {
$hooks = explode( ',', $_GET['show_hooks'] );
foreach( $hooks as $hook ) {
dump( array( 'hook' => $hook, 'callbacks' => $GLOBALS['wp_filter'][ $hook ] ) );
foreach ( $hooks as $hook ) {
dump(
array(
'hook' => $hook,
'callbacks' => $GLOBALS['wp_filter'][ $hook ],
)
);
}
}
);
}

/**
* Logger.
* Custom Logger.
*
* Saves to wp-content/pc_debug.log
*
* @param mixed ...$data
*
Expand Down Expand Up @@ -146,6 +173,13 @@ function pclog( $data, string $type = 'log' ) {
file_put_contents( $log_file, $entry . $log );
}

/**
* Write to the PHP error log.
*
* @param mixed $log
*
* @return void
*/
if ( ! function_exists( 'write_log' ) ) {
function write_log( $log ) {
if ( is_array( $log ) || is_object( $log ) ) {
Expand Down
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ pclog($var);
```
> Will only create the file if it does not exist.

### pinkcrab_is_rest()

This function will return true if the current request is a rest request.

```php
if(pinkcrab_is_rest()){
// Do something
}
```

## URL Parameters

### ?show_enqueue
Expand Down
29 changes: 29 additions & 0 deletions views/ajax-error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
$stack_trace = explode( "\n", $error['message'] );
// Extract main error message
$subheader = array_shift( $stack_trace );
// Remove stack trace title from remainder.
$stack_trace = array_filter(
$stack_trace,
function( $message ) {
return $message !== 'Stack trace:';
}
);
?>

************************************************************ <?php echo "\n"; ?>
Response: <?php echo esc_html( $message['response'] ); ?> <?php echo "\n"; ?>
************************************************************ <?php echo "\n"; ?>
<?php echo esc_html( $subheader ); ?> <?php echo "\n"; ?>
************************************************************ <?php echo "\n"; ?>
Stack Trace: <?php echo "\n"; ?>
<?php foreach ( $stack_trace as $stack_trace_entry ) : ?>
--<?php echo esc_html( $stack_trace_entry ); ?> <?php echo "\n"; ?>
<?php endforeach; ?>
************************************************************<?php echo "\n"; ?>
File: <?php echo "{$error['file']} on line {$error['line']}"; ?><?php echo "\n"; ?>
************************************************************<?php echo "\n"; ?>
Full Backtrace:<?php echo "\n"; ?>
<?php adump( debug_backtrace() ); ?>
************************************************************<?php echo "\n"; ?>