WC_REST_Controller::validate_setting_multiselect_field()publicWC 3.0.0

Validate multiselect based settings.

Method of the class: WC_REST_Controller{}

No Hooks.

Return

Array|WP_Error.

Usage

$WC_REST_Controller = new WC_REST_Controller();
$WC_REST_Controller->validate_setting_multiselect_field( $values, $setting );
$values(array) (required)
Values.
$setting(array) (required)
Setting.

Changelog

Since 3.0.0 Introduced.

WC_REST_Controller::validate_setting_multiselect_field() code WC 8.6.1

public function validate_setting_multiselect_field( $values, $setting ) {
	if ( empty( $values ) ) {
		return array();
	}

	if ( ! is_array( $values ) ) {
		return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'woocommerce' ), array( 'status' => 400 ) );
	}

	$final_values = array();
	foreach ( $values as $value ) {
		if ( array_key_exists( $value, $setting['options'] ) ) {
			$final_values[] = $value;
		}
	}

	return $final_values;
}