Make WordPress Core

Ticket #43208: 43208.6.diff

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

     
    280280                                }
    281281                                $value = wp_unslash( $value );
    282282                        }
    283                         update_option( $option, $value );
     283
     284                        $validity = validate_option( $option, $value );
     285
     286                        if ( is_wp_error( $validity ) ) {
     287                                foreach ( $validity->errors as $code => $messages ) {
     288                                        foreach ( $messages as $message ) {
     289                                                add_settings_error( $option, $code, $message );
     290                                        }
     291                                }
     292                        } else {
     293                                update_option( $option, $value );
     294                        }
    284295                }
    285296
    286297                /*
  • src/wp-includes/class-wp-customize-manager.php

     
    23022302                                $validity = $setting->validate( $unsanitized_value );
    23032303                        }
    23042304                        if ( ! is_wp_error( $validity ) ) {
     2305                                $late_validity = new WP_Error();
     2306
     2307                                // Use the regular option validation if the Customize setting is an option.
     2308                                if ( 'option' === $setting->type && ! $setting->is_multidimensional() ) {
     2309                                        $option_validity = validate_option( $setting->id, $unsanitized_value );
     2310                                        if ( is_wp_error( $option_validity ) ) {
     2311                                                $late_validity = $option_validity;
     2312                                        }
     2313                                }
     2314
    23052315                                /** This filter is documented in wp-includes/class-wp-customize-setting.php */
    2306                                 $late_validity = apply_filters( "customize_validate_{$setting->id}", new WP_Error(), $unsanitized_value, $setting );
     2316                                $late_validity = apply_filters( "customize_validate_{$setting->id}", $late_validity, $unsanitized_value, $setting );
    23072317                                if ( is_wp_error( $late_validity ) && $late_validity->has_errors() ) {
    23082318                                        $validity = $late_validity;
    23092319                                }
  • 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

     
    43814381}
    43824382
    43834383/**
     4384 * Validates an option value based on the nature of the option.
     4385 *
     4386 * The {@see 'validate_option_$option'} action should be used to add errors
     4387 * to the `WP_Error` object passed-through.
     4388 *
     4389 * @since 5.0.0
     4390 *
     4391 * @param string $option The name of the option.
     4392 * @param string $value  The unsanitized value.
     4393 * @return true|WP_Error True if the input was validated, otherwise WP_Error.
     4394 */
     4395function validate_option( $option, $value ) {
     4396        $errors = new WP_Error();
     4397
     4398        /**
     4399         * Validates an option value.
     4400         *
     4401         * Plugins should amend the `$errors` object via its `WP_Error::add()` method.
     4402         *
     4403         * The dynamic portion of the hook name, `$option`, refers to the option name.
     4404         *
     4405         * @since 5.0.0
     4406         *
     4407         * @param WP_Error $errors Error object to add validation errors to.
     4408         * @param mixed    $value  The option value.
     4409         */
     4410        do_action( "validate_option_{$option}", $errors, $value );
     4411
     4412        if ( empty( $errors->errors ) ) {
     4413                return true;
     4414        }
     4415
     4416        return $errors;
     4417}
     4418
     4419/**
    43844420 * Sanitises various option values based on the nature of the option.
    43854421 *
    43864422 * This is basically a switch statement which will pass $value through a number
  • src/wp-includes/option.php

     
    20752075 *
    20762076 * @since 2.7.0
    20772077 * @since 4.7.0 `$args` can be passed to set flags on the setting, similar to `register_meta()`.
     2078 * @since 5.0.0 Introduced the `$validate_callback` argument.
    20782079 *
    20792080 * @global array $new_whitelist_options
    20802081 * @global array $wp_registered_settings
     
    20882089 *     @type string   $type              The type of data associated with this setting.
    20892090 *                                       Valid values are 'string', 'boolean', 'integer', and 'number'.
    20902091 *     @type string   $description       A description of the data attached to this setting.
     2092 *     @type callable $validate_callback A callback that checks validity of the option's value.
    20912093 *     @type callable $sanitize_callback A callback function that sanitizes the option's value.
    20922094 *     @type bool     $show_in_rest      Whether data associated with this setting should be included in the REST API.
    20932095 *     @type mixed    $default           Default value when calling `get_option()`.
     
    21002102                'type'              => 'string',
    21012103                'group'             => $option_group,
    21022104                'description'       => '',
     2105                'validate_callback' => null,
    21032106                'sanitize_callback' => null,
    21042107                'show_in_rest'      => false,
    21052108        );
     
    21552158        }
    21562159
    21572160        $new_whitelist_options[ $option_group ][] = $option_name;
     2161        if ( ! empty( $args['validate_callback'] ) ) {
     2162                add_action( "validate_option_{$option_name}", $args['validate_callback'], 10, 2 );
     2163        }
    21582164        if ( ! empty( $args['sanitize_callback'] ) ) {
    21592165                add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] );
    21602166        }
     
    22252231        }
    22262232
    22272233        if ( isset( $wp_registered_settings[ $option_name ] ) ) {
     2234                // Remove the validate callback if one was set during registration.
     2235                if ( ! empty( $wp_registered_settings[ $option_name ]['validate_callback'] ) ) {
     2236                        remove_filter( "validate_option_{$option_name}", $wp_registered_settings[ $option_name ]['validate_callback'], 10 );
     2237                }
     2238
    22282239                // Remove the sanitize callback if one was set during registration.
    22292240                if ( ! empty( $wp_registered_settings[ $option_name ]['sanitize_callback'] ) ) {
    22302241                        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

     
    197197
    198198                                delete_option( $args['option_name'] );
    199199                        } else {
     200                                $validity = validate_option( $args['option_name'], $request[ $name ] );
     201                                if ( is_wp_error( $validity ) ) {
     202                                        foreach ( $validity->errors as $code => $messages ) {
     203                                                $validity->add_data( array( 'status' => 400 ), $code );
     204                                        }
     205
     206                                        return $validity;
     207                                }
     208
    200209                                update_option( $args['option_name'], $request[ $name ] );
    201210                        }
    202211                }