PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / WordPress / FileUploaderService.php

FileUploaderService.php in Depicter — Popup & Slider Builder trunk, at app/src/WordPress/FileUploaderService.php

89 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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