Make WordPress Core

Ticket #43208: 43208.5.diff

File 43208.5.diff, 8.1 KB (added by flixos90, 6 years ago)
  • src/wp-admin/options.php

     
    220220                                }
    221221                                $value = wp_unslash( $value );
    222222                        }
    223                         update_option( $option, $value );
     223
     224                        $validity = validate_option( $option, $value );
     225
     226                        if ( is_wp_error( $validity ) ) {
     227                                foreach ( $validity->errors as $code => $messages ) {
     228                                        foreach ( $messages as $message ) {
     229                                                add_settings_error( $option, $code, $message );
     230                                        }
     231                                }
     232                        } else {
     233                                update_option( $option, $value );
     234                        }
    224235                }
    225236
    226237                /*
  • src/wp-includes/class-wp-customize-manager.php

     
    23002300                                $validity = $setting->validate( $unsanitized_value );
    23012301                        }
    23022302                        if ( ! is_wp_error( $validity ) ) {
     2303                                $late_validity = new WP_Error();
     2304
     2305                                // Use the regular option validation if the Customize setting is an option.
     2306                                if ( 'option' === $setting->type && ! $setting->is_multidimensional() ) {
     2307                                        $option_validity = validate_option( $setting->id, $unsanitized_value );
     2308                                        if ( is_wp_error( $option_validity ) ) {
     2309                                                $late_validity = $option_validity;
     2310                                        }
     2311                                }
     2312
    23032313                                /** This filter is documented in wp-includes/class-wp-customize-setting.php */
    2304                                 $late_validity = apply_filters( "customize_validate_{$setting->id}", new WP_Error(), $unsanitized_value, $setting );
     2314                                $late_validity = apply_filters( "customize_validate_{$setting->id}", $late_validity, $unsanitized_value, $setting );
    23052315                                if ( $late_validity->has_errors() ) {
    23062316                                        $validity = $late_validity;
    23072317                                }
  • src/wp-includes/class-wp-customize-setting.php

     
    311311                }
    312312
    313313                $id_base                 = $this->id_data['base'];
    314                 $is_multidimensional     = ! empty( $this->id_data['keys'] );
     314                $is_multidimensional     = $this->is_multidimensional();
    315315                $multidimensional_filter = array( $this, '_multidimensional_preview_filter' );
    316316
    317317                /*
     
    579579
    580580                $validity = new WP_Error();
    581581
     582                // Use the regular option validation if the Customize setting is an option.
     583                if ( 'option' === $this->type && ! $this->is_multidimensional() ) {
     584                        $option_validity = validate_option( $this->id, $value );
     585                        if ( is_wp_error( $option_validity ) ) {
     586                                $validity = $option_validity;
     587                        }
     588                }
     589
    582590                /**
    583591                 * Validates a Customize setting value.
    584592                 *
     
    635643        protected function set_root_value( $value ) {
    636644                $id_base = $this->id_data['base'];
    637645                if ( 'option' === $this->type ) {
     646                        $option_validity = validate_option( $id_base, $value );
     647                        if ( is_wp_error( $option_validity ) ) {
     648                                return false;
     649                        }
     650
    638651                        $autoload = true;
    639652                        if ( isset( self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'] ) ) {
    640653                                $autoload = self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'];
     
    827840        }
    828841
    829842        /**
     843         * Checks whether the setting is part of a multidimensional root.
     844         *
     845         * @since 5.0.0
     846         *
     847         * @return bool True if the setting is multidimensional, false otherwise.
     848         */
     849        final public function is_multidimensional() {
     850                return ! empty( $this->id_data['keys'] );
     851        }
     852
     853        /**
    830854         * Multidimensional helper function.
    831855         *
    832856         * @since 3.4.0
  • src/wp-includes/formatting.php

     
    43774377}
    43784378
    43794379/**
     4380 * Validates an option value based on the nature of the option.
     4381 *
     4382 * The {@see 'validate_option_$option'} filter should be used to add errors
     4383 * to the `WP_Error` object passed-through.
     4384 *
     4385 * @since 5.0.0
     4386 *
     4387 * @param string $option The name of the option.
     4388 * @param string $value  The unsanitized value.
     4389 * @return true|WP_Error True if the input was validated, otherwise WP_Error.
     4390 */
     4391function validate_option( $option, $value ) {
     4392        $validity = new WP_Error();
     4393
     4394        /**
     4395         * Validates an option value.
     4396         *
     4397         * Plugins should amend the `$validity` object via its `WP_Error::add()` method.
     4398         *
     4399         * The dynamic portion of the hook name, `$option`, refers to the option name.
     4400         *
     4401         * @since 5.0.0
     4402         *
     4403         * @param WP_Error $validity Filtered from `true` to `WP_Error` when invalid.
     4404         * @param mixed    $value    The option value.
     4405         */
     4406        $validity = apply_filters( "validate_option_{$option}", $validity, $value );
     4407
     4408        if ( is_wp_error( $validity ) && empty( $validity->errors ) ) {
     4409                $validity = true;
     4410        }
     4411
     4412        return $validity;
     4413}
     4414
     4415/**
    43804416 * Sanitises various option values based on the nature of the option.
    43814417 *
    43824418 * This is basically a switch statement which will pass $value through a number
  • src/wp-includes/option.php

     
    20372037 *
    20382038 * @since 2.7.0
    20392039 * @since 4.7.0 `$args` can be passed to set flags on the setting, similar to `register_meta()`.
     2040 * @since 5.0.0 Introduced the `$validate_callback` argument.
    20402041 *
    20412042 * @global array $new_whitelist_options
    20422043 * @global array $wp_registered_settings
     
    20502051 *     @type string   $type              The type of data associated with this setting.
    20512052 *                                       Valid values are 'string', 'boolean', 'integer', and 'number'.
    20522053 *     @type string   $description       A description of the data attached to this setting.
     2054 *     @type callable $validate_callback A callback that checks validity of the option's value.
    20532055 *     @type callable $sanitize_callback A callback function that sanitizes the option's value.
    20542056 *     @type bool     $show_in_rest      Whether data associated with this setting should be included in the REST API.
    20552057 *     @type mixed    $default           Default value when calling `get_option()`.
     
    20622064                'type'              => 'string',
    20632065                'group'             => $option_group,
    20642066                'description'       => '',
     2067                'validate_callback' => null,
    20652068                'sanitize_callback' => null,
    20662069                'show_in_rest'      => false,
    20672070        );
     
    21152118        }
    21162119
    21172120        $new_whitelist_options[ $option_group ][] = $option_name;
     2121        if ( ! empty( $args['validate_callback'] ) ) {
     2122                add_filter( "validate_option_{$option_name}", $args['validate_callback'], 10, 2 );
     2123        }
    21182124        if ( ! empty( $args['sanitize_callback'] ) ) {
    21192125                add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] );
    21202126        }
     
    21822188        }
    21832189
    21842190        if ( isset( $wp_registered_settings[ $option_name ] ) ) {
     2191                // Remove the validate callback if one was set during registration.
     2192                if ( ! empty( $wp_registered_settings[ $option_name ]['validate_callback'] ) ) {
     2193                        remove_filter( "validate_option_{$option_name}", $wp_registered_settings[ $option_name ]['validate_callback'], 10 );
     2194                }
     2195
    21852196                // Remove the sanitize callback if one was set during registration.
    21862197                if ( ! empty( $wp_registered_settings[ $option_name ]['sanitize_callback'] ) ) {
    21872198                        remove_filter( "sanitize_option_{$option_name}", $wp_registered_settings[ $option_name ]['sanitize_callback'] );
  • src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php

     
    193193
    194194                                delete_option( $args['option_name'] );
    195195                        } else {
     196                                $validity = validate_option( $args['option_name'], $request[ $name ] );
     197                                if ( is_wp_error( $validity ) ) {
     198                                        foreach ( $validity->errors as $code => $messages ) {
     199                                                $validity->add_data( array( 'status' => 400 ), $code );
     200                                        }
     201
     202                                        return $validity;
     203                                }
     204
    196205                                update_option( $args['option_name'], $request[ $name ] );
    197206                        }
    198207                }