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