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
depicter / app / src / Services / ImportService.php

ImportService.php in Depicter — Popup & Slider Builder 1.9.2, at app/src/Services/ImportService.php

143 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Services;
3
4 use Averta\WordPress\Utility\Sanitize;
5 use GuzzleHttp\Psr7\UploadedFile;
6
7 class ImportService
8 {
9
10 protected $importFolderName = 'import';
11
12 protected $assetsFolderName = 'assets';
13
14 /**
15 * Extract uploaded zip file and import slider
16 * @param $file
17 *
18 * @return bool
19 */
20 public function unpack( $file ) {
21
22 $wp_upload_dir = \Depicter::storage()->uploads();
23 $fileSystem = \Depicter::storage()->filesystem();
24
25 $depicterUploadPath = \Depicter::storage()->getPluginUploadsDirectory() . '/';
26
27 try{
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 }
43 }
44
45 $zipFile = new \ZipArchive();
46 $zipFile->open( $uploadedZipFilePath );
47 $zipFile->extractTo($depicterUploadPath . $this->importFolderName );
48 $zipFile->close();
49
50 $importedAssetIDs = $this->importAssets( $fileSystem, $wp_upload_dir );
51 $this->importSlider( $importedAssetIDs, $depicterUploadPath );
52
53 if ( $file instanceof UploadedFile ) {
54 unlink( $uploadedZipFilePath );
55 }
56 $fileSystem->rmdir( $depicterUploadPath . $this->importFolderName, true );
57
58 return true;
59
60 } catch( \Exception $e ) {
61 return false;
62 }
63 }
64
65 /**
66 * Import available assets inside assets directory
67 * @param $fileSystem
68 * @param $uploadDirectory
69 *
70 * @return array $importedIDs
71 */
72 protected function importAssets( $fileSystem, $uploadDirectory ) {
73 $allowedMimeTypes = array_values( get_allowed_mime_types() );
74 $importedIDs = [];
75 $uploadPath = \Depicter::storage()->getPluginUploadsDirectory() . '/';
76
77 // scan assets directory to import assets
78 $assets = $fileSystem->scan( $uploadPath . $this->importFolderName . '/' . $this->assetsFolderName );
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 );
95
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 );
102
103 $attachmentTitleParts = explode( '-', $attachmentTitle );
104 $oldID = end( $attachmentTitleParts );
105 $importedIDs[ $oldID ] = $attachID;
106 }
107 }
108 }
109
110 return $importedIDs;
111 }
112
113 /**
114 * Import Slider
115 *
116 * @param $importedIDs
117 * @param $uploadPath
118 *
119 * @return mixed|null
120 * @throws \Exception
121 */
122 protected function importSlider( $importedIDs, $uploadPath ) {
123 $content = file_get_contents( $uploadPath . $this->importFolderName . '/data.json' );
124 $content = preg_replace( '/"activeBreakpoint":".+?"/', '"activeBreakpoint":"default"', $content );
125 preg_match_all( '/\"(source|src)\":\"(\d+)\"/', $content, $assets, PREG_SET_ORDER );
126 if ( !empty( $assets ) ) {
127 foreach( $assets as $asset ) {
128 if ( !empty( $asset[2] ) && !empty( $importedIDs[ $asset[2] ] ) ) {
129 $content = str_replace( $asset[0], '"' . $asset[1] . '":"'. $importedIDs[ $asset[2] ] .'"', $content );
130 }
131 }
132 }
133
134 $document = \Depicter::documentRepository()->create();
135 $document->update([
136 'content' => $content,
137 'status' => 'publish'
138 ]);
139
140 return $document->id;
141 }
142 }
143