예제 #1
1
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 = wp_parse_args($file, $attachment);
    }
    // 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 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));
        }
    }
    // filter for 3rd party customization
    $errors = apply_filters("acf/validate_attachment", $errors, $file, $attachment, $field);
    $errors = apply_filters("acf/validate_attachment/type={$field['type']}", $errors, $file, $attachment, $field);
    $errors = apply_filters("acf/validate_attachment/name={$field['name']}", $errors, $file, $attachment, $field);
    $errors = apply_filters("acf/validate_attachment/key={$field['key']}", $errors, $file, $attachment, $field);
    // return
    return $errors;
}
예제 #2
0
파일: media.php 프로젝트: jewbre/vdlhr
 function handle_upload_prefilter($file)
 {
     // bail early if no acf field
     if (empty($_POST['_acfuploader'])) {
         return $file;
     }
     // load field
     $field = acf_get_field($_POST['_acfuploader']);
     if (!$field) {
         return $file;
     }
     // vars
     $errors = array();
     // image
     if (strpos($file['type'], 'image') !== false) {
         // vars
         $size = getimagesize($file['tmp_name']);
         $width = acf_maybe_get($size, 0);
         $height = acf_maybe_get($size, 1);
         // width
         // * test if width exists to allow .svg to fail gracefully
         if ($width) {
             if (!empty($field['min_width']) && $width < $field['min_width']) {
                 // min width
                 $errors['min_width'] = sprintf(__('Image width must be at least %dpx.', 'acf'), $field['min_width']);
             } elseif (!empty($field['max_width']) && $width > $field['max_width']) {
                 // min width
                 $errors['max_width'] = sprintf(__('Image width must not exceed %dpx.', 'acf'), $field['max_width']);
             }
         }
         // height
         if ($height) {
             if (!empty($field['min_height']) && $height < $field['min_height']) {
                 // min height
                 $errors['min_height'] = sprintf(__('Image height must be at least %dpx.', 'acf'), $field['min_height']);
             } elseif (!empty($field['max_height']) && $height > $field['max_height']) {
                 // min height
                 $errors['max_height'] = sprintf(__('Image height must not exceed %dpx.', 'acf'), $field['max_height']);
             }
         }
     }
     // file size
     $filesize = filesize($file['tmp_name']);
     if (!empty($field['min_size']) && $filesize < acf_get_filesize($field['min_size'])) {
         // min width
         $errors['min_size'] = sprintf(__('File size must be at least %s.', 'acf'), acf_format_filesize($field['min_size']));
     } elseif (!empty($field['max_size']) && $filesize > acf_get_filesize($field['max_size'])) {
         // min width
         $errors['max_size'] = sprintf(__('File size must must not exceed %s.', 'acf'), acf_format_filesize($field['max_size']));
     }
     // file type
     if (!empty($field['mime_types'])) {
         $types = str_replace(array(' ', '.'), '', $field['mime_types']);
         $types = explode(',', $types);
         if (!in_array(pathinfo($file['name'], PATHINFO_EXTENSION), $types)) {
             // glue together last 2 types
             if (count($types) > 1) {
                 $last1 = array_pop($types);
                 $last2 = array_pop($types);
                 $types[] = $last2 . ' ' . __('or', 'acf') . ' ' . $last1;
             }
             $errors['mime_types'] = sprintf(__('File type must be %s.', 'acf'), implode(', ', $types));
         }
     }
     // filter for 3rd party customization
     $errors = apply_filters("acf/upload_prefilter", $errors, $file, $field);
     $errors = apply_filters("acf/upload_prefilter/type={$field['type']}", $errors, $file, $field);
     $errors = apply_filters("acf/upload_prefilter/name={$field['name']}", $errors, $file, $field);
     $errors = apply_filters("acf/upload_prefilter/key={$field['key']}", $errors, $file, $field);
     // append error
     if (!empty($errors)) {
         $file['error'] = implode("\n", $errors);
     }
     // return
     return $file;
 }