mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 12:25:15 +08:00
Generate unique id based on the length of the prefix
This commit is contained in:
parent
5e10d137db
commit
82727ac2a1
1 changed files with 16 additions and 1 deletions
|
@ -13,6 +13,7 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Repository;
|
||||||
* Class CustomerRepository
|
* Class CustomerRepository
|
||||||
*/
|
*/
|
||||||
class CustomerRepository {
|
class CustomerRepository {
|
||||||
|
const CLIENT_ID_MAX_LENGTH = 22;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The prefix.
|
* The prefix.
|
||||||
|
@ -37,13 +38,14 @@ class CustomerRepository {
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function customer_id_for_user( int $user_id ): string {
|
public function customer_id_for_user( int $user_id ): string {
|
||||||
|
$unique_id = $this->generate_unique_id();
|
||||||
if ( 0 === $user_id ) {
|
if ( 0 === $user_id ) {
|
||||||
$guest_customer_id = WC()->session->get( 'ppcp_guest_customer_id' );
|
$guest_customer_id = WC()->session->get( 'ppcp_guest_customer_id' );
|
||||||
if ( is_string( $guest_customer_id ) && $guest_customer_id ) {
|
if ( is_string( $guest_customer_id ) && $guest_customer_id ) {
|
||||||
return $guest_customer_id;
|
return $guest_customer_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
$unique_id = $this->prefix . uniqid();
|
$unique_id = $this->generate_unique_id();
|
||||||
WC()->session->set( 'ppcp_guest_customer_id', $unique_id );
|
WC()->session->set( 'ppcp_guest_customer_id', $unique_id );
|
||||||
|
|
||||||
return $unique_id;
|
return $unique_id;
|
||||||
|
@ -56,4 +58,17 @@ class CustomerRepository {
|
||||||
|
|
||||||
return $this->prefix . (string) $user_id;
|
return $this->prefix . (string) $user_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a unique id based on the length of the prefix.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function generate_unique_id(): string {
|
||||||
|
$offset = self::CLIENT_ID_MAX_LENGTH - strlen( $this->prefix );
|
||||||
|
|
||||||
|
return strlen( uniqid() ) > $offset
|
||||||
|
? $this->prefix . substr( uniqid(), $offset )
|
||||||
|
: $this->prefix . uniqid();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue