From 2f8e2fa05b85a33195a8ac054e8a8d0867b106ad Mon Sep 17 00:00:00 2001 From: Glynn Quelch Date: Thu, 19 Sep 2024 00:38:03 +0100 Subject: [PATCH] Custom errors when being accessed via rest or ajax --- debug-plugin.php | 54 ++++++++++++++++++++++++++++++++++++-------- readme.md | 10 ++++++++ views/ajax-error.php | 29 ++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 views/ajax-error.php diff --git a/debug-plugin.php b/debug-plugin.php index 897275c..35908fa 100644 --- a/debug-plugin.php +++ b/debug-plugin.php @@ -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 @@ -32,7 +51,7 @@ function ( $message, $error ) { * @return void */ function adump( ...$data ) { - if ( ! wp_doing_ajax() ) { + if ( ! wp_doing_ajax() && ! pinkcrab_is_rest() ) { echo '
';
 	}foreach ( $data as $item ) {
 		if ( is_null( $item ) ) {
@@ -45,7 +64,7 @@ function adump( ...$data ) {
 			print_r( $item );
 		}
 	}
-	if ( ! wp_doing_ajax() ) {
+	if ( ! wp_doing_ajax() && ! pinkcrab_is_rest() ) {
 		echo '
'; } } @@ -58,11 +77,11 @@ function adump( ...$data ) { * @return void */ function adie( ...$data ) { - if ( ! wp_doing_ajax() ) { + if ( ! wp_doing_ajax() && ! pinkcrab_is_rest() ) { echo '
';
 	}
 	adump( $data );
-	if ( ! wp_doing_ajax() ) {
+	if ( ! wp_doing_ajax() && ! pinkcrab_is_rest() ) {
 		echo '
'; } die(); @@ -70,6 +89,7 @@ function adie( ...$data ) { /** * Shows all the enqueued scripts and styles in header, if set in url. + * ?show_enqueued */ if ( ! empty( $_GET['show_enqueued'] ) ) { add_action( @@ -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 * @@ -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 ) ) { diff --git a/readme.md b/readme.md index 8f9e2f0..b9cc39f 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/views/ajax-error.php b/views/ajax-error.php new file mode 100644 index 0000000..a11f75f --- /dev/null +++ b/views/ajax-error.php @@ -0,0 +1,29 @@ + + +************************************************************ +Response: +************************************************************ + +************************************************************ +Stack Trace: + + -- + +************************************************************ +File: +************************************************************ +Full Backtrace: + +************************************************************ +