65 lines
4.0 KiB
PHP
65 lines
4.0 KiB
PHP
<?php
|
|
if (!defined('ABSPATH')) exit;
|
|
class WMF_Field_File extends WMF_Field_Base {
|
|
public $type='file'; public $label='Datei-Upload'; public $icon='dashicons-upload';
|
|
public function defaults() { return array_merge(parent::defaults(),array('allowed_types'=>'jpg,jpeg,png,pdf,doc,docx','max_size_mb'=>'5','multiple'=>'0','save_to_media'=>'1')); }
|
|
public function render($field,$value='') { ?>
|
|
<div class="<?php echo esc_attr($this->wrapper_classes($field)); ?>"<?php echo $this->conditional_attrs($field); ?> data-field-id="<?php echo esc_attr($field['id']); ?>">
|
|
<?php $this->render_label($field); ?>
|
|
<input type="file" id="<?php echo esc_attr($field['id']); ?>" name="wmf_files[<?php echo esc_attr($field['id']); ?>]<?php echo(!empty($field['multiple'])&&$field['multiple']==='1')?'[]':''; ?>" accept="<?php echo esc_attr($this->accept_attr($field)); ?>" <?php echo(!empty($field['multiple'])&&$field['multiple']==='1')?'multiple':''; ?> <?php echo(!empty($field['required'])&&$field['required']==='1')?'required':''; ?> class="wmf-input wmf-file-input">
|
|
<p class="wmf-description">Erlaubt: <?php echo esc_html($field['allowed_types']??''); ?> — Max. <?php echo esc_html($field['max_size_mb']??5); ?> MB</p>
|
|
<?php $this->render_description($field); ?>
|
|
<span class="wmf-field-error-msg"></span>
|
|
</div><?php
|
|
}
|
|
private function accept_attr($field) {
|
|
$types=array_map('trim',explode(',',$field['allowed_types']??''));
|
|
return implode(',',array_map(fn($t)=>'.'.$t,$types));
|
|
}
|
|
public function validate($value,$field) {
|
|
$key=$field['id'];
|
|
if(!isset($_FILES['wmf_files']['error'][$key])) return true;
|
|
$err=$_FILES['wmf_files']['error'][$key];
|
|
if(is_array($err)) $err=array_filter($err,fn($e)=>$e!==UPLOAD_ERR_NO_FILE);
|
|
if(empty($err)||$err===UPLOAD_ERR_NO_FILE) {
|
|
if(!empty($field['required'])&&$field['required']==='1') return sprintf('Bitte laden Sie eine Datei für „%s" hoch.',$field['label']);
|
|
return true;
|
|
}
|
|
$max=intval($field['max_size_mb']??5)*1024*1024;
|
|
$sizes=$_FILES['wmf_files']['size'][$key];
|
|
if(is_array($sizes)) { foreach($sizes as $s) if($s>$max) return sprintf('Max. %s MB erlaubt.',$field['max_size_mb']??5); }
|
|
elseif($sizes>$max) return sprintf('Max. %s MB erlaubt.',$field['max_size_mb']??5);
|
|
return true;
|
|
}
|
|
public function sanitize($v,$f) { return $v; }
|
|
|
|
public static function handle_upload($field,$form_id) {
|
|
$key=$field['id'];
|
|
if(!isset($_FILES['wmf_files']['tmp_name'][$key])) return array();
|
|
require_once ABSPATH.'wp-admin/includes/image.php';
|
|
require_once ABSPATH.'wp-admin/includes/file.php';
|
|
require_once ABSPATH.'wp-admin/includes/media.php';
|
|
$tmp=$_FILES['wmf_files']['tmp_name'][$key];
|
|
$names=$_FILES['wmf_files']['name'][$key];
|
|
$results=array();
|
|
if(!is_array($tmp)) { $tmp=array($tmp); $names=array($names); }
|
|
foreach($tmp as $i=>$t) {
|
|
if(empty($t)||!is_uploaded_file($t)) continue;
|
|
$upload=wp_handle_upload(array(
|
|
'tmp_name'=>$t,'name'=>$names[$i],'type'=>mime_content_type($t),'error'=>0,'size'=>filesize($t)
|
|
),array('test_form'=>false));
|
|
if(isset($upload['url'])) {
|
|
if(!empty($field['save_to_media'])&&$field['save_to_media']==='1') {
|
|
$att=array('post_mime_type'=>$upload['type'],'post_title'=>sanitize_file_name($names[$i]),'post_content'=>'','post_status'=>'inherit');
|
|
$att_id=wp_insert_attachment($att,$upload['file']);
|
|
wp_update_attachment_metadata($att_id,wp_generate_attachment_metadata($att_id,$upload['file']));
|
|
$results[]=array('url'=>$upload['url'],'attachment_id'=>$att_id,'name'=>$names[$i]);
|
|
} else {
|
|
$results[]=array('url'=>$upload['url'],'name'=>$names[$i]);
|
|
}
|
|
}
|
|
}
|
|
return $results;
|
|
}
|
|
}
|