| 1 |
<?php |
| 2 |
namespace Depicter\WordPress; |
| 3 |
|
| 4 |
use Averta\WordPress\File\UploadsDirectory; |
| 5 |
use Depicter\Utility\Sanitize; |
| 6 |
use GuzzleHttp\Psr7\UploadedFile; |
| 7 |
|
| 8 |
class FileUploaderService |
| 9 |
{ |
| 10 |
public function upload( array $files ) { |
| 11 |
$results = []; |
| 12 |
$wp_upload_dir = new UploadsDirectory(); |
| 13 |
$allowedMimeTypes = array_values( get_allowed_mime_types() ); |
| 14 |
foreach( $files as $file ) { |
| 15 |
if ( ! $file instanceof UploadedFile ) { |
| 16 |
continue; |
| 17 |
} |
| 18 |
|
| 19 |
if ( $file->getError() ) { |
| 20 |
$results[ $file->getClientFilename() ] = [ |
| 21 |
'attachment' => 0, |
| 22 |
'errors' => [ |
| 23 |
/* translators: maximum file size allowed to upload */ |
| 24 |
sprintf( __( 'Cannot upload the file, because max permitted file upload size is %s.', 'depicter' ), ini_get('upload_max_filesize') ) |
| 25 |
] |
| 26 |
]; |
| 27 |
continue; |
| 28 |
} |
| 29 |
|
| 30 |
$clientFileName = Sanitize::fileName( $file->getClientFilename() ); |
| 31 |
// make sure that file mime type is allowed and the extension is not .php |
| 32 |
if ( !in_array( $file->getClientMediaType(), $allowedMimeTypes ) || substr( $clientFileName, -4 ) === '.php' ) { |
| 33 |
$results[ $file->getClientFilename() ] = [ |
| 34 |
'attachment' => 0, |
| 35 |
'errors' => [ |
| 36 |
/* translators: mimType of uploading file */ |
| 37 |
sprintf( __( 'Cannot upload the file, uploading %s files are not allowed.', 'depicter' ), $file->getClientMediaType() ) |
| 38 |
] |
| 39 |
]; |
| 40 |
continue; |
| 41 |
} |
| 42 |
|
| 43 |
$filename = $wp_upload_dir->getPath() . "/" . $clientFileName; |
| 44 |
$file->moveTo( $filename ); |
| 45 |
$attachment = array( |
| 46 |
'guid' => $wp_upload_dir->getUrl() . '/' . basename( $filename ), |
| 47 |
'post_mime_type' => $file->getClientMediaType(), |
| 48 |
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), |
| 49 |
'post_content' => '', |
| 50 |
'post_status' => 'inherit' |
| 51 |
); |
| 52 |
|
| 53 |
$attach_id = wp_insert_attachment( $attachment, $filename ); |
| 54 |
|
| 55 |
if ( !is_wp_error( $attach_id ) ) { |
| 56 |
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. |
| 57 |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
| 58 |
|
| 59 |
// Generate the metadata for the attachment, and update the database record. |
| 60 |
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); |
| 61 |
wp_update_attachment_metadata( $attach_id, $attach_data ); |
| 62 |
$results[ $file->getClientFilename() ] = [ |
| 63 |
'attachment' => $attach_id, |
| 64 |
'errors' => [] |
| 65 |
]; |
| 66 |
} else { |
| 67 |
$results[ $file->getClientFilename() ] = [ |
| 68 |
'attachment' => 0, |
| 69 |
'errors' => [ |
| 70 |
$attach_id['error'] |
| 71 |
] |
| 72 |
]; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return $results; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Check if user can upload unfiltered Data or not |
| 81 |
* |
| 82 |
* @return boolean |
| 83 |
*/ |
| 84 |
public function canUploadUnfilteredData() { |
| 85 |
$mimes = get_allowed_mime_types(); |
| 86 |
return array_key_exists('svg', $mimes) && array_key_exists('json', $mimes); |
| 87 |
} |
| 88 |
} |
| 89 |
|