Skip to content

Commit 8faa835

Browse files
authored
Merge pull request #55 from onvardgmbh/allowed-mime-types
Allowed mime types
2 parents 3b3c542 + 5dc43dc commit 8faa835

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ Formendpoint::make('application_form', __('ApplicationForm', 'wptheme'), 'bundle
205205
->add_fields([
206206
// [..]
207207
Input::make('file', 'avatar', __('Avatar', 'wptheme')),
208+
])
209+
//Define allowed mime types (this is optional)
210+
->setAllowedMimeTypes([
211+
'jpg' => 'image/jpeg',
212+
'png' => 'image/png',
208213
]);
209214
```
210215

‎src/Formendpoint.php‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ public function handleformsubmit()
183183
continue;
184184
}
185185
$uploadedFiles = [];
186+
$overrides = ['action' => $this->posttype];
187+
if (!empty($input->allowedMimeTypes)) {
188+
$overrides['mimes'] = $input->allowedMimeTypes;
189+
}
186190

187191
if ('files' === $input->type) {
188192
$files = $_FILES[$input->name];
@@ -195,15 +199,15 @@ public function handleformsubmit()
195199
'error' => $files['error'][$key],
196200
'size' => $files['size'][$key],
197201
];
198-
$file = wp_handle_upload($file, ['action' => $this->posttype]);
202+
$file = wp_handle_upload($file, $overrides);
199203
if (!$file || isset($file['error'])) {
200204
wp_die('Fileupload failed.', '', ['response' => 400]);
201205
}
202206
$uploadedFiles[] = $file;
203207
}
204208
}
205209
} else {
206-
$file = wp_handle_upload($_FILES[$input->name], ['action' => $this->posttype]);
210+
$file = wp_handle_upload($_FILES[$input->name], $overrides);
207211
if (!$file || isset($file['error'])) {
208212
wp_die('Fileupload failed.', '', ['response' => 400]);
209213
}

‎src/Input.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Input
1313
public $validationFunction;
1414
public $title;
1515
public $type;
16+
public $allowedMimeTypes;
1617

1718
public static function make($type, $name, $label = null)
1819
{
@@ -74,6 +75,13 @@ public function setFormat($callback)
7475
return $this;
7576
}
7677

78+
public function setAllowedMimeTypes($array)
79+
{
80+
$this->allowedMimeTypes = $array;
81+
82+
return $this;
83+
}
84+
7785
public function hide()
7886
{
7987
$this->hide = true;

0 commit comments

Comments
 (0)