Make WordPress Core

Changeset 59216


Ignore:
Timestamp:
10/11/2024 05:18:03 PM (4 weeks ago)
Author:
adamsilverstein
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user.php

    r59112 r59216  
    23672367    $meta['admin_color'] = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $admin_color );
    23682368
    2369     $meta['use_ssl'] = empty( $userdata['use_ssl'] ) ? 0 : (bool) $userdata['use_ssl'];
     2369    $meta['use_ssl'] = empty( $userdata['use_ssl'] ) || ! $userdata['use_ssl'] ? '0' : '1';
    23702370
    23712371    $meta['show_admin_bar_front'] = empty( $userdata['show_admin_bar_front'] ) ? 'true' : $userdata['show_admin_bar_front'];
  • trunk/tests/phpunit/tests/user.php

    r59128 r59216  
    22462246        return $additional_profile_data;
    22472247    }
     2248
     2249    /**
     2250     * Test that update_user_meta for 'use_ssl' doesn't write to DB unnecessarily.
     2251     *
     2252     * @ticket 60299
     2253     */
     2254    public function test_unnecessary_assignment_of_use_ssl_in_meta() {
     2255        $user_id = self::$contrib_id;
     2256        // Keep track of db writing calls.
     2257        $set_db_counts = 0;
     2258
     2259        // Track db updates with calls to do_action( "update_user_meta", ... with 'use_ssl' meta key.
     2260        add_action(
     2261            'update_user_meta',
     2262            function ( $meta_id, $object_id, $meta_key ) use ( &$set_db_counts ) {
     2263                if ( 'use_ssl' !== $meta_key ) {
     2264                    return;
     2265                }
     2266                $set_db_counts++;
     2267            },
     2268            10,
     2269            3
     2270        );
     2271
     2272        $_POST = array(
     2273            'nickname' => 'nickname_test',
     2274            'email'    => 'email_test_1@example.com',
     2275            'use_ssl'  => 1,
     2276        );
     2277
     2278        $user_id = edit_user( $user_id );
     2279
     2280        $this->assertIsInt( $user_id );
     2281        $this->assertEquals( 1, $set_db_counts );
     2282
     2283        // Update the user without changing the 'use_ssl' meta.
     2284        $_POST['email'] = 'email_test_2@example.com';
     2285        $user_id        = edit_user( $user_id );
     2286
     2287        // Verify there are no updates to use_ssl user meta.
     2288        $this->assertEquals( 1, $set_db_counts );
     2289    }
    22482290}
Note: See TracChangeset for help on using the changeset viewer.