ACF_Field_User::validate_rest_value()publicACF 1.0

Validates user fields updated via the REST API.

{} Это метод класса: ACF_Field_User{}

Хуков нет.

Возвращает

true|false|WP_Error.

Использование

$ACF_Field_User = new ACF_Field_User();
$ACF_Field_User->validate_rest_value( $valid, $value, $field );
$valid(true|false) (обязательный)
-
$value(int) (обязательный)
-
$field(массив) (обязательный)
-

Код ACF_Field_User::validate_rest_value() ACF 6.0.4

public function validate_rest_value( $valid, $value, $field ) {
	if ( is_null( $value ) ) {
		return $valid;
	}

	$param = sprintf( '%s[%s]', $field['prefix'], $field['name'] );
	$data  = array( 'param' => $param );
	$value = is_array( $value ) ? $value : array( $value );

	$invalid_users      = array();
	$insufficient_roles = array();

	foreach ( $value as $user_id ) {
		$user_data = get_userdata( $user_id );
		if ( ! $user_data ) {
			$invalid_users[] = $user_id;
			continue;
		}

		if ( empty( $field['role'] ) ) {
			continue;
		}

		$has_roles = count( array_intersect( $field['role'], $user_data->roles ) );
		if ( ! $has_roles ) {
			$insufficient_roles[] = $user_id;
		}
	}

	if ( count( $invalid_users ) ) {
		$error         = sprintf(
			__( '%1$s must have a valid user ID.', 'acf' ),
			$param
		);
		$data['value'] = $invalid_users;
		return new WP_Error( 'rest_invalid_param', $error, $data );
	}

	if ( count( $insufficient_roles ) ) {
		$error         = sprintf(
			_n(
				'%1$s must have a user with the %2$s role.',
				'%1$s must have a user with one of the following roles: %2$s',
				count( $field['role'] ),
				'acf'
			),
			$param,
			count( $field['role'] ) > 1 ? implode( ', ', $field['role'] ) : $field['role'][0]
		);
		$data['value'] = $insufficient_roles;
		return new WP_Error( 'rest_invalid_param', $error, $data );
	}

	return $valid;
}