Skip to:
Content

BuddyPress.org

Ticket #8066: 8066.2.patch

File 8066.2.patch, 8.9 KB (added by imath, 5 years ago)
  • src/bp-members/bp-members-functions.php

    diff --git src/bp-members/bp-members-functions.php src/bp-members/bp-members-functions.php
    index e986ca2f0..983f97812 100644
    function bp_core_validate_user_signup( $user_name, $user_email ) { 
    16741674        return apply_filters( 'bp_core_validate_user_signup', $result );
    16751675}
    16761676
     1677/**
     1678 * Validate a user password.
     1679 *
     1680 * @since 6.0.0
     1681 *
     1682 * @param string $pass         The password.
     1683 * @param string $confirm_pass The confirmed password.
     1684 * @return WP_Error A WP error object possibly containing error messages.
     1685 */
     1686function bp_members_validate_user_password( $pass, $confirm_pass ) {
     1687        $errors = new WP_Error();
     1688
     1689        if ( ! $pass || ! $confirm_pass ) {
     1690                $errors->add( 'missing_user_password', __( 'Please make sure you enter your password twice', 'buddypress' ) );
     1691        }
     1692
     1693        if ( $pass && $confirm_pass && $pass !== $confirm_pass ) {
     1694                $errors->add( 'mismatching_user_password', __( 'The passwords you entered do not match.', 'buddypress' ) );
     1695        }
     1696
     1697        /**
     1698         * Filter here to add password validate steps.
     1699         *
     1700         * @since 6.0.0
     1701         *
     1702         * @param WP_Error $errors       Password validation errors.
     1703         * @param string   $pass         The password.
     1704         * @param string   $confirm_pass The confirmed password.
     1705         */
     1706        return apply_filters( 'bp_members_validate_user_password', $errors, $pass, $confirm_pass );
     1707}
     1708
    16771709/**
    16781710 * Validate blog URL and title provided at signup.
    16791711 *
  • src/bp-members/screens/register.php

    diff --git src/bp-members/screens/register.php src/bp-members/screens/register.php
    index 8341f5266..f954f3f41 100644
    function bp_core_screen_signup() { 
    5959                $account_details = bp_core_validate_user_signup( $_POST['signup_username'], $_POST['signup_email'] );
    6060
    6161                // If there are errors with account details, set them for display.
    62                 if ( !empty( $account_details['errors']->errors['user_name'] ) )
     62                if ( ! empty( $account_details['errors']->errors['user_name'] ) ) {
    6363                        $bp->signup->errors['signup_username'] = $account_details['errors']->errors['user_name'][0];
     64                }
    6465
    65                 if ( !empty( $account_details['errors']->errors['user_email'] ) )
     66                if ( ! empty( $account_details['errors']->errors['user_email'] ) ) {
    6667                        $bp->signup->errors['signup_email'] = $account_details['errors']->errors['user_email'][0];
     68                }
    6769
    68                 // Check that both password fields are filled in.
    69                 if ( empty( $_POST['signup_password'] ) || empty( $_POST['signup_password_confirm'] ) )
    70                         $bp->signup->errors['signup_password'] = __( 'Please make sure you enter your password twice', 'buddypress' );
     70                $signup_pass = '';
     71                if ( isset( $_POST['signup_password'] ) ) {
     72                        $signup_pass = wp_unslash( $_POST['signup_password'] );
     73                }
    7174
    72                 // Check that the passwords match.
    73                 if ( ( !empty( $_POST['signup_password'] ) && !empty( $_POST['signup_password_confirm'] ) ) && $_POST['signup_password'] != $_POST['signup_password_confirm'] )
    74                         $bp->signup->errors['signup_password'] = __( 'The passwords you entered do not match.', 'buddypress' );
     75                $signup_pass_confirm = '';
     76                if ( isset( $_POST['signup_password_confirm'] ) ) {
     77                        $signup_pass_confirm = wp_unslash( $_POST['signup_password_confirm'] );
     78                }
     79
     80                // Check the account password for problems.
     81                $account_password = bp_members_validate_user_password( $signup_pass, $signup_pass_confirm );
     82                $password_error   = $account_password->get_error_message();
     83
     84                if ( $password_error ) {
     85                        $bp->signup->errors['signup_password'] = $password_error;
     86                }
    7587
    7688                if ( bp_signup_requires_privacy_policy_acceptance() && ! empty( $_POST['signup-privacy-policy-check'] ) && empty( $_POST['signup-privacy-policy-accept'] ) ) {
    7789                        $bp->signup->errors['signup_privacy_policy'] = __( 'You must indicate that you have read and agreed to the Privacy Policy.', 'buddypress' );
    function bp_core_screen_signup() { 
    237249         */
    238250        bp_core_load_template( apply_filters( 'bp_core_template_register', array( 'register', 'registration/register' ) ) );
    239251}
    240 add_action( 'bp_screens', 'bp_core_screen_signup' );
    241  No newline at end of file
     252add_action( 'bp_screens', 'bp_core_screen_signup' );
  • src/bp-settings/actions/general.php

    diff --git src/bp-settings/actions/general.php src/bp-settings/actions/general.php
    index 62a6c9e9c..88bf9ac7c 100644
    function bp_settings_action_general() { 
    130130
    131131                /* Password Change Attempt ***************************************/
    132132
    133                 if ( !empty( $_POST['pass1'] ) && !empty( $_POST['pass2'] ) ) {
    134 
    135                         if ( ( $_POST['pass1'] == $_POST['pass2'] ) && !strpos( " " . wp_unslash( $_POST['pass1'] ), "\\" ) ) {
     133                if ( ! empty( $_POST['pass1'] ) && ! empty( $_POST['pass2'] ) ) {
     134                        $pass         = wp_unslash( $_POST['pass1'] );
     135                        $pass_confirm = wp_unslash( $_POST['pass2'] );
     136                        $pass_error   = bp_members_validate_user_password( $pass, $pass_confirm );
    136137
     138                        if ( ! $pass_error->get_error_message() ) {
    137139                                // Password change attempt is successful.
    138                                 if ( ( ! empty( $_POST['pwd'] ) && $_POST['pwd'] != $_POST['pass1'] ) || is_super_admin() )  {
     140                                if ( ( ! empty( $_POST['pwd'] ) && wp_unslash( $_POST['pwd'] ) !== $pass ) || is_super_admin() )  {
    139141                                        $update_user->user_pass = $_POST['pass1'];
     142                                        $pass_error   = false;
    140143                                        $pass_changed = true;
    141144
    142145                                // The new password is the same as the current password.
    143146                                } else {
    144                                         $pass_error = 'same';
     147                                        $pass_error->add( 'same_user_password', __( 'The new password must be different from the current password.', 'buddypress' ) );
    145148                                }
    146 
    147                         // Password change attempt was unsuccessful.
    148                         } else {
    149                                 $pass_error = 'mismatch';
    150149                        }
    151150
    152151                // Both password fields were empty.
    function bp_settings_action_general() { 
    154153                        $pass_error = false;
    155154
    156155                // One of the password boxes was left empty.
    157                 } elseif ( ( empty( $_POST['pass1'] ) && !empty( $_POST['pass2'] ) ) || ( !empty( $_POST['pass1'] ) && empty( $_POST['pass2'] ) ) ) {
    158                         $pass_error = 'empty';
     156                } elseif ( ( empty( $_POST['pass1'] ) && ! empty( $_POST['pass2'] ) ) || ( ! empty( $_POST['pass1'] ) && empty( $_POST['pass2'] ) ) ) {
     157                        $pass_error = new WP_Error( 'empty_user_password', __( 'One of the password fields was empty.', 'buddypress' ) );
    159158                }
    160159
    161160                // The structure of the $update_user object changed in WP 3.3, but
    function bp_settings_action_general() { 
    180179
    181180        // Password Error.
    182181        } else {
    183                 $pass_error = 'invalid';
     182                $pass_error = new WP_Error( 'invalid_user_password', __( 'Your current password is invalid.', 'buddypress' ) );
    184183        }
    185184
    186185        // Email feedback.
    function bp_settings_action_general() { 
    202201                        break;
    203202        }
    204203
    205         // Password feedback.
    206         switch ( $pass_error ) {
    207                 case 'invalid' :
    208                         $feedback['pass_error']    = __( 'Your current password is invalid.', 'buddypress' );
    209                         break;
    210                 case 'mismatch' :
    211                         $feedback['pass_mismatch'] = __( 'The new password fields did not match.', 'buddypress' );
    212                         break;
    213                 case 'empty' :
    214                         $feedback['pass_empty']    = __( 'One of the password fields was empty.', 'buddypress' );
    215                         break;
    216                 case 'same' :
    217                         $feedback['pass_same']     = __( 'The new password must be different from the current password.', 'buddypress' );
    218                         break;
    219                 case false :
    220                         // No change.
    221                         break;
     204        if ( is_wp_error( $pass_error ) && $pass_error->get_error_message() ) {
     205                $feedback[ $pass_error->get_error_code() ] = $pass_error->get_error_message();
    222206        }
    223207
    224208        // No errors so show a simple success message.
  • tests/phpunit/testcases/members/functions.php

    diff --git tests/phpunit/testcases/members/functions.php tests/phpunit/testcases/members/functions.php
    index e2492116c..d02db8682 100644
    class BP_Tests_Members_Functions extends BP_UnitTestCase { 
    495495                $this->assertSame( bp_get_signup_page(), wp_registration_url() );
    496496        }
    497497
     498        /**
     499         * @group bp_members_validate_user_password
     500         */
     501        public function test_bp_members_validate_user_password() {
     502                $validate = bp_members_validate_user_password( 'foobar', 'foobar' );
     503
     504                $this->assertEmpty( $validate->get_error_message() );
     505        }
     506
     507        /**
     508         * @group bp_members_validate_user_password
     509         */
     510        public function test_bp_members_validate_user_password_missing() {
     511                $validate = bp_members_validate_user_password( '', '' );
     512
     513                $this->assertEquals( 'missing_user_password', $validate->get_error_code() );
     514
     515                $validate = bp_members_validate_user_password( 'foobar', '' );
     516
     517                $this->assertEquals( 'missing_user_password', $validate->get_error_code() );
     518
     519                $validate = bp_members_validate_user_password( '', 'foobar' );
     520
     521                $this->assertEquals( 'missing_user_password', $validate->get_error_code() );
     522        }
     523
     524        /**
     525         * @group bp_members_validate_user_password
     526         */
     527        public function test_bp_members_validate_user_password_mismatching() {
     528                $validate = bp_members_validate_user_password( 'foobar', 'barfoo' );
     529
     530                $this->assertEquals( 'mismatching_user_password', $validate->get_error_code() );
     531        }
     532
    498533        /**
    499534         * @group bp_core_activate_signup
    500535         */