Skip to content

Commit

Permalink
Options, Meta APIs: Prime salts when stored in database.
Browse files Browse the repository at this point in the history
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.



git-svn-id: https://develop.svn.wordpress.org/trunk@58837 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
peterwilsoncc committed Jul 31, 2024
1 parent 883146e commit 74e03e3
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/wp-includes/pluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2468,6 +2468,41 @@ function wp_salt( $scheme = 'auth' ) {
}
}

/*
* 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(
'key' => '',
'salt' => '',
Expand Down

0 comments on commit 74e03e3

Please sign in to comment.