PageRenderTime 62ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 276 lines | 81 code | 25 blank | 170 comment | 14 complexity | 5acea5e6f1956698dade085507fa91fa MD5 | raw file
Possible License(s): GPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0
  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. * @access public
  67. *
  68. * @param string $name Property to get.
  69. * @return mixed Property.
  70. */
  71. public function __get( $name ) {
  72. return $this->$name;
  73. }
  74. /**
  75. * Make private properties settable for backwards compatibility.
  76. *
  77. * @since 4.0.0
  78. * @access public
  79. *
  80. * @param string $name Property to set.
  81. * @param mixed $value Property value.
  82. * @return mixed Newly-set property.
  83. */
  84. public function __set( $name, $value ) {
  85. return $this->$name = $value;
  86. }
  87. /**
  88. * Make private properties checkable for backwards compatibility.
  89. *
  90. * @since 4.0.0
  91. * @access public
  92. *
  93. * @param string $name Property to check if set.
  94. * @return bool Whether the property is set.
  95. */
  96. public function __isset( $name ) {
  97. return isset( $this->$name );
  98. }
  99. /**
  100. * Make private properties un-settable for backwards compatibility.
  101. *
  102. * @since 4.0.0
  103. * @access public
  104. *
  105. * @param string $name Property to unset.
  106. */
  107. public function __unset( $name ) {
  108. unset( $this->$name );
  109. }
  110. /**
  111. * Retrieve all error codes.
  112. *
  113. * @since 2.1.0
  114. * @access public
  115. *
  116. * @return array List of error codes, if available.
  117. */
  118. public function get_error_codes() {
  119. if ( empty($this->errors) )
  120. return array();
  121. return array_keys($this->errors);
  122. }
  123. /**
  124. * Retrieve first error code available.
  125. *
  126. * @since 2.1.0
  127. * @access public
  128. *
  129. * @return string|int Empty string, if no error codes.
  130. */
  131. public function get_error_code() {
  132. $codes = $this->get_error_codes();
  133. if ( empty($codes) )
  134. return '';
  135. return $codes[0];
  136. }
  137. /**
  138. * Retrieve all error messages or error messages matching code.
  139. *
  140. * @since 2.1.0
  141. *
  142. * @param string|int $code Optional. Retrieve messages matching code, if exists.
  143. * @return array Error strings on success, or empty array on failure (if using code parameter).
  144. */
  145. public function get_error_messages($code = '') {
  146. // Return all messages if no code specified.
  147. if ( empty($code) ) {
  148. $all_messages = array();
  149. foreach ( (array) $this->errors as $code => $messages )
  150. $all_messages = array_merge($all_messages, $messages);
  151. return $all_messages;
  152. }
  153. if ( isset($this->errors[$code]) )
  154. return $this->errors[$code];
  155. else
  156. return array();
  157. }
  158. /**
  159. * Get single error message.
  160. *
  161. * This will get the first message available for the code. If no code is
  162. * given then the first code available will be used.
  163. *
  164. * @since 2.1.0
  165. *
  166. * @param string|int $code Optional. Error code to retrieve message.
  167. * @return string
  168. */
  169. public function get_error_message($code = '') {
  170. if ( empty($code) )
  171. $code = $this->get_error_code();
  172. $messages = $this->get_error_messages($code);
  173. if ( empty($messages) )
  174. return '';
  175. return $messages[0];
  176. }
  177. /**
  178. * Retrieve error data for error code.
  179. *
  180. * @since 2.1.0
  181. *
  182. * @param string|int $code Optional. Error code.
  183. * @return mixed Null, if no errors.
  184. */
  185. public function get_error_data($code = '') {
  186. if ( empty($code) )
  187. $code = $this->get_error_code();
  188. if ( isset($this->error_data[$code]) )
  189. return $this->error_data[$code];
  190. return null;
  191. }
  192. /**
  193. * Add an error or append additional message to an existing error.
  194. *
  195. * @since 2.1.0
  196. * @access public
  197. *
  198. * @param string|int $code Error code.
  199. * @param string $message Error message.
  200. * @param mixed $data Optional. Error data.
  201. */
  202. public function add($code, $message, $data = '') {
  203. $this->errors[$code][] = $message;
  204. if ( ! empty($data) )
  205. $this->error_data[$code] = $data;
  206. }
  207. /**
  208. * Add data for error code.
  209. *
  210. * The error code can only contain one error data.
  211. *
  212. * @since 2.1.0
  213. *
  214. * @param mixed $data Error data.
  215. * @param string|int $code Error code.
  216. */
  217. public function add_data($data, $code = '') {
  218. if ( empty($code) )
  219. $code = $this->get_error_code();
  220. $this->error_data[$code] = $data;
  221. }
  222. /**
  223. * Removes the specified error.
  224. *
  225. * This function removes all error messages associated with the specified
  226. * error code, along with any error data for that code.
  227. *
  228. * @since 4.1.0
  229. *
  230. * @param string|int $code Error code.
  231. */
  232. public function remove( $code ) {
  233. unset( $this->errors[ $code ] );
  234. unset( $this->error_data[ $code ] );
  235. }
  236. }
  237. /**
  238. * Check whether variable is a WordPress Error.
  239. *
  240. * Returns true if $thing is an object of the WP_Error class.
  241. *
  242. * @since 2.1.0
  243. *
  244. * @param mixed $thing Check if unknown variable is a WP_Error object.
  245. * @return bool True, if WP_Error. False, if not WP_Error.
  246. */
  247. function is_wp_error($thing) {
  248. if ( is_object($thing) && is_a($thing, 'WP_Error') )
  249. return true;
  250. return false;
  251. }