Request.php
1.74 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace ACP\Migrate\Export;
use AC;
use AC\Capabilities;
use AC\ListScreenRepository\Storage;
use AC\Type\ListScreenId;
final class Request implements AC\Registrable {
const ACTION = 'acp-export';
const NONCE_NAME = 'acp_export_nonce';
/**
* @var Storage
*/
private $storage;
/**
* @var ResponseFactory
*/
private $response_factory;
public function __construct( Storage $storage, ResponseFactory $response_factory ) {
$this->storage = $storage;
$this->response_factory = $response_factory;
}
public function register() {
add_action( 'admin_init', [ $this, 'handle_request' ] );
}
/**
* @return void
*/
public function handle_request() {
$data = (object) filter_input_array( INPUT_POST, [
'action' => FILTER_SANITIZE_STRING,
'list_screen_ids' => [
'filter' => FILTER_SANITIZE_STRING,
'flags' => FILTER_REQUIRE_ARRAY,
],
self::NONCE_NAME => FILTER_DEFAULT,
] );
if ( ! isset( $data->action ) || $data->action !== self::ACTION ) {
return;
}
if ( ! wp_verify_nonce( $data->{self::NONCE_NAME}, $data->action ) ) {
return;
}
if ( ! current_user_can( Capabilities::MANAGE ) ) {
return;
}
if ( empty( $data->list_screen_ids ) ) {
return;
}
$response = $this->response_factory->create(
$this->get_list_screens_from_request( $data->list_screen_ids )
);
$response->send();
}
/**
* @param array $ids
*
* @return AC\ListScreenCollection
*/
protected function get_list_screens_from_request( array $ids ) {
$list_screens = new AC\ListScreenCollection();
foreach ( $ids as $id ) {
$list_screen = $this->storage->find( new ListScreenId( $id ) );
if ( $list_screen ) {
$list_screens->add( $list_screen );
}
}
return $list_screens;
}
}