WordPress.org

Make WordPress Core

Ticket #43208: 43208.2.diff

File 43208.2.diff, 3.8 KB (added by flixos90, 4 years ago)
  • src/wp-includes/formatting.php

     
    43144314}
    43154315
    43164316/**
     4317 * Validates an option value based on the nature of the option.
     4318 *
     4319 * The {@see 'validate_option_$option'} filter should be used to add errors
     4320 * to the `WP_Error` object passed-through.
     4321 *
     4322 * @since 5.0.0
     4323 *
     4324 * @param string $option The name of the option.
     4325 * @param string $value  The unsanitized value.
     4326 * @return true|WP_Error True if the input was validated, otherwise WP_Error.
     4327 */
     4328function validate_option( $option, $value ) {
     4329        $validity = new WP_Error();
     4330
     4331        /**
     4332         * Validates an option value.
     4333         *
     4334         * Plugins should amend the `$validity` object via its `WP_Error::add()` method.
     4335         *
     4336         * The dynamic portion of the hook name, `$option`, refers to the option name.
     4337         *
     4338         * @since 5.0.0
     4339         *
     4340         * @param WP_Error $validity Filtered from `true` to `WP_Error` when invalid.
     4341         * @param mixed    $value    The option value.
     4342         */
     4343        $validity = apply_filters( "validate_option_{$option}", $validity, $value );
     4344
     4345        if ( is_wp_error( $validity ) && empty( $validity->errors ) ) {
     4346                $validity = true;
     4347        }
     4348
     4349        return $validity;
     4350}
     4351
     4352/**
    43174353 * Sanitises various option values based on the nature of the option.
    43184354 *
    43194355 * This is basically a switch statement which will pass $value through a number
  • src/wp-includes/option.php

     
    20322032 *
    20332033 * @since 2.7.0
    20342034 * @since 4.7.0 `$args` can be passed to set flags on the setting, similar to `register_meta()`.
     2035 * @since 5.0.0 Introduced the `$validate_callback` argument.
    20352036 *
    20362037 * @global array $new_whitelist_options
    20372038 * @global array $wp_registered_settings
     
    20452046 *     @type string   $type              The type of data associated with this setting.
    20462047 *                                       Valid values are 'string', 'boolean', 'integer', and 'number'.
    20472048 *     @type string   $description       A description of the data attached to this setting.
     2049 *     @type callable $validate_callback A callback that checks validity of the option's value.
    20482050 *     @type callable $sanitize_callback A callback function that sanitizes the option's value.
    20492051 *     @type bool     $show_in_rest      Whether data associated with this setting should be included in the REST API.
    20502052 *     @type mixed    $default           Default value when calling `get_option()`.
     
    20572059                'type'              => 'string',
    20582060                'group'             => $option_group,
    20592061                'description'       => '',
     2062                'validate_callback' => null,
    20602063                'sanitize_callback' => null,
    20612064                'show_in_rest'      => false,
    20622065        );
     
    21102113        }
    21112114
    21122115        $new_whitelist_options[ $option_group ][] = $option_name;
     2116        if ( ! empty( $args['validate_callback'] ) ) {
     2117                add_filter( "validate_option_{$option_name}", $args['validate_callback'], 10, 3 );
     2118        }
    21132119        if ( ! empty( $args['sanitize_callback'] ) ) {
    21142120                add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] );
    21152121        }
     
    21772183        }
    21782184
    21792185        if ( isset( $wp_registered_settings[ $option_name ] ) ) {
     2186                // Remove the validate callback if one was set during registration.
     2187                if ( ! empty( $wp_registered_settings[ $option_name ]['validate_callback'] ) ) {
     2188                        remove_filter( "validate_option_{$option_name}", $wp_registered_settings[ $option_name ]['validate_callback'], 10 );
     2189                }
     2190
    21802191                // Remove the sanitize callback if one was set during registration.
    21812192                if ( ! empty( $wp_registered_settings[ $option_name ]['sanitize_callback'] ) ) {
    21822193                        remove_filter( "sanitize_option_{$option_name}", $wp_registered_settings[ $option_name ]['sanitize_callback'] );