class-wp-error.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 is_wp_error() to check if this class is returned. Many
  14. * 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. * @since 2.1.0
  18. */
  19. class WP_Error {
  20. /**
  21. * Stores the list of errors.
  22. *
  23. * @since 2.1.0
  24. * @var array
  25. */
  26. public $errors = array();
  27. /**
  28. * Stores the list of data for error codes.
  29. *
  30. * @since 2.1.0
  31. * @var array
  32. */
  33. public $error_data = array();
  34. /**
  35. * Initialize the error.
  36. *
  37. * If `$code` is empty, the other parameters will be ignored.
  38. * When `$code` is not empty, `$message` will be used even if
  39. * it is empty. The `$data` parameter will be used only if it
  40. * is not empty.
  41. *
  42. * Though the class is constructed with a single error code and
  43. * message, multiple codes can be added using the `add()` method.
  44. *
  45. * @since 2.1.0
  46. *
  47. * @param string|int $code Error code
  48. * @param string $message Error message
  49. * @param mixed $data Optional. Error data.
  50. */
  51. public function __construct( $code = '', $message = '', $data = '' ) {
  52. if ( empty($code) )
  53. return;
  54. $this->errors[$code][] = $message;
  55. if ( ! empty($data) )
  56. $this->error_data[$code] = $data;
  57. }
  58. /**
  59. * Retrieve all error codes.
  60. *
  61. * @since 2.1.0
  62. *
  63. * @return array List of error codes, if available.
  64. */
  65. public function get_error_codes() {
  66. if ( empty($this->errors) )
  67. return array();
  68. return array_keys($this->errors);
  69. }
  70. /**
  71. * Retrieve first error code available.
  72. *
  73. * @since 2.1.0
  74. *
  75. * @return string|int Empty string, if no error codes.
  76. */
  77. public function get_error_code() {
  78. $codes = $this->get_error_codes();
  79. if ( empty($codes) )
  80. return '';
  81. return $codes[0];
  82. }
  83. /**
  84. * Retrieve all error messages or error messages matching code.
  85. *
  86. * @since 2.1.0
  87. *
  88. * @param string|int $code Optional. Retrieve messages matching code, if exists.
  89. * @return array Error strings on success, or empty array on failure (if using code parameter).
  90. */
  91. public function get_error_messages($code = '') {
  92. // Return all messages if no code specified.
  93. if ( empty($code) ) {
  94. $all_messages = array();
  95. foreach ( (array) $this->errors as $code => $messages )
  96. $all_messages = array_merge($all_messages, $messages);
  97. return $all_messages;
  98. }
  99. if ( isset($this->errors[$code]) )
  100. return $this->errors[$code];
  101. else
  102. return array();
  103. }
  104. /**
  105. * Get single error message.
  106. *
  107. * This will get the first message available for the code. If no code is
  108. * given then the first code available will be used.
  109. *
  110. * @since 2.1.0
  111. *
  112. * @param string|int $code Optional. Error code to retrieve message.
  113. * @return string
  114. */
  115. public function get_error_message($code = '') {
  116. if ( empty($code) )
  117. $code = $this->get_error_code();
  118. $messages = $this->get_error_messages($code);
  119. if ( empty($messages) )
  120. return '';
  121. return $messages[0];
  122. }
  123. /**
  124. * Retrieve error data for error code.
  125. *
  126. * @since 2.1.0
  127. *
  128. * @param string|int $code Optional. Error code.
  129. * @return mixed Error data, if it exists.
  130. */
  131. public function get_error_data($code = '') {
  132. if ( empty($code) )
  133. $code = $this->get_error_code();
  134. if ( isset($this->error_data[$code]) )
  135. return $this->error_data[$code];
  136. }
  137. /**
  138. * Add an error or append additional message to an existing error.
  139. *
  140. * @since 2.1.0
  141. *
  142. * @param string|int $code Error code.
  143. * @param string $message Error message.
  144. * @param mixed $data Optional. Error data.
  145. */
  146. public function add($code, $message, $data = '') {
  147. $this->errors[$code][] = $message;
  148. if ( ! empty($data) )
  149. $this->error_data[$code] = $data;
  150. }
  151. /**
  152. * Add data for error code.
  153. *
  154. * The error code can only contain one error data.
  155. *
  156. * @since 2.1.0
  157. *
  158. * @param mixed $data Error data.
  159. * @param string|int $code Error code.
  160. */
  161. public function add_data($data, $code = '') {
  162. if ( empty($code) )
  163. $code = $this->get_error_code();
  164. $this->error_data[$code] = $data;
  165. }
  166. /**
  167. * Removes the specified error.
  168. *
  169. * This function removes all error messages associated with the specified
  170. * error code, along with any error data for that code.
  171. *
  172. * @since 4.1.0
  173. *
  174. * @param string|int $code Error code.
  175. */
  176. public function remove( $code ) {
  177. unset( $this->errors[ $code ] );
  178. unset( $this->error_data[ $code ] );
  179. }
  180. }