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.


Built from https://develop.svn.wordpress.org/trunk@60614


git-svn-id: http://core.svn.wordpress.org/trunk@59950 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson
2025-08-07 01:34:33 +00:00
parent 2ef90d3315
commit 216a4157c9
2 changed files with 16 additions and 1 deletions

View File

@@ -713,6 +713,13 @@ function populate_options( array $options = array() ) {
* @since 2.0.0
*/
function populate_roles() {
$wp_roles = wp_roles();
// Disable role updates to the database while populating roles.
$original_use_db = $wp_roles->use_db;
$wp_roles->use_db = false;
// Populate roles
populate_roles_160();
populate_roles_210();
populate_roles_230();
@@ -721,6 +728,14 @@ function populate_roles() {
populate_roles_270();
populate_roles_280();
populate_roles_300();
// Save the updated roles to the database.
if ( $original_use_db ) {
update_option( $wp_roles->role_key, $wp_roles->roles, true );
}
// Restore original value for writing to database.
$wp_roles->use_db = $original_use_db;
}
/**

View File

@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.9-alpha-60613';
$wp_version = '6.9-alpha-60614';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.