EditableDataFactory.php
3.57 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
<?php
namespace ACP\Editing;
use AC;
use AC\Column;
/**
* Get all data settings needed to load editing for the WordPress list table
*/
class EditableDataFactory {
/**
* @param AC\ListScreen $list_screen
*
* @return array
*/
public function create( AC\ListScreen $list_screen ) {
$is_table_inline_editable = $this->is_list_screen_inline_editable( $list_screen );
$is_table_bulk_editable = $this->is_list_screen_bulk_editable( $list_screen );
if ( ! $is_table_inline_editable && ! $is_table_bulk_editable ) {
return [];
}
$editable_data = [];
foreach ( $list_screen->get_columns() as $column ) {
$service = ServiceFactory::create( $column );
if ( ! $service instanceof Service ) {
continue;
}
$inline_data = $is_table_inline_editable && $this->is_inline_edit_active( $column )
? $this->create_data_by_service( $service, $column, Service::CONTEXT_SINGLE )
: null;
$bulk_data = $is_table_bulk_editable && $this->is_bulk_edit_active( $column )
? $this->create_data_by_service( $service, $column, Service::CONTEXT_BULK )
: null;
if ( ! $inline_data && ! $bulk_data ) {
continue;
}
$editable_data[ $column->get_name() ] = [
'type' => $column->get_type(),
'inline_edit' => $inline_data,
'bulk_edit' => $bulk_data,
];
}
return $editable_data;
}
/**
* @param AC\ListScreen $list_screen
*
* @return bool
*/
private function is_list_screen_bulk_editable( AC\ListScreen $list_screen ) {
$is_enabled = ! ( new HideOnScreen\BulkEdit() )->is_hidden( $list_screen );
return (bool) apply_filters( 'acp/editing/bulk/active', $is_enabled, $list_screen );
}
/**
* @param AC\ListScreen $list_screen
*
* @return bool
*/
private function is_list_screen_inline_editable( AC\ListScreen $list_screen ) {
return ! ( new HideOnScreen\InlineEdit() )->is_hidden( $list_screen );
}
/**
* @param Service $service
* @param Column $column
* @param string $context
*
* @return array|null
*/
private function create_data_by_service( Service $service, Column $column, $context ) {
$view = $service->get_view( $context );
if ( ! $view ) {
return null;
}
$data = apply_filters( 'acp/editing/view_settings', $view->get_args(), $column );
$data = apply_filters( 'acp/editing/view_settings/' . $column->get_type(), $data, $column );
if ( ! is_array( $data ) ) {
return null;
}
if ( isset( $data['options'] ) ) {
$data['options'] = $this->format_js( $data['options'] );
}
return (array) $data;
}
/**
* @param Column $column
*
* @return bool
*/
private function is_bulk_edit_active( Column $column ) {
$setting = $column->get_setting( Settings\BulkEditing::NAME );
$is_active = $setting instanceof Settings\BulkEditing && $setting->is_active();
return (bool) apply_filters( 'acp/editing/bulk-edit-active', $is_active, $column );
}
/**
* @param Column $column
*
* @return bool
*/
private function is_inline_edit_active( Column $column ) {
$setting = $column->get_setting( Settings::NAME );
return $setting instanceof Settings && $setting->is_active();
}
/**
* @param $list
*
* @return array
*/
private function format_js( $list ) {
$options = [];
if ( $list ) {
foreach ( $list as $index => $option ) {
if ( is_array( $option ) && isset( $option['options'] ) ) {
$option['options'] = $this->format_js( $option['options'] );
$options[] = $option;
} else if ( is_scalar( $option ) ) {
$options[] = [
'value' => $index,
'label' => html_entity_decode( $option ),
];
}
}
}
return $options;
}
}