wp/wp-includes/class-wp-error.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
--- a/wp/wp-includes/class-wp-error.php	Tue Dec 15 15:52:01 2020 +0100
+++ b/wp/wp-includes/class-wp-error.php	Wed Sep 21 18:19:35 2022 +0200
@@ -2,8 +2,6 @@
 /**
  * WordPress Error API.
  *
- * Contains the WP_Error class and the is_wp_error() function.
- *
  * @package WordPress
  */
 
@@ -27,7 +25,7 @@
 	public $errors = array();
 
 	/**
-	 * Stores the list of data for error codes.
+	 * Stores the most recently added data for each error code.
 	 *
 	 * @since 2.1.0
 	 * @var array
@@ -35,7 +33,15 @@
 	public $error_data = array();
 
 	/**
-	 * Initialize the error.
+	 * Stores previously added data added for error codes, oldest-to-newest by code.
+	 *
+	 * @since 5.6.0
+	 * @var array[]
+	 */
+	protected $additional_data = array();
+
+	/**
+	 * Initializes the error.
 	 *
 	 * If `$code` is empty, the other parameters will be ignored.
 	 * When `$code` is not empty, `$message` will be used even if
@@ -56,15 +62,11 @@
 			return;
 		}
 
-		$this->errors[ $code ][] = $message;
-
-		if ( ! empty( $data ) ) {
-			$this->error_data[ $code ] = $data;
-		}
+		$this->add( $code, $message, $data );
 	}
 
 	/**
-	 * Retrieve all error codes.
+	 * Retrieves all error codes.
 	 *
 	 * @since 2.1.0
 	 *
@@ -79,7 +81,7 @@
 	}
 
 	/**
-	 * Retrieve first error code available.
+	 * Retrieves the first error code available.
 	 *
 	 * @since 2.1.0
 	 *
@@ -96,12 +98,12 @@
 	}
 
 	/**
-	 * Retrieve all error messages or error messages matching code.
+	 * Retrieves all error messages, or the error messages for the given error code.
 	 *
 	 * @since 2.1.0
 	 *
 	 * @param string|int $code Optional. Retrieve messages matching code, if exists.
-	 * @return array Error strings on success, or empty array on failure (if using code parameter).
+	 * @return array Error strings on success, or empty array if there are none.
 	 */
 	public function get_error_messages( $code = '' ) {
 		// Return all messages if no code specified.
@@ -122,7 +124,7 @@
 	}
 
 	/**
-	 * Get single error message.
+	 * Gets a single error message.
 	 *
 	 * This will get the first message available for the code. If no code is
 	 * given then the first code available will be used.
@@ -130,7 +132,7 @@
 	 * @since 2.1.0
 	 *
 	 * @param string|int $code Optional. Error code to retrieve message.
-	 * @return string
+	 * @return string The error message.
 	 */
 	public function get_error_message( $code = '' ) {
 		if ( empty( $code ) ) {
@@ -144,7 +146,7 @@
 	}
 
 	/**
-	 * Retrieve error data for error code.
+	 * Retrieves the most recently added error data for an error code.
 	 *
 	 * @since 2.1.0
 	 *
@@ -162,11 +164,11 @@
 	}
 
 	/**
-	 * Verify if the instance contains errors.
+	 * Verifies if the instance contains errors.
 	 *
 	 * @since 5.1.0
 	 *
-	 * @return bool
+	 * @return bool If the instance contains errors.
 	 */
 	public function has_errors() {
 		if ( ! empty( $this->errors ) ) {
@@ -176,7 +178,7 @@
 	}
 
 	/**
-	 * 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,17 +188,29 @@
 	 */
 	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 );
 	}
 
 	/**
-	 * Add data for error code.
-	 *
-	 * The error code can only contain one error data.
+	 * Adds data to an error with the given code.
 	 *
 	 * @since 2.1.0
+	 * @since 5.6.0 Errors can now contain more than one item of error data. {@see WP_Error::$additional_data}.
 	 *
 	 * @param mixed      $data Error data.
 	 * @param string|int $code Error code.
@@ -206,10 +220,40 @@
 			$code = $this->get_error_code();
 		}
 
+		if ( isset( $this->error_data[ $code ] ) ) {
+			$this->additional_data[ $code ][] = $this->error_data[ $code ];
+		}
+
 		$this->error_data[ $code ] = $data;
 	}
 
 	/**
+	 * Retrieves all error data for an error code in the order in which the data was added.
+	 *
+	 * @since 5.6.0
+	 *
+	 * @param string|int $code Error code.
+	 * @return mixed[] Array of error data, if it exists.
+	 */
+	public function get_all_error_data( $code = '' ) {
+		if ( empty( $code ) ) {
+			$code = $this->get_error_code();
+		}
+
+		$data = array();
+
+		if ( isset( $this->additional_data[ $code ] ) ) {
+			$data = $this->additional_data[ $code ];
+		}
+
+		if ( isset( $this->error_data[ $code ] ) ) {
+			$data[] = $this->error_data[ $code ];
+		}
+
+		return $data;
+	}
+
+	/**
 	 * Removes the specified error.
 	 *
 	 * This function removes all error messages associated with the specified
@@ -222,5 +266,48 @@
 	public function remove( $code ) {
 		unset( $this->errors[ $code ] );
 		unset( $this->error_data[ $code ] );
+		unset( $this->additional_data[ $code ] );
+	}
+
+	/**
+	 * Merges the errors in the given error object into this one.
+	 *
+	 * @since 5.6.0
+	 *
+	 * @param WP_Error $error Error object to merge.
+	 */
+	public function merge_from( WP_Error $error ) {
+		static::copy_errors( $error, $this );
+	}
+
+	/**
+	 * Exports the errors in this object into the given one.
+	 *
+	 * @since 5.6.0
+	 *
+	 * @param WP_Error $error Error object to export into.
+	 */
+	public function export_to( WP_Error $error ) {
+		static::copy_errors( $this, $error );
+	}
+
+	/**
+	 * Copies errors from one WP_Error instance to another.
+	 *
+	 * @since 5.6.0
+	 *
+	 * @param WP_Error $from The WP_Error to copy from.
+	 * @param WP_Error $to   The WP_Error to copy to.
+	 */
+	protected static function copy_errors( WP_Error $from, WP_Error $to ) {
+		foreach ( $from->get_error_codes() as $code ) {
+			foreach ( $from->get_error_messages( $code ) as $error_message ) {
+				$to->add( $code, $error_message );
+			}
+
+			foreach ( $from->get_all_error_data( $code ) as $data ) {
+				$to->add_data( $data, $code );
+			}
+		}
 	}
 }