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