PluginProbe
Depicter — Popup & Slider Builder / 1.6.2
Depicter — Popup & Slider Builder v1.6.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.6.2, at app/src/Services/ImportService.php

131 lines 4.1 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
5 use Averta\WordPress\File\FileSystem;
6 use Averta\WordPress\File\UploadsDirectory;
7 use Averta\WordPress\Utility\Sanitize;
8
9 class ImportService
10 {
11
12 protected $importFolderName = 'import';
13
14 protected $assetsFolderName = 'assets';
15
16 /**
17 * Extract uploaded zip file and import slider
18 * @param $file
19 *
20 * @return bool
21 */
22 public function unpack( $file ) {
23
24 $wp_upload_dir = new UploadsDirectory();
25 $fileSystem = new FileSystem();
26
27 $depicterUploadPath = \Depicter::storage()->getPluginUploadsDirectory() . '/';
28 $uploadedZipFilePath = \Depicter::storage()->getPluginUploadsDirectory() . '/' . $file->getClientFilename();
29
30 try{
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 $zipFile = new \ZipArchive();
38 $zipFile->open( $uploadedZipFilePath );
39 $zipFile->extractTo($depicterUploadPath . $this->importFolderName );
40 $zipFile->close();
41
42 $importedAssetIDs = $this->importAssets( $fileSystem, $wp_upload_dir );
43 $this->importSlider( $importedAssetIDs, $depicterUploadPath );
44
45 unlink( $uploadedZipFilePath );
46 $fileSystem->rmdir( $depicterUploadPath . $this->importFolderName, true );
47
48 return true;
49
50 } catch( \Exception $e ) {
51 return false;
52 }
53 }
54
55 /**
56 * Import available assets inside assets directory
57 * @param $fileSystem
58 * @param $uploadDirectory
59 *
60 * @return array $importedIDs
61 */
62 protected function importAssets( $fileSystem, $uploadDirectory ) {
63 $allowedMimeTypes = array_values( get_allowed_mime_types() );
64 $importedIDs = [];
65 $uploadPath = \Depicter::storage()->getPluginUploadsDirectory() . '/';
66
67 // scan assets directory to import assets
68 $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 );
84
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 );
91
92 $attachmentTitleParts = explode( '-', $attachmentTitle );
93 $oldID = end( $attachmentTitleParts );
94 $importedIDs[ $oldID ] = $attachID;
95 }
96 }
97
98 return $importedIDs;
99 }
100
101 /**
102 * Import Slider
103 *
104 * @param $importedIDs
105 * @param $uploadPath
106 *
107 * @return mixed|null
108 * @throws \Exception
109 */
110 protected function importSlider( $importedIDs, $uploadPath ) {
111 $content = file_get_contents( $uploadPath . $this->importFolderName . '/data.json' );
112 $content = preg_replace( '/"activeBreakpoint":".+?"/', '"activeBreakpoint":"default"', $content );
113 preg_match_all( '/\"(source|src)\":\"(\d+)\"/', $content, $assets, PREG_SET_ORDER );
114 if ( !empty( $assets ) ) {
115 foreach( $assets as $asset ) {
116 if ( !empty( $asset[2] ) && !empty( $importedIDs[ $asset[2] ] ) ) {
117 $content = str_replace( $asset[0], '"' . $asset[1] . '":"'. $importedIDs[ $asset[2] ] .'"', $content );
118 }
119 }
120 }
121
122 $document = \Depicter::documentRepository()->create();
123 $document->update([
124 'content' => $content,
125 'status' => 'publish'
126 ]);
127
128 return $document->id;
129 }
130 }
131