Ticket #43208: 43208.diff
File 43208.diff, 3.8 KB (added by , 6 years ago) |
---|
-
src/wp-includes/formatting.php
4573 4573 if ( function_exists( 'add_settings_error' ) ) { 4574 4574 add_settings_error( $option, "invalid_{$option}", $error ); 4575 4575 } 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 } 4576 4604 } 4577 4605 4578 4606 /** -
src/wp-includes/option.php
2032 2032 * 2033 2033 * @since 2.7.0 2034 2034 * @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. 2035 2036 * 2036 2037 * @global array $new_whitelist_options 2037 2038 * @global array $wp_registered_settings … … 2045 2046 * @type string $type The type of data associated with this setting. 2046 2047 * Valid values are 'string', 'boolean', 'integer', and 'number'. 2047 2048 * @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. 2048 2050 * @type callable $sanitize_callback A callback function that sanitizes the option's value. 2049 2051 * @type bool $show_in_rest Whether data associated with this setting should be included in the REST API. 2050 2052 * @type mixed $default Default value when calling `get_option()`. … … 2057 2059 'type' => 'string', 2058 2060 'group' => $option_group, 2059 2061 'description' => '', 2062 'validate_callback' => null, 2060 2063 'sanitize_callback' => null, 2061 2064 'show_in_rest' => false, 2062 2065 ); … … 2110 2113 } 2111 2114 2112 2115 $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 } 2113 2119 if ( ! empty( $args['sanitize_callback'] ) ) { 2114 2120 add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] ); 2115 2121 } … … 2177 2183 } 2178 2184 2179 2185 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 2180 2191 // Remove the sanitize callback if one was set during registration. 2181 2192 if ( ! empty( $wp_registered_settings[ $option_name ]['sanitize_callback'] ) ) { 2182 2193 remove_filter( "sanitize_option_{$option_name}", $wp_registered_settings[ $option_name ]['sanitize_callback'] );