Make WordPress Core


Ignore:
Timestamp:
08/07/2025 01:32:17 AM (4 months ago)
Author:
peterwilsoncc
Message:

Upgrade/Install: Reduce number of DB queries populating roles.

Reduces the number of database queries made when populating roles during install/multisite site creation by 344 (347 queries down to 3).

populate_roles() has been modified to prevent an individual database query each time a role or capability is added to the WP_Roles object. Instead the roles option, {$wpdb->prefix}user_roles is updated once at the end of the function call.

Introduces a test to ensure that updating the roles option via WP_Roles and updating the option in the manner now used by populate_roles() results in the same capabilities been applied to a role.

Props fliespl, johnjamesjacoby, ocean90, realloc, rishabhwp, sainathpoojary, sirlouen, spacedmonkey, swissspidy.
Fixes #37687.

File:
1 edited

Legend:

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

    r60497 r60614  
    714714 */
    715715function populate_roles() {
     716    $wp_roles = wp_roles();
     717
     718    // Disable role updates to the database while populating roles.
     719    $original_use_db  = $wp_roles->use_db;
     720    $wp_roles->use_db = false;
     721
     722    // Populate roles
    716723    populate_roles_160();
    717724    populate_roles_210();
     
    722729    populate_roles_280();
    723730    populate_roles_300();
     731
     732    // Save the updated roles to the database.
     733    if ( $original_use_db ) {
     734        update_option( $wp_roles->role_key, $wp_roles->roles, true );
     735    }
     736
     737    // Restore original value for writing to database.
     738    $wp_roles->use_db = $original_use_db;
    724739}
    725740
Note: See TracChangeset for help on using the changeset viewer.