Append random number to reduce recusion

This commit is contained in:
Mike Jolley 2019-03-27 12:22:15 +00:00
parent 2b8f8e38d1
commit ad49085571
2 changed files with 12 additions and 6 deletions

View File

@ -114,10 +114,10 @@ if ( ! function_exists( 'wc_create_new_customer' ) ) {
* @since 3.6.0 * @since 3.6.0
* @param string $email New customer email address. * @param string $email New customer email address.
* @param array $new_user_args Array of new user args, maybe including first and last names. * @param array $new_user_args Array of new user args, maybe including first and last names.
* @param int $suffix Append number to username to make it more unique. * @param string $suffix Append string to username to make it unique.
* @return string Generated username. * @return string Generated username.
*/ */
function wc_create_new_customer_username( $email, $new_user_args, $suffix = 0 ) { function wc_create_new_customer_username( $email, $new_user_args, $suffix = '' ) {
$username_parts = array(); $username_parts = array();
if ( isset( $new_user_args['first_name'] ) ) { if ( isset( $new_user_args['first_name'] ) ) {
@ -162,7 +162,9 @@ function wc_create_new_customer_username( $email, $new_user_args, $suffix = 0 )
} }
if ( username_exists( $username ) ) { if ( username_exists( $username ) ) {
return wc_create_new_customer_username( $email, $new_user_args, ++$suffix ); // Generate something unique to append to the username in case of a conflict with another user.
$suffix = '-' . zeroise( wp_rand( 0, 9999 ), 4 );
return wc_create_new_customer_username( $email, $new_user_args, $suffix );
} }
return $username; return $username;

View File

@ -44,10 +44,12 @@ class WC_Tests_Customer_Functions extends WC_Unit_Test_Case {
$this->assertEquals( 'fred', $userdata->user_login ); $this->assertEquals( 'fred', $userdata->user_login );
$id = wc_create_new_customer( 'fred@mail.com', '', 'testpassword' ); $id = wc_create_new_customer( 'fred@mail.com', '', 'testpassword' );
$userdata = get_userdata( $id ); $userdata = get_userdata( $id );
$this->assertEquals( 'fred1', $userdata->user_login ); $this->assertNotEquals( 'fred', $userdata->user_login );
$this->assertContains( 'fred', $userdata->user_login );
$id = wc_create_new_customer( 'fred@test.com', '', 'testpassword' ); $id = wc_create_new_customer( 'fred@test.com', '', 'testpassword' );
$userdata = get_userdata( $id ); $userdata = get_userdata( $id );
$this->assertEquals( 'fred2', $userdata->user_login ); $this->assertNotEquals( 'fred', $userdata->user_login );
$this->assertContains( 'fred', $userdata->user_login );
// Test extra arguments to generate display_name. // Test extra arguments to generate display_name.
$id = wc_create_new_customer( $id = wc_create_new_customer(
@ -82,7 +84,9 @@ class WC_Tests_Customer_Functions extends WC_Unit_Test_Case {
// Test getting name if username exists. // Test getting name if username exists.
wc_create_new_customer( 'mike@fakemail.com', '', 'testpassword' ); wc_create_new_customer( 'mike@fakemail.com', '', 'testpassword' );
$this->assertEquals( 'mike1', wc_create_new_customer_username( 'mike@fakemail.com', array() ) ); $username = wc_create_new_customer_username( 'mike@fakemail.com', array() );
$this->assertNotEquals( 'mike', $username, $username );
$this->assertContains( 'mike', $username, $username );
// Test common email prefix avoidance. // Test common email prefix avoidance.
$this->assertEquals( 'somecompany.com', wc_create_new_customer_username( 'info@somecompany.com', array() ) ); $this->assertEquals( 'somecompany.com', wc_create_new_customer_username( 'info@somecompany.com', array() ) );