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

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