SaveExportPreference.php
1.34 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
<?php
declare( strict_types=1 );
namespace ACP\Export\RequestHandler\Ajax;
use AC\Nonce;
use AC\Request;
use AC\Type\ListScreenId;
use ACP\Export\ColumnStateCollection;
use ACP\Export\Repository\UserColumnStateRepository;
use ACP\Export\Type\ColumnState;
use ACP\RequestAjaxHandler;
use Exception;
class SaveExportPreference implements RequestAjaxHandler {
private $state_repository;
public function __construct() {
$this->state_repository = new UserColumnStateRepository();
}
public function handle(): void {
$request = new Request();
if ( ! ( new Nonce\Ajax() )->verify( $request ) ) {
wp_send_json_error( 'invalid nonce' );
}
$id = (string) $request->filter( 'list_id', null, FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$data = (string) $request->filter( 'data' );
if ( ! $data || ! ListScreenId::is_valid_id( $id ) ) {
wp_send_json_error();
}
try {
$this->state_repository->save(
new ListScreenId( $id ),
$this->get_column_states( $data )
);
} catch ( Exception $e ) {
wp_send_json_error();
}
wp_send_json_success();
}
private function get_column_states( string $data ): ColumnStateCollection {
$collection = new ColumnStateCollection();
foreach ( json_decode( $data, true ) as $item ) {
$collection->add( new ColumnState( $item['column_name'], $item['active'] ) );
}
return $collection;
}
}