Users: remove unnecessary writes to the database for use_ssl user meta.

When checking for updates to use_ssl, use strings for the comparison values, matching the stored values. Fixes an issue where calls to wp_update_user updated the database meta value for use_ssl even when the value was missing or unchanged. 

Props prettyboymp, rajinsharwar, adamsilverstein, johnbillion, rayhatron, mukesh27, joemcgill.

Fixes #60299.



git-svn-id: https://develop.svn.wordpress.org/trunk@59216 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Adam Silverstein 2024-10-11 17:18:03 +00:00
parent f12827a587
commit dfe7c18065
2 changed files with 43 additions and 1 deletions

View File

@ -2366,7 +2366,7 @@ function wp_insert_user( $userdata ) {
$admin_color = empty( $userdata['admin_color'] ) ? 'fresh' : $userdata['admin_color'];
$meta['admin_color'] = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $admin_color );
$meta['use_ssl'] = empty( $userdata['use_ssl'] ) ? 0 : (bool) $userdata['use_ssl'];
$meta['use_ssl'] = empty( $userdata['use_ssl'] ) || ! $userdata['use_ssl'] ? '0' : '1';
$meta['show_admin_bar_front'] = empty( $userdata['show_admin_bar_front'] ) ? 'true' : $userdata['show_admin_bar_front'];

View File

@ -2245,4 +2245,46 @@ class Tests_User extends WP_UnitTestCase {
return $additional_profile_data;
}
/**
* Test that update_user_meta for 'use_ssl' doesn't write to DB unnecessarily.
*
* @ticket 60299
*/
public function test_unnecessary_assignment_of_use_ssl_in_meta() {
$user_id = self::$contrib_id;
// Keep track of db writing calls.
$set_db_counts = 0;
// Track db updates with calls to do_action( "update_user_meta", ... with 'use_ssl' meta key.
add_action(
'update_user_meta',
function ( $meta_id, $object_id, $meta_key ) use ( &$set_db_counts ) {
if ( 'use_ssl' !== $meta_key ) {
return;
}
$set_db_counts++;
},
10,
3
);
$_POST = array(
'nickname' => 'nickname_test',
'email' => 'email_test_1@example.com',
'use_ssl' => 1,
);
$user_id = edit_user( $user_id );
$this->assertIsInt( $user_id );
$this->assertEquals( 1, $set_db_counts );
// Update the user without changing the 'use_ssl' meta.
$_POST['email'] = 'email_test_2@example.com';
$user_id = edit_user( $user_id );
// Verify there are no updates to use_ssl user meta.
$this->assertEquals( 1, $set_db_counts );
}
}