PageRenderTime 36ms CodeModel.GetById 15ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/class-wp-error.php

https://github.com/davodey/WordPress
PHP | 254 lines | 77 code | 24 blank | 153 comment | 14 complexity | f95bbdd9e9b3ae942407a34dc75f7226 MD5 | raw file
  1<?php
  2/**
  3 * WordPress Error API.
  4 *
  5 * Contains the WP_Error class and the is_wp_error() function.
  6 *
  7 * @package WordPress
  8 */
  9
 10/**
 11 * WordPress Error class.
 12 *
 13 * Container for checking for WordPress errors and error messages. Return
 14 * WP_Error and use {@link is_wp_error()} to check if this class is returned.
 15 * Many core WordPress functions pass this class in the event of an error and
 16 * if not handled properly will result in code errors.
 17 *
 18 * @package WordPress
 19 * @since 2.1.0
 20 */
 21class WP_Error {
 22	/**
 23	 * Stores the list of errors.
 24	 *
 25	 * @since 2.1.0
 26	 * @var array
 27	 * @access private
 28	 */
 29	private $errors = array();
 30
 31	/**
 32	 * Stores the list of data for error codes.
 33	 *
 34	 * @since 2.1.0
 35	 * @var array
 36	 * @access private
 37	 */
 38	private $error_data = array();
 39
 40	/**
 41	 * Initialize the error.
 42	 *
 43	 * If `$code` is empty, the other parameters will be ignored.
 44	 * When `$code` is not empty, `$message` will be used even if
 45	 * it is empty. The `$data` parameter will be used only if it
 46	 * is not empty.
 47	 *
 48	 * Though the class is constructed with a single error code and
 49	 * message, multiple codes can be added using the `add()` method.
 50	 *
 51	 * @since 2.1.0
 52	 *
 53	 * @param string|int $code Error code
 54	 * @param string $message Error message
 55	 * @param mixed $data Optional. Error data.
 56	 * @return WP_Error
 57	 */
 58	public function __construct( $code = '', $message = '', $data = '' ) {
 59		if ( empty($code) )
 60			return;
 61
 62		$this->errors[$code][] = $message;
 63
 64		if ( ! empty($data) )
 65			$this->error_data[$code] = $data;
 66	}
 67
 68	/**
 69	 * Make private properties readable for backwards compatibility
 70	 *
 71	 * @since 4.0.0
 72	 * @param string $name
 73	 * @return mixed
 74	 */
 75	public function __get( $name ) {
 76		return $this->$name;
 77	}
 78
 79	/**
 80	 * Make private properties setable for backwards compatibility
 81	 *
 82	 * @since 4.0.0
 83	 * @param string $name
 84	 * @param string $value
 85	 * @return mixed
 86	 */
 87	public function __set( $name, $value ) {
 88		return $this->$name = $value;
 89	}
 90
 91	/**
 92	 * Make private properties checkable for backwards compatibility
 93	 *
 94	 * @since 4.0.0
 95	 * @param string $name
 96	 * @return mixed
 97	 */
 98	public function __isset( $name ) {
 99		return isset( $this->$name );
100	}
101
102	/**
103	 * Make private properties unsetable for backwards compatibility
104	 *
105	 * @since 4.0.0
106	 * @param string $name
107	 * @return mixed
108	 */
109	public function __unset( $name ) {
110		unset( $this->$name );
111	}
112
113	/**
114	 * Retrieve all error codes.
115	 *
116	 * @since 2.1.0
117	 * @access public
118	 *
119	 * @return array List of error codes, if available.
120	 */
121	public function get_error_codes() {
122		if ( empty($this->errors) )
123			return array();
124
125		return array_keys($this->errors);
126	}
127
128	/**
129	 * Retrieve first error code available.
130	 *
131	 * @since 2.1.0
132	 * @access public
133	 *
134	 * @return string|int Empty string, if no error codes.
135	 */
136	public function get_error_code() {
137		$codes = $this->get_error_codes();
138
139		if ( empty($codes) )
140			return '';
141
142		return $codes[0];
143	}
144
145	/**
146	 * Retrieve all error messages or error messages matching code.
147	 *
148	 * @since 2.1.0
149	 *
150	 * @param string|int $code Optional. Retrieve messages matching code, if exists.
151	 * @return array Error strings on success, or empty array on failure (if using code parameter).
152	 */
153	public function get_error_messages($code = '') {
154		// Return all messages if no code specified.
155		if ( empty($code) ) {
156			$all_messages = array();
157			foreach ( (array) $this->errors as $code => $messages )
158				$all_messages = array_merge($all_messages, $messages);
159
160			return $all_messages;
161		}
162
163		if ( isset($this->errors[$code]) )
164			return $this->errors[$code];
165		else
166			return array();
167	}
168
169	/**
170	 * Get single error message.
171	 *
172	 * This will get the first message available for the code. If no code is
173	 * given then the first code available will be used.
174	 *
175	 * @since 2.1.0
176	 *
177	 * @param string|int $code Optional. Error code to retrieve message.
178	 * @return string
179	 */
180	public function get_error_message($code = '') {
181		if ( empty($code) )
182			$code = $this->get_error_code();
183		$messages = $this->get_error_messages($code);
184		if ( empty($messages) )
185			return '';
186		return $messages[0];
187	}
188
189	/**
190	 * Retrieve error data for error code.
191	 *
192	 * @since 2.1.0
193	 *
194	 * @param string|int $code Optional. Error code.
195	 * @return mixed Null, if no errors.
196	 */
197	public function get_error_data($code = '') {
198		if ( empty($code) )
199			$code = $this->get_error_code();
200
201		if ( isset($this->error_data[$code]) )
202			return $this->error_data[$code];
203		return null;
204	}
205
206	/**
207	 * Add an error or append additional message to an existing error.
208	 *
209	 * @since 2.1.0
210	 * @access public
211	 *
212	 * @param string|int $code Error code.
213	 * @param string $message Error message.
214	 * @param mixed $data Optional. Error data.
215	 */
216	public function add($code, $message, $data = '') {
217		$this->errors[$code][] = $message;
218		if ( ! empty($data) )
219			$this->error_data[$code] = $data;
220	}
221
222	/**
223	 * Add data for error code.
224	 *
225	 * The error code can only contain one error data.
226	 *
227	 * @since 2.1.0
228	 *
229	 * @param mixed $data Error data.
230	 * @param string|int $code Error code.
231	 */
232	public function add_data($data, $code = '') {
233		if ( empty($code) )
234			$code = $this->get_error_code();
235
236		$this->error_data[$code] = $data;
237	}
238}
239
240/**
241 * Check whether variable is a WordPress Error.
242 *
243 * Returns true if $thing is an object of the WP_Error class.
244 *
245 * @since 2.1.0
246 *
247 * @param mixed $thing Check if unknown variable is a WP_Error object.
248 * @return bool True, if WP_Error. False, if not WP_Error.
249 */
250function is_wp_error($thing) {
251	if ( is_object($thing) && is_a($thing, 'WP_Error') )
252		return true;
253	return false;
254}