PluginProbe
WP Coder – Insert & Manage Code Snippets / 3.0
WP Coder – Insert & Manage Code Snippets v3.0
4.5.1 1.1 2.3.1 2.3.2 2.4.1 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 3.2 3.2.1 3.3 3.4 3.5 3.5.1 All 39 releases
wp-coder / classes / Dashboard / ListTable.php

ListTable.php in WP Coder – Insert & Manage Code Snippets 3.0, at classes/Dashboard/ListTable.php

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