Addon.php
5.42 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
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
<?php
namespace ACP\Search;
use AC;
use AC\Asset\Location;
use AC\ListScreenRepository\Storage;
use AC\Registerable;
use ACP;
use ACP\Bookmark\SegmentRepository;
use ACP\Bookmark\Setting\PreferredSegment;
use ACP\Settings\ListScreen\HideOnScreenCollection;
final class Addon implements Registerable {
/**
* @var Storage
*/
private $storage;
/**
* @var Location
*/
private $location;
/**
* @var SegmentRepository
*/
private $segment_repository;
/**
* @var Preferences\SmartFiltering
*/
private $table_preference;
/**
* @var Settings\HideOnScreen\SmartFilters
*/
private $hide_smart_filters;
public function __construct( Storage $storage, Location $location, SegmentRepository $segment_repository ) {
$this->storage = $storage;
$this->location = $location;
$this->segment_repository = $segment_repository;
$this->table_preference = new Preferences\SmartFiltering();
$this->hide_smart_filters = new Settings\HideOnScreen\SmartFilters();
}
/**
* @param AC\ListScreen $list_screen
*
* @return bool
*/
private function is_active( AC\ListScreen $list_screen ) {
return apply_filters( 'acp/search/is_active', $this->table_preference->is_active( $list_screen ), $list_screen );
}
public function register() {
$this->get_column_settings()->register();
$this->get_table_screen_options()->register();
add_action( 'ac/table/list_screen', [ $this, 'table_screen_request' ] );
add_action( 'wp_ajax_acp_search_comparison_request', [ $this, 'comparison_request' ] );
add_action( 'acp/admin/settings/hide_on_screen', [ $this, 'add_hide_on_screen' ], 10, 2 );
}
private function get_column_settings() {
return new Settings( [
new AC\Asset\Style( 'acp-search-admin', $this->location->with_suffix( 'assets/search/css/admin.css' ) ),
] );
}
private function get_table_screen_options() {
return new TableScreenOptions(
[
new AC\Asset\Script( 'acp-search-table-screen-options', $this->location->with_suffix( 'assets/search/js/screen-options.bundle.js' ), [ 'ac-table' ] ),
],
$this->table_preference,
$this->hide_smart_filters
);
}
public function add_hide_on_screen( HideOnScreenCollection $collection, AC\ListScreen $list_screen ) {
if ( ! TableScreenFactory::get_table_screen_reference( $list_screen ) ) {
return;
}
$collection->add( $this->hide_smart_filters, 40 )
->add( new Settings\HideOnScreen\SavedFilters(), 41 );
}
public function comparison_request() {
check_ajax_referer( 'ac-ajax' );
$request = new AC\Request();
$comparison = new RequestHandler\Comparison(
$this->storage,
$request
);
$comparison->dispatch( $request->get( 'method' ) );
}
public function table_screen_request( AC\ListScreen $list_screen ) {
if ( ! $this->is_active( $list_screen ) ) {
return;
}
$preferred_segment = new PreferredSegment( $list_screen, $this->segment_repository );
$request = new AC\Request();
$request->add_middleware( new Middleware\Segment( $preferred_segment ) )
->add_middleware( new Middleware\Request() );
$request_handler = new RequestHandler\Rules( $list_screen );
$request_handler->handle( $request );
if ( $this->hide_smart_filters->is_hidden( $list_screen ) ) {
return;
}
$assets = [
new AC\Asset\Style( 'aca-search-table', $this->location->with_suffix( 'assets/search/css/table.css' ) ),
new AC\Asset\Script( 'aca-search-moment', $this->location->with_suffix( 'assets/search/js/moment.min.js' ) ),
new AC\Asset\Script( 'aca-search-querybuilder', $this->location->with_suffix( 'assets/search/js/query-builder.standalone.min.js' ), [ 'jquery', 'jquery-ui-datepicker' ] ),
new Asset\Script\Table(
'aca-search-table',
$this->location->with_suffix( 'assets/search/js/table.bundle.js' ),
$this->get_filters( $list_screen ),
$request,
$preferred_segment->get_segment()
),
];
$table_screen = TableScreenFactory::create(
$list_screen,
$assets
);
if ( $table_screen ) {
$table_screen->register();
}
}
/**
* @param AC\ListScreen $list_screen
*
* @return array
*/
private function get_filters( AC\ListScreen $list_screen ) {
$filters = [];
foreach ( $list_screen->get_columns() as $column ) {
$setting = $column->get_setting( 'search' );
if ( ! $setting instanceof Settings\Column ) {
continue;
}
$is_active = apply_filters_deprecated( 'acp/search/smart-filtering-active', [ $setting->is_active(), $setting ], '5.2', 'Smart filtering can be disabled using the UI.' );
if ( ! $is_active ) {
continue;
}
if ( ! $column instanceof Searchable || ! $column->search() ) {
continue;
}
$filter = new Middleware\Filter(
$column->get_name(),
$column->search(),
$this->get_filter_label( $column )
);
$filters[] = apply_filters( 'acp/search/filters', $filter(), $column );
}
return $filters;
}
/**
* @param AC\Column $column
*
* @return string
*/
private function get_filter_label( AC\Column $column ) {
$label = $this->sanitize_label( $column->get_custom_label() );
if ( ! $label ) {
$label = $this->sanitize_label( $column->get_label() );
}
if ( ! $label ) {
$label = $column->get_type();
}
return $label;
}
/**
* Allow dashicons as label, all the rest is parsed by 'strip_tags'
*
* @param string $label
*
* @return string
*/
private function sanitize_label( $label ) {
if ( false === strpos( $label, 'dashicons' ) ) {
$label = strip_tags( $label );
}
return trim( $label );
}
}