PluginProbe
Depicter — Popup & Slider Builder / 1.9.2
Depicter — Popup & Slider Builder v1.9.2
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
← All changes | app/src/Services/ImportService.php +47 -35 1.3.81.9.2 View file →
@@ -1,11 +1,9 @@
1 1 <?php
2 2 namespace Depicter\Services;
3 3
4 -
5 -use Averta\WordPress\File\FileSystem;
6 -use Averta\WordPress\File\UploadsDirectory;
7 4 use Averta\WordPress\Utility\Sanitize;
5 +use GuzzleHttp\Psr7\UploadedFile;
8 6
9 7 class ImportService
10 8 {
11 9
@@ -20,21 +18,31 @@
20 18 * @return bool
21 19 */
22 20 public function unpack( $file ) {
23 21
24 - $wp_upload_dir = new UploadsDirectory();
25 - $fileSystem = new FileSystem();
22 + $wp_upload_dir = \Depicter::storage()->uploads();
23 + $fileSystem = \Depicter::storage()->filesystem();
26 24
27 25 $depicterUploadPath = \Depicter::storage()->getPluginUploadsDirectory() . '/';
28 - $uploadedZipFilePath = \Depicter::storage()->getPluginUploadsDirectory() . '/' . $file->getClientFilename();
29 26
30 27 try{
31 - if ( !is_dir( $depicterUploadPath ) ) {
32 - $fileSystem->mkdir( $depicterUploadPath );
28 +
29 + if ( $file instanceof UploadedFile ) {
30 + $uploadedZipFilePath = \Depicter::storage()->getPluginUploadsDirectory() . '/' . $file->getClientFilename();
31 + if ( !is_dir( $depicterUploadPath ) ) {
32 + $fileSystem->mkdir( $depicterUploadPath );
33 + }
34 +
35 + // move uploaded zip file from temp directory to depicter folder inside wp uploads directory and extract it
36 + $file->moveTo( $uploadedZipFilePath );
37 +
38 + } else {
39 + $uploadedZipFilePath = $file;
40 + if ( ! $fileSystem->isFile( $file ) ) {
41 + return false;
42 + }
33 43 }
34 44
35 - // move uploaded zip file from temp directory to depicter folder inside wp uploads directory and extract it
36 - $file->moveTo( $uploadedZipFilePath );
37 45 $zipFile = new \ZipArchive();
38 46 $zipFile->open( $uploadedZipFilePath );
39 47 $zipFile->extractTo($depicterUploadPath . $this->importFolderName );
40 48 $zipFile->close();
@@ -41,9 +49,11 @@
41 49
42 50 $importedAssetIDs = $this->importAssets( $fileSystem, $wp_upload_dir );
43 51 $this->importSlider( $importedAssetIDs, $depicterUploadPath );
44 52
45 - unlink( $uploadedZipFilePath );
53 + if ( $file instanceof UploadedFile ) {
54 + unlink( $uploadedZipFilePath );
55 + }
46 56 $fileSystem->rmdir( $depicterUploadPath . $this->importFolderName, true );
47 57
48 58 return true;
49 59
@@ -65,34 +75,36 @@
65 75 $uploadPath = \Depicter::storage()->getPluginUploadsDirectory() . '/';
66 76
67 77 // scan assets directory to import assets
68 78 $assets = $fileSystem->scan( $uploadPath . $this->importFolderName . '/' . $this->assetsFolderName );
69 - foreach( $assets as $asset ) {
70 - $assetMimeType = wp_check_filetype( $asset['name'] )['type'];
71 - if ( !in_array( $assetMimeType, $allowedMimeTypes ) ) {
72 - continue;
73 - }
74 - $sanitizedFileName = Sanitize::fileName( $asset['name'] );
75 - $fileSystem->move( $uploadPath . $this->importFolderName . '/' . $this->assetsFolderName . '/' . $asset['name'], $uploadDirectory->getPath() . "/" . $sanitizedFileName );
76 - $attachmentTitle = preg_replace( '/\.[^.]+$/', '', $sanitizedFileName );
77 - $attachment = array(
78 - 'guid' => $uploadDirectory->getUrl() . '/' . $sanitizedFileName,
79 - 'post_mime_type' => $assetMimeType,
80 - 'post_title' => $attachmentTitle,
81 - 'post_content' => '',
82 - 'post_status' => 'inherit'
83 - );
79 + if ( $assets ) {
80 + foreach( $assets as $asset ) {
81 + $assetMimeType = wp_check_filetype( $asset['name'] )['type'];
82 + if ( !in_array( $assetMimeType, $allowedMimeTypes ) ) {
83 + continue;
84 + }
85 + $sanitizedFileName = Sanitize::fileName( $asset['name'] );
86 + $fileSystem->move( $uploadPath . $this->importFolderName . '/' . $this->assetsFolderName . '/' . $asset['name'], $uploadDirectory->getPath() . "/" . $sanitizedFileName );
87 + $attachmentTitle = preg_replace( '/\.[^.]+$/', '', $sanitizedFileName );
88 + $attachment = array(
89 + 'guid' => $uploadDirectory->getUrl() . '/' . $sanitizedFileName,
90 + 'post_mime_type' => $assetMimeType,
91 + 'post_title' => $attachmentTitle,
92 + 'post_content' => '',
93 + 'post_status' => 'inherit'
94 + );
84 95
85 - $attachID = wp_insert_attachment( $attachment, $sanitizedFileName );
86 - if ( !is_wp_error( $attachID ) ) {
87 - // generate meta data for the inserted attachment
88 - $attachment_metadata = wp_generate_attachment_metadata( $attachID, $uploadDirectory->getPath() . "/" . $sanitizedFileName );
89 - wp_update_attachment_metadata( $attachID, $attachment_metadata );
90 - update_attached_file( $attachID, $uploadDirectory->getPath() . "/" . $sanitizedFileName );
96 + $attachID = wp_insert_attachment( $attachment, $sanitizedFileName );
97 + if ( !is_wp_error( $attachID ) ) {
98 + // generate meta data for the inserted attachment
99 + $attachment_metadata = wp_generate_attachment_metadata( $attachID, $uploadDirectory->getPath() . "/" . $sanitizedFileName );
100 + wp_update_attachment_metadata( $attachID, $attachment_metadata );
101 + update_attached_file( $attachID, $uploadDirectory->getPath() . "/" . $sanitizedFileName );
91 102
92 - $attachmentTitleParts = explode( '-', $attachmentTitle );
93 - $oldID = end( $attachmentTitleParts );
94 - $importedIDs[ $oldID ] = $attachID;
103 + $attachmentTitleParts = explode( '-', $attachmentTitle );
104 + $oldID = end( $attachmentTitleParts );
105 + $importedIDs[ $oldID ] = $attachID;
106 + }
95 107 }
96 108 }
97 109
98 110 return $importedIDs;