PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.84
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.84
5.5.84 5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 All 160 releases
wp-data-access / WPDataAccess / List_Table / WPDA_List_View.php

WPDA_List_View.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.84, at WPDataAccess/List_Table/WPDA_List_View.php

943 lines 32.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Suppress "error - 0 - No summary was found for this file" on phpdoc generation
4 *
5 * @package WPDataAccess\List_Table
6 */
7
8 namespace WPDataAccess\List_Table {
9
10 use WPDataAccess\CSV_Files\WPDA_CSV_List_Table;
11 use WPDataAccess\Data_Dictionary\WPDA_List_Columns;
12 use WPDataAccess\Data_Dictionary\WPDA_List_Columns_Cache;
13 use WPDataAccess\Data_Publisher\WPDA_Publisher_List_Table;
14 use WPDataAccess\Design_Table\WPDA_Design_Table_Form;
15 use WPDataAccess\Design_Table\WPDA_Design_Table_List_Table;
16 use WPDataAccess\Plugin_Table_Models\WPDA_CSV_Uploads_Model;
17 use WPDataAccess\Plugin_Table_Models\WPDA_Design_Table_Model;
18 use WPDataAccess\Plugin_Table_Models\WPDA_Publisher_Model;
19 use WPDataAccess\Plugin_Table_Models\WPDP_Page_Model;
20 use WPDataAccess\Plugin_Table_Models\WPDP_Project_Design_Table_Model;
21 use WPDataAccess\Plugin_Table_Models\WPDP_Project_Model;
22 use WPDataAccess\Utilities\WPDA_Repository;
23 use WPDataAccess\WPDA;
24 use WPDataProjects\Data_Dictionary\WPDP_List_Columns_Cache;
25 use WPDataProjects\Project\WPDP_Project;
26 use WPDataProjects\Project\WPDP_Project_Page_List;
27 use WPDataProjects\Project\WPDP_Project_Project_List;
28 use WPDataProjects\Project\WPDP_Project_Table_List;
29
30 /**
31 * Class WPDA_List_View
32 *
33 * A list view is an object that consists of a WordPress list table and it's screen options (displayed in the top
34 * right corner). The list view combines these options by identifying the different stages that apply to building
35 * a page containing both.
36 *
37 * Stages:
38 * + Screen options are added in the constructor
39 * + The list table is created in {@see WPDA_List_View::display_list_table()}
40 *
41 * To make sure that screen options are displayed with the list table, the constructor is called in the
42 * 'admin_menu' hook {@see \WP_Data_Access::define_admin_hooks()}. A object reference is stored in the class
43 * and used later when the list table is created on the page displayed.
44 *
45 * @see WPDA_List_View::display_list_table()
46 * @see \WP_Data_Access::define_admin_hooks()
47 *
48 * @author Peter Schulz
49 * @since 1.0.0
50 */
51 class WPDA_List_View {
52
53 const HIDDENCOLUMNS_PREFIX = 'wpda_manage_columnshidden_';
54
55 protected static $screen_settings_saved = false;
56
57 /**
58 * Page hook suffix
59 *
60 * @var object|boolean Reference to (sub) menu or false
61 */
62 protected $page_hook_suffix;
63
64 /**
65 * Database schema name
66 *
67 * @var string
68 */
69 protected $schema_name;
70
71 /**
72 * Database table name
73 *
74 * @var string
75 */
76 protected $table_name;
77
78 /**
79 * Indicates if bulk actions are allow
80 *
81 * @var boolean
82 */
83 protected $bulk_actions_enabled;
84
85 /**
86 * Indicates if a search box is shown
87 *
88 * @var boolean
89 */
90 protected $search_box_enabled;
91
92 /**
93 * Indicates if bulk exports are allowed
94 *
95 * @var boolean
96 */
97 protected $bulk_export_enabled;
98
99 /**
100 * Classname of list table
101 *
102 * @var string
103 */
104 protected $list_table_class;
105
106 /**
107 * Classname of data entry form
108 *
109 * @var string
110 */
111 protected $edit_form_class;
112
113 /**
114 * Reference to list table
115 */
116 protected $wpda_list_table;
117
118 /**
119 * Reference to list columns
120 *
121 * @var WPDA_List_Columns
122 */
123 protected $wpda_list_columns = null;
124
125 /**
126 * Page title
127 *
128 * @var string
129 */
130 protected $title;
131
132 /**
133 * Page subtitle
134 *
135 * @var string
136 */
137 protected $subtitle;
138
139 /**
140 * Action (taken from $_REQUEST)
141 *
142 * @var string
143 */
144 protected $action = '';
145
146 /**
147 * Column headers (labels)
148 *
149 * @var string
150 */
151 protected $column_headers;
152
153 /**
154 * Show view link in list table? (on|off)
155 *
156 * @var string
157 */
158 protected $show_view_link = null;
159
160 /**
161 * Allow insert? (on|off)
162 *
163 * @var string
164 */
165 protected $allow_insert = null;
166
167 /**
168 * Allow update? (on|off)
169 *
170 * @var string
171 */
172 protected $allow_update = null;
173
174 /**
175 * Allow delete? (on|off)
176 *
177 * @var string
178 */
179 protected $allow_delete = null;
180
181 /**
182 * Allow import? (on|off)
183 *
184 * @var string
185 */
186 protected $allow_import = null;
187
188 /**
189 * Default WHERE clause
190 *
191 * @var string
192 */
193 protected $default_where = '';
194
195 /**
196 * Contains child table name if child request, otherwise false
197 *
198 * I don't like this kind of programming! This should be in child class of course! The load action to add
199 * screen options however cannot be delayed and therefore must be in the parent call.
200 *
201 * @var bool|string
202 */
203 protected $child_request = false;
204
205 /**
206 * WPDA_List_View constructor
207 *
208 * Page hook suffix
209 *
210 * We first check if we have a page hook suffix. This is the reference to the sub menu to which we want to
211 * add the list view. If no page hook suffix is provided, the list table might be displayed as expected, the
212 * screen options in the top right corner however will not be shown.
213 *
214 * Database table usage
215 * + The constructor can be called with or without a table name. If a table name is provided, a list table is
216 * generated for that database table.
217 * + If no table name is provided, we need to checked if a table name argument was given in the request. If a
218 * table name was provided with the request, a list table is generated for that table.
219 * + If no table name is provided (neither as an argument nor with the request) a list of all tables available
220 * in the WordPress database schema is generated.
221 *
222 * Table names are always checked! We need to check:
223 * + if a table exists in our database schema and
224 * + if we have access to that table.
225 *
226 * These checks are performed to prevent SQL injection and misuse of our WordPress database. These checks
227 * however are not performed in this class. They are performed in class {@see WPDA_List_Table} as we do not
228 * perform any queries in this class. We do perform queries on the given tables in {@see WPDA_List_Table}.
229 *
230 * @param array $args [
231 *
232 * 'page_hook_suffix' => (string|boolean) Page hook suffix of false (default = false)
233 *
234 * 'wpdaschema_name' => (string) Database schema name (default = '')
235 *
236 * 'table_name' => (string) Database table name (default = '')
237 *
238 * 'bulk_actions_enabled' => (boolean) Allow bulk actions? (default = TRUE)
239 *
240 * 'search_box_enabled' => (boolean) Show search box? (default = TRUE)
241 *
242 * 'bulk_export_enabled' => (boolean) Allow bulk exports? (default = TRUE)
243 *
244 * 'list_table_class' => (string) Class providing list table functionality
245 *
246 * 'edit_form_class' => (string) Class providing data entry functionality
247 *
248 * 'column_headers' => (array|string) Column headers (default = '' : headers taken from data dictionary)
249 *
250 * 'title' => (string) Page title (default = null)
251 *
252 * 'subtitle' => (string) Page subtitle (default = null)
253 *
254 * 'show_view_link' => (string) default = 'on'
255 *
256 * 'allow_insert' => (string) default = 'off'
257 *
258 * 'allow_update' => (string) default = 'off'
259 *
260 * 'allow_delete' => (string) default = 'off'
261 *
262 * 'allow_import' => (string) default = 'off'
263 *
264 * 'default_where' => (string)
265 *
266 * ]
267 * @see WPDA_List_Table
268 *
269 * @since 1.0.0
270 */
271 public function __construct( $args = array() ) {
272 $args = wp_parse_args(
273 $args,
274 array(
275 'page_hook_suffix' => false,
276 'wpdaschema_name' => '',
277 'table_name' => '',
278 'bulk_actions_enabled' => WPDA_List_Table::DEFAULT_BULK_ACTIONS_ENABLED,
279 'search_box_enabled' => WPDA_List_Table::DEFAULT_SEARCH_BOX_ENABLED,
280 'bulk_export_enabled' => WPDA_List_Table::DEFAULT_BULK_EXPORT_ENABLED,
281 'list_table_class' => 'WPDataAccess\\List_Table\\WPDA_List_Table',
282 'edit_form_class' => 'WPDataAccess\\Simple_Form\\WPDA_Simple_Form',
283 'column_headers' => '',
284 'title' => null,
285 'subtitle' => null,
286 )
287 );
288
289 // Check access arguments
290 if ( isset( $args['show_view_link'] ) ) {
291 $this->show_view_link = $args['show_view_link'];
292 }
293 if ( isset( $args['allow_insert'] ) ) {
294 $this->allow_insert = $args['allow_insert'];
295 }
296 if ( isset( $args['allow_update'] ) ) {
297 $this->allow_update = $args['allow_update'];
298 }
299 if ( isset( $args['allow_delete'] ) ) {
300 $this->allow_delete = $args['allow_delete'];
301 }
302 if ( isset( $args['allow_import'] ) ) {
303 $this->allow_import = $args['allow_import'];
304 }
305
306 $this->schema_name = $args['wpdaschema_name'];
307 if ( '' === $this->schema_name ) {
308 // No pre defined schema_name!
309 if ( isset( $_REQUEST['wpdaschema_name'] ) ) { // phpcs:ignore
310 // Get schema name from URL.
311 $this->schema_name = sanitize_text_field( wp_unslash( $_REQUEST['wpdaschema_name'] ) ); // phpcs:ignore
312 }
313 }
314
315 $this->table_name = $args['table_name'];
316 if ( '' === $this->table_name ) {
317 // No pre defined table_name!
318 if ( isset( $_REQUEST['table_name'] ) ) { // phpcs:ignore
319 // Get table name from URL. (later we'll check if the table exists in the WordPress database to
320 // protect ourselves against SQL injection).
321 $this->table_name = sanitize_text_field( wp_unslash( $_REQUEST['table_name'] ) ); // phpcs:ignore
322 }
323 }
324
325 $this->page_hook_suffix = $args['page_hook_suffix'];
326
327 // Set class to provide list table functionality.
328 $this->list_table_class = $args['list_table_class'];
329
330 // Set class for data entry form support (used for new, edit and view actions).
331 $this->edit_form_class = $args['edit_form_class'];
332
333 // Set page title.
334 $this->title = $args['title'];
335
336 // Set page subtitle.
337 $this->subtitle = $args['subtitle'];
338
339 $this->bulk_actions_enabled = $args['bulk_actions_enabled'];
340 $this->search_box_enabled = $args['search_box_enabled'];
341 $this->bulk_export_enabled = $args['bulk_export_enabled'];
342
343 if ( isset( $args['action'] ) ) {
344 $this->action = $args['action'];
345 } else {
346 if ( isset( $_REQUEST['action'] ) ) { // phpcs:ignore
347 $this->action = sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ); // phpcs:ignore
348 }
349 }
350
351 if ( false !== $this->page_hook_suffix ) {
352 if (
353 is_admin() &&
354 ! isset( $_REQUEST['page_action'] ) && // phpcs:ignore
355 (
356 ! ( 'new' === $this->action || 'edit' === $this->action || 'view' === $this->action || 'user_menu' === $this->action ) // phpcs:ignore
357 )
358 ) {
359 $page_action = isset( $_REQUEST['page_action'] ) ? 'defined' : ''; // phpcs:ignore
360 if ( $page_action === '' ) {
361 // Add screen options.
362 add_action( 'load-' . $this->page_hook_suffix, array( $this, 'page_screen_options' ) );
363 }
364 }
365 }
366
367 // Overwrite column header text.
368 $this->column_headers = isset( $args['column_headers'] ) ? $args['column_headers'] : '';
369
370 // Check for default WHERE clause
371 if ( isset( $args['default_where'] ) ) {
372 $this->default_where = $args['default_where'];
373 }
374 }
375
376 /**
377 * Set columns to be queried
378 *
379 * If not set all column (*) will be selected/set/queried.
380 *
381 * @param mixed $columns_queried Column array, '' or '*'.
382 *
383 * @since 1.0.0
384 */
385 public function set_columns_queried( $columns_queried ) {
386
387 $this->wpda_list_table->set_columns_queried( $columns_queried );
388
389 }
390
391 /**
392 * Enable or disable bulk actions
393 *
394 * If enabled user can perform actions on multiple rows at once.
395 *
396 * @param boolean $bulk_actions_enabled TRUE = allow bulk actions, FALSE = no bulk actions.
397 *
398 * @since 1.0.0
399 */
400 public function set_bulk_actions_enabled( $bulk_actions_enabled ) {
401
402 $this->bulk_actions_enabled = $bulk_actions_enabled;
403
404 }
405
406 /**
407 * Enable search box
408 *
409 * Shows a search box if enabled. In WP Data Access only columns with data type varchar or enum are searched.
410 *
411 * @param boolean $search_box_enabled TRUE = show search box, FALSE = no search box.
412 *
413 * @since 1.0.0
414 */
415 public function set_search_box_enabled( $search_box_enabled ) {
416
417 $this->search_box_enabled = $search_box_enabled;
418
419 }
420
421 /**
422 * Display page
423 *
424 * Page types to be displayed:
425 * + List table
426 * + View form
427 * + Data entry form (add new)
428 * + Data entry form (update)
429 *
430 * The type of table displayed depend on the value of the action argument provided in the request. The value
431 * of argument action is stored in $this->action in the constructor.
432 *
433 * @since 1.0.0
434 */
435 public function show() {
436 // Add datetimepicker
437 wp_enqueue_style( 'datetimepicker' );
438 wp_enqueue_script( 'datetimepicker' );
439
440 // Prepare columns for list table. Needed in get_column_headers() and handed over to list table to prevent
441 // processing the same queries multiple times.
442 if ( null === $this->wpda_list_columns ) {
443 $this->wpda_list_columns = WPDA_List_Columns_Cache::get_list_columns( $this->schema_name, $this->table_name );
444 }
445
446 $wpda_repository = new WPDA_Repository();
447 $wpda_repository->inform_user();
448
449 switch ( $this->action ) {
450 case 'new': // Show edit form in editing mode to create new records.
451 case 'edit': // Show edit form in editing mode to update records.
452 case 'view': // Show edit form in view mode to view records.
453 $this->display_edit_form();
454 break;
455 case 'create_table': // Show form to create new table.
456 $this->display_design_menu();
457 break;
458 default: // Show list (default).
459 $this->display_list_table();
460 }
461
462 }
463
464 /**
465 * Display data entry form
466 *
467 * Called when action is:
468 * + 'new' to add a new record to the table
469 * + 'edit' to update a record
470 * + 'view" to show a record (readonly)
471 *
472 * Class WPDA_Simple_Form is the default class used to generate data entry forms. This class provides dynamic
473 * generation of data entry forms for any table, as long as the table has a primary key. The primary key is
474 * necessary to perform updates (unique identification of records).
475 *
476 * For more specific data entry forms WPDA_Simple_Form can be extended. These classes need to implement some
477 * methods to work properly. Check out {@see \WPDataAccess\Simple_Form\WPDA_Simple_Form} for more information.
478 *
479 * @since 1.0.0
480 *
481 * @see \WPDataAccess\Simple_Form\WPDA_Simple_Form
482 */
483 protected function display_edit_form() {
484 $form = new $this->edit_form_class(
485 $this->schema_name,
486 $this->table_name,
487 $this->wpda_list_columns
488 );
489
490 $form->prepare_form();
491
492 if ( isset( $_POST['postaction'] ) && 'list' === $_POST['postaction'] ) { // phpcs:ignore
493 // Jump back to list after pressing SUBMIT > LIST
494 $this->display_list_table();
495 } else {
496 $form->show();
497 }
498 }
499
500 /**
501 * Adds Data Designer menu-item to plugin menu
502 */
503 protected function display_design_menu() {
504 $form = new WPDA_Design_Table_Form();
505 $form->show();
506 }
507
508 /**
509 * Display list table
510 *
511 * A list of tables in the WordPress database schema is in fact a list of rows as well. The MySQL base table
512 * (which is in fact a view) used to show this information is 'information_schema.tables'. The list of tables
513 * contains a link to a list table for every table.
514 *
515 * @since 1.0.0
516 */
517 protected function display_list_table() {
518 $args = array(
519 'wpdaschema_name' => $this->schema_name,
520 'table_name' => $this->table_name,
521 'wpda_list_columns' => $this->wpda_list_columns,
522 'column_headers' => $this->column_headers,
523 'default_where' => $this->default_where,
524 );
525 if ( null !== $this->show_view_link ) {
526 $args['show_view_link'] = $this->show_view_link;
527 }
528 if ( null !== $this->allow_insert ) {
529 $args['allow_insert'] = $this->allow_insert;
530 }
531 if ( null !== $this->allow_update ) {
532 $args['allow_update'] = $this->allow_update;
533 }
534 if ( null !== $this->allow_delete ) {
535 $args['allow_delete'] = $this->allow_delete;
536 }
537 if ( null !== $this->allow_import ) {
538 $args['allow_import'] = $this->allow_import;
539 }
540
541 $this->wpda_list_table = new $this->list_table_class( $args );
542
543 $this->wpda_list_table->set_bulk_actions_enabled( $this->bulk_actions_enabled );
544 $this->wpda_list_table->set_search_box_enabled( $this->search_box_enabled );
545 $this->wpda_list_table->set_bulk_export_enabled( $this->bulk_export_enabled );
546
547 // Reset page title and subtitle to allow empty titles and subtitles as well.
548 if ( null !== $this->title ) {
549 $this->wpda_list_table->set_title( $this->title );
550 }
551 if ( null !== $this->subtitle ) {
552 $this->wpda_list_table->set_subtitle( $this->subtitle );
553 }
554
555 $this->wpda_list_table->show();
556 }
557
558 /**
559 * Set page screen options
560 *
561 * Provided are column selection (enable/disable) and rows per page. The table name is included in the meta_key
562 * to save screen options per table.
563 *
564 * @since 1.0.0
565 */
566 public function page_screen_options() {
567 if ( isset( $_REQUEST['child_tab'] ) ) { // phpcs:ignore
568 $this->child_request = sanitize_text_field( wp_unslash( $_REQUEST['child_tab'] ) ); // phpcs:ignore
569 }
570
571 if ( false !== $this->child_request ) {
572 $table_name = str_replace( '.', '_', $this->schema_name . $this->child_request );
573 } else {
574 global $wpdb;
575 if ( $this->schema_name === $wpdb->dbname && $this->table_name === WPDA_CSV_Uploads_Model::get_base_table_name() ) {
576 $table_name = $this->table_name; // csv upload = exception
577 } else {
578 $table_name = str_replace( '.', '_', $this->schema_name . $this->table_name );
579 }
580 }
581
582 add_filter( 'screen_settings', array( $this, 'show_screen_options' ), 10, 2 );
583 $this->set_screen_option();
584 $screen = get_current_screen();
585
586 if ( is_object( $screen ) && $screen->id === $this->page_hook_suffix ) {
587 // Set default column display values for repository tables if screen options is activated
588 // for the first time
589 if ( WPDA_Design_Table_Model::get_base_table_name() === $this->table_name ) {
590 if ( is_admin() &&
591 false === get_user_option( self::HIDDENCOLUMNS_PREFIX . get_current_screen()->id . $table_name )
592 ) {
593 $hidden = array(
594 'wpda_table_design',
595 );
596
597 update_user_meta(
598 get_current_user_id(),
599 self::HIDDENCOLUMNS_PREFIX . get_current_screen()->id . $table_name,
600 $hidden
601 );
602 }
603 } elseif ( WPDA_Publisher_Model::get_base_table_name() === $this->table_name ) {
604 if ( is_admin() &&
605 false === get_user_option( self::HIDDENCOLUMNS_PREFIX . get_current_screen()->id . $table_name )
606 ) {
607 $hidden = array(
608 'pub_table_name',
609 'pub_column_names',
610 'pub_sort_icons',
611 'pub_styles',
612 'pub_style_premium',
613 'pub_style_user',
614 'pub_style_color',
615 'pub_style_space',
616 'pub_style_corner',
617 'pub_style_modal_width',
618 'pub_responsive_popup_title',
619 'pub_responsive_cols',
620 'pub_responsive_type',
621 'pub_responsive_modal_hyperlinks',
622 'pub_responsive_icon',
623 'pub_flat_scrollx',
624 'pub_format',
625 'pub_default_where',
626 'pub_default_orderby',
627 'pub_table_options_searching',
628 'pub_table_options_ordering',
629 'pub_table_options_paging',
630 'pub_table_options_advanced',
631 'pub_table_options_serverside',
632 'pub_table_options_nl2br',
633 'pub_show_advanced_settings',
634 'pub_extentions',
635 'pub_query',
636 'pub_cpt',
637 'pub_cpt_fields',
638 'pub_cpt_query',
639 'pub_cpt_format',
640 );
641
642 update_user_meta(
643 get_current_user_id(),
644 self::HIDDENCOLUMNS_PREFIX . get_current_screen()->id . $table_name,
645 $hidden
646 );
647 }
648 } elseif ( WPDP_Project_Model::get_base_table_name() === $this->table_name ) {
649 if ( is_admin() &&
650 false === get_user_option( self::HIDDENCOLUMNS_PREFIX . get_current_screen()->id . $table_name )
651 ) {
652 $hidden = array(
653 'project_description',
654 'project_sequence',
655 );
656
657 update_user_meta(
658 get_current_user_id(),
659 self::HIDDENCOLUMNS_PREFIX . get_current_screen()->id . $table_name,
660 $hidden
661 );
662
663 // Set default columns for project pages
664 if ( false === get_user_option( self::HIDDENCOLUMNS_PREFIX . get_current_screen()->id . WPDP_Page_Model::get_base_table_name() ) ) {
665 $hidden = array(
666 'project_id',
667 'page_schema_name',
668 'page_setname',
669 'page_allow_insert',
670 'page_allow_delete',
671 'page_allow_import',
672 'page_allow_bulk',
673 'page_allow_full_export',
674 'page_content',
675 'page_title',
676 'page_subtitle',
677 'page_where',
678 'page_orderby',
679 'page_sequence',
680 );
681
682 update_user_meta(
683 get_current_user_id(),
684 self::HIDDENCOLUMNS_PREFIX . get_current_screen()->id . WPDP_Page_Model::get_base_table_name(),
685 $hidden
686 );
687 }
688 }
689 } elseif ( WPDP_Project_Design_Table_Model::get_base_table_name() === $this->table_name ) {
690 if ( is_admin() &&
691 false === get_user_option( self::HIDDENCOLUMNS_PREFIX . get_current_screen()->id . $table_name )
692 ) {
693 $hidden = array();
694
695 update_user_meta(
696 get_current_user_id(),
697 self::HIDDENCOLUMNS_PREFIX . get_current_screen()->id . $table_name,
698 $hidden
699 );
700 }
701 } elseif ( WPDA_CSV_Uploads_Model::get_base_table_name() === $this->table_name ) {
702 if ( is_admin() &&
703 false === get_user_option( self::HIDDENCOLUMNS_PREFIX . get_current_screen()->id . $table_name )
704 ) {
705 $hidden = array(
706 'csv_real_file_name',
707 'csv_mapping',
708 'csv_encoding',
709 );
710
711 update_user_meta(
712 get_current_user_id(),
713 self::HIDDENCOLUMNS_PREFIX . get_current_screen()->id . $table_name,
714 $hidden
715 );
716 }
717 }
718
719 // Allow external user to store defaults through action hook wpda_default_screen_option.
720 do_action( 'wpda_default_screen_option', $this->table_name, $table_name );
721
722 // Add column selection
723 if ( false !== $this->child_request ) {
724 if ( WPDP_Page_Model::get_base_table_name() === $this->child_request ) {
725 $cols = WPDP_Project_Page_List::column_headers_labels();
726 $hidden = get_user_meta(
727 get_current_user_id(),
728 self::HIDDENCOLUMNS_PREFIX . get_current_screen()->id . WPDP_Page_Model::get_base_table_name()
729 );
730 } else {
731 $setname = 'default';
732 if ( isset( $_REQUEST['page'] ) ) { // phpcs:ignore
733 $ids = explode( '_', sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ) ); // phpcs:ignore
734 if ( 4 === count( $ids ) ) { // phpcs:ignore -- 8.1 proof
735 $proj_id = $ids[2];
736 $page_id = $ids[3];
737 $project = new WPDP_Project( $proj_id, $page_id );
738 if ( null !== $project->get_project() ) {
739 if ( null !== $project->get_setname() ) {
740 $setname = $project->get_setname();
741 }
742 }
743 }
744 }
745 $wpdp_list_columns_child = WPDP_List_Columns_Cache::get_list_columns( $this->schema_name, $this->child_request, 'listtable', $setname );
746 $cols = $wpdp_list_columns_child->get_table_column_headers();
747 $hidden = get_user_meta(
748 get_current_user_id(),
749 self::HIDDENCOLUMNS_PREFIX . get_current_screen()->id . $table_name
750 );
751 }
752 } else {
753 $cols = $this->get_column_headers();
754 $hidden = get_user_meta(
755 get_current_user_id(),
756 self::HIDDENCOLUMNS_PREFIX . get_current_screen()->id . $table_name
757 );
758 }
759 foreach ( $cols as $col => $label ) {
760 if ( '' !== $label ) {
761 $args = array(
762 'option' => self::HIDDENCOLUMNS_PREFIX . $table_name . $col,
763 'column_name' => $col,
764 'label' => $label,
765 'value' => isset( $hidden[0] ) && in_array( $col, $hidden[0] ) ? '' : 'checked="checked"',
766 'default' => '',
767 );
768 add_screen_option( $col, $args );
769 }
770 }
771
772 // Add pagination
773 $pagination = WPDA::get_option( WPDA::OPTION_BE_PAGINATION );
774 $args = array(
775 'label' => __( 'Number of items per page', 'wp-data-access' ),
776 'default' => $pagination,
777 'option' => 'wpda_rows_per_page_' . $table_name,
778 );
779 add_screen_option( 'per_page', $args );
780 }
781 }
782
783 /**
784 * Get column headers
785 *
786 * @return array
787 * @since 1.0.0
788 */
789 public function get_column_headers() {
790 if ( WPDA_Design_Table_Model::get_base_table_name() === $this->table_name ) {
791 return WPDA_Design_Table_List_Table::column_headers_labels();
792 } elseif ( WPDA_Publisher_Model::get_base_table_name() === $this->table_name ) {
793 return WPDA_Publisher_List_Table::column_headers_labels();
794 } elseif ( WPDP_Project_Model::get_base_table_name() === $this->table_name ) {
795 return WPDP_Project_Project_List::column_headers_labels();
796 } elseif ( WPDP_Project_Design_Table_Model::get_base_table_name() === $this->table_name ) {
797 return WPDP_Project_Table_List::column_headers_labels();
798 } elseif ( WPDP_Page_Model::get_base_table_name() === $this->table_name ) {
799 return WPDP_Project_Page_List::column_headers_labels();
800 } elseif ( WPDA_CSV_Uploads_Model::get_base_table_name() === $this->table_name ) {
801 return WPDA_CSV_List_Table::column_headers_labels();
802 } else {
803 if ( has_filter( 'wpda_get_column_headers' ) ) {
804 // Use filter
805 $column_headers = apply_filters( 'wpda_get_column_headers', $this->schema_name, $this->table_name );
806 if ( null !== $column_headers ) {
807 return $column_headers;
808 }
809 }
810 if ( null === $this->wpda_list_columns ) {
811 $this->wpda_list_columns = WPDA_List_Columns_Cache::get_list_columns( $this->schema_name, $this->table_name );
812 }
813 return $this->wpda_list_columns->get_table_column_headers();
814 }
815 }
816
817 /**
818 * Save screen options
819 *
820 * @return mixed
821 * @since 1.0.0
822 */
823 public function set_screen_option() {
824 $screen = get_current_screen();
825 if (
826 is_object( $screen ) &&
827 $screen->id === $this->page_hook_suffix &&
828 isset( $_REQUEST['screenoptionnonce'] ) // phpcs:ignore
829 ) {
830 // check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
831
832 if ( isset( $_REQUEST['wp_screen_options']['option'] ) && isset( $_REQUEST['wp_screen_options']['value'] ) ) { // phpcs:ignore
833 update_user_meta(
834 WPDA::get_current_user_id(),
835 sanitize_text_field( wp_unslash( $_REQUEST['wp_screen_options']['option'] ) ), // phpcs:ignore
836 sanitize_text_field( wp_unslash( $_REQUEST['wp_screen_options']['value'] ) ) // phpcs:ignore
837 );
838 }
839
840 if ( false !== $this->child_request ) {
841 $table_name = str_replace( '.', '_', $this->schema_name . $this->child_request );
842 if ( WPDP_Page_Model::get_base_table_name() === $this->child_request ) {
843 $cols = WPDP_Project_Page_List::column_headers_labels();
844 } else {
845 $wpda_list_columns_child = WPDA_List_Columns_Cache::get_list_columns( $this->schema_name, $this->child_request );
846 $cols = $wpda_list_columns_child->get_table_column_headers();
847 }
848 } else {
849 if ( '' === $this->table_name ) {
850 $table_name = str_replace( '.', '_', WPDA_List_Table::LIST_BASE_TABLE );
851 } else {
852 global $wpdb;
853 if ( $this->schema_name === $wpdb->dbname && $this->table_name === WPDA_CSV_Uploads_Model::get_base_table_name() ) {
854 $table_name = $this->table_name; // csv upload = exception
855 } else {
856 $table_name = str_replace( '.', '_', $this->schema_name . $this->table_name );
857 }
858 }
859 $cols = $this->get_column_headers();
860 }
861
862 $cols_hidden = array();
863 foreach ( $cols as $col => $label ) {
864 if ( isset( $_REQUEST[ $col . '-hide-setting' ] ) && 'HIDE' === $_REQUEST[ $col . '-hide-setting' ] ) { // phpcs:ignore
865 array_push( $cols_hidden, $col ); // phpcs:ignore -- 8.1 proof
866 }
867 }
868
869 if ( false !== $this->child_request && WPDP_Page_Model::get_base_table_name() === $this->child_request ) {
870 update_user_meta(
871 get_current_user_id(),
872 self::HIDDENCOLUMNS_PREFIX . get_current_screen()->id . $this->child_request,
873 $cols_hidden
874 );
875 } else {
876 update_user_meta(
877 get_current_user_id(),
878 self::HIDDENCOLUMNS_PREFIX . get_current_screen()->id . $table_name,
879 $cols_hidden
880 );
881 }
882 }
883 }
884
885 public function show_screen_options( $status, $args ) {
886 ob_start();
887 ?>
888 <fieldset class="metabox-prefs">
889 <legend><?php esc_html_e( 'Columns', 'wp-data-access' ); ?></legend>
890 <?php
891 $screen = get_current_screen();
892 foreach ( $screen->get_options() as $screen_option ) {
893 if ( self::HIDDENCOLUMNS_PREFIX === substr( $screen_option['option'], 0, 26 ) &&
894 '' !== $screen_option['label']
895 ) {
896 $hidden_value = '' === $screen_option['value'] ? 'HIDE' : 'SHOW';
897 ?>
898 <label>
899 <input type="checkbox"
900 id="<?php echo esc_attr( $screen_option['column_name'] ); ?>-hide"
901 name="<?php echo esc_attr( $screen_option['column_name'] ); ?>-hide"
902 value="<?php echo esc_attr( $screen_option['column_name'] ); ?>"
903 class="hide-column-tog"
904 <?php echo esc_attr( $screen_option['value'] ); ?>
905 onclick="update_column_setting(this)"
906 ><?php echo esc_attr( $screen_option['label'] ); ?>
907 <input type="hidden"
908 id="<?php echo esc_attr( $screen_option['column_name'] ); ?>-hide-setting"
909 name="<?php echo esc_attr( $screen_option['column_name'] ); ?>-hide-setting"
910 value="<?php echo esc_attr( $hidden_value ); ?>"
911 >
912 </label>
913 <?php
914 }
915 }
916 ?>
917 </fieldset>
918 <?php
919 if ( false !== $this->child_request ) {
920 echo '<input type="hidden" name="child_tab" value="' . esc_attr( $this->child_request ) . '">';
921 }
922 ?>
923 <script type='text/javascript'>
924 function update_column_setting(item) {
925 if (jQuery(item).is(':checked')) {
926 jQuery('#'+jQuery(item).attr('name')+'-setting').val('SHOW');
927 } else {
928 jQuery('#'+jQuery(item).attr('name')+'-setting').val('HIDE');
929 }
930 }
931 jQuery(function () {
932 // Switch fieldset: columns first = WordPress default
933 jQuery("#adv-settings fieldset:first").insertAfter(jQuery("#adv-settings fieldset:last"));
934 });
935 </script>
936 <?php
937 return ob_get_clean();
938 }
939
940 }
941
942 }
943