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