From 964d0784befec3f3e01bf9844a39f2b1362e995f Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sun, 20 Sep 2020 17:44:07 +0000 Subject: [PATCH] General: Introduce the `wp_error_added` and `wp_error_checked` actions. These actions allow debugging tools to track `WP_Error` instances as they're created and subsequently passed between functions which check for error objects. Props Shelob9, Mte90, TimothyBlynJacobs, johnbillion Fixes #40568 Built from https://develop.svn.wordpress.org/trunk@49022 git-svn-id: http://core.svn.wordpress.org/trunk@48784 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-error.php | 23 ++++++++++++++++------- wp-includes/load.php | 23 ++++++++++++++++++----- wp-includes/version.php | 2 +- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/wp-includes/class-wp-error.php b/wp-includes/class-wp-error.php index 225460b472..e22357ac28 100644 --- a/wp-includes/class-wp-error.php +++ b/wp-includes/class-wp-error.php @@ -56,11 +56,7 @@ class WP_Error { return; } - $this->errors[ $code ][] = $message; - - if ( ! empty( $data ) ) { - $this->error_data[ $code ] = $data; - } + $this->add( $code, $message, $data ); } /** @@ -176,7 +172,7 @@ class WP_Error { } /** - * Add an error or append additional message to an existing error. + * Adds an error or appends an additional message to an existing error. * * @since 2.1.0 * @@ -186,9 +182,22 @@ class WP_Error { */ public function add( $code, $message, $data = '' ) { $this->errors[ $code ][] = $message; + if ( ! empty( $data ) ) { - $this->error_data[ $code ] = $data; + $this->add_data( $data, $code ); } + + /** + * Fires when an error is added to a WP_Error object. + * + * @since 5.6.0 + * + * @param string|int $code Error code. + * @param string $message Error message. + * @param mixed $data Error data. Might be empty. + * @param WP_Error $wp_error The WP_Error object. + */ + do_action( 'wp_error_added', $code, $message, $data, $this ); } /** diff --git a/wp-includes/load.php b/wp-includes/load.php index 0cec068fd7..7a79fc1a63 100644 --- a/wp-includes/load.php +++ b/wp-includes/load.php @@ -1446,17 +1446,30 @@ function wp_doing_cron() { } /** - * Check whether variable is a WordPress Error. + * Checks whether the given variable is a WordPress Error. * - * Returns true if $thing is an object of the WP_Error class. + * Returns whether `$thing` is an instance of the `WP_Error` class. * * @since 2.1.0 * - * @param mixed $thing Check if unknown variable is a WP_Error object. - * @return bool True, if WP_Error. False, if not WP_Error. + * @param mixed $thing The variable to check. + * @return bool Whether the variable is an instance of WP_Error. */ function is_wp_error( $thing ) { - return ( $thing instanceof WP_Error ); + $is = ( $thing instanceof WP_Error ); + + if ( $is ) { + /** + * Fires when `is_wp_error()` is called and it's an instance of `WP_Error`. + * + * @since 5.6.0 + * + * @param WP_Error $thing The error object passed to `is_wp_error()`. + */ + do_action( 'wp_error_checked', $thing ); + } + + return $is; } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index bf5573034b..f3e72b103e 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.6-alpha-49021'; +$wp_version = '5.6-alpha-49022'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.