File.php
1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace ACP\Migrate\Export\Response;
use AC\ListScreenCollection;
use ACP\Migrate\Export\Response;
use ACP\Migrate\MessageTrait;
use ACP\Storage\ListScreen\Encoder;
use ACP\Storage\ListScreen\Serializer\JsonSerializer;
final class File implements Response {
use MessageTrait;
/**
* @var string
*/
private $type;
/**
* @var ListScreenCollection
*/
private $list_screens;
/**
* @var Encoder
*/
private $encoder;
public function __construct( $type, ListScreenCollection $list_screens, Encoder $encoder ) {
$this->type = $type;
$this->list_screens = $list_screens;
$this->encoder = $encoder;
}
/**
* @return void
*/
public function send() {
if ( ! $this->list_screens->count() ) {
$this->set_message( __( 'No screens selected for export.', 'codepress-admin-columns' ) );
return;
}
$output = [];
foreach ( $this->list_screens as $list_screen ) {
$output[] = $this->encoder->encode( $list_screen );
}
$headers = [
'content-disposition' => 'attachment; filename="' . $this->get_file_name() . '"',
'content-type' => 'application/json',
];
foreach ( $headers as $header => $value ) {
header( $header . ': ' . $value );
}
echo ( new JsonSerializer() )->serialize( $output );
exit;
}
private function get_file_name() {
return sprintf(
'%s-%s.%s',
'admin-columns-export',
date( 'Y-m-d-Hi' ),
$this->type
);
}
}