roles, $roles ) ) ) { return null; } return array( 'externalId' => self::external_id( (int) $user->ID ), 'name' => mb_substr( (string) $user->display_name, 0, 120 ), ); } /** * Private ID for a WordPress user. * * @param int $user_id User ID. */ public static function external_id( int $user_id ): string { return 'wp_' . substr( hash_hmac( 'sha256', 'user:' . $user_id, self::secret() ), 0, 32 ); } /** * The site's identity secret, created on first use. */ private static function secret(): string { $secret = get_option( self::SECRET_OPTION, '' ); if ( ! is_string( $secret ) || strlen( $secret ) < 32 ) { // Two requests making one at once both use the one stored first, // so a visitor gets one ID. $stored = Option_Queue::stored( self::SECRET_OPTION ); $new = wp_generate_password( 64, true, true ); if ( null === $stored ) { Option_Queue::add( self::SECRET_OPTION, $new ); } elseif ( strlen( $stored ) < 32 ) { Option_Queue::swap( self::SECRET_OPTION, $stored, $new ); } $secret = (string) Option_Queue::stored( self::SECRET_OPTION ); } return $secret; } }