Нема описа

class-wp-error.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * WordPress Error API.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * WordPress Error class.
  9. *
  10. * Container for checking for WordPress errors and error messages. Return
  11. * WP_Error and use is_wp_error() to check if this class is returned. Many
  12. * core WordPress functions pass this class in the event of an error and
  13. * if not handled properly will result in code errors.
  14. *
  15. * @since 2.1.0
  16. */
  17. class WP_Error {
  18. /**
  19. * Stores the list of errors.
  20. *
  21. * @since 2.1.0
  22. * @var array
  23. */
  24. public $errors = array();
  25. /**
  26. * Stores the most recently added data for each error code.
  27. *
  28. * @since 2.1.0
  29. * @var array
  30. */
  31. public $error_data = array();
  32. /**
  33. * Stores previously added data added for error codes, oldest-to-newest by code.
  34. *
  35. * @since 5.6.0
  36. * @var array[]
  37. */
  38. protected $additional_data = array();
  39. /**
  40. * Initializes the error.
  41. *
  42. * If `$code` is empty, the other parameters will be ignored.
  43. * When `$code` is not empty, `$message` will be used even if
  44. * it is empty. The `$data` parameter will be used only if it
  45. * is not empty.
  46. *
  47. * Though the class is constructed with a single error code and
  48. * message, multiple codes can be added using the `add()` method.
  49. *
  50. * @since 2.1.0
  51. *
  52. * @param string|int $code Error code.
  53. * @param string $message Error message.
  54. * @param mixed $data Optional. Error data.
  55. */
  56. public function __construct( $code = '', $message = '', $data = '' ) {
  57. if ( empty( $code ) ) {
  58. return;
  59. }
  60. $this->add( $code, $message, $data );
  61. }
  62. /**
  63. * Retrieves all error codes.
  64. *
  65. * @since 2.1.0
  66. *
  67. * @return array List of error codes, if available.
  68. */
  69. public function get_error_codes() {
  70. if ( ! $this->has_errors() ) {
  71. return array();
  72. }
  73. return array_keys( $this->errors );
  74. }
  75. /**
  76. * Retrieves the first error code available.
  77. *
  78. * @since 2.1.0
  79. *
  80. * @return string|int Empty string, if no error codes.
  81. */
  82. public function get_error_code() {
  83. $codes = $this->get_error_codes();
  84. if ( empty( $codes ) ) {
  85. return '';
  86. }
  87. return $codes[0];
  88. }
  89. /**
  90. * Retrieves all error messages, or the error messages for the given error code.
  91. *
  92. * @since 2.1.0
  93. *
  94. * @param string|int $code Optional. Retrieve messages matching code, if exists.
  95. * @return array Error strings on success, or empty array if there are none.
  96. */
  97. public function get_error_messages( $code = '' ) {
  98. // Return all messages if no code specified.
  99. if ( empty( $code ) ) {
  100. $all_messages = array();
  101. foreach ( (array) $this->errors as $code => $messages ) {
  102. $all_messages = array_merge( $all_messages, $messages );
  103. }
  104. return $all_messages;
  105. }
  106. if ( isset( $this->errors[ $code ] ) ) {
  107. return $this->errors[ $code ];
  108. } else {
  109. return array();
  110. }
  111. }
  112. /**
  113. * Gets a single error message.
  114. *
  115. * This will get the first message available for the code. If no code is
  116. * given then the first code available will be used.
  117. *
  118. * @since 2.1.0
  119. *
  120. * @param string|int $code Optional. Error code to retrieve message.
  121. * @return string The error message.
  122. */
  123. public function get_error_message( $code = '' ) {
  124. if ( empty( $code ) ) {
  125. $code = $this->get_error_code();
  126. }
  127. $messages = $this->get_error_messages( $code );
  128. if ( empty( $messages ) ) {
  129. return '';
  130. }
  131. return $messages[0];
  132. }
  133. /**
  134. * Retrieves the most recently added error data for an error code.
  135. *
  136. * @since 2.1.0
  137. *
  138. * @param string|int $code Optional. Error code.
  139. * @return mixed Error data, if it exists.
  140. */
  141. public function get_error_data( $code = '' ) {
  142. if ( empty( $code ) ) {
  143. $code = $this->get_error_code();
  144. }
  145. if ( isset( $this->error_data[ $code ] ) ) {
  146. return $this->error_data[ $code ];
  147. }
  148. }
  149. /**
  150. * Verifies if the instance contains errors.
  151. *
  152. * @since 5.1.0
  153. *
  154. * @return bool If the instance contains errors.
  155. */
  156. public function has_errors() {
  157. if ( ! empty( $this->errors ) ) {
  158. return true;
  159. }
  160. return false;
  161. }
  162. /**
  163. * Adds an error or appends an additional message to an existing error.
  164. *
  165. * @since 2.1.0
  166. *
  167. * @param string|int $code Error code.
  168. * @param string $message Error message.
  169. * @param mixed $data Optional. Error data.
  170. */
  171. public function add( $code, $message, $data = '' ) {
  172. $this->errors[ $code ][] = $message;
  173. if ( ! empty( $data ) ) {
  174. $this->add_data( $data, $code );
  175. }
  176. /**
  177. * Fires when an error is added to a WP_Error object.
  178. *
  179. * @since 5.6.0
  180. *
  181. * @param string|int $code Error code.
  182. * @param string $message Error message.
  183. * @param mixed $data Error data. Might be empty.
  184. * @param WP_Error $wp_error The WP_Error object.
  185. */
  186. do_action( 'wp_error_added', $code, $message, $data, $this );
  187. }
  188. /**
  189. * Adds data to an error with the given code.
  190. *
  191. * @since 2.1.0
  192. * @since 5.6.0 Errors can now contain more than one item of error data. {@see WP_Error::$additional_data}.
  193. *
  194. * @param mixed $data Error data.
  195. * @param string|int $code Error code.
  196. */
  197. public function add_data( $data, $code = '' ) {
  198. if ( empty( $code ) ) {
  199. $code = $this->get_error_code();
  200. }
  201. if ( isset( $this->error_data[ $code ] ) ) {
  202. $this->additional_data[ $code ][] = $this->error_data[ $code ];
  203. }
  204. $this->error_data[ $code ] = $data;
  205. }
  206. /**
  207. * Retrieves all error data for an error code in the order in which the data was added.
  208. *
  209. * @since 5.6.0
  210. *
  211. * @param string|int $code Error code.
  212. * @return mixed[] Array of error data, if it exists.
  213. */
  214. public function get_all_error_data( $code = '' ) {
  215. if ( empty( $code ) ) {
  216. $code = $this->get_error_code();
  217. }
  218. $data = array();
  219. if ( isset( $this->additional_data[ $code ] ) ) {
  220. $data = $this->additional_data[ $code ];
  221. }
  222. if ( isset( $this->error_data[ $code ] ) ) {
  223. $data[] = $this->error_data[ $code ];
  224. }
  225. return $data;
  226. }
  227. /**
  228. * Removes the specified error.
  229. *
  230. * This function removes all error messages associated with the specified
  231. * error code, along with any error data for that code.
  232. *
  233. * @since 4.1.0
  234. *
  235. * @param string|int $code Error code.
  236. */
  237. public function remove( $code ) {
  238. unset( $this->errors[ $code ] );
  239. unset( $this->error_data[ $code ] );
  240. unset( $this->additional_data[ $code ] );
  241. }
  242. /**
  243. * Merges the errors in the given error object into this one.
  244. *
  245. * @since 5.6.0
  246. *
  247. * @param WP_Error $error Error object to merge.
  248. */
  249. public function merge_from( WP_Error $error ) {
  250. static::copy_errors( $error, $this );
  251. }
  252. /**
  253. * Exports the errors in this object into the given one.
  254. *
  255. * @since 5.6.0
  256. *
  257. * @param WP_Error $error Error object to export into.
  258. */
  259. public function export_to( WP_Error $error ) {
  260. static::copy_errors( $this, $error );
  261. }
  262. /**
  263. * Copies errors from one WP_Error instance to another.
  264. *
  265. * @since 5.6.0
  266. *
  267. * @param WP_Error $from The WP_Error to copy from.
  268. * @param WP_Error $to The WP_Error to copy to.
  269. */
  270. protected static function copy_errors( WP_Error $from, WP_Error $to ) {
  271. foreach ( $from->get_error_codes() as $code ) {
  272. foreach ( $from->get_error_messages( $code ) as $error_message ) {
  273. $to->add( $code, $error_message );
  274. }
  275. foreach ( $from->get_all_error_data( $code ) as $data ) {
  276. $to->add_data( $data, $code );
  277. }
  278. }
  279. }
  280. }