| 1 |
<?php |
| 2 |
namespace Depicter\Controllers\Ajax; |
| 3 |
|
| 4 |
use Averta\WordPress\File\FileSystem; |
| 5 |
use Depicter\Utility\Sanitize; |
| 6 |
use WPEmerge\Requests\RequestInterface; |
| 7 |
|
| 8 |
class ExportAjaxController |
| 9 |
{ |
| 10 |
protected $namePrefix = DEPICTER_PLUGIN_ID; |
| 11 |
|
| 12 |
/** |
| 13 |
* @param RequestInterface $request |
| 14 |
* @param $view |
| 15 |
* |
| 16 |
* @return \Psr\Http\Message\ResponseInterface|void |
| 17 |
*/ |
| 18 |
public function pack( RequestInterface $request, $view ) { |
| 19 |
if ( ! apply_filters( 'depicter/can/export', true ) ) { |
| 20 |
return \Depicter::json([ |
| 21 |
'errors' => [ 'Export service is disabled.' ] |
| 22 |
]); |
| 23 |
} |
| 24 |
|
| 25 |
$documentID = Sanitize::textfield( $request->query('id') ); |
| 26 |
|
| 27 |
try { |
| 28 |
if ( ! $documentID = Sanitize::textfield( $request->query('id') ) ) { |
| 29 |
throw new \Exception( __( 'Document ID is required', 'depicter' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
$zip = \Depicter::exportService()->pack( $documentID ); |
| 33 |
if ( $zip ) { |
| 34 |
$outputName = "{$this->namePrefix}-{$documentID}-" . gmdate("mdHis"). ".zip"; |
| 35 |
header('Content-Description: File Transfer'); |
| 36 |
header('Content-Type: application/octet-stream'); |
| 37 |
header('Content-Disposition: attachment; filename="'. $outputName .'"'); |
| 38 |
header('Expires: 0'); |
| 39 |
header('Cache-Control: must-revalidate'); |
| 40 |
header('Pragma: public'); |
| 41 |
header('Content-Length: ' . filesize($zip)); |
| 42 |
echo \Depicter::storage()->filesystem()->read($zip); |
| 43 |
exit; |
| 44 |
} |
| 45 |
|
| 46 |
} catch ( \Exception $exception ) { |
| 47 |
return \Depicter::json([ |
| 48 |
'errors' => [ $exception->getMessage() ] |
| 49 |
]); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|