acf_field_flexible_content::validate_rest_value()publicACF 1.0

Additional validation for the flexible content field when submitted via REST.

Метод класса: acf_field_flexible_content{}

Хуков нет.

Возвращает

true|false|WP_Error.

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

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

Код acf_field_flexible_content::validate_rest_value() ACF 6.0.4

public function validate_rest_value( $valid, $value, $field ) {
	$param = sprintf( '%s[%s]', $field['prefix'], $field['name'] );
	$data  = array(
		'param' => $param,
		'value' => $value,
	);

	if ( ! is_array( $value ) && is_null( $value ) ) {
		$error = sprintf( __( '%s must be of type array or null.', 'acf' ), $param );
		return new WP_Error( 'rest_invalid_param', $error, $param );
	}

	$layouts_to_update = array_count_values( array_column( $value, 'acf_fc_layout' ) );

	foreach ( $field['layouts'] as $layout ) {
		$num_layouts = isset( $layouts_to_update[ $layout['name'] ] ) ? $layouts_to_update[ $layout['name'] ] : 0;

		if ( '' !== $layout['min'] && $num_layouts < (int) $layout['min'] ) {
			$error = sprintf(
				_n(
					'%1$s must contain at least %2$s %3$s layout.',
					'%1$s must contain at least %2$s %3$s layouts.',
					$layout['min'],
					'acf'
				),
				$param,
				number_format_i18n( $layout['min'] ),
				$layout['name']
			);

			return new WP_Error( 'rest_invalid_param', $error, $data );
		}

		if ( '' !== $layout['max'] && $num_layouts > (int) $layout['max'] ) {
			$error = sprintf(
				_n(
					'%1$s must contain at most %2$s %3$s layout.',
					'%1$s must contain at most %2$s %3$s layouts.',
					$layout['max'],
					'acf'
				),
				$param,
				number_format_i18n( $layout['max'] ),
				$layout['name']
			);

			return new WP_Error( 'rest_invalid_param', $error, $data );
		}
	}

	return $valid;
}