ListScreenRestoreColumns.php
1.44 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
<?php
namespace AC\Controller;
use AC\Capabilities;
use AC\ListScreenRepository\Storage;
use AC\Message\Notice;
use AC\Registerable;
use AC\Type\ListScreenId;
use LogicException;
class ListScreenRestoreColumns implements Registerable {
/**
* @var Storage
*/
private $repository;
public function __construct( Storage $repository ) {
$this->repository = $repository;
}
public function register(): void
{
add_action( 'admin_init', [ $this, 'handle_request' ] );
}
public function handle_request() {
if ( ! current_user_can( Capabilities::MANAGE ) ) {
return;
}
switch ( filter_input( INPUT_POST, 'action' ) ) {
case 'restore_by_type' :
if ( $this->verify_nonce( 'restore-type' ) ) {
try {
$id = new ListScreenId( filter_input( INPUT_POST, 'layout' ) );
} catch ( LogicException $e ) {
return;
}
$list_screen = $this->repository->find( $id );
if ( ! $list_screen ) {
return;
}
$list_screen->set_settings( [] );
$this->repository->save( $list_screen );
$notice = new Notice( sprintf( __( 'Settings for %s restored successfully.', 'codepress-admin-columns' ), "<strong>" . esc_html( $list_screen->get_title() ) . "</strong>" ) );
$notice->register();
}
break;
}
}
/**
* @param string $action
*
* @return bool
*/
private function verify_nonce( $action ) {
return wp_verify_nonce( filter_input( INPUT_POST, '_ac_nonce' ), $action );
}
}