/bp-forums/bbpress/bb-includes/backpress/class.wp-error.php

  1. <?php 
  2. // Last sync [WP11537] - split out from wp-includes/classes.php 
  3.  
  4. /** 
  5. * WordPress Error class. 
  6. * 
  7. * Container for checking for WordPress errors and error messages. Return 
  8. * WP_Error and use {@link is_wp_error()} to check if this class is returned. 
  9. * Many core WordPress functions pass this class in the event of an error and 
  10. * if not handled properly will result in code errors. 
  11. * 
  12. * @package WordPress 
  13. * @since 2.1.0 
  14. */ 
  15. class WP_Error { 
  16. /** 
  17. * Stores the list of errors. 
  18. * 
  19. * @since 2.1.0 
  20. * @var array 
  21. * @access private 
  22. */ 
  23. var $errors = array(); 
  24.  
  25. /** 
  26. * Stores the list of data for error codes. 
  27. * 
  28. * @since 2.1.0 
  29. * @var array 
  30. * @access private 
  31. */ 
  32. var $error_data = array(); 
  33.  
  34. /** 
  35. * PHP4 Constructor - Sets up error message. 
  36. * 
  37. * If code parameter is empty then nothing will be done. It is possible to 
  38. * add multiple messages to the same code, but with other methods in the 
  39. * class. 
  40. * 
  41. * All parameters are optional, but if the code parameter is set, then the 
  42. * data parameter is optional. 
  43. * 
  44. * @since 2.1.0 
  45. * 
  46. * @param string|int $code Error code 
  47. * @param string $message Error message 
  48. * @param mixed $data Optional. Error data. 
  49. * @return WP_Error 
  50. */ 
  51. function __construct($code = '', $message = '', $data = '') { 
  52. if ( empty($code) ) 
  53. return; 
  54.  
  55. $this->errors[$code][] = $message; 
  56.  
  57. if ( ! empty($data) ) 
  58. $this->error_data[$code] = $data; 
  59.  
  60. function WP_Error($code = '', $message = '', $data = '') { 
  61. $this->__construct($code, $message, $data); 
  62.  
  63. /** 
  64. * Retrieve all error codes. 
  65. * 
  66. * @since 2.1.0 
  67. * @access public 
  68. * 
  69. * @return array List of error codes, if avaiable. 
  70. */ 
  71. function get_error_codes() { 
  72. if ( empty($this->errors) ) 
  73. return array(); 
  74.  
  75. return array_keys($this->errors); 
  76.  
  77. /** 
  78. * Retrieve first error code available. 
  79. * 
  80. * @since 2.1.0 
  81. * @access public 
  82. * 
  83. * @return string|int Empty string, if no error codes. 
  84. */ 
  85. function get_error_code() { 
  86. $codes = $this->get_error_codes(); 
  87.  
  88. if ( empty($codes) ) 
  89. return ''; 
  90.  
  91. return $codes[0]; 
  92.  
  93. /** 
  94. * Retrieve all error messages or error messages matching code. 
  95. * 
  96. * @since 2.1.0 
  97. * 
  98. * @param string|int $code Optional. Retrieve messages matching code, if exists. 
  99. * @return array Error strings on success, or empty array on failure (if using codee parameter). 
  100. */ 
  101. function get_error_messages($code = '') { 
  102. // Return all messages if no code specified. 
  103. if ( empty($code) ) { 
  104. $all_messages = array(); 
  105. foreach ( (array) $this->errors as $code => $messages ) 
  106. $all_messages = array_merge($all_messages, $messages); 
  107.  
  108. return $all_messages; 
  109.  
  110. if ( isset($this->errors[$code]) ) 
  111. return $this->errors[$code]; 
  112. else 
  113. return array(); 
  114.  
  115. /** 
  116. * Get single error message. 
  117. * 
  118. * This will get the first message available for the code. If no code is 
  119. * given then the first code available will be used. 
  120. * 
  121. * @since 2.1.0 
  122. * 
  123. * @param string|int $code Optional. Error code to retrieve message. 
  124. * @return string 
  125. */ 
  126. function get_error_message($code = '') { 
  127. if ( empty($code) ) 
  128. $code = $this->get_error_code(); 
  129. $messages = $this->get_error_messages($code); 
  130. if ( empty($messages) ) 
  131. return ''; 
  132. return $messages[0]; 
  133.  
  134. /** 
  135. * Retrieve error data for error code. 
  136. * 
  137. * @since 2.1.0 
  138. * 
  139. * @param string|int $code Optional. Error code. 
  140. * @return mixed Null, if no errors. 
  141. */ 
  142. function get_error_data($code = '') { 
  143. if ( empty($code) ) 
  144. $code = $this->get_error_code(); 
  145.  
  146. if ( isset($this->error_data[$code]) ) 
  147. return $this->error_data[$code]; 
  148. return null; 
  149.  
  150. /** 
  151. * Append more error messages to list of error messages. 
  152. * 
  153. * @since 2.1.0 
  154. * @access public 
  155. * 
  156. * @param string|int $code Error code. 
  157. * @param string $message Error message. 
  158. * @param mixed $data Optional. Error data. 
  159. */ 
  160. function add($code, $message, $data = '') { 
  161. $this->errors[$code][] = $message; 
  162. if ( ! empty($data) ) 
  163. $this->error_data[$code] = $data; 
  164.  
  165. /** 
  166. * Add data for error code. 
  167. * 
  168. * The error code can only contain one error data. 
  169. * 
  170. * @since 2.1.0 
  171. * 
  172. * @param mixed $data Error data. 
  173. * @param string|int $code Error code. 
  174. */ 
  175. function add_data($data, $code = '') { 
  176. if ( empty($code) ) 
  177. $code = $this->get_error_code(); 
  178.  
  179. $this->error_data[$code] = $data; 
  180.  
  181. /** 
  182. * Check whether variable is a WordPress Error. 
  183. * 
  184. * Looks at the object and if a WP_Error class. Does not check to see if the 
  185. * parent is also WP_Error, so can't inherit WP_Error and still use this 
  186. * function. 
  187. * 
  188. * @since 2.1.0 
  189. * 
  190. * @param mixed $thing Check if unknown variable is WordPress Error object. 
  191. * @return bool True, if WP_Error. False, if not WP_Error. 
  192. */ 
  193. function is_wp_error($thing) { 
  194. if ( is_object($thing) && is_a($thing, 'WP_Error') ) 
  195. return true; 
  196. return false; 
.