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