wp/wp-includes/class-wp-error.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    13  * core WordPress functions pass this class in the event of an error and
    13  * core WordPress functions pass this class in the event of an error and
    14  * if not handled properly will result in code errors.
    14  * if not handled properly will result in code errors.
    15  *
    15  *
    16  * @since 2.1.0
    16  * @since 2.1.0
    17  */
    17  */
       
    18 #[AllowDynamicProperties]
    18 class WP_Error {
    19 class WP_Error {
    19 	/**
    20 	/**
    20 	 * Stores the list of errors.
    21 	 * Stores the list of errors.
    21 	 *
    22 	 *
    22 	 * @since 2.1.0
    23 	 * @since 2.1.0
    53 	 *
    54 	 *
    54 	 * @since 2.1.0
    55 	 * @since 2.1.0
    55 	 *
    56 	 *
    56 	 * @param string|int $code    Error code.
    57 	 * @param string|int $code    Error code.
    57 	 * @param string     $message Error message.
    58 	 * @param string     $message Error message.
    58 	 * @param mixed      $data    Optional. Error data.
    59 	 * @param mixed      $data    Optional. Error data. Default empty string.
    59 	 */
    60 	 */
    60 	public function __construct( $code = '', $message = '', $data = '' ) {
    61 	public function __construct( $code = '', $message = '', $data = '' ) {
    61 		if ( empty( $code ) ) {
    62 		if ( empty( $code ) ) {
    62 			return;
    63 			return;
    63 		}
    64 		}
   100 	/**
   101 	/**
   101 	 * Retrieves all error messages, or the error messages for the given error code.
   102 	 * Retrieves all error messages, or the error messages for the given error code.
   102 	 *
   103 	 *
   103 	 * @since 2.1.0
   104 	 * @since 2.1.0
   104 	 *
   105 	 *
   105 	 * @param string|int $code Optional. Retrieve messages matching code, if exists.
   106 	 * @param string|int $code Optional. Error code to retrieve the messages for.
       
   107 	 *                         Default empty string.
   106 	 * @return string[] Error strings on success, or empty array if there are none.
   108 	 * @return string[] Error strings on success, or empty array if there are none.
   107 	 */
   109 	 */
   108 	public function get_error_messages( $code = '' ) {
   110 	public function get_error_messages( $code = '' ) {
   109 		// Return all messages if no code specified.
   111 		// Return all messages if no code specified.
   110 		if ( empty( $code ) ) {
   112 		if ( empty( $code ) ) {
   129 	 * This will get the first message available for the code. If no code is
   131 	 * This will get the first message available for the code. If no code is
   130 	 * given then the first code available will be used.
   132 	 * given then the first code available will be used.
   131 	 *
   133 	 *
   132 	 * @since 2.1.0
   134 	 * @since 2.1.0
   133 	 *
   135 	 *
   134 	 * @param string|int $code Optional. Error code to retrieve message.
   136 	 * @param string|int $code Optional. Error code to retrieve the message for.
       
   137 	 *                         Default empty string.
   135 	 * @return string The error message.
   138 	 * @return string The error message.
   136 	 */
   139 	 */
   137 	public function get_error_message( $code = '' ) {
   140 	public function get_error_message( $code = '' ) {
   138 		if ( empty( $code ) ) {
   141 		if ( empty( $code ) ) {
   139 			$code = $this->get_error_code();
   142 			$code = $this->get_error_code();
   148 	/**
   151 	/**
   149 	 * Retrieves the most recently added error data for an error code.
   152 	 * Retrieves the most recently added error data for an error code.
   150 	 *
   153 	 *
   151 	 * @since 2.1.0
   154 	 * @since 2.1.0
   152 	 *
   155 	 *
   153 	 * @param string|int $code Optional. Error code.
   156 	 * @param string|int $code Optional. Error code. Default empty string.
   154 	 * @return mixed Error data, if it exists.
   157 	 * @return mixed Error data, if it exists.
   155 	 */
   158 	 */
   156 	public function get_error_data( $code = '' ) {
   159 	public function get_error_data( $code = '' ) {
   157 		if ( empty( $code ) ) {
   160 		if ( empty( $code ) ) {
   158 			$code = $this->get_error_code();
   161 			$code = $this->get_error_code();
   182 	 *
   185 	 *
   183 	 * @since 2.1.0
   186 	 * @since 2.1.0
   184 	 *
   187 	 *
   185 	 * @param string|int $code    Error code.
   188 	 * @param string|int $code    Error code.
   186 	 * @param string     $message Error message.
   189 	 * @param string     $message Error message.
   187 	 * @param mixed      $data    Optional. Error data.
   190 	 * @param mixed      $data    Optional. Error data. Default empty string.
   188 	 */
   191 	 */
   189 	public function add( $code, $message, $data = '' ) {
   192 	public function add( $code, $message, $data = '' ) {
   190 		$this->errors[ $code ][] = $message;
   193 		$this->errors[ $code ][] = $message;
   191 
   194 
   192 		if ( ! empty( $data ) ) {
   195 		if ( ! empty( $data ) ) {