From 74e03e3cbef2f2565028f446c76acb2dabf749bd Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Wed, 31 Jul 2024 22:56:32 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@58837 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/pluggable.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index 0e9e0d4579..ae5355f9c6 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -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( 'key' => '', 'salt' => '',