PageRenderTime 43ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/webgefrickel/frckl-init-wordpress
PHP | 208 lines | 65 code | 20 blank | 123 comment | 14 complexity | be817852a86b56e6da96f54db1bf4d75 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-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. var $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. var $error_data = array();
  37. /**
  38. * Constructor - Sets up error message.
  39. *
  40. * If code parameter is empty then nothing will be done. It is possible to
  41. * add multiple messages to the same code, but with other methods in the
  42. * class.
  43. *
  44. * All parameters are optional, but if the code parameter is set, then the
  45. * data parameter is optional.
  46. *
  47. * @since 2.1.0
  48. *
  49. * @param string|int $code Error code
  50. * @param string $message Error message
  51. * @param mixed $data Optional. Error data.
  52. * @return WP_Error
  53. */
  54. function __construct($code = '', $message = '', $data = '') {
  55. if ( empty($code) )
  56. return;
  57. $this->errors[$code][] = $message;
  58. if ( ! empty($data) )
  59. $this->error_data[$code] = $data;
  60. }
  61. /**
  62. * Retrieve all error codes.
  63. *
  64. * @since 2.1.0
  65. * @access public
  66. *
  67. * @return array List of error codes, if available.
  68. */
  69. function get_error_codes() {
  70. if ( empty($this->errors) )
  71. return array();
  72. return array_keys($this->errors);
  73. }
  74. /**
  75. * Retrieve first error code available.
  76. *
  77. * @since 2.1.0
  78. * @access public
  79. *
  80. * @return string|int Empty string, if no error codes.
  81. */
  82. function get_error_code() {
  83. $codes = $this->get_error_codes();
  84. if ( empty($codes) )
  85. return '';
  86. return $codes[0];
  87. }
  88. /**
  89. * Retrieve all error messages or error messages matching code.
  90. *
  91. * @since 2.1.0
  92. *
  93. * @param string|int $code Optional. Retrieve messages matching code, if exists.
  94. * @return array Error strings on success, or empty array on failure (if using code parameter).
  95. */
  96. function get_error_messages($code = '') {
  97. // Return all messages if no code specified.
  98. if ( empty($code) ) {
  99. $all_messages = array();
  100. foreach ( (array) $this->errors as $code => $messages )
  101. $all_messages = array_merge($all_messages, $messages);
  102. return $all_messages;
  103. }
  104. if ( isset($this->errors[$code]) )
  105. return $this->errors[$code];
  106. else
  107. return array();
  108. }
  109. /**
  110. * Get single error message.
  111. *
  112. * This will get the first message available for the code. If no code is
  113. * given then the first code available will be used.
  114. *
  115. * @since 2.1.0
  116. *
  117. * @param string|int $code Optional. Error code to retrieve message.
  118. * @return string
  119. */
  120. function get_error_message($code = '') {
  121. if ( empty($code) )
  122. $code = $this->get_error_code();
  123. $messages = $this->get_error_messages($code);
  124. if ( empty($messages) )
  125. return '';
  126. return $messages[0];
  127. }
  128. /**
  129. * Retrieve error data for error code.
  130. *
  131. * @since 2.1.0
  132. *
  133. * @param string|int $code Optional. Error code.
  134. * @return mixed Null, if no errors.
  135. */
  136. function get_error_data($code = '') {
  137. if ( empty($code) )
  138. $code = $this->get_error_code();
  139. if ( isset($this->error_data[$code]) )
  140. return $this->error_data[$code];
  141. return null;
  142. }
  143. /**
  144. * Append more error messages to list of error messages.
  145. *
  146. * @since 2.1.0
  147. * @access public
  148. *
  149. * @param string|int $code Error code.
  150. * @param string $message Error message.
  151. * @param mixed $data Optional. Error data.
  152. */
  153. function add($code, $message, $data = '') {
  154. $this->errors[$code][] = $message;
  155. if ( ! empty($data) )
  156. $this->error_data[$code] = $data;
  157. }
  158. /**
  159. * Add data for error code.
  160. *
  161. * The error code can only contain one error data.
  162. *
  163. * @since 2.1.0
  164. *
  165. * @param mixed $data Error data.
  166. * @param string|int $code Error code.
  167. */
  168. function add_data($data, $code = '') {
  169. if ( empty($code) )
  170. $code = $this->get_error_code();
  171. $this->error_data[$code] = $data;
  172. }
  173. }
  174. /**
  175. * Check whether variable is a WordPress Error.
  176. *
  177. * Returns true if $thing is an object of the WP_Error class.
  178. *
  179. * @since 2.1.0
  180. *
  181. * @param mixed $thing Check if unknown variable is a WP_Error object.
  182. * @return bool True, if WP_Error. False, if not WP_Error.
  183. */
  184. function is_wp_error($thing) {
  185. if ( is_object($thing) && is_a($thing, 'WP_Error') )
  186. return true;
  187. return false;
  188. }