Options, Meta APIs: Prime salts when stored in database.

For salts generated and stored in the database, use `wp_prime_site_option_caches()` within `wp_salt()` to prime the options in a single database query, down from up to nine database queries.

The options are primed when the corresponding constant is either undefined or uses the default string `put your unique phrase here`.

Props joemcgill, spacedmonkey, peterwilsoncc.
Fixes #59871.


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


git-svn-id: http://core.svn.wordpress.org/trunk@58233 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2024-07-31 22:58:14 +00:00
parent af6e4904af
commit 61fa836ce5
2 changed files with 36 additions and 1 deletions

View File

@ -2468,6 +2468,41 @@ if ( ! function_exists( 'wp_salt' ) ) :
} }
} }
/*
* Determine which options to prime.
*
* If the salt keys are undefined, use a duplicate value or the
* default `put your unique phrase here` value the salt will be
* generated via `wp_generate_password()` and stored as a site
* option. These options will be primed to avoid repeated
* database requests for undefined salts.
*/
$options_to_prime = array();
foreach ( array( 'auth', 'secure_auth', 'logged_in', 'nonce' ) as $key ) {
foreach ( array( 'key', 'salt' ) as $second ) {
$const = strtoupper( "{$key}_{$second}" );
if ( ! defined( $const ) || true === $duplicated_keys[ constant( $const ) ] ) {
$options_to_prime[] = "{$key}_{$second}";
}
}
}
if ( ! empty( $options_to_prime ) ) {
/*
* Also prime `secret_key` used for undefined salting schemes.
*
* If the scheme is unknown the default value for `secret_key` will be
* used to for the salt. This should rarely happen so the option is only
* primed if other salts are undefined.
*
* At this point of execution is is known that a database call will be made
* to prime salts so the `secret_key` option can be primed regardless of the
* constants status.
*/
$options_to_prime[] = 'secret_key';
wp_prime_site_option_caches( $options_to_prime );
}
$values = array( $values = array(
'key' => '', 'key' => '',
'salt' => '', 'salt' => '',

View File

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