You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

205 lines
4.6 KiB

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