GC_Customize_Manager::validate_setting_values()

最后更新于:2021-11-27 02:42:15

GC_Customize_Manager::validate_setting_values( array$setting_values, array$options=array())

Validates setting values.

参数

$setting_values

(array) (Required) Mapping of setting IDs to values to validate and sanitize.

$options

(array) (Optional) Options.

  • ‘validate_existence’
    (bool) Whether a setting’s existence will be checked.
  • ‘validate_capability’
    (bool) Whether the setting capability will be checked.

Default value: array()

响应

(array) Mapping of setting IDs to return value of validate method calls, either true or GC_Error.

源文件

文件: gc-includes/class-gc-customize-manager.php

	public function validate_setting_values( $setting_values, $options = array() ) {
		$options = gc_parse_args(
			$options,
			array(
				'validate_capability' => false,
				'validate_existence'  => false,
			)
		);

		$validities = array();
		foreach ( $setting_values as $setting_id => $unsanitized_value ) {
			$setting = $this->get_setting( $setting_id );
			if ( ! $setting ) {
				if ( $options['validate_existence'] ) {
					$validities[ $setting_id ] = new GC_Error( 'unrecognized', __( 'Setting does not exist or is unrecognized.' ) );
				}
				continue;
			}
			if ( $options['validate_capability'] && ! current_user_can( $setting->capability ) ) {
				$validity = new GC_Error( 'unauthorized', __( 'Unauthorized to modify setting due to capability.' ) );
			} else {
				if ( is_null( $unsanitized_value ) ) {
					continue;
				}
				$validity = $setting->validate( $unsanitized_value );
			}
			if ( ! is_gc_error( $validity ) ) {
				/** This filter is documented in gc-includes/class-gc-customize-setting.php */
				$late_validity = apply_filters( "customize_validate_{$setting->id}", new GC_Error(), $unsanitized_value, $setting );
				if ( is_gc_error( $late_validity ) && $late_validity->has_errors() ) {
					$validity = $late_validity;
				}
			}
			if ( ! is_gc_error( $validity ) ) {
				$value = $setting->sanitize( $unsanitized_value );
				if ( is_null( $value ) ) {
					$validity = false;
				} elseif ( is_gc_error( $value ) ) {
					$validity = $value;
				}
			}
			if ( false === $validity ) {
				$validity = new GC_Error( 'invalid_value', __( 'Invalid value.' ) );
			}
			$validities[ $setting_id ] = $validity;
		}
		return $validities;
	}