class-wp-error.php 7.2 KB

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