From 9ebb4bbc36b2ae0ad2fc0cdfe8c48c6c8cb370fb Mon Sep 17 00:00:00 2001
From: Michael Joseph Panaga <donmhico@gmail.com>
Date: Sat, 18 Jul 2020 01:15:17 +0800
Subject: [PATCH] Add  to allow creation of user with pre-given ID

---
 src/wp-includes/user.php     | 10 ++++++++++
 tests/phpunit/tests/user.php | 25 +++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php
index 63047c7219..b306a4e036 100644
--- a/src/wp-includes/user.php
+++ b/src/wp-includes/user.php
@@ -1492,6 +1492,7 @@ function validate_username( $username ) {
  * @since 4.7.0 The user's locale can be passed to `$userdata`.
  * @since 5.3.0 The `user_activation_key` field can be passed to `$userdata`.
  * @since 5.3.0 The `spam` field can be passed to `$userdata` (Multisite only).
+ * @since 5.5.0 Add `import_id`.
  *
  * @global wpdb $wpdb WordPress database abstraction object.
  *
@@ -1536,6 +1537,7 @@ function validate_username( $username ) {
  *                                        as a string literal, not boolean. Default 'true'.
  *     @type string $role                 User's role.
  *     @type string $locale               User's locale. Default empty.
+ *     @type int    $import_id            Suggested Uer ID, use it if not already present.
  * }
  * @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not
  *                      be created.
@@ -1633,6 +1635,8 @@ function wp_insert_user( $userdata ) {
 
 	$user_nicename_check = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1", $user_nicename, $user_login ) );
 
+	$import_id = empty( $userdata['import_id'] ) ? 0 : absint( $userdata['import_id'] );
+
 	if ( $user_nicename_check ) {
 		$suffix = 2;
 		while ( $user_nicename_check ) {
@@ -1823,6 +1827,12 @@ function wp_insert_user( $userdata ) {
 		$wpdb->update( $wpdb->users, $data, compact( 'ID' ) );
 		$user_id = (int) $ID;
 	} else {
+		// If there is a suggested ID, use it if not already present.
+        if ( ! empty( $import_id ) ) {
+            if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE ID = %d", $import_id ) ) ) {
+				$data['ID'] = $import_id;
+			}
+        }
 		$wpdb->insert( $wpdb->users, $data );
 		$user_id = (int) $wpdb->insert_id;
 	}
diff --git a/tests/phpunit/tests/user.php b/tests/phpunit/tests/user.php
index d3e9a581e5..8f60d49a74 100644
--- a/tests/phpunit/tests/user.php
+++ b/tests/phpunit/tests/user.php
@@ -644,6 +644,31 @@ function test_user_meta_error() {
 		$this->assertNotContains( 'key', $metas );
 	}
 
+	/**
+	 * @ticket 37818
+	 */
+	function test_insert_user_with_import_id() {
+		$user_1 = wp_insert_user(
+			array(
+				'import_id'  => 72,
+				'user_login' => 'user_log_1',
+				'user_pass'  => 'user_pass_1',
+				'user_email' => 'user1@pass.com',
+			)
+		);
+		$this->assertEquals( $user_1, 72 );
+
+		$user_2 = wp_insert_user(
+			array(
+				'import_id'  => 88,
+				'user_login' => 'user_log_2',
+				'user_pass'  => 'user_pass_2',
+				'user_email' => 'user2@pass.com',
+			)
+		);
+		$this->assertEquals( $user_2, 88 );
+	}
+
 	/**
 	 * @ticket 30647
 	 */
