/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 WP_Error($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. /** 
  61. * Retrieve all error codes. 
  62. * 
  63. * @since 2.1.0 
  64. * @access public 
  65. * 
  66. * @return array List of error codes, if avaiable. 
  67. */ 
  68. function get_error_codes() { 
  69. if ( empty($this->errors) ) 
  70. return array(); 
  71.  
  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.  
  85. if ( empty($codes) ) 
  86. return ''; 
  87.  
  88. return $codes[0]; 
  89.  
  90. /** 
  91. * Retrieve all error messages or error messages matching code. 
  92. * 
  93. * @since 2.1.0 
  94. * 
  95. * @param string|int $code Optional. Retrieve messages matching code, if exists. 
  96. * @return array Error strings on success, or empty array on failure (if using codee parameter). 
  97. */ 
  98. function get_error_messages($code = '') { 
  99. // Return all messages if no code specified. 
  100. if ( empty($code) ) { 
  101. $all_messages = array(); 
  102. foreach ( (array) $this->errors as $code => $messages ) 
  103. $all_messages = array_merge($all_messages, $messages); 
  104.  
  105. return $all_messages; 
  106.  
  107. if ( isset($this->errors[$code]) ) 
  108. return $this->errors[$code]; 
  109. else 
  110. return array(); 
  111.  
  112. /** 
  113. * Get single error message. 
  114. * 
  115. * This will get the first message available for the code. If no code is 
  116. * given then the first code available will be used. 
  117. * 
  118. * @since 2.1.0 
  119. * 
  120. * @param string|int $code Optional. Error code to retrieve message. 
  121. * @return string 
  122. */ 
  123. function get_error_message($code = '') { 
  124. if ( empty($code) ) 
  125. $code = $this->get_error_code(); 
  126. $messages = $this->get_error_messages($code); 
  127. if ( empty($messages) ) 
  128. return ''; 
  129. return $messages[0]; 
  130.  
  131. /** 
  132. * Retrieve error data for error code. 
  133. * 
  134. * @since 2.1.0 
  135. * 
  136. * @param string|int $code Optional. Error code. 
  137. * @return mixed Null, if no errors. 
  138. */ 
  139. function get_error_data($code = '') { 
  140. if ( empty($code) ) 
  141. $code = $this->get_error_code(); 
  142.  
  143. if ( isset($this->error_data[$code]) ) 
  144. return $this->error_data[$code]; 
  145. return null; 
  146.  
  147. /** 
  148. * Append more error messages to list of error messages. 
  149. * 
  150. * @since 2.1.0 
  151. * @access public 
  152. * 
  153. * @param string|int $code Error code. 
  154. * @param string $message Error message. 
  155. * @param mixed $data Optional. Error data. 
  156. */ 
  157. function add($code, $message, $data = '') { 
  158. $this->errors[$code][] = $message; 
  159. if ( ! empty($data) ) 
  160. $this->error_data[$code] = $data; 
  161.  
  162. /** 
  163. * Add data for error code. 
  164. * 
  165. * The error code can only contain one error data. 
  166. * 
  167. * @since 2.1.0 
  168. * 
  169. * @param mixed $data Error data. 
  170. * @param string|int $code Error code. 
  171. */ 
  172. function add_data($data, $code = '') { 
  173. if ( empty($code) ) 
  174. $code = $this->get_error_code(); 
  175.  
  176. $this->error_data[$code] = $data; 
  177.  
  178. /** 
  179. * Check whether variable is a WordPress Error. 
  180. * 
  181. * Looks at the object and if a WP_Error class. Does not check to see if the 
  182. * parent is also WP_Error, so can't inherit WP_Error and still use this 
  183. * function. 
  184. * 
  185. * @since 2.1.0 
  186. * 
  187. * @param mixed $thing Check if unknown variable is WordPress Error object. 
  188. * @return bool True, if WP_Error. False, if not WP_Error. 
  189. */ 
  190. function is_wp_error($thing) { 
  191. if ( is_object($thing) && is_a($thing, 'WP_Error') ) 
  192. return true; 
  193. return false; 
.