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
|
b
|
function validate_username( $username ) { |
| 1492 | 1492 | * @since 4.7.0 The user's locale can be passed to `$userdata`. |
| 1493 | 1493 | * @since 5.3.0 The `user_activation_key` field can be passed to `$userdata`. |
| 1494 | 1494 | * @since 5.3.0 The `spam` field can be passed to `$userdata` (Multisite only). |
| | 1495 | * @since 5.5.0 Add `import_id`. |
| 1495 | 1496 | * |
| 1496 | 1497 | * @global wpdb $wpdb WordPress database abstraction object. |
| 1497 | 1498 | * |
| … |
… |
function validate_username( $username ) { |
| 1536 | 1537 | * as a string literal, not boolean. Default 'true'. |
| 1537 | 1538 | * @type string $role User's role. |
| 1538 | 1539 | * @type string $locale User's locale. Default empty. |
| | 1540 | * @type int $import_id Suggested Uer ID, use it if not already present. |
| 1539 | 1541 | * } |
| 1540 | 1542 | * @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not |
| 1541 | 1543 | * be created. |
| … |
… |
function wp_insert_user( $userdata ) { |
| 1633 | 1635 | |
| 1634 | 1636 | $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 ) ); |
| 1635 | 1637 | |
| | 1638 | $import_id = empty( $userdata['import_id'] ) ? 0 : absint( $userdata['import_id'] ); |
| | 1639 | |
| 1636 | 1640 | if ( $user_nicename_check ) { |
| 1637 | 1641 | $suffix = 2; |
| 1638 | 1642 | while ( $user_nicename_check ) { |
| … |
… |
function wp_insert_user( $userdata ) { |
| 1823 | 1827 | $wpdb->update( $wpdb->users, $data, compact( 'ID' ) ); |
| 1824 | 1828 | $user_id = (int) $ID; |
| 1825 | 1829 | } else { |
| | 1830 | // If there is a suggested ID, use it if not already present. |
| | 1831 | if ( ! empty( $import_id ) ) { |
| | 1832 | if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE ID = %d", $import_id ) ) ) { |
| | 1833 | $data['ID'] = $import_id; |
| | 1834 | } |
| | 1835 | } |
| 1826 | 1836 | $wpdb->insert( $wpdb->users, $data ); |
| 1827 | 1837 | $user_id = (int) $wpdb->insert_id; |
| 1828 | 1838 | } |
diff --git a/tests/phpunit/tests/user.php b/tests/phpunit/tests/user.php
index d3e9a581e5..8f60d49a74 100644
|
a
|
b
|
function test_user_meta_error() { |
| 644 | 644 | $this->assertNotContains( 'key', $metas ); |
| 645 | 645 | } |
| 646 | 646 | |
| | 647 | /** |
| | 648 | * @ticket 37818 |
| | 649 | */ |
| | 650 | function test_insert_user_with_import_id() { |
| | 651 | $user_1 = wp_insert_user( |
| | 652 | array( |
| | 653 | 'import_id' => 72, |
| | 654 | 'user_login' => 'user_log_1', |
| | 655 | 'user_pass' => 'user_pass_1', |
| | 656 | 'user_email' => 'user1@pass.com', |
| | 657 | ) |
| | 658 | ); |
| | 659 | $this->assertEquals( $user_1, 72 ); |
| | 660 | |
| | 661 | $user_2 = wp_insert_user( |
| | 662 | array( |
| | 663 | 'import_id' => 88, |
| | 664 | 'user_login' => 'user_log_2', |
| | 665 | 'user_pass' => 'user_pass_2', |
| | 666 | 'user_email' => 'user2@pass.com', |
| | 667 | ) |
| | 668 | ); |
| | 669 | $this->assertEquals( $user_2, 88 ); |
| | 670 | } |
| | 671 | |
| 647 | 672 | /** |
| 648 | 673 | * @ticket 30647 |
| 649 | 674 | */ |