Skip to content

Commit

Permalink
Adds wp_die_ajax_handler().
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Lannoy committed Jan 16, 2022
1 parent 07806c7 commit 6174a14
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Improved layout for WordPress traces viewer.
- Better events messages for lost password form submission.
- Improved APCu storage for loggers.
- Reduction of the events' level for the `wp_die` ajax handler because it is not used consistently in the WordPress core (thanks to [Jan Thiel](https://github.com/JanThiel) for the suggestion).
- Updated DecaLog SDK from version 2.0.0 to version 2.0.2.
- Updated PerfOps One library from 2.2.1 to 2.2.2.
- Improved bubbles display when width is less than 500px (thanks to [Pat Ol](https://profiles.wordpress.org/pasglop/)).
Expand Down
38 changes: 35 additions & 3 deletions includes/listeners/class-corelistener.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ protected function launch() {
add_action( 'after_setup_theme', [ $this, 'after_setup_theme' ], PHP_INT_MAX );
add_action( 'switch_theme', [ $this, 'switch_theme' ], 10, 3 );
// Errors.
add_filter( 'wp_die_ajax_handler', [ $this, 'wp_die_handler' ], 10, 1 );
add_filter( 'wp_die_ajax_handler', [ $this, 'wp_die_ajax_handler' ], 10, 1 );
add_filter( 'wp_die_xmlrpc_handler', [ $this, 'wp_die_handler' ], 10, 1 );
add_filter( 'wp_die_handler', [ $this, 'wp_die_handler' ], 10, 1 );
add_filter( 'wp_die_json_handler', [ $this, 'wp_die_handler' ], 10, 1 );
Expand Down Expand Up @@ -1220,8 +1220,6 @@ public function wp_die_handler( $handler ) {
} elseif ( '' !== $msg ) {
if ( 0 === strpos( $msg, '[' ) || 0 === strpos( $msg, '{' ) ) {
$this->logger->debug( wp_kses( $msg, [] ), $code );
} elseif ( false !== strpos( $msg, '‎' ) ) { // hack to filter wp_ajax_sample_permalink hook.
$this->logger->debug( wp_kses( $msg, [] ), $code );
} else {
$this->logger->critical( wp_kses( $msg, [] ), $code );
}
Expand All @@ -1230,6 +1228,40 @@ public function wp_die_handler( $handler ) {
};
}

/**
* "wp_die_*" events.
*
* @since 1.0.0
*/
public function wp_die_ajax_handler( $handler ) {
if ( ! $handler || ! is_callable( $handler ) ) {
return $handler;
}
return function ( $message, $title = '', $args = [] ) use ( $handler ) {
$msg = '';
$code = 0;
if ( is_string( $title ) && '' !== $title ) {
$title .= ': ';
}
if ( is_numeric( $title ) ) {
$code = $title;
$title = '';
}
if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) {
$msg = $title . $message->get_error_message();
$code = $message->get_error_code();
} elseif ( is_string( $message ) ) {
$msg = $title . $message;
}
if ( is_numeric( $msg ) ) {
$this->logger->debug( 'Malformed wp_die call.', $code );
} elseif ( '' !== $msg ) {
$this->logger->debug( wp_kses( $msg, [] ), $code );
}
return $handler( $message, $title, $args );
};
}

/**
* "wp" event.
*
Expand Down

0 comments on commit 6174a14

Please sign in to comment.