TableScreen.php
5.12 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace ACP\Export;
use AC;
use AC\Asset\Location;
use AC\Preferences;
use AC\Registerable;
use AC\Type\ListScreenId;
use ACP;
use ACP\Export\Asset\Script;
class TableScreen implements Registerable {
/**
* @var Location
*/
protected $location;
public function __construct( Location $location ) {
$this->location = $location;
}
public function register() {
add_action( 'ac/table/list_screen', [ $this, 'load_list_screen' ] );
add_filter( 'ac/table/body_class', [ $this, 'add_hide_export_button_class' ], 10, 2 );
add_action( 'wp_ajax_acp_export_show_export_button', [ $this, 'update_table_option_show_export_button' ] );
}
/**
* Load a list screen and potentially attach the proper exporting information to it
*
* @param AC\ListScreen $list_screen List screen for current table screen
*/
public function load_list_screen( AC\ListScreen $list_screen ): void {
if ( ! $this->is_exportable( $list_screen ) ) {
return;
}
$list_screen->export()->attach();
add_action( 'ac/table', [ $this, 'register_screen_option' ] );
add_action( 'ac/table_scripts', [ $this, 'scripts' ] );
}
private function is_exportable( AC\ListScreen $list_screen ): bool {
return $list_screen instanceof ListScreen && $list_screen->has_id() && $list_screen->export()->is_active();
}
private function get_user_preference_column_names( ListScreenId $id ): array {
$user_preference = new UserPreference\ExportedColumns();
return $user_preference->exists( $id )
? $user_preference->get( $id )
: [];
}
/**
* @param AC\ListScreen $list_screen
*
* @return AC\Column[]
*/
private function get_exportable_column_labels( AC\ListScreen $list_screen ): array {
if ( ! $list_screen instanceof ListScreen ) {
return [];
}
$columns = [];
$hidden_columns = get_hidden_columns( $list_screen->get_screen_id() );
$user_preference_columns = $this->get_user_preference_column_names( $list_screen->get_id() );
foreach ( $list_screen->export()->get_exportable_columns() as $column ) {
$column_name = $column->get_name();
if ( $user_preference_columns ) {
$default_state = in_array( $column_name, $user_preference_columns, true ) ? 'on' : 'off';
} else {
$default_state = in_array( $column_name, $hidden_columns, true ) ? 'off' : 'on';
}
$columns[] = [
'name' => $column_name,
'label' => $this->get_sanitized_label( $column ),
'default_state' => $default_state,
];
}
return $columns;
}
private function get_sanitized_label( AC\Column $column ): string {
return $this->sanitize_column_label( $column->get_custom_label() ) ?: sprintf( '%s (%s)', $column->get_name(), $column->get_label() );
}
/**
* Allows plain text and dashicons
*/
private function sanitize_column_label( string $label ): string {
if ( false === strpos( $label, 'dashicons' ) ) {
$label = strip_tags( $label );
}
return trim( $label );
}
public function scripts( AC\ListScreen $list_screen ) {
$style = new AC\Asset\Style(
'acp-export-listscreen',
$this->location->with_suffix( 'assets/export/css/listscreen.css' )
);
$style->enqueue();
$script = new Script\Table(
'acp-export-listscreen',
$this->location->with_suffix( 'assets/export/js/listscreen.js' ),
$list_screen->export(),
$this->get_exportable_column_labels( $list_screen )
);
$script->enqueue();
}
/**
* @param AC\Table\Screen $table
*/
public function register_screen_option( AC\Table\Screen $table ) {
$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 );
$button = new AC\Table\Button( 'export' );
$button->set_label( __( 'Export to CSV', 'codepress-admin-columns' ) )
->set_text( __( 'Export', 'codepress-admin-columns' ) )
->set_url( '#' );
$table->register_button( $button );
}
public function preferences() {
return new Preferences\Site( 'show_export_button' );
}
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 $list_screen_key
* @param int $value
*/
private function set_export_button_setting( $list_screen_key, $value ) {
$this->preferences()->set( $list_screen_key, (int) $value );
}
public function update_table_option_show_export_button() {
check_ajax_referer( 'ac-ajax' );
$this->set_export_button_setting( filter_input( INPUT_POST, 'list_screen' ), ( 'true' === filter_input( INPUT_POST, 'value' ) ) ? 1 : 0 );
exit;
}
/**
* @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;
}
}