WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru Получай пассивный доход от сайта
функция не описана

acf_validate_attachment() ACF 5.2.3

This function will validate an attachment based on a field's resrictions and return an array of errors

Возвращает

$errors. (array)

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

acf_validate_attachment( $attachment, $field, $context );
$attachment (обязательный)
-
$field (обязательный)
-
$context **
-
По умолчанию: 'prepare'

Список изменений

С версии 5.2.3 Введена.

Код acf_validate_attachment() ACF 5.9.1

<?php
function acf_validate_attachment( $attachment, $field, $context = 'prepare' ) {
	
	// vars
	$errors = array();
	$file = array(
		'type'		=> '',
		'width'		=> 0,
		'height'	=> 0,
		'size'		=> 0
	);
	
	
	// upload
	if( $context == 'upload' ) {
		
		// vars
		$file['type'] = pathinfo($attachment['name'], PATHINFO_EXTENSION);
		$file['size'] = filesize($attachment['tmp_name']);
		
		if( strpos($attachment['type'], 'image') !== false ) {
			
			$size = getimagesize($attachment['tmp_name']);
			$file['width'] = acf_maybe_get($size, 0);
			$file['height'] = acf_maybe_get($size, 1);
				
		}
	
	// prepare
	} elseif( $context == 'prepare' ) {
		
		$file['type'] = pathinfo($attachment['url'], PATHINFO_EXTENSION);
		$file['size'] = acf_maybe_get($attachment, 'filesizeInBytes', 0);
		$file['width'] = acf_maybe_get($attachment, 'width', 0);
		$file['height'] = acf_maybe_get($attachment, 'height', 0);
	
	// custom
	} else {
		
		$file = array_merge($file, $attachment);
		$file['type'] = pathinfo($attachment['url'], PATHINFO_EXTENSION);
		
	}
	
	
	// image
	if( $file['width'] || $file['height'] ) {
		
		// width
		$min_width = (int) acf_maybe_get($field, 'min_width', 0);
		$max_width = (int) acf_maybe_get($field, 'max_width', 0);
		
		if( $file['width'] ) {
			
			if( $min_width && $file['width'] < $min_width ) {
				
				// min width
				$errors['min_width'] = sprintf(__('Image width must be at least %dpx.', 'acf'), $min_width );
				
			} elseif( $max_width && $file['width'] > $max_width ) {
				
				// min width
				$errors['max_width'] = sprintf(__('Image width must not exceed %dpx.', 'acf'), $max_width );
				
			}
			
		}
		
		
		// height
		$min_height = (int) acf_maybe_get($field, 'min_height', 0);
		$max_height = (int) acf_maybe_get($field, 'max_height', 0);
		
		if( $file['height'] ) {
			
			if( $min_height && $file['height'] < $min_height ) {
				
				// min height
				$errors['min_height'] = sprintf(__('Image height must be at least %dpx.', 'acf'), $min_height );
				
			}  elseif( $max_height && $file['height'] > $max_height ) {
				
				// min height
				$errors['max_height'] = sprintf(__('Image height must not exceed %dpx.', 'acf'), $max_height );
				
			}
			
		}
			
	}
	
	
	// file size
	if( $file['size'] ) {
		
		$min_size = acf_maybe_get($field, 'min_size', 0);
		$max_size = acf_maybe_get($field, 'max_size', 0);
		
		if( $min_size && $file['size'] < acf_get_filesize($min_size) ) {
				
			// min width
			$errors['min_size'] = sprintf(__('File size must be at least %s.', 'acf'), acf_format_filesize($min_size) );
			
		} elseif( $max_size && $file['size'] > acf_get_filesize($max_size) ) {
				
			// min width
			$errors['max_size'] = sprintf(__('File size must not exceed %s.', 'acf'), acf_format_filesize($max_size) );
			
		}
	
	}
	
	
	// file type
	if( $file['type'] ) {
		
		$mime_types = acf_maybe_get($field, 'mime_types', '');
		
		// lower case
		$file['type'] = strtolower($file['type']);
		$mime_types = strtolower($mime_types);
		
		
		// explode
		$mime_types = str_replace(array(' ', '.'), '', $mime_types);
		$mime_types = explode(',', $mime_types); // split pieces
		$mime_types = array_filter($mime_types); // remove empty pieces
		
		if( !empty($mime_types) && !in_array($file['type'], $mime_types) ) {
			
			// glue together last 2 types
			if( count($mime_types) > 1 ) {
				
				$last1 = array_pop($mime_types);
				$last2 = array_pop($mime_types);
				
				$mime_types[] = $last2 . ' ' . __('or', 'acf') . ' ' . $last1;
				
			}
			
			$errors['mime_types'] = sprintf(__('File type must be %s.', 'acf'), implode(', ', $mime_types) );
			
		}
				
	}
	
	
	/**
	*  Filters the errors for a file before it is uploaded or displayed in the media modal.
	*
	*  @date	3/07/2015
	*  @since	5.2.3
	*
	*  @param	array $errors An array of errors.
	*  @param	array $file An array of data for a single file.
	*  @param	array $attachment An array of attachment data which differs based on the context.
	*  @param	array $field The field array.
	*  @param	string $context The curent context (uploading, preparing)
	*/
	$errors = apply_filters( "acf/validate_attachment/type={$field['type']}",	$errors, $file, $attachment, $field, $context );
	$errors = apply_filters( "acf/validate_attachment/name={$field['_name']}", 	$errors, $file, $attachment, $field, $context );
	$errors = apply_filters( "acf/validate_attachment/key={$field['key']}", 	$errors, $file, $attachment, $field, $context );
	$errors = apply_filters( "acf/validate_attachment", 						$errors, $file, $attachment, $field, $context );
	
	
	// return
	return $errors;
	
}