wp/wp-includes/class-wp-error.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
    22 	/**
    22 	/**
    23 	 * Stores the list of errors.
    23 	 * Stores the list of errors.
    24 	 *
    24 	 *
    25 	 * @since 2.1.0
    25 	 * @since 2.1.0
    26 	 * @var array
    26 	 * @var array
    27 	 * @access private
    27 	 */
    28 	 */
    28 	public $errors = array();
    29 	var $errors = array();
       
    30 
    29 
    31 	/**
    30 	/**
    32 	 * Stores the list of data for error codes.
    31 	 * Stores the list of data for error codes.
    33 	 *
    32 	 *
    34 	 * @since 2.1.0
    33 	 * @since 2.1.0
    35 	 * @var array
    34 	 * @var array
    36 	 * @access private
    35 	 */
    37 	 */
    36 	public $error_data = array();
    38 	var $error_data = array();
    37 
    39 
    38 	/**
    40 	/**
    39 	 * Initialize the error.
    41 	 * Constructor - Sets up error message.
    40 	 *
    42 	 *
    41 	 * If `$code` is empty, the other parameters will be ignored.
    43 	 * If code parameter is empty then nothing will be done. It is possible to
    42 	 * When `$code` is not empty, `$message` will be used even if
    44 	 * add multiple messages to the same code, but with other methods in the
    43 	 * it is empty. The `$data` parameter will be used only if it
    45 	 * class.
    44 	 * is not empty.
    46 	 *
    45 	 *
    47 	 * All parameters are optional, but if the code parameter is set, then the
    46 	 * Though the class is constructed with a single error code and
    48 	 * data parameter is optional.
    47 	 * message, multiple codes can be added using the `add()` method.
    49 	 *
    48 	 *
    50 	 * @since 2.1.0
    49 	 * @since 2.1.0
    51 	 *
    50 	 *
    52 	 * @param string|int $code Error code
    51 	 * @param string|int $code Error code
    53 	 * @param string $message Error message
    52 	 * @param string $message Error message
    54 	 * @param mixed $data Optional. Error data.
    53 	 * @param mixed $data Optional. Error data.
    55 	 * @return WP_Error
    54 	 */
    56 	 */
    55 	public function __construct( $code = '', $message = '', $data = '' ) {
    57 	function __construct($code = '', $message = '', $data = '') {
       
    58 		if ( empty($code) )
    56 		if ( empty($code) )
    59 			return;
    57 			return;
    60 
    58 
    61 		$this->errors[$code][] = $message;
    59 		$this->errors[$code][] = $message;
    62 
    60 
    70 	 * @since 2.1.0
    68 	 * @since 2.1.0
    71 	 * @access public
    69 	 * @access public
    72 	 *
    70 	 *
    73 	 * @return array List of error codes, if available.
    71 	 * @return array List of error codes, if available.
    74 	 */
    72 	 */
    75 	function get_error_codes() {
    73 	public function get_error_codes() {
    76 		if ( empty($this->errors) )
    74 		if ( empty($this->errors) )
    77 			return array();
    75 			return array();
    78 
    76 
    79 		return array_keys($this->errors);
    77 		return array_keys($this->errors);
    80 	}
    78 	}
    85 	 * @since 2.1.0
    83 	 * @since 2.1.0
    86 	 * @access public
    84 	 * @access public
    87 	 *
    85 	 *
    88 	 * @return string|int Empty string, if no error codes.
    86 	 * @return string|int Empty string, if no error codes.
    89 	 */
    87 	 */
    90 	function get_error_code() {
    88 	public function get_error_code() {
    91 		$codes = $this->get_error_codes();
    89 		$codes = $this->get_error_codes();
    92 
    90 
    93 		if ( empty($codes) )
    91 		if ( empty($codes) )
    94 			return '';
    92 			return '';
    95 
    93 
   102 	 * @since 2.1.0
   100 	 * @since 2.1.0
   103 	 *
   101 	 *
   104 	 * @param string|int $code Optional. Retrieve messages matching code, if exists.
   102 	 * @param string|int $code Optional. Retrieve messages matching code, if exists.
   105 	 * @return array Error strings on success, or empty array on failure (if using code parameter).
   103 	 * @return array Error strings on success, or empty array on failure (if using code parameter).
   106 	 */
   104 	 */
   107 	function get_error_messages($code = '') {
   105 	public function get_error_messages($code = '') {
   108 		// Return all messages if no code specified.
   106 		// Return all messages if no code specified.
   109 		if ( empty($code) ) {
   107 		if ( empty($code) ) {
   110 			$all_messages = array();
   108 			$all_messages = array();
   111 			foreach ( (array) $this->errors as $code => $messages )
   109 			foreach ( (array) $this->errors as $code => $messages )
   112 				$all_messages = array_merge($all_messages, $messages);
   110 				$all_messages = array_merge($all_messages, $messages);
   129 	 * @since 2.1.0
   127 	 * @since 2.1.0
   130 	 *
   128 	 *
   131 	 * @param string|int $code Optional. Error code to retrieve message.
   129 	 * @param string|int $code Optional. Error code to retrieve message.
   132 	 * @return string
   130 	 * @return string
   133 	 */
   131 	 */
   134 	function get_error_message($code = '') {
   132 	public function get_error_message($code = '') {
   135 		if ( empty($code) )
   133 		if ( empty($code) )
   136 			$code = $this->get_error_code();
   134 			$code = $this->get_error_code();
   137 		$messages = $this->get_error_messages($code);
   135 		$messages = $this->get_error_messages($code);
   138 		if ( empty($messages) )
   136 		if ( empty($messages) )
   139 			return '';
   137 			return '';
   146 	 * @since 2.1.0
   144 	 * @since 2.1.0
   147 	 *
   145 	 *
   148 	 * @param string|int $code Optional. Error code.
   146 	 * @param string|int $code Optional. Error code.
   149 	 * @return mixed Null, if no errors.
   147 	 * @return mixed Null, if no errors.
   150 	 */
   148 	 */
   151 	function get_error_data($code = '') {
   149 	public function get_error_data($code = '') {
   152 		if ( empty($code) )
   150 		if ( empty($code) )
   153 			$code = $this->get_error_code();
   151 			$code = $this->get_error_code();
   154 
   152 
   155 		if ( isset($this->error_data[$code]) )
   153 		if ( isset($this->error_data[$code]) )
   156 			return $this->error_data[$code];
   154 			return $this->error_data[$code];
   157 		return null;
   155 		return null;
   158 	}
   156 	}
   159 
   157 
   160 	/**
   158 	/**
   161 	 * Append more error messages to list of error messages.
   159 	 * Add an error or append additional message to an existing error.
   162 	 *
   160 	 *
   163 	 * @since 2.1.0
   161 	 * @since 2.1.0
   164 	 * @access public
   162 	 * @access public
   165 	 *
   163 	 *
   166 	 * @param string|int $code Error code.
   164 	 * @param string|int $code Error code.
   167 	 * @param string $message Error message.
   165 	 * @param string $message Error message.
   168 	 * @param mixed $data Optional. Error data.
   166 	 * @param mixed $data Optional. Error data.
   169 	 */
   167 	 */
   170 	function add($code, $message, $data = '') {
   168 	public function add($code, $message, $data = '') {
   171 		$this->errors[$code][] = $message;
   169 		$this->errors[$code][] = $message;
   172 		if ( ! empty($data) )
   170 		if ( ! empty($data) )
   173 			$this->error_data[$code] = $data;
   171 			$this->error_data[$code] = $data;
   174 	}
   172 	}
   175 
   173 
   181 	 * @since 2.1.0
   179 	 * @since 2.1.0
   182 	 *
   180 	 *
   183 	 * @param mixed $data Error data.
   181 	 * @param mixed $data Error data.
   184 	 * @param string|int $code Error code.
   182 	 * @param string|int $code Error code.
   185 	 */
   183 	 */
   186 	function add_data($data, $code = '') {
   184 	public function add_data($data, $code = '') {
   187 		if ( empty($code) )
   185 		if ( empty($code) )
   188 			$code = $this->get_error_code();
   186 			$code = $this->get_error_code();
   189 
   187 
   190 		$this->error_data[$code] = $data;
   188 		$this->error_data[$code] = $data;
       
   189 	}
       
   190 
       
   191 	/**
       
   192 	 * Removes the specified error.
       
   193 	 *
       
   194 	 * This function removes all error messages associated with the specified
       
   195 	 * error code, along with any error data for that code.
       
   196 	 *
       
   197 	 * @since 4.1.0
       
   198 	 *
       
   199 	 * @param string|int $code Error code.
       
   200 	 */
       
   201 	public function remove( $code ) {
       
   202 		unset( $this->errors[ $code ] );
       
   203 		unset( $this->error_data[ $code ] );
   191 	}
   204 	}
   192 }
   205 }
   193 
   206 
   194 /**
   207 /**
   195  * Check whether variable is a WordPress Error.
   208  * Check whether variable is a WordPress Error.
   199  * @since 2.1.0
   212  * @since 2.1.0
   200  *
   213  *
   201  * @param mixed $thing Check if unknown variable is a WP_Error object.
   214  * @param mixed $thing Check if unknown variable is a WP_Error object.
   202  * @return bool True, if WP_Error. False, if not WP_Error.
   215  * @return bool True, if WP_Error. False, if not WP_Error.
   203  */
   216  */
   204 function is_wp_error($thing) {
   217 function is_wp_error( $thing ) {
   205 	if ( is_object($thing) && is_a($thing, 'WP_Error') )
   218 	return ( $thing instanceof WP_Error );
   206 		return true;
       
   207 	return false;
       
   208 }
   219 }