-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.php
More file actions
429 lines (325 loc) · 11.3 KB
/
view.php
File metadata and controls
429 lines (325 loc) · 11.3 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php defined( 'ABSPATH' ) || exit;
class TDB_View {
/**
* @var string View name
*/
protected $name = '';
/**
* @var array View args
*/
public $args = array();
/**
* TDB_View constructor.
*
* @since 1.0.0
*
* @param string $name
* @param array $args
*/
public function __construct( $name, $args ) {
$this->name = $name;
$this->args = wp_parse_args( $args, array(
'menu_title' => ucfirst( $this->name ),
'page_title' => ucfirst( $this->name ),
'menu_slug' => $this->name,
'parent_slug' => '',
'show_in_menu' => true,
'menu_icon' => '',
'menu_position' => null,
'capability' => 'manage_options',
) );
$this->add_hooks();
}
/**
* View hooks (called on constructor).
*
* @since 1.0.0
*/
public function add_hooks() {
add_action( 'admin_init', array( $this, 'admin_init' ) );
// Note: sub-menus need to be registered after parent
add_action( 'admin_menu', array( $this, 'admin_menu' ), empty( $this->args['parent_slug'] ) ? 10 : 11 );
add_filter( 'parent_file', array( $this, 'parent_file' ), 10 );
add_filter( 'submenu_file', array( $this, 'submenu_file' ), 10, 2 );
add_action( 'adminmenu', array( $this, 'restore_plugin_page' ), 10, 2 );
add_filter( 'screen_options_show_screen', array( $this, 'show_screen_options' ), 10, 2 );
add_filter( 'screen_settings', array( $this, 'maybe_screen_settings' ), 10, 2 );
add_filter( 'admin_init', array( $this, 'maybe_set_screen_settings' ), 11 );
add_filter( 'tdb-set-screen-option', array( $this, 'set_screen_settings' ), 10, 3 );
}
public function show_screen_options( $show_screen, $screen ) {
if( ! $this->is_current_view() ) {
return $show_screen;
}
$screen_slug = explode( '_page_', $screen->id );
if( isset( $screen_slug[1] ) && $screen_slug[1] === $this->args['menu_slug'] ) {
return true;
}
return $show_screen;
}
/**
* Check if current screen is own.
*
* @since 1.0.0
*
* @param string $screen_settings Screen settings.
* @param WP_Screen $screen WP_Screen object.
*
* @return string $screen_settings
*/
public function maybe_screen_settings( $screen_settings, $screen ) {
if( ! $this->is_current_view() ) {
return $screen_settings;
}
$screen_slug = explode( '_page_', $screen->id );
// Check if current screen matches this menu slug
if( isset( $screen_slug[1] ) && $screen_slug[1] === $this->args['menu_slug'] ) {
global $tdb_registered_tables, $tdb_table;
if( ! isset( $tdb_registered_tables[$this->name] ) ) {
return $screen_settings;
}
// Set up global vars
$tdb_table = $tdb_registered_tables[$this->name];
ob_start();
$this->screen_settings( $screen_settings, $screen );
$screen_settings .= ob_get_clean();
}
return $screen_settings;
}
/**
* Screen settings text displayed in the Screen Options tab.
*
* @since 1.0.0
*
* @param string $screen_settings Screen settings.
* @param WP_Screen $screen WP_Screen object.
*/
public function screen_settings( $screen_settings, $screen ) {
// Override
}
/**
* Saves view options.
*
* Function based on set_screen_options()
*
* @since 1.0.0
*
* @see set_screen_options()
*/
function maybe_set_screen_settings() {
if( ! $this->is_current_view() ) {
return;
}
if ( isset( $_POST['wp_screen_options'] ) && is_array( $_POST['wp_screen_options'] ) ) {
check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
if ( ! $user = wp_get_current_user() ) {
return;
}
$option = $_POST['wp_screen_options']['option'];
$value = $_POST['wp_screen_options']['value'];
if ( $option != sanitize_key( $option ) ) {
return;
}
$option = str_replace('-', '_', $option);
/**
* Filters a screen option value before it is set.
*
* The filter can also be used to modify non-standard [items]_per_page
* settings. See the parent function for a full list of standard options.
*
* Returning false to the filter will skip saving the current option.
*
* @since 1.0.0
*
* @see set_screen_options()
*
* @param bool|int $value Screen option value. Default false to skip.
* @param string $option The option name.
* @param int $value The number of rows to use.
*/
$value = apply_filters( 'tdb-set-screen-option', false, $option, $value );
if ( false === $value )
return;
update_user_meta( $user->ID, $option, $value );
$url = remove_query_arg( array( 'pagenum', 'apage', 'paged' ), wp_get_referer() );
if ( isset( $_POST['mode'] ) ) {
$url = add_query_arg( array( 'mode' => $_POST['mode'] ), $url );
}
wp_safe_redirect( $url );
exit;
}
}
/**
* Screen option value before it is set.
*
* The filter can also be used to modify non-standard [items]_per_page
* settings. See the parent function for a full list of standard options.
*
* Returning false to the filter will skip saving the current option.
*
* @since 1.0.0
*
* @see set_screen_options()
*
* @param bool|int $value_to_set Screen option value to set. Default false to skip.
* @param string $option The option name.
* @param int $value The option value.
*
* @return bool|mixed False to skip or any other value to set as option value
*/
public function set_screen_settings( $value_to_set, $option, $value ) {
// Override
return $value_to_set;
}
/**
* Create a new menu
*
* @since 1.0.0
*/
public function admin_menu() {
// Override capability by the table capability
$tdb_table = tdb_get_table_object( $this->name );
if( ! empty( $tdb_table->capability ) ) {
$this->args['capability'] = $tdb_table->capability;
}
if( ! $this->args['show_in_menu'] ) {
add_submenu_page( '', $this->args['page_title'], $this->args['menu_title'], $this->args['capability'], $this->args['menu_slug'], array( $this, 'render' ) );
} else {
if( empty( $this->args['parent_slug'] ) ) {
// Top menu
add_menu_page( $this->args['page_title'], $this->args['menu_title'], $this->args['capability'], $this->args['menu_slug'], array( $this, 'render' ), $this->args['menu_icon'], $this->args['menu_position'] );
} else {
// Sub menu
add_submenu_page( $this->args['parent_slug'], $this->args['page_title'], $this->args['menu_title'], $this->args['capability'], $this->args['menu_slug'], array( $this, 'render' ) );
}
}
}
/**
* Parent file fix when a view is registered in a submenu
*
* @since 1.0.0
*/
public function parent_file( $parent_file ) {
global $tdb_table, $plugin_page;
if( ! $this->is_current_view() ) {
return $parent_file;
}
$list_view_args = $tdb_table->views->list->args;
// If not empty parent slug, override actual parent slug
if( ! empty( $this->args['parent_slug'] ) ) {
$parent_file = $this->args['parent_slug'];
if( $this->args['menu_slug'] !== $list_view_args['menu_slug'] ) {
// Hack required to make parent file work because get overwritten on get_admin_page_parent() function
$plugin_page = null;
}
}
// If we are on an add or edit view and list is displayed on menu, apply the list parent slug
if( $this->args['menu_slug'] !== $list_view_args['menu_slug'] ) {
if( $list_view_args['show_in_menu'] && ! empty( $list_view_args['parent_slug'] ) ) {
$parent_file = $list_view_args['parent_slug'];
// Hack required to make parent file work because get overwritten on get_admin_page_parent() function
$plugin_page = null;
}
}
return $parent_file;
}
/**
* Submenu file fix when a view is registered in a submenu
*
* @since 1.0.0
*/
public function submenu_file( $submenu_file, $parent_file ) {
global $tdb_table;
if( ! $this->is_current_view() ) {
return $submenu_file;
}
$list_view_args = $tdb_table->views->list->args;
// If we are on an add or edit view and list is displayed on menu, then highlight list view
if( $this->args['menu_slug'] !== $list_view_args['menu_slug'] ) {
if( $list_view_args['show_in_menu'] && ! empty( $list_view_args['parent_slug'] ) ) {
$submenu_file = $list_view_args['menu_slug'];
}
}
return $submenu_file;
}
/**
* Restore global $plugin_page since it was required to get overwritten on parent_file()
*
* @since 1.0.0
*/
public function restore_plugin_page() {
global $tdb_table, $plugin_page;
if( ! $this->is_current_view() ) {
return;
}
$list_view_args = $tdb_table->views->list->args;
// If we are on an add or edit view and list is displayed on menu, restore plugin page too
if( $this->args['menu_slug'] !== $list_view_args['menu_slug'] ) {
// If not empty parent slug then restore plugin page
if( ! empty( $this->args['parent_slug'] ) ) {
$plugin_page = $this->args['menu_slug'];
}
if( $list_view_args['show_in_menu'] && ! empty( $list_view_args['parent_slug'] ) ) {
$plugin_page = $this->args['menu_slug'];
}
}
}
public function is_current_view() {
global $tdb_registered_tables, $pagenow;
if( $pagenow !== 'admin.php' ) {
return false;
}
if( ! isset( $_GET['page'] ) ) {
return false;
}
if( empty( $_GET['page'] ) || $_GET['page'] !== $this->args['menu_slug'] ) {
return false;
}
if( ! isset( $tdb_registered_tables[$this->name] ) ) {
return false;
}
return true;
}
public function get_slug() {
return $this->args['menu_slug'];
}
public function get_link() {
return admin_url( "admin.php?page=" . $this->args['menu_slug'] );
}
/**
* View admin init.
*
* This function is called on admin_init hook.
* Includes some checks to determine if the init() function should be called.
*
* @since 1.0.0
*/
public function admin_init() {
global $tdb_registered_tables, $tdb_table;
if( ! $this->is_current_view() ) {
return;
}
// Setup the global TDB_Table object for this screen
$tdb_table = $tdb_registered_tables[$this->name];
// Run the init function
$this->init();
}
/**
* View init.
*
* Run redirects here to avoid "headers already sent" error.
*
* @since 1.0.0
*/
public function init() {
do_action( "tdb_init_{$this->name}_view", $this );
}
/**
* View content.
*
* @since 1.0.0
*/
public function render() {
do_action( "tdb_render_{$this->name}_view", $this );
}
}