General: Introduce `WP_Error::has_errors()` method and use it where appropriate.

Props robdxw, DrewAPicture, SergeyBiryukov.
Fixes #42742.
Built from https://develop.svn.wordpress.org/trunk@42761


git-svn-id: http://core.svn.wordpress.org/trunk@42591 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2018-02-27 02:31:31 +00:00
parent 71f206839d
commit 0e802a627d
21 changed files with 57 additions and 41 deletions

View File

@ -3692,7 +3692,7 @@ function wp_ajax_install_theme() {
$status['errorCode'] = $skin->result->get_error_code();
$status['errorMessage'] = $skin->result->get_error_message();
wp_send_json_error( $status );
} elseif ( $skin->get_errors()->get_error_code() ) {
} elseif ( $skin->get_errors()->has_errors() ) {
$status['errorMessage'] = $skin->get_error_messages();
wp_send_json_error( $status );
} elseif ( is_null( $result ) ) {
@ -3702,7 +3702,7 @@ function wp_ajax_install_theme() {
$status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );
// Pass through the error from WP_Filesystem if one was raised.
if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
$status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
}
@ -3805,7 +3805,7 @@ function wp_ajax_update_theme() {
$status['errorCode'] = $skin->result->get_error_code();
$status['errorMessage'] = $skin->result->get_error_message();
wp_send_json_error( $status );
} elseif ( $skin->get_errors()->get_error_code() ) {
} elseif ( $skin->get_errors()->has_errors() ) {
$status['errorMessage'] = $skin->get_error_messages();
wp_send_json_error( $status );
} elseif ( is_array( $result ) && ! empty( $result[ $stylesheet ] ) ) {
@ -3829,7 +3829,7 @@ function wp_ajax_update_theme() {
$status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );
// Pass through the error from WP_Filesystem if one was raised.
if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
$status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
}
@ -3891,7 +3891,7 @@ function wp_ajax_delete_theme() {
$status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );
// Pass through the error from WP_Filesystem if one was raised.
if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
$status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
}
@ -3980,7 +3980,7 @@ function wp_ajax_install_plugin() {
$status['errorCode'] = $skin->result->get_error_code();
$status['errorMessage'] = $skin->result->get_error_message();
wp_send_json_error( $status );
} elseif ( $skin->get_errors()->get_error_code() ) {
} elseif ( $skin->get_errors()->has_errors() ) {
$status['errorMessage'] = $skin->get_error_messages();
wp_send_json_error( $status );
} elseif ( is_null( $result ) ) {
@ -3990,7 +3990,7 @@ function wp_ajax_install_plugin() {
$status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );
// Pass through the error from WP_Filesystem if one was raised.
if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
$status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
}
@ -4081,7 +4081,7 @@ function wp_ajax_update_plugin() {
$status['errorCode'] = $skin->result->get_error_code();
$status['errorMessage'] = $skin->result->get_error_message();
wp_send_json_error( $status );
} elseif ( $skin->get_errors()->get_error_code() ) {
} elseif ( $skin->get_errors()->has_errors() ) {
$status['errorMessage'] = $skin->get_error_messages();
wp_send_json_error( $status );
} elseif ( is_array( $result ) && ! empty( $result[ $plugin ] ) ) {
@ -4115,7 +4115,7 @@ function wp_ajax_update_plugin() {
$status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );
// Pass through the error from WP_Filesystem if one was raised.
if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
$status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
}
@ -4182,7 +4182,7 @@ function wp_ajax_delete_plugin() {
$status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );
// Pass through the error from WP_Filesystem if one was raised.
if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
$status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
}

View File

