module-option-table.php
303 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('WP_List_Table')){ |
| 8 | |
| 9 | require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php'); |
| 10 | |
| 11 | } |
| 12 | |
| 13 | class ACFE_Admin_Options_List extends WP_List_Table{ |
| 14 | |
| 15 | /** |
| 16 | * Constructor |
| 17 | */ |
| 18 | public function __construct(){ |
| 19 | |
| 20 | parent::__construct(array( |
| 21 | 'singular' => __('Option', 'acfe'), |
| 22 | 'plural' => __('Options', 'acfe'), |
| 23 | 'ajax' => false |
| 24 | )); |
| 25 | |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Retrieve data from the database |
| 30 | * |
| 31 | * @param int $per_page |
| 32 | * @param int $page_number |
| 33 | * |
| 34 | * @return mixed |
| 35 | */ |
| 36 | public static function get_options($per_page = 100, $page_number = 1, $search = ''){ |
| 37 | |
| 38 | global $wpdb; |
| 39 | |
| 40 | $sql = "SELECT * FROM {$wpdb->options}"; |
| 41 | |
| 42 | if(!empty($search)){ |
| 43 | |
| 44 | $search = '%' . $wpdb->esc_like($search) . '%'; |
| 45 | $sql .= $wpdb->prepare(" WHERE option_name LIKE %s", $search); |
| 46 | |
| 47 | } |
| 48 | |
| 49 | if(empty($_REQUEST['orderby'])){ |
| 50 | |
| 51 | $sql .= ' ORDER BY option_id ASC'; |
| 52 | |
| 53 | }else{ |
| 54 | |
| 55 | $orderby = esc_sql($_REQUEST['orderby']); |
| 56 | $order = !empty($_REQUEST['order']) ? esc_sql($_REQUEST['order']) : 'ASC'; |
| 57 | |
| 58 | $orderby_order = sanitize_sql_orderby($orderby . ' ' . $order); |
| 59 | |
| 60 | if($orderby_order){ |
| 61 | $sql .= ' ORDER BY ' . $orderby_order; |
| 62 | } |
| 63 | |
| 64 | } |
| 65 | |
| 66 | if(empty($search)){ |
| 67 | |
| 68 | $sql .= " LIMIT $per_page"; |
| 69 | $sql .= ' OFFSET ' . ($page_number - 1) * $per_page; |
| 70 | |
| 71 | } |
| 72 | |
| 73 | |
| 74 | $result = $wpdb->get_results($sql, 'ARRAY_A'); |
| 75 | |
| 76 | return $result; |
| 77 | |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns the count of records in the database. |
| 82 | * |
| 83 | * @return null|string |
| 84 | */ |
| 85 | public static function record_count($search = ''){ |
| 86 | |
| 87 | global $wpdb; |
| 88 | |
| 89 | $sql = "SELECT COUNT(*) FROM {$wpdb->options}"; |
| 90 | |
| 91 | if(!empty($search)){ |
| 92 | |
| 93 | $search = '%' . $wpdb->esc_like($search) . '%'; |
| 94 | $sql .= $wpdb->prepare(" WHERE option_name LIKE %s", $search); |
| 95 | |
| 96 | } |
| 97 | |
| 98 | return $wpdb->get_var($sql); |
| 99 | |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /** Text displayed when no data is available */ |
| 104 | public function no_items(){ |
| 105 | |
| 106 | _e('No options avaliable.', 'acfe'); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /** |
| 112 | * Render a column when no column specific method exist. |
| 113 | * |
| 114 | * @param array $item |
| 115 | * @param string $column_name |
| 116 | * |
| 117 | * @return mixed |
| 118 | */ |
| 119 | public function column_default($item, $column_name){ |
| 120 | |
| 121 | if($column_name === 'option_id'){ |
| 122 | |
| 123 | return $item['option_id']; |
| 124 | |
| 125 | } |
| 126 | |
| 127 | elseif($column_name === 'option_value'){ |
| 128 | |
| 129 | // raw |
| 130 | $raw = map_deep($item['option_value'], '_wp_specialchars'); |
| 131 | |
| 132 | // serialized |
| 133 | if(is_serialized($item['option_value'])){ |
| 134 | |
| 135 | $value = maybe_unserialize($item['option_value']); |
| 136 | |
| 137 | if(is_object($value) && is_a($value, '__PHP_Incomplete_Class')){ |
| 138 | // do nothing |
| 139 | }else{ |
| 140 | $value = @map_deep($value, '_wp_specialchars'); |
| 141 | } |
| 142 | |
| 143 | return '<pre style="max-height:200px; overflow:auto; white-space: pre;">' . print_r($value, true) . '</pre><pre style="max-height:200px; overflow:auto; white-space: pre; margin-top:10px;">' . print_r($raw, true) . '</pre>'; |
| 144 | |
| 145 | // html |
| 146 | }elseif($item['option_value'] != strip_tags($item['option_value'])){ |
| 147 | |
| 148 | return '<pre style="max-height:200px; overflow:auto; white-space: pre;">' . print_r($raw, true) . '</pre>'; |
| 149 | |
| 150 | // json |
| 151 | }elseif(acfe_is_json($item['option_value'])){ |
| 152 | |
| 153 | $value = json_decode($item['option_value']); |
| 154 | $value = @map_deep($value, '_wp_specialchars'); |
| 155 | |
| 156 | return '<pre style="max-height:200px; overflow:auto; white-space: pre;">' . print_r($value, true) . '</pre><pre style="max-height:200px; overflow:auto; white-space: pre; margin-top:10px;">' . print_r($raw, true) . '</pre>'; |
| 157 | |
| 158 | // default |
| 159 | }else{ |
| 160 | |
| 161 | return $raw; |
| 162 | |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | |
| 167 | elseif($column_name === 'autoload'){ |
| 168 | |
| 169 | return $item['autoload']; |
| 170 | |
| 171 | }else{ |
| 172 | |
| 173 | return print_r($item, true); |
| 174 | |
| 175 | } |
| 176 | |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Render the bulk edit checkbox |
| 181 | * |
| 182 | * @param array $item |
| 183 | * |
| 184 | * @return string |
| 185 | */ |
| 186 | public function column_cb($item){ |
| 187 | |
| 188 | return sprintf( |
| 189 | '<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['option_id'] |
| 190 | ); |
| 191 | |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Method for name column |
| 196 | * |
| 197 | * @param array $item an array of DB data |
| 198 | * |
| 199 | * @return string |
| 200 | */ |
| 201 | public function column_option_name($item){ |
| 202 | |
| 203 | $delete_nonce = wp_create_nonce('acfe_options_delete_option'); |
| 204 | |
| 205 | $title = '<strong>' . $item['option_name'] . '</strong>'; |
| 206 | |
| 207 | $actions = array( |
| 208 | 'edit' => sprintf('<a href="?page=%s&action=edit&option=%s">' . __('Edit') . '</a>', esc_attr($_REQUEST['page']), absint($item['option_id'])), |
| 209 | 'delete' => sprintf('<a href="?page=%s&action=delete&option=%s&_wpnonce=%s">' . __('Delete') . '</a>', esc_attr($_REQUEST['page']), absint($item['option_id']), $delete_nonce), |
| 210 | ); |
| 211 | |
| 212 | return $title . $this->row_actions($actions); |
| 213 | |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Associative array of columns |
| 218 | * |
| 219 | * @return array |
| 220 | */ |
| 221 | public function get_columns(){ |
| 222 | |
| 223 | $columns = array( |
| 224 | 'cb' => '<input type="checkbox" />', |
| 225 | 'option_id' => __('ID', 'acfe'), |
| 226 | 'option_name' => __('Name', 'acfe'), |
| 227 | 'option_value' => __('Value', 'acfe'), |
| 228 | 'autoload' => __('Autoload', 'acfe'), |
| 229 | ); |
| 230 | |
| 231 | return $columns; |
| 232 | |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Columns to make sortable. |
| 237 | * |
| 238 | * @return array |
| 239 | */ |
| 240 | public function get_sortable_columns(){ |
| 241 | |
| 242 | $sortable_columns = array( |
| 243 | 'option_id' => array('option_id', true), |
| 244 | 'option_name' => array('option_name', true), |
| 245 | 'option_value' => array('option_value', true), |
| 246 | 'autoload' => array('autoload', true), |
| 247 | ); |
| 248 | |
| 249 | return $sortable_columns; |
| 250 | |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Returns an associative array containing the bulk action |
| 255 | * |
| 256 | * @return array |
| 257 | */ |
| 258 | public function get_bulk_actions(){ |
| 259 | |
| 260 | $actions = array( |
| 261 | 'bulk-delete' => __('Delete') |
| 262 | ); |
| 263 | |
| 264 | return $actions; |
| 265 | |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Handles data query and filter, sorting, and pagination. |
| 270 | */ |
| 271 | public function prepare_items(){ |
| 272 | |
| 273 | // Get columns |
| 274 | $this->_column_headers = array($this->get_columns(), array(), $this->get_sortable_columns()); |
| 275 | |
| 276 | // Vars |
| 277 | $per_page = $this->get_items_per_page('options_per_page', 100); |
| 278 | $current_page = $this->get_pagenum(); |
| 279 | |
| 280 | // Search |
| 281 | $search = (isset( $_REQUEST['s'])) ? $_REQUEST['s'] : false; |
| 282 | |
| 283 | // Get items |
| 284 | $this->items = self::get_options($per_page, $current_page, $search); |
| 285 | /* |
| 286 | foreach($this->items as &$item){ |
| 287 | $item = json_encode($item); |
| 288 | }*/ |
| 289 | |
| 290 | // Get total |
| 291 | $total_items = self::record_count($search); |
| 292 | |
| 293 | if(!empty($search)) |
| 294 | $per_page = $total_items; |
| 295 | |
| 296 | $this->set_pagination_args(array( |
| 297 | 'total_items' => $total_items, |
| 298 | 'per_page' => $per_page |
| 299 | )); |
| 300 | |
| 301 | } |
| 302 | |
| 303 | } |