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