Options, Meta APIs: Improve error handling in sanitize_option().

To prevent potential false negatives, set `$error` to `null` initially, so we can better tell if it was ever changed during the sanitization and be able to better react if an empty string is added to it.

Additionally, and mainly for the sake of the Settings API at this point, add error messages to some `WP_Error` objects returned from `wpdb` methods that were previously causing the issues here.

Follow-up to [32791].

Props iCaleb, audrasjb, hellofromTonya, SergeyBiryukov.
Fixes #53986.

git-svn-id: https://develop.svn.wordpress.org/trunk@52294 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2021-11-30 21:00:32 +00:00
parent 8facd44c4f
commit 1b92a6db07
3 changed files with 13 additions and 5 deletions
+10 -3
View File
@@ -4711,7 +4711,7 @@ function sanitize_option( $option, $value ) {
global $wpdb;
$original_value = $value;
$error = '';
$error = null;
switch ( $option ) {
case 'admin_email':
@@ -4919,7 +4919,9 @@ function sanitize_option( $option, $value ) {
$value = str_replace( 'http://', '', $value );
}
if ( 'permalink_structure' === $option && '' !== $value && ! preg_match( '/%[^\/%]+%/', $value ) ) {
if ( 'permalink_structure' === $option && null === $error
&& '' !== $value && ! preg_match( '/%[^\/%]+%/', $value )
) {
$error = sprintf(
/* translators: %s: Documentation URL. */
__( 'A structure tag is required when using custom permalinks. <a href="%s">Learn more</a>' ),
@@ -4948,7 +4950,12 @@ function sanitize_option( $option, $value ) {
break;
}
if ( ! empty( $error ) ) {
if ( null !== $error ) {
if ( '' === $error && is_wp_error( $value ) ) {
/* translators: 1: Option name, 2: Error code. */
$error = sprintf( __( 'Could not sanitize the %1$s option. Error code: %2$s' ), $option, $value->get_error_code() );
}
$value = get_option( $option );
if ( function_exists( 'add_settings_error' ) ) {
add_settings_error( $option, "invalid_{$option}", $error );