Code_Manager.php
4 years ago
Code_Manager_Dashboard.php
4 years ago
Code_Manager_Export.php
4 years ago
Code_Manager_Form.php
4 years ago
Code_Manager_Import.php
4 years ago
Code_Manager_Import_File.php
4 years ago
Code_Manager_List.php
4 years ago
Code_Manager_List_View.php
4 years ago
Code_Manager_Model.php
4 years ago
Code_Manager_Preview.php
4 years ago
Code_Manager_Settings.php
4 years ago
Code_Manager_Tabs.php
4 years ago
Message_Box.php
4 years ago
WP_List_Table.php
4 years ago
Code_Manager_List_View.php
149 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Code_Manager { |
| 4 | |
| 5 | /** |
| 6 | * Class Code_Manager_List_View |
| 7 | * |
| 8 | * Implements a layer between the menu and the list table/data entry form. |
| 9 | * |
| 10 | * @author Peter Schulz |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | class Code_Manager_List_View { |
| 14 | |
| 15 | /** |
| 16 | * Page hook suffix |
| 17 | * |
| 18 | * @var object|boolean Reference to (sub) menu or false |
| 19 | */ |
| 20 | protected $page_hook_suffix; |
| 21 | |
| 22 | /** |
| 23 | * Action (taken from $_REQUEST) |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $action; |
| 28 | |
| 29 | /** |
| 30 | * Code_Manager_List_View constructor |
| 31 | * |
| 32 | * page_hook_suffix |
| 33 | * |
| 34 | * @param array $args [ |
| 35 | * 'page_hook_suffix' => (string|boolean) Page hook suffix or false (default = false) |
| 36 | * ] |
| 37 | */ |
| 38 | public function __construct( $args = [] ) { |
| 39 | $this->action = |
| 40 | isset( $_REQUEST['action'] ) ? |
| 41 | sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) : |
| 42 | ''; // input var okay. |
| 43 | |
| 44 | if ( 'new' !== $this->action && 'edit' !== $this->action ) { |
| 45 | $this->page_hook_suffix = isset( $args['page_hook_suffix'] ) ? $args['page_hook_suffix'] : false; |
| 46 | if ( false !== $this->page_hook_suffix ) { |
| 47 | add_action( 'load-' . $this->page_hook_suffix, [ $this, 'page_screen_options' ] ); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Show the list table or data entry form |
| 54 | * |
| 55 | * Depending on the value of argument action: |
| 56 | * (1) The list table is shown when no actions is defined |
| 57 | * (2) The data entry form is shown for actions new and edit |
| 58 | * |
| 59 | * @since 1.0.0 |
| 60 | */ |
| 61 | public function show() { |
| 62 | switch ( $this->action ) { |
| 63 | case 'new': |
| 64 | case 'edit': |
| 65 | $this->display_edit_form(); |
| 66 | break; |
| 67 | default: |
| 68 | $this->display_list_table(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Show data entry form |
| 74 | * |
| 75 | * @since 1.0.0 |
| 76 | */ |
| 77 | public function display_edit_form() { |
| 78 | $formclass = CODE_MANAGER_FORM_CLASS; |
| 79 | $form = new $formclass(); |
| 80 | $form->show(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Show list table |
| 85 | * |
| 86 | * @since 1.0.0 |
| 87 | */ |
| 88 | public function display_list_table() { |
| 89 | $listclass = CODE_MANAGER_LIST_CLASS; |
| 90 | $list = new $listclass(); |
| 91 | $list->show(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Prepare screen options |
| 96 | * |
| 97 | * @since 1.0.0 |
| 98 | */ |
| 99 | public function page_screen_options() { |
| 100 | if ( is_admin() ) { |
| 101 | add_filter('set-screen-option', [ $this, 'set_screen_option' ], 10, 3); |
| 102 | |
| 103 | set_screen_options(); |
| 104 | $screen = get_current_screen(); |
| 105 | |
| 106 | if ( is_object( $screen ) && $screen->id === $this->page_hook_suffix ) { |
| 107 | // Add column selection |
| 108 | $hidden = get_user_meta( |
| 109 | get_current_user_id(), |
| 110 | 'manage' . get_current_screen()->id . 'columnshidden' |
| 111 | ); |
| 112 | |
| 113 | if ( 0 === sizeof( $hidden ) ) { |
| 114 | add_user_meta( |
| 115 | get_current_user_id(), |
| 116 | 'manage' . get_current_screen()->id . 'columnshidden', |
| 117 | [ 0 => 'code_author', 1 => 'code_description' ] |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | add_filter( |
| 122 | 'manage_' . get_current_screen()->id . '_columns', |
| 123 | [ Code_Manager_List::class, 'get_column_labels_default' ], |
| 124 | 0 |
| 125 | ); |
| 126 | |
| 127 | // Add pagination |
| 128 | $pagination = 10; |
| 129 | $args = [ |
| 130 | 'label' => __( 'Number of items per page', 'code-manager' ), |
| 131 | 'default' => $pagination, |
| 132 | 'option' => 'code_manager_rows_per_page', |
| 133 | ]; |
| 134 | add_screen_option( 'per_page', $args ); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | function set_screen_option($status, $option, $value) { |
| 140 | if ( 'code_manager_rows_per_page' == $option ) { |
| 141 | return $value; |
| 142 | } |
| 143 | |
| 144 | return $status; |
| 145 | } |
| 146 | |
| 147 | } |
| 148 | |
| 149 | } |