Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 19 additions & 80 deletions src/wp-includes/abilities/class-wp-abilities-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*
* @access private
*
* @phpstan-type Exposed_Setting array{option: string, group: string, default: mixed, schema: array<string, mixed>}
* @phpstan-type Exposed_Setting array{option: string, group: string, default: mixed, schema: array<string, mixed>, validate_callback: mixed}
*/
final class WP_Abilities_Settings {

Expand Down Expand Up @@ -278,8 +278,9 @@ private function execute_settings_update( $input ) {

/*
* Intentionally strict: every value is checked before the first write, so a call refused
* here changes nothing. A value is refused when the setting cannot use it, instead of being
* saved as sent or silently replaced with the stored value. A null value is refused while
* here changes nothing. A value is refused when the validate callback of its setting refuses
* it, instead of being saved as sent or silently replaced with the stored value. The callback
* does not check a null value, which resets the setting. A null value is refused while
* the setting has a stored value that `core/settings-get` leaves out, because the value does
* not match the setting's schema: the caller cannot see what would be replaced or deleted.
* Unusable values are reported first.
Expand All @@ -306,9 +307,14 @@ private function execute_settings_update( $input ) {
}

if ( null !== $data[ $exposed_name ] ) {
$message = $this->get_invalid_value_message( $setting['option'], $value, "data[{$exposed_name}]" );
if ( null !== $message ) {
$invalid_params[ "data[{$exposed_name}]" ] = $message;
$validity = true;
if ( ! empty( $setting['validate_callback'] ) ) {
// A callback that cannot be called refuses every value, instead of letting any value through.
$validity = is_callable( $setting['validate_callback'] ) ? call_user_func( $setting['validate_callback'], $value, $setting['option'] ) : false;
}

if ( true !== $validity ) {
$invalid_params[ "data[{$exposed_name}]" ] = is_wp_error( $validity ) ? $validity->get_error_message() : __( 'Invalid parameter.' );
}
} elseif (
// The stored value reads as null when there is none, and then there is nothing to replace.
Expand Down Expand Up @@ -400,75 +406,6 @@ private function get_refused_settings_error( string $code, array $params, int $s
);
}

/**
* Checks a value that matches its schema against what the option can use.
*
* The rules belong to the option, whatever name exposes it.
*
* @since 7.3.0
*
* @param string $option The option name.
* @param mixed $value The value to save, which is not null.
* @param string $param The input path of the setting, used in the message.
* @return string|null The error message when the option cannot use the value, null otherwise.
*/
private function get_invalid_value_message( string $option, $value, string $param ): ?string {
switch ( strtolower( $option ) ) {
case 'timezone_string':
if ( '' !== $value && ! in_array( $value, timezone_identifiers_list( DateTimeZone::ALL_WITH_BC ), true ) ) {
return __( 'The timezone you have entered is not valid. Please select a valid timezone.' );
}
break;

case 'wplang':
$languages = get_available_languages();
if ( '' !== $value && ! in_array( $value, $languages, true ) ) {
/* translators: 1: Parameter, 2: List of valid values. */
return wp_sprintf( __( '%1$s is not one of %2$l.' ), $param, array_merge( array( 'en_US' ), $languages ) );
}
break;

case 'default_post_format':
// An empty value is the standard format, as `0` is.
if ( '0' !== $value && '' !== $value && ! in_array( $value, get_post_format_slugs(), true ) ) {
return __( 'Invalid post format.' );
}
break;

case 'page_on_front':
case 'page_for_posts':
case 'wp_page_for_privacy_policy':
/*
* The Settings screens never offer a page that is in the trash or an auto-draft.
*
* Known gap: a page in any other status is accepted, such as a draft, pending, private
* or scheduled page, which logged-out visitors cannot see. Settings > Reading offers only
* published pages for the front page and the page for posts, but only in its form: the
* Settings API has no rule for the status of these pages yet.
*/
if (
0 !== $value
&& (
! is_int( $value )
|| 'page' !== get_post_type( $value )
|| in_array( get_post_status( $value ), array( 'trash', 'auto-draft' ), true )
)
) {
return __( 'Invalid post ID.' );
}
break;

case 'default_category':
// A negative ID would be looked up as its absolute value.
if ( ! is_int( $value ) || $value <= 0 || ! term_exists( $value, 'category' ) ) {
return __( 'Invalid term ID.' );
}
break;
}

return null;
}

/**
* Reads the current values of the given exposed settings.
*
Expand Down Expand Up @@ -681,7 +618,8 @@ private function get_settings_output_schema( array $settings, string $descriptio
* `show_in_abilities` argument, of a type the settings endpoint supports. Each entry is
* keyed by its exposed name and carries the underlying option name, the settings group,
* the default (the exposure schema's default when it declares one, otherwise the registered
* default, or null when there is none), and a JSON Schema describing the value.
* default, or null when there is none), a JSON Schema describing the value, and the
* registered validate callback, or null when there is none.
*
* Two settings exposed under the same name trigger a notice, and the later one is exposed.
*
Expand Down Expand Up @@ -732,10 +670,11 @@ private function get_exposed_settings(): array {
* The options API trims the name and the database ignores its case, so the rules
* that name an option compare the trimmed name in lower case.
*/
'option' => trim( $option_name ),
'group' => $args['group'] ?? '',
'default' => array_key_exists( 'default', $schema ) ? $schema['default'] : ( $args['default'] ?? null ),
'schema' => $schema,
'option' => trim( $option_name ),
'group' => $args['group'] ?? '',
'default' => array_key_exists( 'default', $schema ) ? $schema['default'] : ( $args['default'] ?? null ),
'schema' => $schema,
'validate_callback' => $args['validate_callback'] ?? null,
);
}

