TableScreen.php
3.45 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace ACP\Export;
use AC;
use AC\Asset\Location;
use AC\ColumnRepository;
use AC\Registerable;
use ACP\Export\Asset\TableScriptFactory;
use ACP\Export\ColumnRepository\Filter;
class TableScreen implements Registerable
{
protected $location;
public function __construct(Location $location)
{
$this->location = $location;
}
public function register(): void
{
add_action('ac/table/list_screen', [$this, 'load_list_screen']);
//add_filter( 'ac/table/body_class', [ $this, 'add_hide_export_button_class' ], 10, 2 );
}
/**
* Load a list screen and potentially attach the proper exporting information to it
*/
public function load_list_screen(AC\ListScreen $list_screen): void
{
if ( ! self::is_exportable($list_screen)) {
return;
}
if ($list_screen instanceof ListScreen) {
$list_screen->export()->attach();
}
add_action('ac/table', [$this, 'register_screen_option']);
add_action('ac/table/admin_footer', [$this, 'scripts']);
}
public static function is_exportable(AC\ListScreen $list_screen): bool
{
if ( ! $list_screen instanceof ListScreen || ! $list_screen->has_id()) {
return false;
}
$column_repository = new ColumnRepository($list_screen);
$columns = $column_repository->find_all([
'filter' => [
new Filter\ExportableColumns(),
],
]);
if ( ! $columns) {
return false;
}
$is_active = ! (new HideOnScreen\Export())->is_hidden($list_screen);
return (new ApplyFilter\ListScreenActive($list_screen))->apply_filters($is_active);
}
public function scripts(AC\ListScreen $list_screen): void
{
if ( ! $list_screen instanceof ListScreen) {
return;
}
$style = new AC\Asset\Style(
'acp-export-listscreen',
$this->location->with_suffix('assets/export/css/listscreen.css')
);
$style->enqueue();
$factory = new TableScriptFactory($this->location);
$factory->create($list_screen, $this->get_export_button_setting($list_screen))
->enqueue();
}
public function register_screen_option(AC\Table\Screen $table): void
{
$list_screen = $table->get_list_screen();
$check_box = new AC\Form\Element\Checkbox('acp_export_show_export_button');
$check_box->set_options([1 => __('Export Button', 'codepress-admin-columns')])
->set_value($this->get_export_button_setting($list_screen) ? 1 : 0);
$table->register_screen_option($check_box);
}
public function preferences(): UserPreference\ShowExportButton
{
return new UserPreference\ShowExportButton();
}
private function get_export_button_setting(AC\ListScreen $list_screen): bool
{
$setting = $this->preferences()->get($list_screen->get_key());
// No setting found, enable export
if ($setting === null) {
$setting = 1;
}
return 1 === $setting;
}
/**
* @param string $classes
* @param AC\Table\Screen $table
*
* @return string
*/
public function add_hide_export_button_class($classes, $table)
{
if ( ! $this->get_export_button_setting($table->get_list_screen())) {
$classes .= ' ac-hide-export-button';
}
return $classes;
}
}