Skip to:
Content

BuddyPress.org

Ticket #8066: 8066.patch

File 8066.patch, 9.1 KB (added by tharsheblows, 5 years ago)

filter with user object

  • src/bp-members/bp-members-functions.php

    diff --git a/src/bp-members/bp-members-functions.php b/src/bp-members/bp-members-functions.php
    index caa8e319e..24f3f4ad2 100644
    a b function bp_core_validate_user_signup( $user_name, $user_email ) { 
    17331733        return apply_filters( 'bp_core_validate_user_signup', $result );
    17341734}
    17351735
     1736/**
     1737 * Validate a user password.
     1738 *
     1739 * @since 6.0.0
     1740 *
     1741 * @param string  $pass         The password.
     1742 * @param string  $confirm_pass The confirmed password.
     1743 * @param WP_User $userdata     The user object if there is one.
     1744 * @return WP_Error A WP error object possibly containing error messages.
     1745 */
     1746function bp_members_validate_user_password( $pass, $confirm_pass, $userdata = false ) {
     1747        $errors = new WP_Error();
     1748
     1749        if ( ! $pass || ! $confirm_pass ) {
     1750                $errors->add( 'missing_user_password', __( 'Please make sure you enter your password twice', 'buddypress' ) );
     1751        }
     1752
     1753        if ( $pass && $confirm_pass && $pass !== $confirm_pass ) {
     1754                $errors->add( 'mismatching_user_password', __( 'The passwords you entered do not match.', 'buddypress' ) );
     1755        }
     1756
     1757        /**
     1758         * Filter here to add password validate steps.
     1759         *
     1760         * @since 6.0.0
     1761         *
     1762         * @param WP_Error $errors       Password validation errors.
     1763         * @param string   $pass         The password.
     1764         * @param string   $confirm_pass The confirmed password.
     1765         */
     1766        return apply_filters( 'bp_members_validate_user_password', $errors, $pass, $confirm_pass, $userdata );
     1767}
     1768
    17361769/**
    17371770 * Validate blog URL and title provided at signup.
    17381771 *
  • src/bp-members/screens/register.php

    diff --git a/src/bp-members/screens/register.php b/src/bp-members/screens/register.php
    index 8341f5266..9e79f113d 100644
    a b 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. On signup there is no user object yet.
     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 a/src/bp-settings/actions/general.php b/src/bp-settings/actions/general.php
    index 6de596d6f..9f90c227b 100644
    a b 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, $update_user );
    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 a/tests/phpunit/testcases/members/functions.php b/tests/phpunit/testcases/members/functions.php
    index b5fcf9347..2de31db25 100644
    a b class BP_Tests_Members_Functions extends BP_UnitTestCase { 
    555555                $this->assertSame( bp_get_signup_page(), wp_registration_url() );
    556556        }
    557557
     558        /**
     559         * @group bp_members_validate_user_password
     560         */
     561        public function test_bp_members_validate_user_password() {
     562                $validate = bp_members_validate_user_password( 'foobar', 'foobar' );
     563
     564                $this->assertEmpty( $validate->get_error_message() );
     565        }
     566
     567        /**
     568         * @group bp_members_validate_user_password
     569         */
     570        public function test_bp_members_validate_user_password_missing() {
     571                $validate = bp_members_validate_user_password( '', '' );
     572
     573                $this->assertEquals( 'missing_user_password', $validate->get_error_code() );
     574
     575                $validate = bp_members_validate_user_password( 'foobar', '' );
     576
     577                $this->assertEquals( 'missing_user_password', $validate->get_error_code() );
     578
     579                $validate = bp_members_validate_user_password( '', 'foobar' );
     580
     581                $this->assertEquals( 'missing_user_password', $validate->get_error_code() );
     582        }
     583
     584        /**
     585         * @group bp_members_validate_user_password
     586         */
     587        public function test_bp_members_validate_user_password_mismatching() {
     588                $validate = bp_members_validate_user_password( 'foobar', 'barfoo' );
     589
     590                $this->assertEquals( 'mismatching_user_password', $validate->get_error_code() );
     591        }
     592
    558593        /**
    559594         * @group bp_core_activate_signup
    560595         */