-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathScreen.php
365 lines (303 loc) · 9.65 KB
/
Screen.php
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
namespace AC\Table;
use AC;
use AC\Asset;
use AC\Capabilities;
use AC\ColumnSize;
use AC\Form;
use AC\ListScreen;
use AC\Registerable;
use AC\Renderable;
use AC\ScreenController;
final class Screen implements Registerable
{
/**
* @var Asset\Location\Absolute
*/
private $location;
/**
* @var ListScreen
*/
private $list_screen;
/**
* @var Form\Element[]
*/
private $screen_options;
/**
* @var Button[]
*/
private $buttons = [];
private $column_size_list_storage;
private $column_size_user_storage;
private $primary_column_factory;
private $edit_button;
public function __construct(
Asset\Location\Absolute $location,
ListScreen $list_screen,
ColumnSize\ListStorage $column_size_list_storage,
ColumnSize\UserStorage $column_size_user_storage,
PrimaryColumnFactory $primary_column_factory,
AC\Settings\General\EditButton $edit_button
) {
$this->location = $location;
$this->list_screen = $list_screen;
$this->column_size_list_storage = $column_size_list_storage;
$this->column_size_user_storage = $column_size_user_storage;
$this->primary_column_factory = $primary_column_factory;
$this->edit_button = $edit_button;
}
/**
* Register hooks
*/
public function register(): void
{
$controller = new ScreenController($this->list_screen);
$controller->register();
if ($this->list_screen->has_id()) {
$render = new TableFormView(
$this->list_screen->get_meta_type(),
sprintf('<input type="hidden" name="layout" value="%s">', $this->list_screen->get_id())
);
$render->register();
}
(new AdminHeadStyle())->register();
add_filter(
'list_table_primary_column',
[
$this->primary_column_factory->create($this->list_screen),
'set_primary_column',
],
20
);
add_action('admin_enqueue_scripts', [$this, 'admin_scripts']);
add_action('admin_footer', [$this, 'admin_footer_scripts']);
add_action('admin_head', [$this, 'admin_head_scripts']);
add_action('admin_head', [$this, 'register_settings_button']);
add_filter('admin_body_class', [$this, 'admin_class']);
add_action('admin_footer', [$this, 'render_actions']);
add_filter('screen_settings', [$this, 'screen_options']);
}
public function get_buttons(): array
{
return array_merge([], ...$this->buttons);
}
public function register_button(Button $button, int $priority = 10): bool
{
$button->set_attribute('data-priority', $priority);
$this->buttons[$priority][] = $button;
ksort($this->buttons, SORT_NUMERIC);
return true;
}
/**
* Adds a body class which is used to set individual column widths
*
* @param string $classes body classes
*
* @return string
* @since 1.4.0
*/
public function admin_class($classes)
{
$classes .= ' ac-' . $this->list_screen->get_key();
return apply_filters('ac/table/body_class', $classes, $this);
}
public function register_settings_button()
{
if ( ! current_user_can(Capabilities::MANAGE)) {
return;
}
if ( ! $this->edit_button->is_enabled()) {
return;
}
$button = new Button('edit-columns');
$button->set_label(__('Edit columns', 'codepress-admin-columns'))
->set_url((string)$this->list_screen->get_editor_url())
->set_dashicon('admin-generic');
$this->register_button($button, 1);
}
public function admin_scripts()
{
$style = new Asset\Style('ac-table', $this->location->with_suffix('assets/css/table.css'), ['ac-ui']);
$style->enqueue();
$table_translation = Asset\Script\Localize\Translation::create([
'value_loading' => __('Loading...', 'codepress-admin-columns'),
'edit' => __('Edit', 'codepress-admin-columns'),
'download' => __('Download', 'codepress-admin-columns'),
]);
$script = new Asset\Script(
'ac-table',
$this->location->with_suffix('assets/js/table.js'),
['jquery', Asset\Script\GlobalTranslationFactory::HANDLE]
);
$script
->add_inline_variable('AC', [
'assets' => $this->location->with_suffix('assets/')->get_url(),
'list_screen' => $this->list_screen->get_key(),
'layout' => $this->list_screen->has_id() ? (string)$this->list_screen->get_id() : '',
'column_types' => $this->get_column_types_mapping(),
'ajax_nonce' => wp_create_nonce('ac-ajax'),
'read_only' => $this->list_screen->is_read_only(),
'table_id' => $this->list_screen->get_table_attr_id(),
'screen' => $this->get_current_screen_id(),
'meta_type' => $this->list_screen->get_meta_type(),
'list_screen_link' => $this->get_list_screen_clear_link(),
'current_user_id' => get_current_user_id(),
'number_format' => [
'decimal_point' => $this->get_local_number_format('decimal_point'),
'thousands_sep' => $this->get_local_number_format('thousands_sep'),
],
])
->localize('AC_I18N', $table_translation)
->enqueue();
/**
* @param ListScreen $list_screen
*/
do_action('ac/table_scripts', $this->list_screen, $this);
// Column specific scripts
foreach ($this->list_screen->get_columns() as $column) {
$column->scripts();
}
}
private function get_local_number_format(string $var)
{
global $wp_locale;
return $wp_locale->number_format[$var] ?? null;
}
/**
* @return string
*/
private function get_list_screen_clear_link(): string
{
$url = $this->list_screen->get_table_url();
$query_args_whitelist = [
'layout',
'orderby',
'order',
];
switch (true) {
case $this->list_screen instanceof ListScreen\Post :
$query_args_whitelist[] = 'post_status';
break;
case $this->list_screen instanceof ListScreen\User :
$query_args_whitelist[] = 'role';
break;
case $this->list_screen instanceof ListScreen\Comment :
$query_args_whitelist[] = 'comment_status';
break;
}
foreach ($query_args_whitelist as $query_arg) {
if (isset($_GET[$query_arg]) && is_string($_GET[$query_arg])) {
$url = $url->with_arg($query_arg, $_GET[$query_arg]);
}
}
return (string)$url;
}
/**
* @return false|string
*/
private function get_current_screen_id()
{
$screen = get_current_screen();
if ( ! $screen) {
return false;
}
return $screen->id;
}
/**
* @return array
*/
private function get_column_types_mapping()
{
$types = [];
foreach ($this->list_screen->get_columns() as $column) {
$types[$column->get_name()] = $column->get_type();
}
return $types;
}
/**
* @return ListScreen
*/
public function get_list_screen()
{
return $this->list_screen;
}
/**
* Admin header scripts
* @since 3.1.4
*/
public function admin_head_scripts()
{
$inline_style = new AC\Table\InlineStyle\ColumnSize(
$this->list_screen,
$this->column_size_list_storage,
$this->column_size_user_storage
);
echo $inline_style->render();
/**
* Add header scripts that only apply to column screens.
*
* @param ListScreen
* @param self
*
* @since 3.1.4
*/
do_action('ac/admin_head', $this->list_screen, $this);
}
public function admin_footer_scripts(): void
{
do_action('ac/table/admin_footer', $this->list_screen, $this);
}
public function render_actions(): void
{
?>
<div id="ac-table-actions" class="ac-table-actions">
<?php
$this->render_buttons(); ?>
<?php
do_action('ac/table/actions', $this); ?>
</div>
<?php
}
private function render_buttons(): void
{
?>
<div class="ac-table-actions-buttons">
<?php
foreach ($this->get_buttons() as $button) {
$button->render();
}
?>
</div>
<?php
}
public function register_screen_option(Renderable $option): void
{
$this->screen_options[] = $option;
}
/**
* @param string $html
*
* @return string
*/
public function screen_options($html)
{
if (empty($this->screen_options)) {
return $html;
}
ob_start();
?>
<fieldset class='acp-screen-option-prefs'>
<legend><?= __('Admin Columns', 'codepress-admin-columns'); ?></legend>
<div class="acp-so-container">
<?php
foreach ($this->screen_options as $option) {
echo $option->render();
}
?>
</div>
</fieldset>
<?php
$html .= ob_get_clean();
return $html;
}
}