acf_validate_is_image_attachment_upload()ACF 6.8.7

Validates image attachment data during upload.

Returns

Array.

Usage

acf_validate_is_image_attachment_upload( $errors, $file, $attachment, $field, $context, $error_message );
$errors(array) (required)
Validation errors.
$file(array) (required)
Normalized file data.
$attachment(array) (required)
Raw attachment data for the current context.
$field(array) (required)
The field array.
$context(string) (required)
The validation context.
$error_message(string) (required)
The error message to use when validation fails.

Changelog

Since 6.8.7 Introduced.

acf_validate_is_image_attachment_upload() code ACF 6.8.8

function acf_validate_is_image_attachment_upload( $errors, $file, $attachment, $field, $context, $error_message ) {
	if ( empty( $attachment['tmp_name'] ) || ! file_exists( $attachment['tmp_name'] ) ) {
		$errors['invalid_image'] = $error_message;

		return acf_apply_validate_is_image_attachment_filters( $errors, $file, $attachment, $field, $context );
	}

	$checked = wp_check_filetype_and_ext( $attachment['tmp_name'], $attachment['name'], acf_get_image_mime_types( $field ) );

	if ( empty( $checked['ext'] ) || empty( $checked['type'] ) || empty( wp_match_mime_types( 'image', $checked['type'] ) ) ) {
		$errors['invalid_image'] = $error_message;

		return acf_apply_validate_is_image_attachment_filters( $errors, $file, $attachment, $field, $context );
	}

	/**
	 * Filters the list of image MIME types that cannot be validated by
	 * wp_get_image_mime(), which reads a binary magic number and only
	 * recognizes raster formats. Vector / XML image types (e.g. SVG)
	 * always fail that check by design and must be excluded from it.
	 *
	 * @since 6.8.8
	 *
	 * @param array $vector_mime_types MIME types to skip the binary magic-number check for.
	 */
	$vector_mime_types = (array) apply_filters( 'acf/validate_is_image_attachment/vector_mime_types', array( 'image/svg+xml' ) );

	if ( ! in_array( $checked['type'], $vector_mime_types, true ) ) {
		$real_mime = wp_get_image_mime( $attachment['tmp_name'] );

		if ( ! $real_mime || empty( wp_match_mime_types( 'image', $real_mime ) ) ) {
			$errors['invalid_image'] = $error_message;
		}
	}

	return acf_apply_validate_is_image_attachment_filters( $errors, $file, $attachment, $field, $context );
}