TableScreenOptions.php
1.94 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
<?php
namespace ACP\Search;
use AC;
use AC\Asset\Enqueueable;
class TableScreenOptions {
const INPUT_NAME = 'acp_enable_smart_filtering_button';
/**
* @var Enqueueable[] $assets
*/
private $assets;
/**
* @var Preferences\SmartFiltering
*/
private $preferences;
/**
* @var Settings\HideOnScreen\SmartFilters
*/
private $hide_smart_filters;
public function __construct(
array $assets,
Preferences\SmartFiltering $preferences,
Settings\HideOnScreen\SmartFilters $hide_smart_filters
) {
$this->assets = $assets;
$this->preferences = $preferences;
$this->hide_smart_filters = $hide_smart_filters;
}
public function register() {
add_action( 'ac/table_scripts', [ $this, 'scripts' ] );
add_action( 'ac/table', [ $this, 'register_screen_option' ] );
add_action( 'wp_ajax_' . self::INPUT_NAME, [ $this, 'update_smart_filtering_preference' ] );
}
/**
* @param AC\ListScreen $list_screen
*
* @return bool
*/
private function is_active( AC\ListScreen $list_screen ) {
return $this->preferences->is_active( $list_screen );
}
public function update_smart_filtering_preference() {
check_ajax_referer( 'ac-ajax' );
$is_active = ( 'true' === filter_input( INPUT_POST, 'value' ) ) ? 1 : 0;
$this->preferences->set( filter_input( INPUT_POST, 'list_screen' ), $is_active );
}
/**
* @param AC\Table\Screen $table
*/
public function register_screen_option( $table ) {
$list_screen = $table->get_list_screen();
if ( ! $list_screen->has_id() ) {
return;
}
if ( $this->hide_smart_filters->is_hidden( $list_screen ) ) {
return;
}
$check_box = new AC\Form\Element\Checkbox( self::INPUT_NAME );
$check_box->set_options( [ 1 => __( 'Smart Filtering', 'codepress-admin-columns' ) ] )
->set_value( $this->is_active( $list_screen ) ? 1 : 0 );
$table->register_screen_option( $check_box );
}
public function scripts() {
foreach ( $this->assets as $asset ) {
$asset->enqueue();
}
}
}