class-wp-error.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. }
  55. $this->errors[ $code ][] = $message;
  56. if ( ! empty( $data ) ) {
  57. $this->error_data[ $code ] = $data;
  58. }
  59. }
  60. /**
  61. * Retrieve all error codes.
  62. *
  63. * @since 2.1.0
  64. *
  65. * @return array List of error codes, if available.
  66. */
  67. public function get_error_codes() {
  68. if ( ! $this->has_errors() ) {
  69. return array();
  70. }
  71. return array_keys( $this->errors );
  72. }
  73. /**
  74. * Retrieve first error code available.
  75. *
  76. * @since 2.1.0
  77. *
  78. * @return string|int Empty string, if no error codes.
  79. */
  80. public function get_error_code() {
  81. $codes = $this->get_error_codes();
  82. if ( empty( $codes ) ) {
  83. return '';
  84. }
  85. return $codes[0];
  86. }
  87. /**
  88. * Retrieve all error messages or error messages matching code.
  89. *
  90. * @since 2.1.0
  91. *
  92. * @param string|int $code Optional. Retrieve messages matching code, if exists.
  93. * @return array Error strings on success, or empty array on failure (if using code parameter).
  94. */
  95. public function get_error_messages( $code = '' ) {
  96. // Return all messages if no code specified.
  97. if ( empty( $code ) ) {
  98. $all_messages = array();
  99. foreach ( (array) $this->errors as $code => $messages ) {
  100. $all_messages = array_merge( $all_messages, $messages );
  101. }
  102. return $all_messages;
  103. }
  104. if ( isset( $this->errors[ $code ] ) ) {
  105. return $this->errors[ $code ];
  106. } else {
  107. return array();
  108. }
  109. }
  110. /**
  111. * Get single error message.
  112. *
  113. * This will get the first message available for the code. If no code is
  114. * given then the first code available will be used.
  115. *
  116. * @since 2.1.0
  117. *
  118. * @param string|int $code Optional. Error code to retrieve message.
  119. * @return string
  120. */
  121. public function get_error_message( $code = '' ) {
  122. if ( empty( $code ) ) {
  123. $code = $this->get_error_code();
  124. }
  125. $messages = $this->get_error_messages( $code );
  126. if ( empty( $messages ) ) {
  127. return '';
  128. }
  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 Error data, if it exists.
  138. */
  139. public 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. }
  146. }
  147. /**
  148. * Verify if the instance contains errors.
  149. *
  150. * @since 5.1.0
  151. *
  152. * @return bool
  153. */
  154. public function has_errors() {
  155. if ( ! empty( $this->errors ) ) {
  156. return true;
  157. }
  158. return false;
  159. }
  160. /**
  161. * Add an error or append additional message to an existing error.
  162. *
  163. * @since 2.1.0
  164. *
  165. * @param string|int $code Error code.
  166. * @param string $message Error message.
  167. * @param mixed $data Optional. Error data.
  168. */
  169. public function add( $code, $message, $data = '' ) {
  170. $this->errors[ $code ][] = $message;
  171. if ( ! empty( $data ) ) {
  172. $this->error_data[ $code ] = $data;
  173. }
  174. }
  175. /**
  176. * Add data for error code.
  177. *
  178. * The error code can only contain one error data.
  179. *
  180. * @since 2.1.0
  181. *
  182. * @param mixed $data Error data.
  183. * @param string|int $code Error code.
  184. */
  185. public function add_data( $data, $code = '' ) {
  186. if ( empty( $code ) ) {
  187. $code = $this->get_error_code();
  188. }
  189. $this->error_data[ $code ] = $data;
  190. }
  191. /**
  192. * Removes the specified error.
  193. *
  194. * This function removes all error messages associated with the specified
  195. * error code, along with any error data for that code.
  196. *
  197. * @since 4.1.0
  198. *
  199. * @param string|int $code Error code.
  200. */
  201. public function remove( $code ) {
  202. unset( $this->errors[ $code ] );
  203. unset( $this->error_data[ $code ] );
  204. }
  205. }