Skip to content

Commit bac565c

Browse files
committed
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: https://core.svn.wordpress.org/trunk@59950 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent af57cb5 commit bac565c

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

wp-admin/includes/schema.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,13 @@ function populate_options( array $options = array() ) {
713713
* @since 2.0.0
714714
*/
715715
function 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();
718725
populate_roles_230();
@@ -721,6 +728,14 @@ function populate_roles() {
721728
populate_roles_270();
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

726741
/**

wp-includes/version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @global string $wp_version
1818
*/
19-
$wp_version = '6.9-alpha-60613';
19+
$wp_version = '6.9-alpha-60614';
2020

2121
/**
2222
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

0 commit comments

Comments
 (0)