Expand Down
143 changes: 143 additions & 0 deletions src/wp-includes/option.php
Original file line number Diff line number Diff line change
Expand Up @@ -2741,6 +2741,8 @@ function set_site_transient( $transient, $value, $expiration = 0 ) {
* @since 6.0.1 The `show_on_front`, `page_on_front`, and `page_for_posts` options were added.
* @since 7.2.0 The `wp_page_for_privacy_policy` option was registered, exposed as `page_for_privacy_policy`.
* @since 7.2.0 Added `show_in_abilities` support for the exposed settings.
* @since 7.3.0 Added a `validate_callback` to the timezone, language, default category, default post format,
* and page settings.
*/
function register_initial_settings() {
register_setting(
Expand Down Expand Up @@ -2815,6 +2817,7 @@ function register_initial_settings() {
'name' => 'timezone',
),
'show_in_abilities' => true,
'validate_callback' => '_wp_validate_timezone_string_setting',
'type' => 'string',
'description' => __( 'A city in the same timezone as you.' ),
)
Expand Down Expand Up @@ -2866,6 +2869,7 @@ function register_initial_settings() {
'name' => 'language',
),
'show_in_abilities' => true,
'validate_callback' => '_wp_validate_language_setting',
'type' => 'string',
'description' => __( 'WordPress locale code.' ),
'default' => 'en_US',
Expand All @@ -2890,6 +2894,7 @@ function register_initial_settings() {
array(
'show_in_rest' => true,
'show_in_abilities' => true,
'validate_callback' => '_wp_validate_default_category_setting',
'type' => 'integer',
'description' => __( 'Default post category.' ),
)
Expand All @@ -2901,6 +2906,7 @@ function register_initial_settings() {
array(
'show_in_rest' => true,
'show_in_abilities' => true,
'validate_callback' => '_wp_validate_default_post_format_setting',
'type' => 'string',
'description' => __( 'Default post format.' ),
)
Expand Down Expand Up @@ -2941,6 +2947,7 @@ function register_initial_settings() {
array(
'show_in_rest' => true,
'show_in_abilities' => true,
'validate_callback' => '_wp_validate_page_setting',
'type' => 'integer',
'label' => __( 'Page on front' ),
'description' => __( 'The ID of the page that should be displayed on the front page' ),
Expand All @@ -2953,6 +2960,7 @@ function register_initial_settings() {
array(
'show_in_rest' => true,
'show_in_abilities' => true,
'validate_callback' => '_wp_validate_page_setting',
'type' => 'integer',
'description' => __( 'The ID of the page that should display the latest posts' ),
)
Expand All @@ -2966,6 +2974,7 @@ function register_initial_settings() {
'name' => 'page_for_privacy_policy',
),
'show_in_abilities' => true,
'validate_callback' => '_wp_validate_page_setting',
'type' => 'integer',
'description' => __( 'The ID of the page that should be displayed as the privacy policy page' ),
)
Expand Down Expand Up @@ -3037,6 +3046,121 @@ function _wp_register_initial_settings_for_abilities(): void {
$new_allowed_options = $allowed_options;
}

/**
* Checks whether the `timezone_string` setting can use a value.
*
* An empty value means no timezone is set. Any other value must be a timezone identifier
* known to PHP, including its older names.
*
* @since 7.3.0
* @access private
*
* @param mixed $value The value to check.
* @return true|WP_Error True when the setting can use the value, an error otherwise.
*/
function _wp_validate_timezone_string_setting( $value ) {
if ( '' !== $value && ! in_array( $value, timezone_identifiers_list( DateTimeZone::ALL_WITH_BC ), true ) ) {
return new WP_Error( 'invalid_timezone', __( 'The timezone you have entered is not valid. Please select a valid timezone.' ) );
}

return true;
}

/**
* Checks whether the `WPLANG` setting can use a value.
*
* English is stored as an empty value. Any other value must be a language installed on the site.
*
* @since 7.3.0
* @access private
*
* @param mixed $value The value to check.
* @return true|WP_Error True when the setting can use the value, an error otherwise.
*/
function _wp_validate_language_setting( $value ) {
$languages = get_available_languages();
if ( '' !== $value && ! in_array( $value, $languages, true ) ) {
return new WP_Error(
'invalid_language',
/* translators: %l: List of valid values. */
wp_sprintf( __( 'The language must be one of %l.' ), array_merge( array( 'en_US' ), $languages ) )
);
}

return true;
}

/**
* Checks whether the `default_post_format` setting can use a value.
*
* `0` and an empty value mean the standard format. Any other value must be a post format.
*
* @since 7.3.0
* @access private
*
* @param mixed $value The value to check.
* @return true|WP_Error True when the setting can use the value, an error otherwise.
*/
function _wp_validate_default_post_format_setting( $value ) {
if ( '0' !== $value && '' !== $value && ! in_array( $value, get_post_format_slugs(), true ) ) {
return new WP_Error( 'invalid_post_format', __( 'Invalid post format.' ) );
}

return true;
}

/**
* Checks whether the `default_category` setting can use a value.
*
* The value must be the ID of a category.
*
* @since 7.3.0
* @access private
*
* @param mixed $value The value to check.
* @return true|WP_Error True when the setting can use the value, an error otherwise.
*/
function _wp_validate_default_category_setting( $value ) {
// A negative ID would be looked up as its absolute value.
if ( ! is_int( $value ) || $value <= 0 || ! term_exists( $value, 'category' ) ) {
return new WP_Error( 'invalid_term', __( 'Invalid term ID.' ) );
}

return true;
}

/**
* Checks whether a page setting can use a value.
*
* Used by the `page_on_front`, `page_for_posts` and `wp_page_for_privacy_policy` settings.
* The value must be 0, for no page, or the ID of a page in a status that the Settings screens
* offer for the setting: a published page for the front page and the page for posts, and a
* published or draft page for the privacy policy page.
*
* @since 7.3.0
* @access private
*
* @param mixed $value The value to check.
* @param string $option The option name.
* @return true|WP_Error True when the setting can use the value, an error otherwise.
*/
function _wp_validate_page_setting( $value, $option ) {
if ( 0 === $value ) {
return true;
}

$statuses = 'wp_page_for_privacy_policy' === strtolower( trim( $option ) ) ? array( 'publish', 'draft' ) : array( 'publish' );
if (
! is_int( $value )
|| 'page' !== get_post_type( $value )
|| ! in_array( get_post_status( $value ), $statuses, true )
) {
return new WP_Error( 'invalid_page', __( 'Invalid post ID.' ) );
}

return true;
}

/**
* Registers a setting and its data.
*
Expand All @@ -3050,6 +3174,7 @@ function _wp_register_initial_settings_for_abilities(): void {
* @since 7.2.0 Added the `show_in_abilities` argument.
* @since 7.3.0 Settings exposed with the `show_in_abilities` argument can be updated through the Abilities API,
* unless their schema sets 'readonly' to true.
* @since 7.3.0 Added the `validate_callback` argument.
*
* @global array $new_allowed_options
* @global array $wp_registered_settings
Expand All @@ -3069,6 +3194,14 @@ function _wp_register_initial_settings_for_abilities(): void {
* @type string $description A description of the data attached to this setting.
* @type callable|null $sanitize_callback A callback function that sanitizes the option's value.
* Default null.
* @type callable|null $validate_callback A callback function that checks whether the option can use a value,
* before it is saved. It receives the value and the option name, and
* returns true when the value can be used, or a WP_Error object whose
* message says why it cannot. Any other return value also refuses the
* value. It is not called when the option is saved with update_option():
* the Abilities API calls it before updating a setting exposed with
* `$show_in_abilities`. Registering the setting again without one keeps
* the existing callback. Default null.
* @type bool|array $show_in_rest Whether data associated with this setting should be included in the
* REST API. When registering complex settings, this argument may
* optionally be an array with a 'schema' key.
Expand Down Expand Up @@ -3103,6 +3236,7 @@ function register_setting( $option_group, $option_name, $args = array() ) {
'label' => '',
'description' => '',
'sanitize_callback' => null,
'validate_callback' => null,
'show_in_rest' => false,
'show_in_abilities' => false,
);
Expand Down Expand Up @@ -3137,6 +3271,11 @@ function register_setting( $option_group, $option_name, $args = array() ) {
$wp_registered_settings = array();
}

// Registering the setting again without a validate callback keeps the rule it was registered with.
if ( empty( $args['validate_callback'] ) && ! empty( $wp_registered_settings[ $option_name ]['validate_callback'] ) ) {
$args['validate_callback'] = $wp_registered_settings[ $option_name ]['validate_callback'];
}

if ( 'misc' === $option_group ) {
_deprecated_argument(
__FUNCTION__,
Expand Down Expand Up @@ -3291,6 +3430,7 @@ function unregister_setting( $option_group, $option_name, $deprecated = '' ) {
*
* @since 4.7.0
* @since 7.2.0 Registered setting data includes the `show_in_abilities` argument.
* @since 7.3.0 Registered setting data includes the `validate_callback` argument.
*
* @global array $wp_registered_settings
*
Expand All @@ -3307,6 +3447,9 @@ function unregister_setting( $option_group, $option_name, $deprecated = '' ) {
* @type string $label A label of the data attached to this setting.
* @type string $description A description of the data attached to this setting.
* @type callable|null $sanitize_callback A callback function that sanitizes the option's value.
* @type callable|null $validate_callback A callback function that checks whether the option can use a
* value, before it is saved. It receives the value and the option
* name, and returns true or a WP_Error object.
* @type bool|array $show_in_rest Whether data associated with this setting should be included in the
* REST API. When registering complex settings, this argument may
* optionally be an array with a 'schema' key.
Expand Down
Loading
Loading