Skip to content

Commit

Permalink
Users: remove unnecessary writes to the database for use_ssl user meta.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
adamsilverstein committed Oct 11, 2024
1 parent f12827a commit dfe7c18
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/wp-includes/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down
42 changes: 42 additions & 0 deletions tests/phpunit/tests/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -2245,4 +2245,46 @@ public function export_additional_user_profile_data_with_dup_name() {

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 protected]',
'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 protected]';
$user_id = edit_user( $user_id );

// Verify there are no updates to use_ssl user meta.
$this->assertEquals( 1, $set_db_counts );
}
}

0 comments on commit dfe7c18

Please sign in to comment.