WordPress.org

Make WordPress Core

Ticket #43208: 43208.diff

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

     
    45734573                if ( function_exists( 'add_settings_error' ) ) {
    45744574                        add_settings_error( $option, "invalid_{$option}", $error );
    45754575                }
     4576        } else { // Value is not invalid per core definitions, so allow further validation to occur.
     4577                $validity = new WP_Error();
     4578
     4579                /**
     4580                 * Validates an option value.
     4581                 *
     4582                 * Plugins should amend the `$validity` object via its `WP_Error::add()` method.
     4583                 *
     4584                 * The dynamic portion of the hook name, `$option`, refers to the option name.
     4585                 *
     4586                 * @since 5.0.0
     4587                 *
     4588                 * @param WP_Error $validity       Filtered from `true` to `WP_Error` when invalid.
     4589                 * @param mixed    $value          The option value.
     4590                 * @param mixed    $original_value The original value passed to the function.
     4591                 */
     4592                $validity = apply_filters( "validate_option_{$option}", $validity, $value, $original_value );
     4593
     4594                if ( is_wp_error( $validity ) && ! empty( $validity->errors ) ) {
     4595                        $value = get_option( $option );
     4596                        if ( function_exists( 'add_settings_error' ) ) {
     4597                                foreach ( $validity->errors as $code => $messages ) {
     4598                                        foreach ( $messages as $message ) {
     4599                                                add_settings_error( $option, $code, $message );
     4600                                        }
     4601                                }
     4602                        }
     4603                }
    45764604        }
    45774605
    45784606        /**
  • 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'] );