PluginProbe
Button Generator – Easily Create Custom Buttons with Icons and Analytics / 2.3.9
Button Generator – Easily Create Custom Buttons with Icons and Analytics v2.3.9
trunk 1.1.1 2.0 2.1 2.2 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 3.0 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.2 3.1.3 3.2 3.2.1 3.2.2 3.2.3 All 28 releases
button-generation / admin / partials / class-list-table.php

class-list-table.php in Button Generator – Easily Create Custom Buttons with Icons and Analytics 2.3.9, at admin/partials/class-list-table.php

330 lines 8.8 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'] ) ? $_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 $input_id ?>"><?php echo $text; ?>:</label>
81 <input type="search" id="<?php echo $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=' . esc_attr( $slug ) . '&tab=add-new&act=update&id=' . urlencode( $item['ID'] ) );
106 $duplicate_url =
107 admin_url( '/admin.php?page=' . esc_attr( $slug ) . '&tab=add-new&act=duplicate&id=' . urlencode( $item['ID'] ) );
108 $delete_url = admin_url( '/admin.php?page=' . esc_attr( $slug ) . '&info=delete&did=' . urlencode( $item['ID'] ) );
109 $actions = array(
110 'edit' => '<a href="' . esc_url( $edit_url ) . '">' . esc_attr__( 'Edit', $text ) . '</a>',
111 'duplicate' => '<a href="' . esc_url( $duplicate_url ) . '" style="color:green;">' . esc_attr__( 'Duplicate', $text ) . '</a>',
112 'delete' => '<a href="' . esc_url( wp_nonce_url($delete_url, $slug . '_nonce' ) ) . '" style="color:red;">' . esc_attr__( 'Delete', $text ) . '</a>',
113 );
114
115 return '<a href="' . esc_url( $edit_url ) . '">' . $title . '</a>' . $this->row_actions( $actions );
116 }
117
118 /**
119 * Prepare the items for the table to process
120 *
121 * @return Void
122 */
123 public function prepare_items() {
124 $columns = $this->get_columns();
125 $hidden = $this->get_hidden_columns();
126 $sortable = $this->get_sortable_columns();
127 $data = $this->table_data();
128 $perPage = 30;
129 $currentPage = $this->get_pagenum();
130 if ( $data ) {
131 usort( $data, array( &$this, 'sort_data' ) );
132 $data = array_slice( $data, ( ( $currentPage - 1 ) * $perPage ), $perPage );
133 }
134 $totalItems = $this->list_count();
135 $this->_column_headers = array( $columns, $hidden, $sortable );
136 $this->items = $data;
137 $this->set_pagination_args( array(
138 'total_items' => $totalItems,
139 'per_page' => $perPage,
140 'total_pages' => ceil( $totalItems / $perPage ),
141 ) );
142 }
143
144 /**
145 * Override the parent columns method. Defines the columns to use in your listing table
146 *
147 * @return Array
148 */
149 public function get_columns() {
150 $columns = array(
151 'cb' => '<input type="checkbox" />',
152 'title' => 'Title',
153 'code' => 'Shortcode',
154 'view' => 'Views',
155 'action' => 'Actions',
156 'conversion' => 'Conversion',
157 );
158
159 return $columns;
160
161 }
162
163 /**
164 * Define which columns are hidden
165 *
166 * @return Array
167 */
168 public function get_hidden_columns() {
169 return array();
170 }
171
172 /**
173 * Define the sortable columns
174 *
175 * @return array
176 */
177 public function get_sortable_columns() {
178 return array(
179 'ID' => array( 'ID', false ),
180 );
181 }
182
183 /**
184 * Get the table data
185 *
186 * @return Array
187 */
188 private function table_data() {
189 global $wpdb;
190 $data = array();
191 $paged = $this->get_paged();
192 $offset = $this->per_page * ( $paged - 1 );
193 $search = $this->get_search();
194
195 $table = $this->data;
196
197
198 if ( ! $search || empty( $search ) ) {
199 $result = $wpdb->get_results( "SELECT * FROM " . $table . " order by id desc" );
200 } elseif ( is_numeric( $search ) ) {
201 $result = $wpdb->get_results( "SELECT * FROM " . $table . " WHERE id=" . $search );
202 } else {
203 $result = $wpdb->get_results( "SELECT * FROM " . $table . " WHERE title='" . $search . "' order by id desc" );
204 }
205
206 $slug = $this->plugin['slug'];
207 $text = $this->plugin['text'];
208 $shortcode = $this->plugin['shortcode'];
209 $prefix = $this->plugin['prefix'];
210
211
212 if ( $result ) {
213 foreach ( $result as $key => $value ) {
214 $option_name_view = '_' . $prefix . '_view_counter_' . $value->id;
215 $option_name_action = '_' . $prefix . '_action_counter_' . $value->id;
216 $tool_view = get_option( $option_name_view, '0' );
217 $tool_action = get_option( $option_name_action, '0' );
218 if ( ! empty( $tool_view ) ) {
219 $conversion = round( $tool_action / $tool_view * 100, 2 ) . '%';
220 } else {
221 $conversion = '0%';
222 }
223 $title = ! empty( $value->title ) ? $value->title : '<em>' . esc_attr__( 'Untitle', $text ) . '</em>';
224 $data[] = array(
225 'ID' => $value->id,
226 'title' => '<a href="admin.php?page=' . $slug . '&tab=add-new&act=update&id=' . $value->id . '">' .
227 $title . '</a>',
228 'code' => '[' . $shortcode . ' id="' . $value->id . '"]',
229 'view' => $tool_view,
230 'action' => $tool_action,
231 'conversion' => $conversion,
232 );
233 }
234 }
235
236 return $data;
237 }
238
239 /**
240 * Retrieve the current page number
241 *
242 * @access public
243 * @return int Current page number
244 * @since 1.5
245 */
246 public function get_paged() {
247 return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
248 }
249
250 /**
251 * Retrieves the search query string
252 *
253 * @access public
254 * @return mixed string If search is present, false otherwise
255 * @since 1.0
256 */
257 public function get_search() {
258 return ! empty( $_POST['s'] ) ? urldecode( trim( $_POST['s'] ) ) : false;
259 }
260
261 public function list_count() {
262 global $wpdb;
263 $data = $this->data;
264 $result = $wpdb->get_results( "SELECT * FROM " . $data . " order by id asc" );
265 $count = count( $result );
266
267 return $count;
268 }
269
270 /**
271 * Render the checkbox column
272 *
273 * @access public
274 *
275 * @param array $item Contains all the data for the checkbox column
276 *
277 * @return string Displays a checkbox
278 * @since 1.0
279 */
280 public function column_cb( $item ) {
281 return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', 'ID', $item['ID'] );
282 }
283
284 /**
285 * Retrieve the bulk actions
286 *
287 * @access public
288 * @return array $actions Array of the bulk actions
289 * @since 1.4
290 */
291 public function get_bulk_actions() {
292 $actions = array(
293 'delete-items' => 'Delate',
294 );
295
296 return $actions;
297 }
298
299 /**
300 * Gets the name of the primary column.
301 *
302 * @return string Name of the primary column.
303 * @since 1.0
304 * @access protected
305 *
306 */
307 protected function get_primary_column_name() {
308 return 'ID';
309 }
310
311 /**
312 * Allows you to sort the data by the variables set in the $_GET
313 *
314 * @return Mixed
315 */
316 private function sort_data( $a, $b ) {
317 // If no sort, default to title
318 $orderby = ( ! empty( $_GET['orderby'] ) ) ? sanitize_text_field( $_GET['orderby'] ) : 'ID';
319 // If no order, default to asc
320 $order = ( ! empty( $_GET['order'] ) ) ? sanitize_text_field( $_GET['order'] ) : 'desc';
321 // Determine sort order
322 $result = strnatcmp( $a[ $orderby ], $b[ $orderby ] );
323
324 // Send final sort direction to usort
325 return ( $order === 'asc' ) ? $result : - $result;
326
327 }
328
329 }
330