TableScreen.php
5.05 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
<?php
namespace ACA\EC\Service;
use AC;
use AC\Asset\Location;
use AC\Asset\Style;
use AC\Registerable;
use ACA\EC\Column;
use ACA\EC\ListScreen;
use ACA\EC\Sorting;
use WP_Query;
class TableScreen implements Registerable {
/**
* @var array
*/
private $notices;
/**
* @var array
*/
private $filter_vars;
/**
* @var Location\Absolute
*/
private $location;
public function __construct( Location\Absolute $location ) {
$this->location = $location;
}
public function register() {
add_action( 'ac/table_scripts', [ $this, 'table_scripts' ] );
add_action( 'ac/table/list_screen', [ $this, 'add_events_filter_vars' ] );
add_action( 'ac/table/list_screen', [ $this, 'register_event_sorting_fix' ] );
add_action( 'parse_query', [ $this, 'parse_query' ], 51 );
}
public function table_scripts(): void {
$script = new Style( 'aca-ec-table', $this->location->with_suffix( 'assets/css/table.css' ) );
$script->enqueue();
}
/**
* @param WP_Query $query
*/
public function parse_query( WP_Query $query ): void {
if ( 'tribe_events' !== $query->get( 'post_type' ) ) {
return;
}
if ( ! filter_input( INPUT_GET, 'orderby' ) ) {
return;
}
// This prevents the default tribe event query changes
$query->tribe_is_event = false;
}
public function add_events_filter_vars( $list_screen ): void {
if ( ! $list_screen instanceof ListScreen\Event ) {
return;
}
$prefix = 'ac_related_filter_';
$input = filter_input_array( INPUT_GET, [
$prefix . 'value' => FILTER_SANITIZE_NUMBER_INT,
] );
foreach ( $input as $k => $v ) {
unset( $input[ $k ] );
$input[ str_replace( $prefix, '', $k ) ] = $v;
}
$input = (object) $input;
switch ( $input->post_type ?? '') {
case 'tribe_venue':
$this->filter_on_venue( $input->value );
break;
case 'tribe_organizer':
$this->filter_on_organizer( $input->value );
break;
default:
return; // invalid post type
}
$post_type_object = get_post_type_object( $input->post_type );
if ( ! $post_type_object ) {
return;
}
switch ( $input->date ) {
case 'future':
$this->filter_on_future_events();
$date = __( 'upcoming', 'codepress-admin-columns' );
break;
case 'past':
$this->filter_on_past_events();
$date = __( 'previous', 'codepress-admin-columns' );
break;
default:
$date = __( 'all', 'codepress-admin-columns' );
}
// General notice
$notice[] = sprintf( __( 'Filtering on %s: Showing %s events from %s.', 'codepress-admin-columns' ),
$post_type_object->labels->singular_name,
$date,
get_the_title( $input->value )
);
// Return to related overview link
$notice[] = sprintf( '<a href="%s" class="notice__actionlink">%s</a>',
esc_url( admin_url( 'edit.php?' . base64_decode( $input->return_url ) ) ),
sprintf( __( 'Return to %s', 'codepress-admin-columns' ), $post_type_object->labels->name )
);
// Remove filters and stay on this overview link
$input_remove = [];
foreach ( $input as $k => $v ) {
$input_remove[ $prefix . $k ] = false;
}
$notice[] = sprintf( '<a href="%s" class="notice-aca-ec-filter__dismiss notice__actionlink">%s</a>',
add_query_arg( $input_remove ),
__( 'Remove this filter', 'codepress-admin-columns' )
);
$this->notices[] = [
'type' => 'success',
'notice' => implode( ' ', $notice ),
];
add_action( 'admin_notices', [ $this, 'display_notices' ] );
add_action( 'pre_get_posts', [ $this, 'events_query_callback' ] );
}
/**
* @param WP_Query $wp_query
*/
public function events_query_callback( WP_Query $wp_query ): void {
if ( ! $wp_query->is_main_query() ) {
return;
}
$wp_query->query_vars = array_merge( $wp_query->query_vars, $this->filter_vars );
}
public function display_notices(): void {
foreach ( $this->notices as $notice ) : ?>
<div class="notice notice-<?php echo $notice['type']; ?> notice-aca-ec-filter">
<div class="info">
<p><?php echo $notice['notice']; ?></p>
</div>
</div>
<?php endforeach;
}
private function filter_on_venue( $value ): void {
$column = new Column\Event\Venue();
$this->filter_vars['meta_query'][] = [
'key' => $column->get_meta_key(),
'value' => $value,
];
}
private function filter_on_organizer( $value ): void {
$column = new Column\Event\Organizer();
$this->filter_vars['meta_query'][] = [
'key' => $column->get_meta_key(),
'value' => $value,
];
}
private function filter_on_past_events(): void {
$column = new Column\Event\EndDate();
$this->filter_vars['meta_query'][] = [
'key' => $column->get_meta_key(),
'value' => date( 'Y-m-d H:i' ),
'compare' => '<',
];
}
private function filter_on_future_events(): void {
$column = new Column\Event\StartDate();
$this->filter_vars['meta_query'][] = [
'key' => $column->get_meta_key(),
'value' => date( 'Y-m-d H:i' ),
'compare' => '>',
];
}
public function register_event_sorting_fix( AC\ListScreen $list_screen ): void {
if ( $list_screen instanceof ListScreen\Event ) {
$service = new Sorting\EventSortingFix( $list_screen );
$service->register();
}
}
}