PluginProbe
WP Coder – Insert & Manage Code Snippets / 3.4
WP Coder – Insert & Manage Code Snippets v3.4
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.4, at classes/Dashboard/ListTable.php

331 lines 11.2 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\WPCoder;
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 = WPCoder::SLUG;
111 $shortcode = WPCoder::SHORTCODE;
112
113 if ( empty( $result ) ) {
114 return $data;
115 }
116
117 $main_link = add_query_arg( [
118 'page' => $slug . '-settings',
119 'action' => 'update'
120 ], admin_url( 'admin.php' ) );
121 foreach ( $result as $key => $value ) {
122
123 $title = ! empty( $value->title ) ? $value->title : __( 'UnTitle', 'wpcoder' );
124 $tooltip_off = esc_attr__( 'Click for Deactivate.', 'wpcoder' );
125 $tooltip_on = esc_attr__( 'Click for Activate.', 'wpcoder' );
126 $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>';
127 $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>';
128 $status = ! empty( $value->status ) ? $status_off : $status_on;
129
130 $mode_tooltip_off = esc_attr__( 'Click for OFF.', 'wpcoder' );
131 $mode_tooltip_on = esc_attr__( 'Click for ON.', 'wpcoder' );
132
133 $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>';
134 $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>';
135
136 $mode = empty( $value->mode ) ? $mode_off : $mode_on;
137
138 $tag = '';
139 if ( isset( $value->tag ) ) {
140 $tag_url = add_query_arg( [
141 'page' => WPCoder::SLUG,
142 'tag' => $value->tag
143 ], admin_url( 'admin.php' ) );
144 $tag = '<a href="' . esc_url( $tag_url ) . '">' . esc_attr( $value->tag ) . '</a>';
145 }
146
147 $link = add_query_arg( [ 'id' => $value->id ], $main_link );
148 $data[] = array(
149 'ID' => $value->id,
150 'title' => '<a href="' . esc_url( $link ) . '">' . esc_attr( $title ) . '</a>',
151 'code' => '<div class="wowp-input-group"><input type="text" value="[' . esc_attr( $shortcode ) . ' id=&quot;' . absint( $value->id ) . '&quot;]" readonly="readonly"><span class="wowp-copy codericon codericon-copy"></span></div>',
152 'code-alt' => '<div class="wowp-input-group"><input type="text" value="[' . esc_attr( $shortcode ) . ' title=&quot;' . esc_attr( $title ) . '&quot;]" readonly="readonly"><span class="wowp-copy codericon codericon-copy"></span></div>',
153 'tag' => $tag,
154 'mode' => $mode,
155 'status' => $status,
156 );
157 }
158
159 return $data;
160
161
162 }
163
164 public function column_cb( $item ) {
165 return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', 'ID', $item['ID'] );
166 }
167
168 public function get_paged(): int {
169 return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
170 }
171
172 public function get_search() {
173 return ! empty( $_POST['s'] ) ? urldecode( trim( $_POST['s'] ) ) : false;
174 }
175
176 public function list_count(): int {
177 $result = $this->get_results();
178
179 if ( empty( $result ) ) {
180 return 0;
181 }
182 $count = count( $result );
183
184
185 return (int) $count;
186 }
187
188 public function get_results() {
189 global $wpdb;
190
191 $search = $this->get_search();
192
193 $tag_search = ( ! empty( $_REQUEST['tag'] ) ) ? sanitize_text_field( $_REQUEST ['tag'] ) : '';
194 $tag_search = ( $tag_search === 'all' ) ? '' : $tag_search;
195
196
197 $result = '';
198
199 $table = $wpdb->prefix . WPCoder::PREFIX;
200
201 if ( empty( $search ) ) {
202 $result = $wpdb->get_results( "SELECT * FROM $table order by id desc" );
203 if ( ! empty( $tag_search ) ) {
204 $result = $wpdb->get_results( "SELECT * FROM $table WHERE tag='$tag_search' order by id desc" );
205 }
206 } elseif ( trim( $search ) === 'UnTitle' ) {
207 $result = $wpdb->get_results( "SELECT * FROM $table WHERE title='' order by id desc" );
208 if ( ! empty( $tag_search ) ) {
209 $result = $wpdb->get_results( "SELECT * FROM $table WHERE title='' AND tag='$tag_search' order by id desc" );
210 }
211 } elseif ( is_numeric( $search ) ) {
212 $query = $wpdb->prepare( "SELECT * FROM $table WHERE id=%d", absint( $search ) );
213 if ( ! empty( $tag_search ) ) {
214 $query = $wpdb->prepare( "SELECT * FROM $table WHERE id=%d AND tag='%s'", absint( $search ),
215 $tag_search );
216 }
217 $result = $wpdb->get_results( $query );
218 } else {
219 $wild = '%';
220 $find = sanitize_text_field( $search );
221 $like = $wild . $wpdb->esc_like( $find ) . $wild;
222 $query = $wpdb->prepare( "SELECT * FROM $table WHERE title LIKE %s order by id desc", $like );
223 if ( ! empty( $tag_search ) ) {
224 $query = $wpdb->prepare( "SELECT * FROM $table WHERE title LIKE %s AND tag='%s' order by id desc",
225 $like, $tag_search );
226 }
227 $result = $wpdb->get_results( $query );
228 }
229
230 return $result;
231 }
232
233
234 public function get_bulk_actions() {
235 $actions = [
236 'delete' => __( 'Delate', 'wpcoder' ),
237 'activate' => __( 'Activate', 'wpcoder' ),
238 'deactivate' => __( 'Deactivate', 'wpcoder' ),
239 'test_on' => __( 'Test mode ON', 'wpcoder' ),
240 'test_off' => __( 'Test mode OFF', 'wpcoder' ),
241 ];
242
243 return $actions;
244 }
245
246 public function process_bulk_action() {
247 $ids = isset( $_POST['ID'] ) ? ( map_deep( $_POST['ID'], 'absint' ) ) : false;
248 $action = $this->current_action();
249 if ( ! is_array( $ids ) ) {
250 $ids = [ $ids ];
251 }
252 if ( empty( $action ) ) {
253 return false;
254 }
255
256 $verify = $this->verify();
257
258 if ( ! $verify ) {
259 return false;
260 }
261
262 foreach ( $ids as $id ) {
263 if ( 'delete' === $this->current_action() ) {
264 DBManager::delete( $id );
265 }
266 if ( 'activate' === $this->current_action() ) {
267 Settings::activate_item( $id );
268 }
269 if ( 'deactivate' === $this->current_action() ) {
270 Settings::deactivate_item( $id );
271 }
272 if ( 'test_on' === $this->current_action() ) {
273 Settings::activate_mode( $id );
274 }
275 if ( 'test_off' === $this->current_action() ) {
276 Settings::deactivate_mode( $id );
277 }
278 }
279
280 }
281
282 protected function extra_tablenav( $which ) {
283 if ( 'top' === $which ) {
284
285 $tags = DBManager::get_tags_from_table();
286
287 $tag_search = ( ! empty( $_REQUEST['tag'] ) ) ? sanitize_text_field( $_REQUEST ['tag'] ) : '';
288 $tag_search = ( $tag_search === 'all' ) ? '' : $tag_search;
289
290 echo '<div class="alignleft actions"><label for="filter-by-tag" class="screen-reader-text">' . __( 'Filter by tag',
291 'wpcoder' ) . '</label>';
292 echo '<select name="tag" id="filter-by-tag">';
293 echo '<option value="all"' . selected( 'all', $tag_search, false ) . '>' . __( 'All',
294 'wpcoder' ) . '</option>';
295
296 if ( ! empty( $tags ) ) {
297 foreach ( $tags as $tag ) {
298 if ( empty( $tag['tag'] ) ) {
299 continue;
300 }
301 echo '<option value="' . trim( esc_attr( $tag['tag'] ) ) . '"' . selected( $tag['tag'], $tag_search,
302 false ) . '>' . esc_attr( $tag['tag'] ) . '</option>';
303 }
304 }
305 echo '</select>';
306 submit_button( __( 'Filter', 'wpcoder' ), 'secondary', 'action', false );
307 echo '</div>';
308 }
309 }
310
311 private function sort_data( $a, $b ) {
312 // If no sort, default to title
313 $orderby = ( ! empty( $_GET['orderby'] ) ) ? sanitize_text_field( $_GET['orderby'] ) : 'ID';
314 // If no order, default to asc
315 $order = ( ! empty( $_GET['order'] ) ) ? sanitize_text_field( $_GET['order'] ) : 'desc';
316 // Determine sort order
317 $result = strnatcmp( $a[ $orderby ], $b[ $orderby ] );
318
319 // Send final sort direction to usort
320 return ( $order === 'asc' ) ? $result : - $result;
321
322 }
323
324 private function verify() {
325 $name = WPCoder::PREFIX . '_list_action';
326 $nonce_action = WPCoder::PREFIX . '_nonce';
327
328 return ! ( ! isset( $_POST[ $name ] ) || ! wp_verify_nonce( $_POST[ $name ], $nonce_action ) || ! current_user_can( 'unfiltered_html' ) );
329 }
330
331 }