Make WordPress Core

Changeset 42761


Ignore:
Timestamp:
02/27/2018 02:30:46 AM (9 years ago)
Author:
SergeyBiryukov
Message:

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

Props robdxw, DrewAPicture, SergeyBiryukov.
Fixes #42742.

Location:
trunk
Files:
21 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ajax-actions.php

    r42720 r42761  
    36933693                $status['errorMessage'] = $skin->result->get_error_message();
    36943694                wp_send_json_error( $status );
    3695         } elseif ( $skin->get_errors()->get_error_code() ) {
     3695        } elseif ( $skin->get_errors()->has_errors() ) {
    36963696                $status['errorMessage'] = $skin->get_error_messages();
    36973697                wp_send_json_error( $status );
     
    37033703
    37043704                // Pass through the error from WP_Filesystem if one was raised.
    3705                 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
     3705                if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
    37063706                        $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
    37073707                }
     
    38063806                $status['errorMessage'] = $skin->result->get_error_message();
    38073807                wp_send_json_error( $status );
    3808         } elseif ( $skin->get_errors()->get_error_code() ) {
     3808        } elseif ( $skin->get_errors()->has_errors() ) {
    38093809                $status['errorMessage'] = $skin->get_error_messages();
    38103810                wp_send_json_error( $status );
     
    38303830
    38313831                // Pass through the error from WP_Filesystem if one was raised.
    3832                 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
     3832                if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
    38333833                        $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
    38343834                }
     
    38923892
    38933893                // Pass through the error from WP_Filesystem if one was raised.
    3894                 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
     3894                if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
    38953895                        $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
    38963896                }
     
    39813981                $status['errorMessage'] = $skin->result->get_error_message();
    39823982                wp_send_json_error( $status );
    3983         } elseif ( $skin->get_errors()->get_error_code() ) {
     3983        } elseif ( $skin->get_errors()->has_errors() ) {
    39843984                $status['errorMessage'] = $skin->get_error_messages();
    39853985                wp_send_json_error( $status );
     
    39913991
    39923992                // Pass through the error from WP_Filesystem if one was raised.
    3993                 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
     3993                if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
    39943994                        $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
    39953995                }
     
    40824082                $status['errorMessage'] = $skin->result->get_error_message();
    40834083                wp_send_json_error( $status );
    4084         } elseif ( $skin->get_errors()->get_error_code() ) {
     4084        } elseif ( $skin->get_errors()->has_errors() ) {
    40854085                $status['errorMessage'] = $skin->get_error_messages();
    40864086                wp_send_json_error( $status );
     
    41164116
    41174117                // Pass through the error from WP_Filesystem if one was raised.
    4118                 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
     4118                if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
    41194119                        $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
    41204120                }
     
    41834183
    41844184                // Pass through the error from WP_Filesystem if one was raised.
    4185                 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
     4185                if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
    41864186                        $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
    41874187                }
  • trunk/src/wp-admin/includes/class-wp-ajax-upgrader-skin.php

    r42343 r42761  
    6262
    6363                foreach ( $this->errors->get_error_codes() as $error_code ) {
    64                         if ( $this->errors->get_error_data( $error_code ) && is_string( $this->errors->get_error_data( $error_code ) ) ) {
    65                                 $messages[] = $this->errors->get_error_message( $error_code ) . ' ' . esc_html( strip_tags( $this->errors->get_error_data( $error_code ) ) );
     64                        $error_data = $this->errors->get_error_data( $error_code );
     65
     66                        if ( $error_data && is_string( $error_data ) ) {
     67                                $messages[] = $this->errors->get_error_message( $error_code ) . ' ' . esc_html( strip_tags( $error_data ) );
    6668                        } else {
    6769                                $messages[] = $this->errors->get_error_message( $error_code );
  • trunk/src/wp-admin/includes/class-wp-upgrader-skin.php

    r42343 r42761  
    128128                if ( is_string( $errors ) ) {
    129129                        $this->feedback( $errors );
    130                 } elseif ( is_wp_error( $errors ) && $errors->get_error_code() ) {
     130                } elseif ( is_wp_error( $errors ) && $errors->has_errors() ) {
    131131                        foreach ( $errors->get_error_messages() as $message ) {
    132132                                if ( $errors->get_error_data() && is_string( $errors->get_error_data() ) ) {
  • trunk/src/wp-admin/includes/class-wp-upgrader.php

    r42745 r42761  
    192192                if ( ! WP_Filesystem( $credentials, $directories[0], $allow_relaxed_file_ownership ) ) {
    193193                        $error = true;
    194                         if ( is_object( $wp_filesystem ) && $wp_filesystem->errors->get_error_code() ) {
     194                        if ( is_object( $wp_filesystem ) && $wp_filesystem->errors->has_errors() ) {
    195195                                $error = $wp_filesystem->errors;
    196196                        }
     
    204204                }
    205205
    206                 if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
     206                if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
    207207                        return new WP_Error( 'fs_error', $this->strings['fs_error'], $wp_filesystem->errors );
    208208                }
  • trunk/src/wp-admin/includes/file.php

    r42719 r42761  
    14611461        }
    14621462
    1463         if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
     1463        if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
    14641464                return false;
    14651465        }
  • trunk/src/wp-admin/includes/plugin.php

    r42710 r42761  
    866866        }
    867867
    868         if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
     868        if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
    869869                return new WP_Error( 'fs_error', __( 'Filesystem error.' ), $wp_filesystem->errors );
    870870        }
  • trunk/src/wp-admin/includes/schema.php

    r42343 r42761  
    981981        }
    982982
    983         if ( $errors->get_error_code() ) {
     983        if ( $errors->has_errors() ) {
    984984                return $errors;
    985985        }
  • trunk/src/wp-admin/includes/theme.php

    r42711 r42761  
    6161        }
    6262
    63         if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
     63        if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
    6464                return new WP_Error( 'fs_error', __( 'Filesystem error.' ), $wp_filesystem->errors );
    6565        }
  • trunk/src/wp-admin/includes/user.php

    r42343 r42761  
    208208        do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) );
    209209
    210         if ( $errors->get_error_codes() ) {
     210        if ( $errors->has_errors() ) {
    211211                return $errors;
    212212        }
  • trunk/src/wp-admin/network/user-new.php

    r42343 r42761  
    4545
    4646        $user_details = wpmu_validate_user_signup( $user['username'], $user['email'] );
    47         if ( is_wp_error( $user_details['errors'] ) && ! empty( $user_details['errors']->errors ) ) {
     47        if ( is_wp_error( $user_details['errors'] ) && $user_details['errors']->has_errors() ) {
    4848                $add_user_errors = $user_details['errors'];
    4949        } else {
  • trunk/src/wp-admin/update-core.php

    r42631 r42761  
    500500}
    501501
    502 if ( $wp_filesystem->errors->get_error_code() ) {
     502if ( $wp_filesystem->errors->has_errors() ) {
    503503        foreach ( $wp_filesystem->errors->get_error_messages() as $message ) {
    504504                show_message( $message );
  • trunk/src/wp-admin/user-new.php

    r42719 r42761  
    161161                $new_user_email = wp_unslash( $_REQUEST['email'] );
    162162                $user_details   = wpmu_validate_user_signup( $_REQUEST['user_login'], $new_user_email );
    163                 if ( is_wp_error( $user_details['errors'] ) && ! empty( $user_details['errors']->errors ) ) {
     163                if ( is_wp_error( $user_details['errors'] ) && $user_details['errors']->has_errors() ) {
    164164                        $add_user_errors = $user_details['errors'];
    165165                } else {
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r42719 r42761  
    23032303                                /** This filter is documented in wp-includes/class-wp-customize-setting.php */
    23042304                                $late_validity = apply_filters( "customize_validate_{$setting->id}", new WP_Error(), $unsanitized_value, $setting );
    2305                                 if ( ! empty( $late_validity->errors ) ) {
     2305                                if ( $late_validity->has_errors() ) {
    23062306                                        $validity = $late_validity;
    23072307                                }
  • trunk/src/wp-includes/class-wp-customize-setting.php

    r42747 r42761  
    595595                $validity = apply_filters( "customize_validate_{$this->id}", $validity, $value, $this );
    596596
    597                 if ( is_wp_error( $validity ) && empty( $validity->errors ) ) {
     597                if ( is_wp_error( $validity ) && ! $validity->has_errors() ) {
    598598                        $validity = true;
    599599                }
  • trunk/src/wp-includes/class-wp-error.php

    r42343 r42761  
    7272         */
    7373        public function get_error_codes() {
    74                 if ( empty( $this->errors ) ) {
     74                if ( ! $this->has_errors() ) {
    7575                        return array();
    7676                }
     
    163163
    164164        /**
     165         * Verify if the instance contains errors.
     166         *
     167         * @since 5.0.0
     168         *
     169         * @return bool
     170         */
     171        public function has_errors() {
     172                if ( ! empty( $this->errors ) ) {
     173                        return true;
     174                }
     175                return false;
     176        }
     177
     178        /**
    165179         * Add an error or append additional message to an existing error.
    166180         *
  • trunk/src/wp-includes/customize/class-wp-customize-custom-css-setting.php

    r42343 r42761  
    162162                }
    163163
    164                 if ( empty( $validity->errors ) ) {
     164                if ( ! $validity->has_errors() ) {
    165165                        $validity = parent::validate( $css );
    166166                }
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r42343 r42761  
    471471                        $ret = wpmu_validate_user_signup( $user->user_login, $user->user_email );
    472472
    473                         if ( is_wp_error( $ret['errors'] ) && ! empty( $ret['errors']->errors ) ) {
     473                        if ( is_wp_error( $ret['errors'] ) && $ret['errors']->has_errors() ) {
    474474                                $error = new WP_Error( 'rest_invalid_param', __( 'Invalid user parameter(s).' ), array( 'status' => 400 ) );
    475475                                foreach ( $ret['errors']->errors as $code => $messages ) {
  • trunk/src/wp-includes/user.php

    r42710 r42761  
    24702470        $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
    24712471
    2472         if ( $errors->get_error_code() ) {
     2472        if ( $errors->has_errors() ) {
    24732473                return $errors;
    24742474        }
  • trunk/src/wp-login.php

    r42406 r42761  
    5454        $shake_error_codes = apply_filters( 'shake_error_codes', $shake_error_codes );
    5555
    56         if ( $shake_error_codes && $wp_error->get_error_code() && in_array( $wp_error->get_error_code(), $shake_error_codes ) ) {
     56        if ( $shake_error_codes && $wp_error->has_errors() && in_array( $wp_error->get_error_code(), $shake_error_codes ) ) {
    5757                add_action( 'login_head', 'wp_shake_js', 12 );
    5858        }
     
    209209        }
    210210
    211         if ( $wp_error->get_error_code() ) {
     211        if ( $wp_error->has_errors() ) {
    212212                $errors   = '';
    213213                $messages = '';
     
    342342        do_action( 'lostpassword_post', $errors );
    343343
    344         if ( $errors->get_error_code() ) {
     344        if ( $errors->has_errors() ) {
    345345                return $errors;
    346346        }
     
    688688                do_action( 'validate_password_reset', $errors, $user );
    689689
    690                 if ( ( ! $errors->get_error_code() ) && isset( $_POST['pass1'] ) && ! empty( $_POST['pass1'] ) ) {
     690                if ( ( ! $errors->has_errors() ) && isset( $_POST['pass1'] ) && ! empty( $_POST['pass1'] ) ) {
    691691                        reset_password( $user, $_POST['pass1'] );
    692692                        setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
     
    974974
    975975                if ( $interim_login ) {
    976                         if ( ! $errors->get_error_code() ) {
     976                        if ( ! $errors->has_errors() ) {
    977977                                $errors->add( 'expired', __( 'Your session has expired. Please log in to continue where you left off.' ), 'message' );
    978978                        }
     
    10161016                $rememberme = ! empty( $_POST['rememberme'] );
    10171017
    1018                 if ( ! empty( $errors->errors ) ) {
     1018                if ( $errors->has_errors() ) {
    10191019                        $aria_describedby_error = ' aria-describedby="login_error"';
    10201020                } else {
  • trunk/src/wp-signup.php

    r42343 r42761  
    324324        echo '<h2>' . sprintf( __( 'Get <em>another</em> %s site in seconds' ), get_network()->site_name ) . '</h2>';
    325325
    326         if ( $errors->get_error_code() ) {
     326        if ( $errors->has_errors() ) {
    327327                echo '<p>' . __( 'There was a problem, please correct the form below and try again.' ) . '</p>';
    328328        }
     
    390390        $errors     = $result['errors'];
    391391
    392         if ( $errors->get_error_code() ) {
     392        if ( $errors->has_errors() ) {
    393393                signup_another_blog( $blogname, $blog_title, $errors );
    394394                return false;
     
    605605        $errors     = $result['errors'];
    606606
    607         if ( $errors->get_error_code() ) {
     607        if ( $errors->has_errors() ) {
    608608                signup_user( $user_name, $user_email, $errors );
    609609                return false;
     
    730730        $user_errors = $user_result['errors'];
    731731
    732         if ( $user_errors->get_error_code() ) {
     732        if ( $user_errors->has_errors() ) {
    733733                signup_user( $user_name, $user_email, $user_errors );
    734734                return false;
     
    742742        $errors     = $result['errors'];
    743743
    744         if ( $errors->get_error_code() ) {
     744        if ( $errors->has_errors() ) {
    745745                signup_blog( $user_name, $user_email, $blogname, $blog_title, $errors );
    746746                return false;
  • trunk/tests/phpunit/tests/general/wpError.php

    r42646 r42761  
    377377
    378378        /**
     379         * @covers ::has_errors()
     380         */
     381        public function test_has_errors_with_no_errors_returns_false() {
     382                $this->assertFalse( $this->WP_Error->has_errors() );
     383        }
     384
     385        /**
     386         * @covers ::has_errors()
     387         */
     388        public function test_has_errors_with_errors_returns_true() {
     389                $this->WP_Error->add( 'code', 'message', 'data' );
     390                $this->assertTrue( $this->WP_Error->has_errors() );
     391        }
     392
     393        /**
    379394         * @covers ::add()
    380395         */
Note: See TracChangeset for help on using the changeset viewer.