PluginProbe
Floating Button – Easily Create Sticky, Fixed & Floating Buttons / 6.0.6
Floating Button – Easily Create Sticky, Fixed & Floating Buttons v6.0.6
trunk 2.0.2 3.0 4.0 4.1.2 4.3 5.0 5.0.1 5.1 5.1.1 5.2 5.3 5.3.1 6.0 6.0.1 6.0.10 6.0.11 6.0.12 6.0.13 6.0.2 6.0.3 6.0.4 6.0.5 6.0.6 6.0.7 All 31 releases
floating-button / classes / Dashboard / ListTable.php

ListTable.php in Floating Button – Easily Create Sticky, Fixed & Floating Buttons 6.0.6, at classes/Dashboard/ListTable.php

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