Changeset 51738
- Timestamp:
- 09/07/2021 09:30:07 PM (3 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/user.php
r51657 r51738 1712 1712 * @since 3.6.0 The `aim`, `jabber`, and `yim` fields were removed as default user contact 1713 1713 * methods for new installations. See wp_get_user_contact_methods(). 1714 * @since 4.7.0 The user's localecan be passed to `$userdata`.1714 * @since 4.7.0 The `locale` field can be passed to `$userdata`. 1715 1715 * @since 5.3.0 The `user_activation_key` field can be passed to `$userdata`. 1716 1716 * @since 5.3.0 The `spam` field can be passed to `$userdata` (Multisite only). 1717 * @since 5.9.0 The `meta_input` field can be passed to `$userdata` to allow addition of user meta data. 1717 1718 * 1718 1719 * @global wpdb $wpdb WordPress database abstraction object. … … 1759 1760 * @type string $role User's role. 1760 1761 * @type string $locale User's locale. Default empty. 1762 * @type array $meta_input Array of custom user meta values keyed by meta key. 1763 * Default empty. 1761 1764 * } 1762 1765 * @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not … … 2058 2061 * 2059 2062 * Does not include contact methods. These are added using `wp_get_user_contact_methods( $user )`. 2063 * 2064 * For custom meta fields, see the {@see 'insert_custom_user_meta'} filter. 2060 2065 * 2061 2066 * @since 4.4.0 … … 2084 2089 */ 2085 2090 $meta = apply_filters( 'insert_user_meta', $meta, $user, $update, $userdata ); 2091 2092 $custom_meta = array(); 2093 if ( array_key_exists( 'meta_input', $userdata ) && is_array( $userdata['meta_input'] ) && ! empty( $userdata['meta_input'] ) ) { 2094 $custom_meta = $userdata['meta_input']; 2095 } 2096 2097 /** 2098 * Filters a user's custom meta values and keys immediately after the user is created or updated 2099 * and before any user meta is inserted or updated. 2100 * 2101 * For non-custom meta fields, see the {@see 'insert_user_meta'} filter. 2102 * 2103 * @since 5.9.0 2104 * 2105 * @param array $custom_meta Array of custom user meta values keyed by meta key. 2106 * @param WP_User $user User object. 2107 * @param bool $update Whether the user is being updated rather than created. 2108 * @param array $userdata The raw array of data passed to wp_insert_user(). 2109 */ 2110 $custom_meta = apply_filters( 'insert_custom_user_meta', $custom_meta, $user, $update, $userdata ); 2111 2112 $meta = array_merge( $meta, $custom_meta ); 2086 2113 2087 2114 // Update user meta. -
trunk/tests/phpunit/tests/user.php
r51568 r51738 1847 1847 1848 1848 /** 1849 * Test `$user_data['meta_input']` args in `wp_insert_user( $user_data )`. 1850 * 1851 * @ticket 41950 1852 */ 1853 public function test_wp_insert_user_with_meta() { 1854 $user_data = array( 1855 'user_login' => 'test_user', 1856 'user_pass' => 'test_password', 1857 'user_email' => 'user@example.com', 1858 'meta_input' => array( 1859 'test_meta_key' => 'test_meta_value', 1860 'custom_meta' => 'custom_value', 1861 ), 1862 ); 1863 $create_user = wp_insert_user( $user_data ); 1864 1865 $this->assertSame( 'test_meta_value', get_user_meta( $create_user, 'test_meta_key', true ) ); 1866 $this->assertSame( 'custom_value', get_user_meta( $create_user, 'custom_meta', true ) ); 1867 1868 // Update the user meta thru wp_insert_user. 1869 $update_data = array( 1870 'ID' => $create_user, 1871 'user_login' => 'test_user', 1872 'meta_input' => array( 1873 'test_meta_key' => 'test_meta_updated', 1874 'custom_meta' => 'updated_value', 1875 'new_meta_k' => 'new_meta_val', 1876 ), 1877 ); 1878 $update_user = wp_insert_user( $update_data ); 1879 1880 $this->assertSame( 'test_meta_updated', get_user_meta( $update_user, 'test_meta_key', true ) ); 1881 $this->assertSame( 'updated_value', get_user_meta( $update_user, 'custom_meta', true ) ); 1882 $this->assertSame( 'new_meta_val', get_user_meta( $update_user, 'new_meta_k', true ) ); 1883 1884 // Create new user. 1885 $new_user_data = array( 1886 'user_login' => 'new_test', 1887 'user_pass' => 'new_password', 1888 'user_email' => 'new_user@newexample.com', 1889 'meta_input' => array( 1890 'test_meta_key' => 'test_meta_value', 1891 'custom_meta' => 'new_user_custom_value', 1892 ), 1893 ); 1894 1895 // Hook filter 1896 add_filter( 'insert_custom_user_meta', array( $this, 'filter_custom_meta' ) ); 1897 1898 $new_user = wp_insert_user( $new_user_data ); 1899 1900 // This meta is updated by the filter. 1901 $this->assertSame( 'update_from_filter', get_user_meta( $new_user, 'test_meta_key', true ) ); 1902 $this->assertSame( 'new_user_custom_value', get_user_meta( $new_user, 'custom_meta', true ) ); 1903 // This meta is inserted by the filter. 1904 $this->assertSame( 'new_from_filter', get_user_meta( $new_user, 'new_meta_from_filter', true ) ); 1905 } 1906 1907 /** 1908 * Hook a filter to alter custom meta when inserting new user. 1909 * This hook is used in `test_wp_insert_user_with_meta()`. 1910 */ 1911 public function filter_custom_meta( $meta_input ) { 1912 // Update some meta inputs 1913 $meta_input['test_meta_key'] = 'update_from_filter'; 1914 // Add a new meta 1915 $meta_input['new_meta_from_filter'] = 'new_from_filter'; 1916 1917 return $meta_input; 1918 } 1919 1920 /** 1849 1921 * Testing the `wp_privacy_additional_user_profile_data` filter works. 1850 1922 *
Note: See TracChangeset
for help on using the changeset viewer.