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 / ExportService.php

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

66 lines 1.7 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\JSON;
5 use Depicter\Exception\EntityException;
6
7 class ExportService
8 {
9
10 /**
11 * Create zip file from slider data
12 *
13 * @param $documentID
14 *
15 * @return false|string
16 * @throws EntityException
17 */
18 public function pack( $documentID ) {
19
20 $sliderData = $this->sliderData( $documentID );
21
22 $zip = new \ZipArchive();
23 $tmp = tempnam('temp','zip');
24 $zip->open( $tmp, \ZipArchive::OVERWRITE );
25 $zip->addFromString( 'data.json', $sliderData['data'] );
26 if ( !empty( $sliderData['assets'] ) ){
27 foreach( $sliderData['assets'] as $assetID ){
28 $attachmentUrl = wp_get_attachment_url( $assetID );
29 $attachmentName = pathinfo( $attachmentUrl, PATHINFO_BASENAME );
30 if ( strpos( $attachmentName, ' ' ) !== false ) {
31 $attachmentUrl = str_replace( $attachmentName, rawurlencode( $attachmentName ), $attachmentUrl );
32 }
33 $zip->addFromString( 'assets/' . get_the_title( $assetID ) . '-' . $assetID . '.' . pathinfo( $attachmentUrl, PATHINFO_EXTENSION ), file_get_contents( $attachmentUrl ) );
34 }
35 }
36 $zip->close();
37 return $tmp;
38 }
39
40 /**
41 * Get slider data
42 *
43 * @param $documentID
44 *
45 * @return array
46 * @throws EntityException
47 */
48 protected function sliderData( $documentID ) {
49 $jsonContent = \Depicter::document()->getEditorData( $documentID );
50 $jsonContent = JSON::encode( $jsonContent );
51 $assetIDs = [];
52 preg_match_all( '/\"(source|src)\":\"(\d+)\"/', $jsonContent, $assets, PREG_SET_ORDER );
53 if ( !empty( $assets ) ) {
54 foreach( $assets as $asset ) {
55 if ( !empty( $asset[2] ) ) {
56 $assetIDs[] = $asset[2];
57 }
58 }
59 }
60 return [
61 'data' => $jsonContent,
62 'assets' => $assetIDs
63 ];
64 }
65 }
66