PluginProbe
Float menu – awesome floating side menu / 3.5.2
Float menu – awesome floating side menu v3.5.2
7.2.5 trunk 2.1 2.2 3.0.1 3.1 3.2.2 3.3.1 3.5 3.5.1 3.5.2 3.5.3. 3.5.4 4.0 4.1 4.1.1 4.2 4.3 4.3.1 4.3.2 5.0 5.0.1 5.0.2 5.0.3 5.1 All 57 releases
float-menu / admin / partials / class-list-table.php

class-list-table.php in Float menu – awesome floating side menu 3.5.2, at admin/partials/class-list-table.php

339 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if ( ! defined( 'ABSPATH' ) ) {
2 exit;
3 }
4
5 // WP_List_Table is not loaded automatically so we need to load it in our application
6 if ( ! class_exists( 'WP_List_Table' ) ) {
7 require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
8 }
9
10 /**
11 * Create a new table class that will extend the WP_List_Table
12 *
13 */
14 class Wow_List_Table extends WP_List_Table {
15
16 /**
17 * Number of items per page
18 *
19 * @var int
20 * @since 1.5
21 */
22 public $per_page = 30;
23
24 private $data;
25 private $plugin;
26
27 /**
28 * Wow_List_Table constructor.
29 *
30 * @param string $data - Name of the datatable
31 * @param array $plugin - Information about the plugin
32 */
33 public function __construct( $data, $plugin ) {
34 $this->data = $data;
35 $this->plugin = $plugin;
36
37 // Set parent defaults
38 parent::__construct( array(
39 'ajax' => false,
40 ) );
41 $this->process_bulk_action();
42 }
43
44 /**
45 * Process the bulk actions
46 *
47 * @access public
48 * @return void
49 * @since 1.4
50 */
51 public function process_bulk_action() {
52 $ids = isset( $_POST['ID'] ) ? absint($_POST['ID']) : false;
53 $action = $this->current_action();
54 if ( ! is_array( $ids ) ) {
55 $ids = array( $ids );
56 }
57 if ( empty( $action ) ) {
58 return;
59 }
60 global $wpdb;
61 $table = $this->data;
62 foreach ( $ids as $id ) {
63 if ( 'delete-items' === $this->current_action() ) {
64 $wpdb->delete( $table, array( 'id' => $id ) );
65 }
66 }
67
68 }
69
70 public function search_box( $text, $input_id ) {
71 $input_id = $input_id . '-search-input';
72 if ( ! empty( $_REQUEST['orderby'] ) ) {
73 echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
74 }
75 if ( ! empty( $_REQUEST['order'] ) ) {
76 echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
77 }
78 ?>
79 <p class="search-box">
80 <label class="screen-reader-text" for="<?php echo esc_attr($input_id) ?>"><?php echo esc_html($text); ?>:</label>
81 <input type="search" id="<?php echo esc_attr($input_id) ?>" name="s" value="<?php _admin_search_query(); ?>"/>
82 <?php submit_button( $text, 'button', false, false, array( 'ID' => 'search-submit' ) ); ?>
83 </p>
84 <?php
85 }
86
87 /**
88 * Define what data to show on each column of the table
89 *
90 * @param array $item Data
91 * @param String $column_name - Current column name
92 *
93 * @return Mixed
94 */
95 public function column_default( $item, $column_name ) {
96 $value = $item[ $column_name ];
97
98 return $value;
99 }
100
101 public function column_title( $item ) {
102 $slug = $this->plugin['slug'];
103 $text = $this->plugin['text'];
104 $title = ! empty( $item['title'] ) ? $item['title'] : '<em>Untitle</em>';
105 $edit_url = admin_url( '/admin.php?page=' . $slug . '&tab=add-new&act=update&id='
106 . urlencode( $item['ID'] ) );
107 $duplicate_url = admin_url( '/admin.php?page=' . $slug . '&tab=add-new&act=duplicate&id='
108 . urlencode( $item['ID'] ) );
109 $delete_url = admin_url( '/admin.php?page=' . $slug . '&info=delete&did=' . urlencode( $item['ID'] ) );
110 $actions = array(
111 'edit' => '<a href="' . esc_url($edit_url) . '">' . esc_attr__( 'Edit', $text ) . '</a>',
112 'duplicate' => '<a href="' . esc_url($duplicate_url) . '" style="color:green;">' . esc_attr__( 'Duplicate', $text )
113 . '</a>',
114 'delete' => '<a href="' . esc_url($delete_url) . '" style="color:red;">' . esc_attr__( 'Delete', $text ) . '</a>',
115 );
116
117 return '<a href="' . esc_url( $edit_url ) . '">' . ($title) . '</a>' . $this->row_actions( $actions );
118 }
119
120 /**
121 * Prepare the items for the table to process
122 *
123 * @return Void
124 */
125 public function prepare_items() {
126 $columns = $this->get_columns();
127 $hidden = $this->get_hidden_columns();
128 $sortable = $this->get_sortable_columns();
129 $data = $this->table_data();
130 $perPage = 30;
131 $currentPage = $this->get_pagenum();
132 if ( $data ) {
133 usort( $data, array( &$this, 'sort_data' ) );
134 $data = array_slice( $data, ( ( $currentPage - 1 ) * $perPage ), $perPage );
135 }
136 $totalItems = $this->list_count();
137 $this->_column_headers = array( $columns, $hidden, $sortable );
138 $this->items = $data;
139 $this->set_pagination_args( array(
140 'total_items' => $totalItems,
141 'per_page' => $perPage,
142 'total_pages' => ceil( $totalItems / $perPage ),
143 ) );
144 }
145
146 /**
147 * Override the parent columns method. Defines the columns to use in your listing table
148 *
149 * @return Array
150 */
151 public function get_columns() {
152 $columns = array(
153 'cb' => '<input type="checkbox" />',
154 'title' => 'Title',
155 'code' => 'Shortcode',
156 'edit' => 'Edit',
157 'delete' => 'Delete',
158 'duplicate' => 'Duplicate',
159 );
160
161 return $columns;
162
163 }
164
165 /**
166 * Define which columns are hidden
167 *
168 * @return Array
169 */
170 public function get_hidden_columns() {
171 return array();
172 }
173
174 /**
175 * Define the sortable columns
176 *
177 * @return array
178 */
179 public function get_sortable_columns() {
180 return array(
181 'ID' => array( 'ID', false ),
182 );
183 }
184
185 /**
186 * Get the table data
187 *
188 * @return Array
189 */
190 private function table_data() {
191 global $wpdb;
192 $data = array();
193 $paged = $this->get_paged();
194 $offset = $this->per_page * ( $paged - 1 );
195 $search = $this->get_search();
196
197 $table = $this->data;
198
199
200 if ( ! $search || empty( $search ) ) {
201 $result = $wpdb->get_results( "SELECT * FROM " . $table . " order by id desc" );
202 } elseif ( is_numeric( $search ) ) {
203 $result = $wpdb->get_results( "SELECT * FROM " . $table . " WHERE id=" . $search );
204 } else {
205 $result = $wpdb->get_results( "SELECT * FROM " . $table . " WHERE title='" . $search
206 . "' order by id desc" );
207 }
208
209 $slug = $this->plugin['slug'];
210 $text = $this->plugin['text'];
211 $shortcode = $this->plugin['shortcode'];
212 $prefix = $this->plugin['prefix'];
213
214
215 if ( $result ) {
216 foreach ( $result as $key => $value ) {
217 $option_name_view = '_' . $prefix . '_view_counter_' . $value->id;
218 $option_name_action = '_' . $prefix . '_action_counter_' . $value->id;
219 $tool_view = get_option( $option_name_view, '0' );
220 $tool_action = get_option( $option_name_action, '0' );
221 if ( ! empty( $tool_view ) ) {
222 $conversion = round( $tool_action / $tool_view * 100, 2 ) . '%';
223 } else {
224 $conversion = '0%';
225 }
226 $title = ! empty( $value->title ) ? $value->title : '<em>' . esc_attr__( 'Untitle', $text ) . '</em>';
227 $data[] = array(
228 'ID' => $value->id,
229 'title' => '<a href="admin.php?page=' . $slug . '&tab=add-new&act=update&id=' . $value->id
230 . '">' . $title . '</a>',
231 'code' => '[' . $shortcode . ' id="' . $value->id . '"]',
232 'view' => $tool_view,
233 'action' => $tool_action,
234 'conversion' => $conversion,
235 'edit' => '<a href="admin.php?page=' . $slug . '&tab=add-new&act=update&id='
236 . $value->id . '">edit</a>',
237 'delete' => '<a href="admin.php?page=' . $slug . '&info=delete&did=' . $value->id
238 . '" style="color:red;">delete</a>',
239 'duplicate' => '<a href="admin.php?page=' . $slug . '&tab=add-new&act=duplicate&id='
240 . $value->id . '" style="color:green;">duplicate</a>',
241 );
242 }
243 }
244
245 return $data;
246 }
247
248 /**
249 * Retrieve the current page number
250 *
251 * @access public
252 * @return int Current page number
253 * @since 1.5
254 */
255 public function get_paged() {
256 return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
257 }
258
259 /**
260 * Retrieves the search query string
261 *
262 * @access public
263 * @return mixed string If search is present, false otherwise
264 * @since 1.0
265 */
266 public function get_search() {
267 return ! empty( $_POST['s'] ) ? urldecode( trim( $_POST['s'] ) ) : false;
268 }
269
270 public function list_count() {
271 global $wpdb;
272 $data = $this->data;
273 $result = $wpdb->get_results( "SELECT * FROM " . $data . " order by id asc" );
274 $count = count( $result );
275
276 return $count;
277 }
278
279 /**
280 * Render the checkbox column
281 *
282 * @access public
283 *
284 * @param array $item Contains all the data for the checkbox column
285 *
286 * @return string Displays a checkbox
287 * @since 1.0
288 */
289 public function column_cb( $item ) {
290 return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', 'ID', $item['ID'] );
291 }
292
293 /**
294 * Retrieve the bulk actions
295 *
296 * @access public
297 * @return array $actions Array of the bulk actions
298 * @since 1.4
299 */
300 public function get_bulk_actions() {
301 $actions = array(
302 'delete-items' => 'Delate',
303 );
304
305 return $actions;
306 }
307
308 /**
309 * Gets the name of the primary column.
310 *
311 * @return string Name of the primary column.
312 * @since 1.0
313 * @access protected
314 *
315 */
316 protected function get_primary_column_name() {
317 return 'ID';
318 }
319
320 /**
321 * Allows you to sort the data by the variables set in the $_GET
322 *
323 * @return Mixed
324 */
325 private function sort_data( $a, $b ) {
326 // If no sort, default to title
327 $orderby = ( ! empty( $_GET['orderby'] ) ) ? sanitize_text_field( $_GET['orderby'] ) : 'ID';
328 // If no order, default to asc
329 $order = ( ! empty( $_GET['order'] ) ) ? sanitize_text_field( $_GET['order'] ) : 'desc';
330 // Determine sort order
331 $result = strnatcmp( $a[ $orderby ], $b[ $orderby ] );
332
333 // Send final sort direction to usort
334 return ( $order === 'asc' ) ? $result : - $result;
335
336 }
337
338 }
339