WordPress.org

Make WordPress Core

Changeset 49022


Ignore:
Timestamp:
09/20/2020 05:43:00 PM (4 months ago)
Author:
johnbillion
Message:

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

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-error.php

    r48586 r49022  
    5757        }
    5858
    59         $this->errors[ $code ][] = $message;
    60 
    61         if ( ! empty( $data ) ) {
    62             $this->error_data[ $code ] = $data;
    63         }
     59        $this->add( $code, $message, $data );
    6460    }
    6561
     
    177173
    178174    /**
    179      * Add an error or append additional message to an existing error.
     175     * Adds an error or appends an additional message to an existing error.
    180176     *
    181177     * @since 2.1.0
     
    187183    public function add( $code, $message, $data = '' ) {
    188184        $this->errors[ $code ][] = $message;
     185
    189186        if ( ! empty( $data ) ) {
    190             $this->error_data[ $code ] = $data;
    191         }
     187            $this->add_data( $data, $code );
     188        }
     189
     190        /**
     191         * Fires when an error is added to a WP_Error object.
     192         *
     193         * @since 5.6.0
     194         *
     195         * @param string|int $code     Error code.
     196         * @param string     $message  Error message.
     197         * @param mixed      $data     Error data. Might be empty.
     198         * @param WP_Error   $wp_error The WP_Error object.
     199         */
     200        do_action( 'wp_error_added', $code, $message, $data, $this );
    192201    }
    193202
  • trunk/src/wp-includes/load.php

    r48941 r49022  
    14471447
    14481448/**
    1449  * Check whether variable is a WordPress Error.
    1450  *
    1451  * Returns true if $thing is an object of the WP_Error class.
     1449 * Checks whether the given variable is a WordPress Error.
     1450 *
     1451 * Returns whether `$thing` is an instance of the `WP_Error` class.
    14521452 *
    14531453 * @since 2.1.0
    14541454 *
    1455  * @param mixed $thing Check if unknown variable is a WP_Error object.
    1456  * @return bool True, if WP_Error. False, if not WP_Error.
     1455 * @param mixed $thing The variable to check.
     1456 * @return bool Whether the variable is an instance of WP_Error.
    14571457 */
    14581458function is_wp_error( $thing ) {
    1459     return ( $thing instanceof WP_Error );
     1459    $is = ( $thing instanceof WP_Error );
     1460
     1461    if ( $is ) {
     1462        /**
     1463         * Fires when `is_wp_error()` is called and it's an instance of `WP_Error`.
     1464         *
     1465         * @since 5.6.0
     1466         *
     1467         * @param WP_Error $thing The error object passed to `is_wp_error()`.
     1468         */
     1469        do_action( 'wp_error_checked', $thing );
     1470    }
     1471
     1472    return $is;
    14601473}
    14611474
Note: See TracChangeset for help on using the changeset viewer.