@ -61,8 +61,10 @@ class WP_Ajax_Upgrader_Skin extends Automatic_Upgrader_Skin {
$messages = array();
foreach ( $this->errors->get_error_codes() as $error_code ) {
if ( $this->errors->get_error_data( $error_code ) && is_string( $this->errors->get_error_data( $error_code ) ) ) {
$messages[] = $this->errors->get_error_message( $error_code ) . ' ' . esc_html( strip_tags( $this->errors->get_error_data( $error_code ) ) );
$error_data = $this->errors->get_error_data( $error_code );
if ( $error_data && is_string( $error_data ) ) {
$messages[] = $this->errors->get_error_message( $error_code ) . ' ' . esc_html( strip_tags( $error_data ) );
} else {
$messages[] = $this->errors->get_error_message( $error_code );
}

View File

@ -127,7 +127,7 @@ class WP_Upgrader_Skin {
}
if ( is_string( $errors ) ) {
$this->feedback( $errors );
} elseif ( is_wp_error( $errors ) && $errors->get_error_code() ) {
} elseif ( is_wp_error( $errors ) && $errors->has_errors() ) {
foreach ( $errors->get_error_messages() as $message ) {
if ( $errors->get_error_data() && is_string( $errors->get_error_data() ) ) {
$this->feedback( $message . ' ' . esc_html( strip_tags( $errors->get_error_data() ) ) );

View File

@ -191,7 +191,7 @@ class WP_Upgrader {
if ( ! WP_Filesystem( $credentials, $directories[0], $allow_relaxed_file_ownership ) ) {
$error = true;
if ( is_object( $wp_filesystem ) && $wp_filesystem->errors->get_error_code() ) {
if ( is_object( $wp_filesystem ) && $wp_filesystem->errors->has_errors() ) {
$error = $wp_filesystem->errors;
}
// Failed to connect, Error and request again
@ -203,7 +203,7 @@ class WP_Upgrader {
return new WP_Error( 'fs_unavailable', $this->strings['fs_unavailable'] );
}
if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
return new WP_Error( 'fs_error', $this->strings['fs_error'], $wp_filesystem->errors );
}

View File

@ -1460,7 +1460,7 @@ function WP_Filesystem( $args = false, $context = false, $allow_relaxed_file_own
define( 'FS_TIMEOUT', 30 );
}
if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
return false;
}

View File

@ -865,7 +865,7 @@ function delete_plugins( $plugins, $deprecated = '' ) {
return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
}
if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
return new WP_Error( 'fs_error', __( 'Filesystem error.' ), $wp_filesystem->errors );
}

View File

@ -980,7 +980,7 @@ function populate_network( $network_id = 1, $domain = '', $email = '', $site_nam
$errors->add( 'invalid_email', __( 'You must provide a valid email address.' ) );
}
if ( $errors->get_error_code() ) {
if ( $errors->has_errors() ) {
return $errors;
}

View File

@ -60,7 +60,7 @@ function delete_theme( $stylesheet, $redirect = '' ) {
return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
}
if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
return new WP_Error( 'fs_error', __( 'Filesystem error.' ), $wp_filesystem->errors );
}

View File

@ -207,7 +207,7 @@ function edit_user( $user_id = 0 ) {
*/
do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) );
if ( $errors->get_error_codes() ) {
if ( $errors->has_errors() ) {
return $errors;
}

View File

@ -44,7 +44,7 @@ if ( isset( $_REQUEST['action'] ) && 'add-user' == $_REQUEST['action'] ) {
$user = wp_unslash( $_POST['user'] );
$user_details = wpmu_validate_user_signup( $user['username'], $user['email'] );
if ( is_wp_error( $user_details['errors'] ) && ! empty( $user_details['errors']->errors ) ) {
if ( is_wp_error( $user_details['errors'] ) && $user_details['errors']->has_errors() ) {
$add_user_errors = $user_details['errors'];
} else {
$password = wp_generate_password( 12, false );

View File

@ -499,7 +499,7 @@ if ( ! WP_Filesystem( $credentials, ABSPATH, $allow_relaxed_file_ownership ) ) {
return;
}
if ( $wp_filesystem->errors->get_error_code() ) {
if ( $wp_filesystem->errors->has_errors() ) {
foreach ( $wp_filesystem->errors->get_error_messages() as $message ) {
show_message( $message );
}

View File

@ -160,7 +160,7 @@ Please click the following link to confirm the invite:
// Adding a new user to this site
$new_user_email = wp_unslash( $_REQUEST['email'] );
$user_details = wpmu_validate_user_signup( $_REQUEST['user_login'], $new_user_email );
if ( is_wp_error( $user_details['errors'] ) && ! empty( $user_details['errors']->errors ) ) {
if ( is_wp_error( $user_details['errors'] ) && $user_details['errors']->has_errors() ) {
$add_user_errors = $user_details['errors'];
} else {
/**

View File

@ -2302,7 +2302,7 @@ final class WP_Customize_Manager {
if ( ! is_wp_error( $validity ) ) {
/** This filter is documented in wp-includes/class-wp-customize-setting.php */
$late_validity = apply_filters( "customize_validate_{$setting->id}", new WP_Error(), $unsanitized_value, $setting );
if ( ! empty( $late_validity->errors ) ) {
if ( $late_validity->has_errors() ) {
$validity = $late_validity;
}
}

View File

@ -594,7 +594,7 @@ class WP_Customize_Setting {
*/
$validity = apply_filters( "customize_validate_{$this->id}", $validity, $value, $this );
if ( is_wp_error( $validity ) && empty( $validity->errors ) ) {
if ( is_wp_error( $validity ) && ! $validity->has_errors() ) {
$validity = true;
}
return $validity;

View File

@ -71,7 +71,7 @@ class WP_Error {
* @return array List of error codes, if available.
*/
public function get_error_codes() {
if ( empty( $this->errors ) ) {
if ( ! $this->has_errors() ) {
return array();
}
@ -161,6 +161,20 @@ class WP_Error {
}
}
/**
* Verify if the instance contains errors.
*
* @since 5.0.0
*
* @return bool
*/
public function has_errors() {
if ( ! empty( $this->errors ) ) {
return true;
}
return false;
}
/**
* Add an error or append additional message to an existing error.
*

View File

@ -161,7 +161,7 @@ final class WP_Customize_Custom_CSS_Setting extends WP_Customize_Setting {
$validity->add( 'illegal_markup', __( 'Markup is not allowed in CSS.' ) );
}
if ( empty( $validity->errors ) ) {
if ( ! $validity->has_errors() ) {
$validity = parent::validate( $css );
}
return $validity;

View File

@ -470,7 +470,7 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
if ( is_multisite() ) {
$ret = wpmu_validate_user_signup( $user->user_login, $user->user_email );
if ( is_wp_error( $ret['errors'] ) && ! empty( $ret['errors']->errors ) ) {
if ( is_wp_error( $ret['errors'] ) && $ret['errors']->has_errors() ) {
$error = new WP_Error( 'rest_invalid_param', __( 'Invalid user parameter(s).' ), array( 'status' => 400 ) );
foreach ( $ret['errors']->errors as $code => $messages ) {
foreach ( $messages as $message ) {

View File

@ -2469,7 +2469,7 @@ function register_new_user( $user_login, $user_email ) {
*/
$errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
if ( $errors->get_error_code() ) {
if ( $errors->has_errors() ) {
return $errors;
}

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.0-alpha-42760';
$wp_version = '5.0-alpha-42761';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -53,7 +53,7 @@ function login_header( $title = 'Log In', $message = '', $wp_error = '' ) {
*/
$shake_error_codes = apply_filters( 'shake_error_codes', $shake_error_codes );
if ( $shake_error_codes && $wp_error->get_error_code() && in_array( $wp_error->get_error_code(), $shake_error_codes ) ) {
if ( $shake_error_codes && $wp_error->has_errors() && in_array( $wp_error->get_error_code(), $shake_error_codes ) ) {
add_action( 'login_head', 'wp_shake_js', 12 );
}
@ -208,7 +208,7 @@ function login_header( $title = 'Log In', $message = '', $wp_error = '' ) {
unset( $error );
}
if ( $wp_error->get_error_code() ) {
if ( $wp_error->has_errors() ) {
$errors = '';
$messages = '';
foreach ( $wp_error->get_error_codes() as $code ) {
@ -341,7 +341,7 @@ function retrieve_password() {
*/
do_action( 'lostpassword_post', $errors );
if ( $errors->get_error_code() ) {
if ( $errors->has_errors() ) {
return $errors;
}
@ -687,7 +687,7 @@ switch ( $action ) {
*/
do_action( 'validate_password_reset', $errors, $user );
if ( ( ! $errors->get_error_code() ) && isset( $_POST['pass1'] ) && ! empty( $_POST['pass1'] ) ) {
if ( ( ! $errors->has_errors() ) && isset( $_POST['pass1'] ) && ! empty( $_POST['pass1'] ) ) {
reset_password( $user, $_POST['pass1'] );
setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
login_header( __( 'Password Reset' ), '<p class="message reset-pass">' . __( 'Your password has been reset.' ) . ' <a href="' . esc_url( wp_login_url() ) . '">' . __( 'Log in' ) . '</a></p>' );
@ -973,7 +973,7 @@ switch ( $action ) {
}
if ( $interim_login ) {
if ( ! $errors->get_error_code() ) {
if ( ! $errors->has_errors() ) {
$errors->add( 'expired', __( 'Your session has expired. Please log in to continue where you left off.' ), 'message' );
}
} else {
@ -1015,7 +1015,7 @@ switch ( $action ) {
}
$rememberme = ! empty( $_POST['rememberme'] );
if ( ! empty( $errors->errors ) ) {
if ( $errors->has_errors() ) {
$aria_describedby_error = ' aria-describedby="login_error"';
} else {
$aria_describedby_error = '';

View File

@ -323,7 +323,7 @@ function signup_another_blog( $blogname = '', $blog_title = '', $errors = '' ) {
echo '<h2>' . sprintf( __( 'Get <em>another</em> %s site in seconds' ), get_network()->site_name ) . '</h2>';
if ( $errors->get_error_code() ) {
if ( $errors->has_errors() ) {
echo '<p>' . __( 'There was a problem, please correct the form below and try again.' ) . '</p>';
}
?>
@ -389,7 +389,7 @@ function validate_another_blog_signup() {
$blog_title = $result['blog_title'];
$errors = $result['errors'];
if ( $errors->get_error_code() ) {
if ( $errors->has_errors() ) {
signup_another_blog( $blogname, $blog_title, $errors );
return false;
}
@ -604,7 +604,7 @@ function validate_user_signup() {
$user_email = $result['user_email'];
$errors = $result['errors'];
if ( $errors->get_error_code() ) {
if ( $errors->has_errors() ) {
signup_user( $user_name, $user_email, $errors );
return false;
}
@ -729,7 +729,7 @@ function validate_blog_signup() {
$user_email = $user_result['user_email'];
$user_errors = $user_result['errors'];
if ( $user_errors->get_error_code() ) {
if ( $user_errors->has_errors() ) {
signup_user( $user_name, $user_email, $user_errors );
return false;
}
@ -741,7 +741,7 @@ function validate_blog_signup() {
$blog_title = $result['blog_title'];
$errors = $result['errors'];
if ( $errors->get_error_code() ) {
if ( $errors->has_errors() ) {
signup_blog( $user_name, $user_email, $blogname, $blog_title, $errors );
return false;
}