PluginProbe
Counter Box – Add Countdowns, Timers & Dynamic Counters to WordPress / 2.0
Counter Box – Add Countdowns, Timers & Dynamic Counters to WordPress v2.0
2.0.14 trunk 1.0 1.2 1.2.1 1.2.2 1.2.3 1.2.4 2.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
counter-box / classes / Admin / ListTable.php

ListTable.php in Counter Box – Add Countdowns, Timers & Dynamic Counters to WordPress 2.0, at classes/Admin/ListTable.php

349 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace CounterBox\Admin;
4
5 defined( 'ABSPATH' ) || exit;
6
7 use WP_List_Table;
8 use CounterBox\WOWP_Plugin;
9
10 class ListTable extends WP_List_Table {
11
12 public int $per_page = 30;
13
14 public function __construct() {
15 // Set parent defaults
16 parent::__construct( array(
17 'ajax' => false,
18 ) );
19 $this->process_bulk_action();
20 }
21
22 public function column_default( $item, $column_name ) {
23 return $item[ $column_name ];
24 }
25
26 public function search_box( $text, $input_id ) {
27 $input_id .= '-search-input';
28 if ( ! empty( $_REQUEST['orderby'] ) ) {
29 echo '<input type="hidden" name="orderby" value="' . esc_attr( sanitize_text_field( $_REQUEST['orderby'] ) ) . '" />';
30 }
31 if ( ! empty( $_REQUEST['order'] ) ) {
32 echo '<input type="hidden" name="order" value="' . esc_attr( sanitize_text_field( $_REQUEST['order'] ) ) . '" />';
33 }
34 ?>
35 <p class="search-box">
36 <label class="screen-reader-text" for="<?php
37 echo esc_attr( $input_id ) ?>"><?php
38 echo esc_html( $text ); ?>
39 :</label>
40 <input type="search" id="<?php
41 echo esc_attr( $input_id ) ?>" name="s" value="<?php
42 _admin_search_query(); ?>"/>
43 <?php
44 submit_button( $text, 'button', false, false, array( 'ID' => 'search-submit' ) ); ?>
45 </p>
46 <?php
47 }
48
49 public function column_title( $item ): string {
50 $title = ! empty( $item['title'] ) ? $item['title'] : __( 'Untitled', 'counter-box' );
51 $param = DBManager::get_param_id( $item['ID'] );
52 $actions = [
53 'id' => '#' . $item['ID'],
54 'edit' => '<a href="' . esc_url( Link::edit( $item['ID'] ) ) . '">' . esc_html__( 'Edit',
55 'counter-box' ) . '</a>',
56 'duplicate' => '<a href="' . esc_url( Link::duplicate( $item['ID'] ) ) . '">' . esc_html__( 'Duplicate',
57 'counter-box' ) . '</a>',
58 'delete' => '<a href="' . esc_url( Link::remove( $item['ID'] ) ) . '" >' . esc_html__( 'Delete',
59 'counter-box' ) . '</a>',
60 'export' => '<a href="' . esc_url( Link::export( $item['ID'] ) ) . '" >' . esc_html__( 'Export',
61 'counter-box' ) . '</a>',
62 ];
63 if ( ! empty( $param['link'] ) ) {
64 $actions['view'] = '<a href="' . esc_url( $param['link'] ) . '" target="_blank">' . esc_html__( 'View',
65 'counter-box' ) . '</a>';
66 }
67
68
69 return $title . $this->row_actions( $actions );
70 }
71
72 public function prepare_items(): void {
73 $columns = $this->get_columns();
74 $hidden = $this->get_hidden_columns();
75 $sortable = $this->get_sortable_columns();
76 $data = $this->table_data();
77 $perPage = 30;
78 $currentPage = $this->get_pagenum();
79 if ( $data ) {
80 usort( $data, [ &$this, 'sort_data' ] );
81 $data = array_slice( $data, ( ( $currentPage - 1 ) * $perPage ), $perPage );
82 }
83 $totalItems = $this->list_count();
84 $this->_column_headers = [ $columns, $hidden, $sortable ];
85 $this->items = $data;
86 $this->set_pagination_args( [
87 'total_items' => $totalItems,
88 'per_page' => $perPage,
89 'total_pages' => ceil( $totalItems / $perPage ),
90 ] );
91 }
92
93 public function get_columns(): array {
94 return [
95 'cb' => '<input type="checkbox" />',
96 'title' => __( 'Title', 'counter-box' ),
97 'code' => __( 'Shortcode', 'counter-box' ),
98 'tag' => __( 'Tag', 'counter-box' ),
99 'mode' => __( 'Test mode',
100 'counter-box' ) . '<sup class="has-tooltip" data-tooltip="' . __( 'The item will only be displayed for administrators.',
101 'counter-box' ) . '">ℹ</sup>',
102 'status' => __( 'Status',
103 'counter-box' ) . '<sup class="has-tooltip" data-tooltip="' . __( 'Display item on the Frontend.',
104 'counter-box' ) . '">ℹ</sup>',
105 ];
106 }
107
108 public function get_hidden_columns(): array {
109 return [];
110 }
111
112 public function get_sortable_columns(): array {
113 return [
114 'ID' => [ 'ID', false ],
115 ];
116 }
117
118 private function table_data(): array {
119 $data = [];
120 $paged = $this->get_paged();
121 $offset = $this->per_page * ( $paged - 1 );
122
123 $result = $this->get_results();
124
125 $slug = WOWP_Plugin::SLUG;
126 $shortcode = WOWP_Plugin::SHORTCODE;
127
128 if ( empty( $result ) ) {
129 return $data;
130 }
131
132 $main_link = add_query_arg( [
133 'page' => $slug,
134 'tab' => 'settings',
135 'action' => 'update'
136 ], admin_url( 'admin.php' ) );
137 foreach ( $result as $key => $value ) {
138 $title = ! empty( $value->title ) ? $value->title : __( 'UnTitle', 'counter-box' );
139 $tooltip_off = esc_attr__( 'Click for Deactivate.', 'counter-box' );
140 $tooltip_on = esc_attr__( 'Click for Activate.', 'counter-box' );
141 $status_off = '<a href="' . esc_url( Link::activate_url( $value->id ) ) . '" class="wpie-toogle is-off" data-tooltip="' . esc_attr( $tooltip_on ) . '"><span>' . esc_attr__( 'OFF',
142 'counter-box' ) . '</span></a>';
143 $status_on = '<a href="' . esc_url( Link::deactivate_url( $value->id ) ) . '" class="wpie-toogle is-on" data-tooltip="' . esc_attr( $tooltip_off ) . '"><span>' . esc_attr__( 'ON',
144 'counter-box' ) . '</span></a>';
145 $status = ! empty( $value->status ) ? $status_off : $status_on;
146
147 $mode_tooltip_off = esc_attr__( 'Click for OFF.', 'counter-box' );
148 $mode_tooltip_on = esc_attr__( 'Click for ON.', 'counter-box' );
149
150 $mode_off = '<a href="' . esc_url( Link::activate_mode( $value->id ) ) . '" class="wpie-toogle is-off" data-tooltip="' . esc_attr( $mode_tooltip_on ) . '"><span>' . esc_attr__( 'OFF',
151 'counter-box' ) . '</span></a>';
152 $mode_on = '<a href="' . esc_url( Link::deactivate_mode( $value->id ) ) . '" class="wpie-toogle is-on" data-tooltip="' . esc_attr( $mode_tooltip_off ) . '"><span>' . esc_attr__( 'ON',
153 'counter-box' ) . '</span></a>';
154
155 $mode = empty( $value->mode ) ? $mode_off : $mode_on;
156
157 $tag = '';
158 if ( isset( $value->tag ) ) {
159 $tag_url = add_query_arg( [
160 'page' => WOWP_Plugin::SLUG,
161 'tag' => $value->tag
162 ], admin_url( 'admin.php' ) );
163 $tag = '<a href="' . esc_url( $tag_url ) . '">' . esc_attr( $value->tag ) . '</a>';
164 }
165
166 $link = add_query_arg( [ 'id' => $value->id ], $main_link );
167 $data[] = array(
168 'ID' => $value->id,
169 'title' => '<a href="' . esc_url( $link ) . '">' . esc_html( $title ) . '</a>',
170 'code' => '<input type="text" value="[' . esc_attr( $shortcode ) . ' id=\'' . absint( $value->id ) . '\']" readonly>',
171 'tag' => $tag,
172 'mode' => $mode,
173 'status' => $status,
174 );
175 }
176
177 return $data;
178 }
179
180 public function column_cb( $item ) {
181 return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', 'ID', $item['ID'] );
182 }
183
184 public function get_paged(): int {
185 return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
186 }
187
188 public function get_search() {
189 $verify = AdminActions::verify(WOWP_Plugin::PREFIX . '_list_action');
190
191 if ( ! $verify ) {
192 return false;
193 }
194
195 return ! empty( $_POST['s'] ) ? urldecode( trim( sanitize_text_field( $_POST['s'] ) ) ) : false;
196 }
197
198 public function list_count(): int {
199 $result = $this->get_results();
200
201 if ( empty( $result ) ) {
202 return 0;
203 }
204 $count = count( $result );
205
206
207 return (int) $count;
208 }
209
210 public function get_results() {
211 global $wpdb;
212
213 $search = $this->get_search();
214
215 $tag_search = ( ! empty( $_REQUEST['tag'] ) ) ? sanitize_text_field( $_REQUEST ['tag'] ) : '';
216 $tag_search = ( $tag_search === 'all' ) ? '' : $tag_search;
217
218
219 $result = '';
220
221 $table = $wpdb->prefix . WOWP_Plugin::PREFIX;
222
223 if ( empty( $search ) ) {
224 $result = $wpdb->get_results( "SELECT * FROM {$table} ORDER BY id DESC" );
225 if ( ! empty( $tag_search ) ) {
226 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} WHERE tag=%s ORDER BY id DESC",
227 $tag_search ) );
228 }
229 } elseif ( trim( $search ) === 'UnTitle' ) {
230 $result = $wpdb->get_results( "SELECT * FROM {$table} WHERE title='' ORDER BY id DESC" );
231 if ( ! empty( $tag_search ) ) {
232 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} WHERE title='' AND tag=%s ORDER BY id DESC",
233 $tag_search ) );
234 }
235 } elseif ( is_numeric( $search ) ) {
236 if ( ! empty( $tag_search ) ) {
237 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} WHERE id=%d AND tag=%s ORDER BY id DESC",
238 absint( $search ), $tag_search ) );
239 } else {
240 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} WHERE id=%d ORDER BY id DESC",
241 absint( $search ) ) );
242 }
243 } else {
244 $wild = '%';
245 $find = sanitize_text_field( $search );
246 $like = $wild . $wpdb->esc_like( $find ) . $wild;
247 if ( ! empty( $tag_search ) ) {
248 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} WHERE title LIKE %s AND tag=%s ORDER BY id DESC",
249 $like, $tag_search ) );
250 } else {
251 $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} WHERE title LIKE %s ORDER BY id DESC",
252 $like ) );
253 }
254 }
255
256 return $result;
257 }
258
259
260 public function get_bulk_actions(): array {
261 $actions = [
262 'delete' => __( 'Delate', 'counter-box' ),
263 'activate' => __( 'Activate', 'counter-box' ),
264 'deactivate' => __( 'Deactivate', 'counter-box' ),
265 'test_on' => __( 'Test mode ON', 'counter-box' ),
266 'test_off' => __( 'Test mode OFF', 'counter-box' ),
267 ];
268
269 return $actions;
270 }
271
272 public function process_bulk_action(): bool {
273
274 $verify = AdminActions::verify(WOWP_Plugin::PREFIX . '_list_action');
275
276 if ( ! $verify ) {
277 return false;
278 }
279
280 $ids = isset( $_POST['ID'] ) ? ( map_deep( $_POST['ID'], 'absint' ) ) : false;
281 $action = $this->current_action();
282 if ( ! is_array( $ids ) ) {
283 $ids = [ $ids ];
284 }
285 if ( empty( $action ) ) {
286 return false;
287 }
288
289 foreach ( $ids as $id ) {
290 if ( 'delete' === $this->current_action() ) {
291 DBManager::delete( $id );
292 }
293 if ( 'activate' === $this->current_action() ) {
294 DBManager::update( [ 'status' => '' ], [ 'ID' => $id ], [ '%d' ] );
295 }
296 if ( 'deactivate' === $this->current_action() ) {
297 DBManager::update( [ 'status' => '1' ], [ 'ID' => $id ], [ '%d' ] );
298 }
299 if ( 'test_on' === $this->current_action() ) {
300 DBManager::update( [ 'mode' => '1' ], [ 'ID' => $id ], [ '%d' ] );
301 }
302 if ( 'test_off' === $this->current_action() ) {
303 DBManager::update( [ 'mode' => '' ], [ 'ID' => $id ], [ '%d' ] );
304 }
305 }
306
307 return true;
308 }
309
310 protected function extra_tablenav( $which ) {
311 if ( 'top' === $which ) {
312 $tags = DBManager::get_tags_from_table();
313
314 $tag_search = ( ! empty( $_REQUEST['tag'] ) ) ? sanitize_text_field( $_REQUEST ['tag'] ) : '';
315 $tag_search = ( $tag_search === 'all' ) ? '' : $tag_search;
316
317 echo '<div class="alignleft actions"><label for="filter-by-tag" class="screen-reader-text">' . esc_html__( 'Filter by tag',
318 'counter-box' ) . '</label>';
319 echo '<select name="tag" id="filter-by-tag">';
320 echo '<option value="all"' . selected( 'all', $tag_search, false ) . '>' . esc_html__( 'All',
321 'counter-box' ) . '</option>';
322
323 if ( ! empty( $tags ) ) {
324 foreach ( $tags as $tag ) {
325 if ( empty( $tag['tag'] ) ) {
326 continue;
327 }
328 echo '<option value="' . esc_attr( trim( $tag['tag'] ) ) . '"' . selected( $tag['tag'], $tag_search,
329 false ) . '>' . esc_html( $tag['tag'] ) . '</option>';
330 }
331 }
332 echo '</select>';
333 submit_button( __( 'Filter', 'counter-box' ), 'secondary', 'action', false );
334 echo '</div>';
335 }
336 }
337
338 private function sort_data( $a, $b ): int {
339 // If no sort, default to title
340 $orderby = ( ! empty( $_GET['orderby'] ) ) ? sanitize_text_field( $_GET['orderby'] ) : 'ID';
341 // If no order, default to asc
342 $order = ( ! empty( $_GET['order'] ) ) ? sanitize_text_field( $_GET['order'] ) : 'desc';
343 // Determine sort order
344 $result = strnatcmp( $a[ $orderby ], $b[ $orderby ] );
345
346 // Send final sort direction to usort
347 return ( $order === 'asc' ) ? $result : - $result;
348 }
349 }