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 WP_Error($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. /** 
  41. * Retrieve all error codes. 
  42. * @since 2.1.0 
  43. * @access public 
  44. * @return array List of error codes, if avaiable. 
  45. */ 
  46. function get_error_codes() { 
  47. if ( empty($this->errors) ) 
  48. return array(); 
  49.  
  50. return array_keys($this->errors); 
  51.  
  52. /** 
  53. * Retrieve first error code available. 
  54. * @since 2.1.0 
  55. * @access public 
  56. * @return string|int Empty string, if no error codes. 
  57. */ 
  58. function get_error_code() { 
  59. $codes = $this->get_error_codes(); 
  60.  
  61. if ( empty($codes) ) 
  62. return ''; 
  63.  
  64. return $codes[0]; 
  65.  
  66. /** 
  67. * Retrieve all error messages or error messages matching code. 
  68. * @since 2.1.0 
  69. * @param string|int $code Optional. Retrieve messages matching code, if exists. 
  70. * @return array Error strings on success, or empty array on failure (if using codee parameter). 
  71. */ 
  72. function get_error_messages($code = '') { 
  73. // Return all messages if no code specified. 
  74. if ( empty($code) ) { 
  75. $all_messages = array(); 
  76. foreach ( (array) $this->errors as $code => $messages ) 
  77. $all_messages = array_merge($all_messages, $messages); 
  78.  
  79. return $all_messages; 
  80.  
  81. if ( isset($this->errors[$code]) ) 
  82. return $this->errors[$code]; 
  83. else 
  84. return array(); 
  85.  
  86. /** 
  87. * Get single error message. 
  88. * This will get the first message available for the code. If no code is 
  89. * given then the first code available will be used. 
  90. * @since 2.1.0 
  91. * @param string|int $code Optional. Error code to retrieve message. 
  92. * @return string 
  93. */ 
  94. function get_error_message($code = '') { 
  95. if ( empty($code) ) 
  96. $code = $this->get_error_code(); 
  97. $messages = $this->get_error_messages($code); 
  98. if ( empty($messages) ) 
  99. return ''; 
  100. return $messages[0]; 
  101.  
  102. /** 
  103. * Retrieve error data for error code. 
  104. * @since 2.1.0 
  105. * @param string|int $code Optional. Error code. 
  106. * @return mixed Null, if no errors. 
  107. */ 
  108. function get_error_data($code = '') { 
  109. if ( empty($code) ) 
  110. $code = $this->get_error_code(); 
  111.  
  112. if ( isset($this->error_data[$code]) ) 
  113. return $this->error_data[$code]; 
  114. return null; 
  115.  
  116. /** 
  117. * Append more error messages to list of error messages. 
  118. * @since 2.1.0 
  119. * @access public 
  120. * @param string|int $code Error code. 
  121. * @param string $message Error message. 
  122. * @param mixed $data Optional. Error data. 
  123. */ 
  124. function add($code, $message, $data = '') { 
  125. $this->errors[$code][] = $message; 
  126. if ( ! empty($data) ) 
  127. $this->error_data[$code] = $data; 
  128.  
  129. /** 
  130. * Add data for error code. 
  131. * The error code can only contain one error data. 
  132. * @since 2.1.0 
  133. * @param mixed $data Error data. 
  134. * @param string|int $code Error code. 
  135. */ 
  136. function add_data($data, $code = '') { 
  137. if ( empty($code) ) 
  138. $code = $this->get_error_code(); 
  139.  
  140. $this->error_data[$code] = $data;