| [ Index ] |
PHP Cross Reference of BackPress |
[Summary view] [Print] [Text view]
1 <?php 2 // Last sync [WP11537] - split out from wp-includes/classes.php 3 4 /** 5 * WordPress Error class. 6 * 7 * Container for checking for WordPress errors and error messages. Return 8 * WP_Error and use {@link is_wp_error()} to check if this class is returned. 9 * Many core WordPress functions pass this class in the event of an error and 10 * if not handled properly will result in code errors. 11 * 12 * @package WordPress 13 * @since 2.1.0 14 */ 15 class WP_Error { 16 /** 17 * Stores the list of errors. 18 * 19 * @since 2.1.0 20 * @var array 21 * @access private 22 */ 23 var $errors = array(); 24 25 /** 26 * Stores the list of data for error codes. 27 * 28 * @since 2.1.0 29 * @var array 30 * @access private 31 */ 32 var $error_data = array(); 33 34 /** 35 * PHP4 Constructor - Sets up error message. 36 * 37 * If code parameter is empty then nothing will be done. It is possible to 38 * add multiple messages to the same code, but with other methods in the 39 * class. 40 * 41 * All parameters are optional, but if the code parameter is set, then the 42 * data parameter is optional. 43 * 44 * @since 2.1.0 45 * 46 * @param string|int $code Error code 47 * @param string $message Error message 48 * @param mixed $data Optional. Error data. 49 * @return WP_Error 50 */ 51 function WP_Error($code = '', $message = '', $data = '') { 52 if ( empty($code) ) 53 return; 54 55 $this->errors[$code][] = $message; 56 57 if ( ! empty($data) ) 58 $this->error_data[$code] = $data; 59 } 60 61 /** 62 * Retrieve all error codes. 63 * 64 * @since 2.1.0 65 * @access public 66 * 67 * @return array List of error codes, if avaiable. 68 */ 69 function get_error_codes() { 70 if ( empty($this->errors) ) 71 return array(); 72 73 return array_keys($this->errors); 74 } 75 76 /** 77 * Retrieve first error code available. 78 * 79 * @since 2.1.0 80 * @access public 81 * 82 * @return string|int Empty string, if no error codes. 83 */ 84 function get_error_code() { 85 $codes = $this->get_error_codes(); 86 87 if ( empty($codes) ) 88 return ''; 89 90 return $codes[0]; 91 } 92 93 /** 94 * Retrieve all error messages or error messages matching code. 95 * 96 * @since 2.1.0 97 * 98 * @param string|int $code Optional. Retrieve messages matching code, if exists. 99 * @return array Error strings on success, or empty array on failure (if using codee parameter). 100 */ 101 function get_error_messages($code = '') { 102 // Return all messages if no code specified. 103 if ( empty($code) ) { 104 $all_messages = array(); 105 foreach ( (array) $this->errors as $code => $messages ) 106 $all_messages = array_merge($all_messages, $messages); 107 108 return $all_messages; 109 } 110 111 if ( isset($this->errors[$code]) ) 112 return $this->errors[$code]; 113 else 114 return array(); 115 } 116 117 /** 118 * Get single error message. 119 * 120 * This will get the first message available for the code. If no code is 121 * given then the first code available will be used. 122 * 123 * @since 2.1.0 124 * 125 * @param string|int $code Optional. Error code to retrieve message. 126 * @return string 127 */ 128 function get_error_message($code = '') { 129 if ( empty($code) ) 130 $code = $this->get_error_code(); 131 $messages = $this->get_error_messages($code); 132 if ( empty($messages) ) 133 return ''; 134 return $messages[0]; 135 } 136 137 /** 138 * Retrieve error data for error code. 139 * 140 * @since 2.1.0 141 * 142 * @param string|int $code Optional. Error code. 143 * @return mixed Null, if no errors. 144 */ 145 function get_error_data($code = '') { 146 if ( empty($code) ) 147 $code = $this->get_error_code(); 148 149 if ( isset($this->error_data[$code]) ) 150 return $this->error_data[$code]; 151 return null; 152 } 153 154 /** 155 * Append more error messages to list of error messages. 156 * 157 * @since 2.1.0 158 * @access public 159 * 160 * @param string|int $code Error code. 161 * @param string $message Error message. 162 * @param mixed $data Optional. Error data. 163 */ 164 function add($code, $message, $data = '') { 165 $this->errors[$code][] = $message; 166 if ( ! empty($data) ) 167 $this->error_data[$code] = $data; 168 } 169 170 /** 171 * Add data for error code. 172 * 173 * The error code can only contain one error data. 174 * 175 * @since 2.1.0 176 * 177 * @param mixed $data Error data. 178 * @param string|int $code Error code. 179 */ 180 function add_data($data, $code = '') { 181 if ( empty($code) ) 182 $code = $this->get_error_code(); 183 184 $this->error_data[$code] = $data; 185 } 186 } 187 188 /** 189 * Check whether variable is a WordPress Error. 190 * 191 * Looks at the object and if a WP_Error class. Does not check to see if the 192 * parent is also WP_Error, so can't inherit WP_Error and still use this 193 * function. 194 * 195 * @since 2.1.0 196 * 197 * @param mixed $thing Check if unknown variable is WordPress Error object. 198 * @return bool True, if WP_Error. False, if not WP_Error. 199 */ 200 function is_wp_error($thing) { 201 if ( is_object($thing) && is_a($thing, 'WP_Error') ) 202 return true; 203 return false; 204 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Feb 8 03:57:40 2012 | Hosted by follow the white rabbit. |