| 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', JSON::encode( $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 ), \Depicter::storage()->filesystem()->read( $attachmentUrl ) ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
if ( !empty( $sliderData['data']['jsonAssets'] ) ) { |
| 38 |
foreach( $sliderData['data']['jsonAssets'] as $key => $jsonURL ) { |
| 39 |
$zip->addFromString( 'assets/' . pathinfo( $jsonURL, PATHINFO_BASENAME ), \Depicter::storage()->filesystem()->read( $jsonURL ) ); |
| 40 |
} |
| 41 |
} |
| 42 |
$zip->close(); |
| 43 |
return $tmp; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get slider data |
| 48 |
* |
| 49 |
* @param $documentID |
| 50 |
* |
| 51 |
* @return array |
| 52 |
* @throws EntityException |
| 53 |
*/ |
| 54 |
protected function sliderData( $documentID ) { |
| 55 |
$type = \Depicter::documentRepository()->getFieldValue( $documentID, 'type' ); |
| 56 |
$jsonContent = \Depicter::document()->getEditorData( $documentID ); |
| 57 |
$jsonContent = JSON::encode( $jsonContent ); |
| 58 |
$assetIDs = []; |
| 59 |
preg_match_all( '/\"(source|src)\":\"(\d+)\"/', $jsonContent, $assets, PREG_SET_ORDER ); |
| 60 |
if ( !empty( $assets ) ) { |
| 61 |
foreach( $assets as $asset ) { |
| 62 |
if ( !empty( $asset[2] ) ) { |
| 63 |
$assetIDs[] = $asset[2]; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
preg_match_all( '/"src":\{[^\}]+\}/', $jsonContent, $backgroundImages, PREG_SET_ORDER ); |
| 69 |
if ( ! empty( $backgroundImages ) ) { |
| 70 |
foreach( $backgroundImages as $backgroundImage ) { |
| 71 |
if ( ! empty( $backgroundImage[0] ) ) { |
| 72 |
$patterns = [ '"default":"(\d+)"','"tablet":"(\d+)"','"mobile":"(\d+)"']; |
| 73 |
foreach( $patterns as $pattern ) { |
| 74 |
if ( preg_match( '/' . $pattern . '/', $backgroundImage[0], $asset ) ) { |
| 75 |
$assetIDs[] = $asset[1]; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
$jsonAssets = []; |
| 83 |
if ( strpos( $jsonContent, 'dpcLottie') ) { |
| 84 |
preg_match_all( '/"src":"(http[^"]+?.json)/', $jsonContent, $jsonFiles, PREG_SET_ORDER ); |
| 85 |
if ( !empty( $jsonFiles ) ) { |
| 86 |
foreach( $jsonFiles as $jsonFile ) { |
| 87 |
$jsonAssets[] = stripslashes( $jsonFile[1] ); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return [ |
| 93 |
'data' => [ |
| 94 |
'content' => $jsonContent, |
| 95 |
'type' => $type, |
| 96 |
'jsonAssets' => $jsonAssets, |
| 97 |
"uploadURL" => \Depicter::storage()->uploads()->getBaseUrl() |
| 98 |
], |
| 99 |
'assets' => $assetIDs, |
| 100 |
]; |
| 101 |
} |
| 102 |
} |
| 103 |
|