<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[51738] trunk: Users: Introduce a `meta_input` argument for `wp_insert_user()`.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/51738">51738</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/51738","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>johnbillion</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-09-07 21:30:07 +0000 (Tue, 07 Sep 2021)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Users: Introduce a `meta_input` argument for `wp_insert_user()`.

This allows custom user meta values to be provided when creating or updating a user in the same way custom post meta can be provided to `wp_insert_post()` when creating or updating a post.

Also introduces the `insert_custom_user_meta` to filter these values.

Props desrosj, donmhico

Fixes <a href="https://core.trac.wordpress.org/ticket/41950">#41950</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesuserphp">trunk/src/wp-includes/user.php</a></li>
<li><a href="#trunktestsphpunittestsuserphp">trunk/tests/phpunit/tests/user.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesuserphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/user.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/user.php    2021-09-07 19:23:00 UTC (rev 51737)
+++ trunk/src/wp-includes/user.php      2021-09-07 21:30:07 UTC (rev 51738)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1711,9 +1711,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 2.0.0
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 3.6.0 The `aim`, `jabber`, and `yim` fields were removed as default user contact
</span><span class="cx" style="display: block; padding: 0 10px">  *              methods for new installations. See wp_get_user_contact_methods().
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 4.7.0 The user's locale can be passed to `$userdata`.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0 The `locale` field can be passed to `$userdata`.
</ins><span class="cx" style="display: block; padding: 0 10px">  * @since 5.3.0 The `user_activation_key` field can be passed to `$userdata`.
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.3.0 The `spam` field can be passed to `$userdata` (Multisite only).
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.9.0 The `meta_input` field can be passed to `$userdata` to allow addition of user meta data.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @global wpdb $wpdb WordPress database abstraction object.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1758,6 +1759,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *                                        as a string literal, not boolean. Default 'true'.
</span><span class="cx" style="display: block; padding: 0 10px">  *     @type string $role                 User's role.
</span><span class="cx" style="display: block; padding: 0 10px">  *     @type string $locale               User's locale. Default empty.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *     @type array  $meta_input           Array of custom user meta values keyed by meta key.
+ *                                        Default empty.
</ins><span class="cx" style="display: block; padding: 0 10px">  * }
</span><span class="cx" style="display: block; padding: 0 10px">  * @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not
</span><span class="cx" style="display: block; padding: 0 10px">  *                      be created.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2058,6 +2061,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * Does not include contact methods. These are added using `wp_get_user_contact_methods( $user )`.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * For custom meta fields, see the {@see 'insert_custom_user_meta'} filter.
+        *
</ins><span class="cx" style="display: block; padding: 0 10px">          * @since 4.4.0
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.8.0 The $userdata parameter was added.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2084,6 +2089,28 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        $meta = apply_filters( 'insert_user_meta', $meta, $user, $update, $userdata );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        $custom_meta = array();
+       if ( array_key_exists( 'meta_input', $userdata ) && is_array( $userdata['meta_input'] ) && ! empty( $userdata['meta_input'] ) ) {
+               $custom_meta = $userdata['meta_input'];
+       }
+
+       /**
+        * Filters a user's custom meta values and keys immediately after the user is created or updated
+        * and before any user meta is inserted or updated.
+        *
+        * For non-custom meta fields, see the {@see 'insert_user_meta'} filter.
+        *
+        * @since 5.9.0
+        *
+        * @param array   $custom_meta Array of custom user meta values keyed by meta key.
+        * @param WP_User $user        User object.
+        * @param bool    $update      Whether the user is being updated rather than created.
+        * @param array   $userdata    The raw array of data passed to wp_insert_user().
+        */
+       $custom_meta = apply_filters( 'insert_custom_user_meta', $custom_meta, $user, $update, $userdata );
+
+       $meta = array_merge( $meta, $custom_meta );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         // Update user meta.
</span><span class="cx" style="display: block; padding: 0 10px">        foreach ( $meta as $key => $value ) {
</span><span class="cx" style="display: block; padding: 0 10px">                update_user_meta( $user_id, $key, $value );
</span></span></pre></div>
<a id="trunktestsphpunittestsuserphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/user.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/user.php        2021-09-07 19:23:00 UTC (rev 51737)
+++ trunk/tests/phpunit/tests/user.php  2021-09-07 21:30:07 UTC (rev 51738)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1846,6 +1846,78 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Test `$user_data['meta_input']` args in `wp_insert_user( $user_data )`.
+        *
+        * @ticket 41950
+        */
+       public function test_wp_insert_user_with_meta() {
+               $user_data   = array(
+                       'user_login' => 'test_user',
+                       'user_pass'  => 'test_password',
+                       'user_email' => 'user@example.com',
+                       'meta_input' => array(
+                               'test_meta_key' => 'test_meta_value',
+                               'custom_meta'   => 'custom_value',
+                       ),
+               );
+               $create_user = wp_insert_user( $user_data );
+
+               $this->assertSame( 'test_meta_value', get_user_meta( $create_user, 'test_meta_key', true ) );
+               $this->assertSame( 'custom_value', get_user_meta( $create_user, 'custom_meta', true ) );
+
+               // Update the user meta thru wp_insert_user.
+               $update_data = array(
+                       'ID'         => $create_user,
+                       'user_login' => 'test_user',
+                       'meta_input' => array(
+                               'test_meta_key' => 'test_meta_updated',
+                               'custom_meta'   => 'updated_value',
+                               'new_meta_k'    => 'new_meta_val',
+                       ),
+               );
+               $update_user = wp_insert_user( $update_data );
+
+               $this->assertSame( 'test_meta_updated', get_user_meta( $update_user, 'test_meta_key', true ) );
+               $this->assertSame( 'updated_value', get_user_meta( $update_user, 'custom_meta', true ) );
+               $this->assertSame( 'new_meta_val', get_user_meta( $update_user, 'new_meta_k', true ) );
+
+               // Create new user.
+               $new_user_data = array(
+                       'user_login' => 'new_test',
+                       'user_pass'  => 'new_password',
+                       'user_email' => 'new_user@newexample.com',
+                       'meta_input' => array(
+                               'test_meta_key' => 'test_meta_value',
+                               'custom_meta'   => 'new_user_custom_value',
+                       ),
+               );
+
+               // Hook filter
+               add_filter( 'insert_custom_user_meta', array( $this, 'filter_custom_meta' ) );
+
+               $new_user = wp_insert_user( $new_user_data );
+
+               // This meta is updated by the filter.
+               $this->assertSame( 'update_from_filter', get_user_meta( $new_user, 'test_meta_key', true ) );
+               $this->assertSame( 'new_user_custom_value', get_user_meta( $new_user, 'custom_meta', true ) );
+               // This meta is inserted by the filter.
+               $this->assertSame( 'new_from_filter', get_user_meta( $new_user, 'new_meta_from_filter', true ) );
+       }
+
+       /**
+        * Hook a filter to alter custom meta when inserting new user.
+        * This hook is used in `test_wp_insert_user_with_meta()`.
+        */
+       public function filter_custom_meta( $meta_input ) {
+               // Update some meta inputs
+               $meta_input['test_meta_key'] = 'update_from_filter';
+               // Add a new meta
+               $meta_input['new_meta_from_filter'] = 'new_from_filter';
+
+               return $meta_input;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Testing the `wp_privacy_additional_user_profile_data` filter works.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.4.0
</span></span></pre>
</div>
</div>

</body>
</html>