PluginProbe
Floating Button – Easily Create Sticky, Fixed & Floating Buttons / 6.0.5
Floating Button – Easily Create Sticky, Fixed & Floating Buttons v6.0.5
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.5, at classes/Dashboard/ListTable.php

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