WP_Error

WordPress Error class.

Defined (1)

The class is defined in the following location(s).

/bp-forums/bbpress/bb-includes/backpress/class.wp-error.php  
  1. class WP_Error { 
  2. /** 
  3. * Stores the list of errors. 
  4. * @since 2.1.0 
  5. * @var array 
  6. * @access private 
  7. */ 
  8. var $errors = array(); 
  9.  
  10. /** 
  11. * Stores the list of data for error codes. 
  12. * @since 2.1.0 
  13. * @var array 
  14. * @access private 
  15. */ 
  16. var $error_data = array(); 
  17.  
  18. /** 
  19. * PHP4 Constructor - Sets up error message. 
  20. * If code parameter is empty then nothing will be done. It is possible to 
  21. * add multiple messages to the same code, but with other methods in the 
  22. * class. 
  23. * All parameters are optional, but if the code parameter is set, then the 
  24. * data parameter is optional. 
  25. * @since 2.1.0 
  26. * @param string|int $code Error code 
  27. * @param string $message Error message 
  28. * @param mixed $data Optional. Error data. 
  29. * @return WP_Error 
  30. */ 
  31. function __construct($code = '', $message = '', $data = '') { 
  32. if ( empty($code) ) 
  33. return; 
  34.  
  35. $this->errors[$code][] = $message; 
  36.  
  37. if ( ! empty($data) ) 
  38. $this->error_data[$code] = $data; 
  39.  
  40. function WP_Error($code = '', $message = '', $data = '') { 
  41. $this->__construct($code, $message, $data); 
  42.  
  43. /** 
  44. * Retrieve all error codes. 
  45. * @since 2.1.0 
  46. * @access public 
  47. * @return array List of error codes, if avaiable. 
  48. */ 
  49. function get_error_codes() { 
  50. if ( empty($this->errors) ) 
  51. return array(); 
  52.  
  53. return array_keys($this->errors); 
  54.  
  55. /** 
  56. * Retrieve first error code available. 
  57. * @since 2.1.0 
  58. * @access public 
  59. * @return string|int Empty string, if no error codes. 
  60. */ 
  61. function get_error_code() { 
  62. $codes = $this->get_error_codes(); 
  63.  
  64. if ( empty($codes) ) 
  65. return ''; 
  66.  
  67. return $codes[0]; 
  68.  
  69. /** 
  70. * Retrieve all error messages or error messages matching code. 
  71. * @since 2.1.0 
  72. * @param string|int $code Optional. Retrieve messages matching code, if exists. 
  73. * @return array Error strings on success, or empty array on failure (if using codee parameter). 
  74. */ 
  75. function get_error_messages($code = '') { 
  76. // Return all messages if no code specified. 
  77. if ( empty($code) ) { 
  78. $all_messages = array(); 
  79. foreach ( (array) $this->errors as $code => $messages ) 
  80. $all_messages = array_merge($all_messages, $messages); 
  81.  
  82. return $all_messages; 
  83.  
  84. if ( isset($this->errors[$code]) ) 
  85. return $this->errors[$code]; 
  86. else 
  87. return array(); 
  88.  
  89. /** 
  90. * Get single error message. 
  91. * This will get the first message available for the code. If no code is 
  92. * given then the first code available will be used. 
  93. * @since 2.1.0 
  94. * @param string|int $code Optional. Error code to retrieve message. 
  95. * @return string 
  96. */ 
  97. function get_error_message($code = '') { 
  98. if ( empty($code) ) 
  99. $code = $this->get_error_code(); 
  100. $messages = $this->get_error_messages($code); 
  101. if ( empty($messages) ) 
  102. return ''; 
  103. return $messages[0]; 
  104.  
  105. /** 
  106. * Retrieve error data for error code. 
  107. * @since 2.1.0 
  108. * @param string|int $code Optional. Error code. 
  109. * @return mixed Null, if no errors. 
  110. */ 
  111. function get_error_data($code = '') { 
  112. if ( empty($code) ) 
  113. $code = $this->get_error_code(); 
  114.  
  115. if ( isset($this->error_data[$code]) ) 
  116. return $this->error_data[$code]; 
  117. return null; 
  118.  
  119. /** 
  120. * Append more error messages to list of error messages. 
  121. * @since 2.1.0 
  122. * @access public 
  123. * @param string|int $code Error code. 
  124. * @param string $message Error message. 
  125. * @param mixed $data Optional. Error data. 
  126. */ 
  127. function add($code, $message, $data = '') { 
  128. $this->errors[$code][] = $message; 
  129. if ( ! empty($data) ) 
  130. $this->error_data[$code] = $data; 
  131.  
  132. /** 
  133. * Add data for error code. 
  134. * The error code can only contain one error data. 
  135. * @since 2.1.0 
  136. * @param mixed $data Error data. 
  137. * @param string|int $code Error code. 
  138. */ 
  139. function add_data($data, $code = '') { 
  140. if ( empty($code) ) 
  141. $code = $this->get_error_code(); 
  142.  
  143. $this->error_data[$code] = $data;