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

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