| @@ -9,9 +9,9 @@ | ||
| 9 | 9 | /** |
| 10 | 10 | * Include WP_List_Table if not defined. |
| 11 | 11 | */ |
| 12 | 12 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 13 | - require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; | |
| 13 | + require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); | |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | /** |
| 17 | 17 | * Class Multi_Value_Field_Table |
| @@ -16,83 +16,46 @@ | ||
| 16 | 16 | /** |
| 17 | 17 | * Class Multi_Value_Field_Table |
| 18 | 18 | */ |
| 19 | 19 | class Multi_Value_Field_Table extends WP_List_Table { |
| 20 | + private $_bulk_actions = array(); | |
| 21 | + private $_columns = array(); | |
| 22 | + private $_sortable_columns = array(); | |
| 23 | + private $_data = array(); | |
| 20 | 24 | |
| 21 | - /** | |
| 22 | - * Holds the supported bulk actions. | |
| 23 | - * | |
| 24 | - * @var array | |
| 25 | - */ | |
| 26 | - private $bulk_actions = array(); | |
| 25 | + function __construct() { | |
| 26 | + global $status, $page; | |
| 27 | 27 | |
| 28 | - /** | |
| 29 | - * Holds the table columns. | |
| 30 | - * | |
| 31 | - * @var array | |
| 32 | - */ | |
| 33 | - private $columns = array(); | |
| 34 | - | |
| 35 | - /** | |
| 36 | - * Holds the sortable table columns. | |
| 37 | - * | |
| 38 | - * @var array | |
| 39 | - */ | |
| 40 | - private $sortable_columns = array(); | |
| 41 | - | |
| 42 | - /** | |
| 43 | - * Holds the table rows and their data. | |
| 44 | - * | |
| 45 | - * @var array | |
| 46 | - */ | |
| 47 | - private $data = array(); | |
| 48 | - | |
| 49 | - /** | |
| 50 | - * Constructor. | |
| 51 | - * | |
| 52 | - * @since 1.0.0 | |
| 53 | - */ | |
| 54 | - public function __construct() { | |
| 55 | - | |
| 56 | - parent::__construct( | |
| 57 | - array( | |
| 58 | - 'singular' => 'item', | |
| 59 | - 'plural' => 'items', | |
| 60 | - 'ajax' => false, | |
| 61 | - ) | |
| 62 | - ); | |
| 63 | - | |
| 28 | + parent::__construct( array( | |
| 29 | + 'singular' => 'item', | |
| 30 | + 'plural' => 'items', | |
| 31 | + 'ajax' => false, | |
| 32 | + ) ); | |
| 64 | 33 | } |
| 65 | 34 | |
| 66 | 35 | /** |
| 67 | 36 | * Set default column attributes |
| 68 | 37 | * |
| 69 | - * @since 1.0.0 | |
| 70 | - * | |
| 71 | - * @param array $item A singular item (one full row's worth of data). | |
| 72 | - * @param string $column_name The name/slug of the column to be processed. | |
| 38 | + * @param array $item A singular item (one full row's worth of data). | |
| 39 | + * @param array $column_name The name/slug of the column to be processed. | |
| 73 | 40 | * @return string Text or HTML to be placed inside the column <td> |
| 74 | 41 | */ |
| 75 | 42 | public function column_default( $item, $column_name ) { |
| 76 | - | |
| 77 | 43 | return $item[ $column_name ]; |
| 78 | - | |
| 79 | 44 | } |
| 80 | 45 | |
| 81 | 46 | /** |
| 82 | 47 | * Provide a callback function to render the checkbox column |
| 83 | 48 | * |
| 84 | - * @param array $item A row's worth of data. | |
| 49 | + * @param array $item A row's worth of data. | |
| 85 | 50 | * @return string The formatted string with a checkbox |
| 86 | 51 | */ |
| 87 | 52 | public function column_cb( $item ) { |
| 88 | - | |
| 89 | 53 | return sprintf( |
| 90 | 54 | '<input type="checkbox" name="%1$s[]" value="%2$s" />', |
| 91 | 55 | $this->_args['singular'], |
| 92 | 56 | $item['id'] |
| 93 | 57 | ); |
| 94 | - | |
| 95 | 58 | } |
| 96 | 59 | |
| 97 | 60 | /** |
| 98 | 61 | * Get the bulk actions for this table |
| @@ -99,11 +62,9 @@ | ||
| 99 | 62 | * |
| 100 | 63 | * @return array Bulk actions |
| 101 | 64 | */ |
| 102 | 65 | public function get_bulk_actions() { |
| 103 | - | |
| 104 | - return $this->bulk_actions; | |
| 105 | - | |
| 66 | + return $this->_bulk_actions; | |
| 106 | 67 | } |
| 107 | 68 | |
| 108 | 69 | /** |
| 109 | 70 | * Get a list of columns |
| @@ -110,11 +71,9 @@ | ||
| 110 | 71 | * |
| 111 | 72 | * @return array |
| 112 | 73 | */ |
| 113 | 74 | public function get_columns() { |
| 114 | - | |
| 115 | - return $this->columns; | |
| 116 | - | |
| 75 | + return $this->_columns; | |
| 117 | 76 | } |
| 118 | 77 | |
| 119 | 78 | /** |
| 120 | 79 | * Add a column to the table |
| @@ -120,18 +79,16 @@ | ||
| 120 | 79 | * Add a column to the table |
| 121 | 80 | * |
| 122 | 81 | * @param string $key Machine-readable column name. |
| 123 | 82 | * @param string $title Title shown to the user. |
| 124 | - * @param boolean $sortable Whether or not this is sortable (defaults false). | |
| 83 | + * @param boolean $sortable Whether or not this is sortable (defaults false) | |
| 125 | 84 | */ |
| 126 | 85 | public function add_column( $key, $title, $sortable = false ) { |
| 86 | + $this->_columns[ $key ] = $title; | |
| 127 | 87 | |
| 128 | - $this->columns[ $key ] = $title; | |
| 129 | - | |
| 130 | 88 | if ( $sortable ) { |
| 131 | - $this->sortable_columns[ $key ] = array( $key, false ); | |
| 89 | + $this->_sortable_columns[ $key ] = array( $key, false ); | |
| 132 | 90 | } |
| 133 | - | |
| 134 | 91 | } |
| 135 | 92 | |
| 136 | 93 | /** |
| 137 | 94 | * Add an item (row) to the table |
| @@ -138,23 +95,19 @@ | ||
| 138 | 95 | * |
| 139 | 96 | * @param array $item A row's worth of data. |
| 140 | 97 | */ |
| 141 | 98 | public function add_item( $item ) { |
| 142 | - | |
| 143 | - array_push( $this->data, $item ); | |
| 144 | - | |
| 99 | + array_push( $this->_data, $item ); | |
| 145 | 100 | } |
| 146 | 101 | |
| 147 | 102 | /** |
| 148 | 103 | * Add a bulk action to the table |
| 149 | 104 | * |
| 150 | - * @param string $key Machine-readable action name. | |
| 151 | - * @param string $name Title shown to the user. | |
| 105 | + * @param string $key Machine-readable action name | |
| 106 | + * @param string $name Title shown to the user | |
| 152 | 107 | */ |
| 153 | 108 | public function add_bulk_action( $key, $name ) { |
| 154 | - | |
| 155 | - $this->bulk_actions[ $key ] = $name; | |
| 156 | - | |
| 109 | + $this->_bulk_actions[ $key ] = $name; | |
| 157 | 110 | } |
| 158 | 111 | |
| 159 | 112 | /** |
| 160 | 113 | * Prepares the items (rows) to be rendered |
| @@ -159,68 +112,59 @@ | ||
| 159 | 112 | /** |
| 160 | 113 | * Prepares the items (rows) to be rendered |
| 161 | 114 | */ |
| 162 | 115 | public function prepare_items() { |
| 163 | - | |
| 164 | - $total_items = count( $this->data ); | |
| 116 | + $total_items = count( $this->_data ); | |
| 165 | 117 | $per_page = 25; |
| 166 | 118 | |
| 167 | - $columns = $this->columns; | |
| 119 | + $columns = $this->_columns; | |
| 168 | 120 | $hidden = array(); |
| 169 | - $sortable = $this->sortable_columns; | |
| 121 | + $sortable = $this->_sortable_columns; | |
| 170 | 122 | |
| 171 | 123 | $this->_column_headers = array( $columns, $hidden, $sortable ); |
| 172 | 124 | |
| 173 | 125 | $current_page = $this->get_pagenum(); |
| 174 | 126 | |
| 175 | - $sorted_data = $this->reorder( $this->data ); | |
| 127 | + $sorted_data = $this->reorder( $this->_data ); | |
| 176 | 128 | |
| 177 | - $data = array_slice( $sorted_data, ( ( $current_page - 1 ) * $per_page ), $per_page ); | |
| 129 | + $data = array_slice( $sorted_data, ( ( $current_page - 1 ) * $per_page ),$per_page ); | |
| 178 | 130 | |
| 179 | 131 | $this->items = $data; |
| 180 | 132 | |
| 181 | - $this->set_pagination_args( | |
| 182 | - array( | |
| 183 | - 'total_items' => $total_items, | |
| 184 | - 'per_page' => $per_page, | |
| 185 | - 'total_pages' => (int) ceil( $total_items / $per_page ), | |
| 186 | - ) | |
| 187 | - ); | |
| 188 | - | |
| 133 | + $this->set_pagination_args( array( | |
| 134 | + 'total_items' => $total_items, | |
| 135 | + 'per_page' => $per_page, | |
| 136 | + 'total_pages' => ceil( $total_items / $per_page ), | |
| 137 | + )); | |
| 189 | 138 | } |
| 190 | 139 | |
| 191 | 140 | /** |
| 192 | 141 | * Reorder the data according to the sort parameters |
| 193 | 142 | * |
| 194 | - * @param array $data Row data, unsorted. | |
| 195 | 143 | * @return array Row data, sorted |
| 196 | 144 | */ |
| 197 | 145 | public function reorder( $data ) { |
| 146 | + function usort_reorder( $a, $b ) { | |
| 198 | 147 | |
| 199 | - usort( | |
| 200 | - $data, | |
| 201 | - function ( $a, $b ) { | |
| 148 | + if ( empty( $_REQUEST['orderby'] ) ) { // WPCS: CSRF ok. | |
| 149 | + $orderby = 'title'; | |
| 150 | + } else { | |
| 151 | + $orderby = sanitize_text_field( wp_unslash( $_REQUEST['orderby'] ) ); // WPCS: CSRF ok. | |
| 152 | + } | |
| 202 | 153 | |
| 203 | - if ( empty( $_REQUEST['orderby'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 204 | - $orderby = 'title'; | |
| 205 | - } else { | |
| 206 | - $orderby = sanitize_sql_orderby( wp_unslash( $_REQUEST['orderby'] ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 207 | - } | |
| 208 | - | |
| 209 | - if ( empty( $_REQUEST['order'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 210 | - $order = 'asc'; | |
| 211 | - } else { | |
| 212 | - $order = sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 213 | - } | |
| 214 | - $result = strcmp( $a[ $orderby ], $b[ $orderby ] ); // Determine sort order. | |
| 215 | - return ( 'asc' === $order ) ? $result : -$result; // Send final sort direction to usort. | |
| 216 | - | |
| 154 | + if ( empty( $_REQUEST['order'] ) ) { // WPCS: CSRF ok. | |
| 155 | + $order = 'asc'; | |
| 156 | + } else { | |
| 157 | + $order = sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ); // WPCS: CSRF ok. | |
| 217 | 158 | } |
| 218 | - ); | |
| 159 | + $result = strcmp( $a[ $orderby ], $b[ $orderby ] ); //Determine sort order. | |
| 160 | + return ( 'asc' === $order ) ? $result : -$result; //Send final sort direction to usort. | |
| 161 | + } | |
| 162 | + usort( $data, 'usort_reorder' ); | |
| 219 | 163 | |
| 220 | 164 | return $data; |
| 165 | + } | |
| 221 | 166 | |
| 222 | - } | |
| 223 | 167 | |
| 224 | 168 | /** |
| 225 | 169 | * Display the table without the nonce at the top. |
| 226 | 170 | * |
| @@ -227,9 +171,8 @@ | ||
| 227 | 171 | * @since 3.1.0 |
| 228 | 172 | * @access public |
| 229 | 173 | */ |
| 230 | 174 | public function display_no_nonce() { |
| 231 | - | |
| 232 | 175 | $singular = $this->_args['singular']; |
| 233 | 176 | |
| 234 | 177 | $this->display_tablenav( 'bottom' ); |
| 235 | 178 | |
| @@ -234,9 +177,9 @@ | ||
| 234 | 177 | $this->display_tablenav( 'bottom' ); |
| 235 | 178 | |
| 236 | 179 | $this->screen->render_screen_reader_content( 'heading_list' ); |
| 237 | 180 | ?> |
| 238 | - <table class="wp-list-table <?php echo esc_attr( implode( ' ', $this->get_table_classes() ) ); ?>"> | |
| 181 | + <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>"> | |
| 239 | 182 | <thead> |
| 240 | 183 | <tr> |
| 241 | 184 | <?php $this->print_column_headers(); ?> |
| 242 | 185 | </tr> |
| @@ -241,15 +184,12 @@ | ||
| 241 | 184 | <?php $this->print_column_headers(); ?> |
| 242 | 185 | </tr> |
| 243 | 186 | </thead> |
| 244 | 187 | |
| 245 | - <tbody id="the-list" | |
| 246 | - <?php | |
| 188 | + <tbody id="the-list"<?php | |
| 247 | 189 | if ( $singular ) { |
| 248 | - echo " data-wp-lists='list:" . esc_attr( $singular ) . "'"; | |
| 249 | - } | |
| 250 | - ?> | |
| 251 | - > | |
| 190 | + echo " data-wp-lists='list:$singular'"; | |
| 191 | + } ?>> | |
| 252 | 192 | <?php $this->display_rows_or_placeholder(); ?> |
| 253 | 193 | </tbody> |
| 254 | 194 | |
| 255 | 195 | <tfoot> |
| @@ -260,7 +200,6 @@ | ||
| 260 | 200 | |
| 261 | 201 | </table> |
| 262 | 202 | <?php |
| 263 | 203 | $this->display_tablenav( 'bottom' ); |
| 264 | - | |
| 265 | 204 | } |
| 266 | 205 | } |