PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.3
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.3
8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 7.8.7 All 46 releases
gamipress / libraries / ct / includes / class-ct-list-view.php

class-ct-list-view.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 8.0.3, at libraries/ct/includes/class-ct-list-view.php

582 lines 20.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * List View class
4 *
5 * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
6 *
7 * @since 1.0.0
8 */
9 // Exit if accessed directly
10 defined( 'ABSPATH' ) || exit;
11
12 if ( ! class_exists( 'CT_List_View' ) ) :
13
14 class CT_List_View extends CT_View {
15
16 protected $per_page = 20;
17
18 protected $columns = array();
19
20 public $add_form = false;
21
22 public function __construct( $name, $args ) {
23
24 parent::__construct( $name, $args );
25
26 $this->per_page = isset( $args['per_page'] ) ? $args['per_page'] : 20;
27 $this->columns = isset( $args['columns'] ) ? $args['columns'] : array();
28 $this->add_form = isset( $args['add_form'] ) ? $args['add_form'] : false;
29
30 }
31
32 public function add_hooks() {
33
34 parent::add_hooks();
35
36 add_filter( "manage_{$this->name}_columns", array( $this, 'get_columns' ) );
37 add_filter( "manage_{$this->name}_sortable_columns", array( $this, 'get_sortable_columns' ) );
38
39 }
40
41 /**
42 * Set columns passed from view columns arg.
43 *
44 * @since 1.0.0
45 *
46 * @param array $columns
47 *
48 * @return array
49 */
50 public function get_columns( $columns ) {
51
52 foreach( $this->columns as $column => $column_args ) {
53
54 if( is_array( $column_args ) && isset( $column_args['label'] ) ) {
55 // 'column_name' => array( 'label' => 'Column Label' )
56 $columns[$column] = $column_args['label'];
57 } else if( gettype( $column_args ) === 'string' ) {
58 // 'column_name' => 'Column Label'
59 $columns[$column] = $column_args;
60 }
61
62 }
63
64 return $columns;
65
66 }
67
68 /**
69 * Set columns passed from view columns arg.
70 *
71 * @since 1.0.0
72 *
73 * @param array $sortable_columns
74 *
75 * @return array
76 */
77 public function get_sortable_columns( $sortable_columns ) {
78
79 foreach( $this->columns as $column => $column_args ) {
80
81 if( is_array( $column_args ) && isset( $column_args['sortable'] ) ) {
82 // 'column_name' => array( 'sortable' => 'sortable_setup' )
83 $sortable_columns[$column] = $column_args['sortable'];
84 }
85
86 }
87
88 return $sortable_columns;
89
90 }
91
92 public function init() {
93
94 global $ct_registered_tables, $ct_table, $ct_query, $ct_list_table;
95
96 if( ! isset( $ct_registered_tables[$this->name] ) ) {
97 return;
98 }
99
100 // Setup CT_Table
101 $ct_table = $ct_registered_tables[$this->name];
102
103 // Check for bulk delete
104 if( isset( $_GET['action'] ) ) {
105
106 if( $_GET['action'] === 'delete' ) {
107 // Deleting
108 $this->bulk_delete();
109 }
110
111 }
112
113 // Check for delete action
114 if( isset( $_GET['ct-action'] ) ) {
115
116 if( $_GET['ct-action'] === 'delete' ) {
117 // Deleting
118 $this->delete();
119 }
120
121 }
122
123 if( isset( $_POST['ct-add'] ) ) {
124 // Add new
125 $this->add_new();
126 }
127
128 // Setup the query and the list table objects
129 $ct_query = new CT_Query( $_GET );
130 $ct_list_table = new CT_List_Table();
131
132 }
133
134 /**
135 * Screen settings text displayed in the Screen Options tab.
136 *
137 * @since 1.0.0
138 *
139 * @param string $screen_settings Screen settings.
140 * @param WP_Screen $screen WP_Screen object.
141 */
142 public function screen_settings( $screen_settings, $screen ) {
143
144 $this->render_list_table_columns_preferences();
145 $this->render_per_page_options();
146
147 }
148
149 /**
150 * Render the list table columns preferences.
151 *
152 * @since 1.0.0
153 */
154 public function render_list_table_columns_preferences() {
155
156 global $ct_table, $ct_list_table;
157
158 // Set up vars
159 $columns = $ct_list_table->get_columns();
160 $hidden = get_hidden_columns( $ct_table->name );
161
162 if ( ! $columns )
163 return;
164
165 $legend = ! empty( $columns['_title'] ) ? $columns['_title'] : __( 'Columns' );
166 ?>
167 <fieldset class="metabox-prefs">
168 <legend><?php echo $legend; ?></legend>
169 <?php
170 $special = array( '_title', 'cb' );
171
172 foreach ( $columns as $column => $title ) {
173 // Can't hide these for they are special
174 if ( in_array( $column, $special ) || empty( $title ) ) {
175 continue;
176 }
177
178 $id = "$column-hide";
179 echo '<label>';
180 echo '<input class="hide-column-tog" name="' . esc_attr( $id ) . '" type="checkbox" id="' . esc_attr( $id ) . '" value="' . esc_attr( $column ) . '"' . checked( ! in_array( $column, $hidden ), true, false ) . ' />';
181 echo "$title</label>\n";
182 }
183 ?>
184 </fieldset>
185 <?php
186 }
187
188 /**
189 * Render the items per page option
190 *
191 * @since 1.0.0
192 */
193 public function render_per_page_options() {
194
195 global $ct_table;
196
197 if ( ! $this->per_page ) {
198 return;
199 }
200
201 // Set up vars
202 $per_page_label = __( 'Number of items per page:', 'ct' );
203
204 $option = str_replace( '-', '_', "edit_{$ct_table->name}_per_page" );
205
206 $per_page = (int) get_user_option( $option );
207
208 if ( empty( $per_page ) || $per_page < 1 )
209 $per_page = $this->per_page;
210
211 $per_page = apply_filters( "{$option}", $per_page );
212
213 // This needs a submit button
214 add_filter( 'screen_options_show_submit', '__return_true' );
215
216 ?>
217 <fieldset class="screen-options">
218 <legend><?php esc_html_e( 'Pagination' ); ?></legend>
219 <?php if ( $per_page_label ) : ?>
220 <label for="<?php echo esc_attr( $option ); ?>"><?php echo esc_html( $per_page_label ); ?></label>
221 <input type="number" step="1" min="1" max="999" class="screen-per-page" name="wp_screen_options[value]"
222 id="<?php echo esc_attr( $option ); ?>" maxlength="3"
223 value="<?php echo esc_attr( $per_page ); ?>" />
224 <?php endif; ?>
225 <input type="hidden" name="wp_screen_options[option]" value="<?php echo esc_attr( $option ); ?>" />
226 </fieldset>
227 <?php
228 }
229
230 /**
231 * Screen option value before it is set.
232 *
233 * @since 1.0.0
234 *
235 * @param bool|int $value_to_set Screen option value to set. Default false to skip.
236 * @param string $option The option name.
237 * @param int $value The option value.
238 *
239 * @return bool|mixed False to skip or any other value to set as option value
240 */
241 public function set_screen_settings( $value_to_set, $option, $value ) {
242
243 global $ct_table, $ct_list_table;
244
245 if( ! $ct_table ) {
246 return $value_to_set;
247 }
248
249 if( ! $ct_list_table ) {
250 return $value_to_set;
251 }
252
253 $view_settings = array(
254 str_replace( '-', '_', "edit_{$ct_table->name}_per_page" ) // Per page
255 );
256
257 // Columns hidden setting
258 $columns = $ct_list_table->get_columns();
259 $special = array( '_title', 'cb' );
260
261 foreach ( $columns as $column => $title ) {
262 // Can't hide these for they are special
263 if ( in_array( $column, $special ) || empty( $title ) )
264 continue;
265
266 $view_settings[] = "$column-hide";
267 }
268
269 // If option is on this view settings list, then save it
270 if( in_array( $option, $view_settings ) ) {
271 $value_to_set = $value;
272 }
273
274 return $value_to_set;
275
276 }
277
278 public function bulk_delete() {
279
280 global $ct_table;
281
282 // If not CT object, die
283 if ( ! $ct_table )
284 wp_die( esc_html__( 'Sorry, you are not allowed to perform this action.' ) );
285
286 // If not CT object allow ui, die
287 if ( ! $ct_table->show_ui ) {
288 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'delete_items_not_allowed' ) ) );
289 }
290
291 // Nonce check
292 if ( ! isset( $_REQUEST['_wpnonce'] ) ) {
293 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'delete_items_not_allowed' ) ) );
294 }
295
296 if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash ( $_REQUEST['_wpnonce'] ) ), 'bulk-' . sanitize_key( $ct_table->labels->plural_name ) ) ) {
297 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'delete_items_not_allowed' ) ) );
298 }
299
300 $object_ids = array();
301
302 // Check received items
303 if ( ! empty( $_REQUEST['item'] ) ) {
304 $object_ids = array_map('intval', $_REQUEST['item']);
305 }
306
307 $deleted = 0;
308
309 foreach ( (array) $object_ids as $object_id ) {
310
311 // If not current user can delete, die
312 if ( ! current_user_can( $ct_table->cap->delete_item, $object_id ) ) {
313 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'delete_item_not_allowed' ) ) );
314 }
315
316 if ( ! ct_delete_object( $object_id, true ) )
317 wp_die( esc_html__( 'Sorry, you are not allowed to perform this action.' ) );
318
319 $deleted++;
320 }
321
322 $location = add_query_arg( array( 'deleted' => $deleted ), $this->get_link() );
323
324 wp_redirect( $location );
325 exit;
326 }
327
328 public function delete() {
329
330 global $ct_table;
331
332 // If not CT object, die
333 if ( ! $ct_table ) {
334 wp_die( esc_html__( 'Sorry, you are not allowed to perform this action.' ) );
335 }
336
337 // If not CT object allow ui, die
338 if ( ! $ct_table->show_ui ) {
339 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'delete_items_not_allowed' ) ) );
340 }
341
342 $primary_key = $ct_table->db->primary_key;
343
344 // Object ID is required
345 if( ! isset( $_GET[$primary_key] ) ) {
346 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'delete_items_not_allowed' ) ) );
347 }
348
349 $object_id = (int) $_GET[$primary_key];
350
351 // Nonce check
352 if ( ! isset( $_REQUEST['_wpnonce'] ) ) {
353 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'delete_item_not_allowed' ) ) );
354 }
355
356 if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash ( $_REQUEST['_wpnonce'] ) ), 'ct_delete_' . $object_id ) ) {
357 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'delete_item_not_allowed' ) ) );
358 }
359
360 // If user can not delete it, bail
361 if ( ! current_user_can( $ct_table->cap->delete_item, $object_id ) ) {
362 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'delete_item_not_allowed' ) ) );
363 }
364
365 if ( ! ct_delete_object( $object_id ) )
366 wp_die( esc_html__( 'Sorry, you are not allowed to perform this action.' ) );
367
368 $location = add_query_arg( array( 'deleted' => 1 ), $this->get_link() );
369
370 wp_redirect( $location );
371 exit;
372
373 }
374
375 public function add_new() {
376
377 global $ct_registered_tables, $ct_table;
378
379 // If not CT object, die
380 if ( ! $ct_table )
381 wp_die( esc_html__( 'Sorry, you are not allowed to perform this action.' ) );
382
383 // If not CT object allow ui, die
384 if ( ! $ct_table->show_ui ) {
385 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'add_item_not_allowed' ) ) );
386 }
387
388 // Nonce check
389 if ( ! isset( $_REQUEST['_wpnonce'] ) ) {
390 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'add_item_not_allowed' ) ) );
391 }
392
393 if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash ( $_REQUEST['_wpnonce'] ) ), 'ct_add_new' ) ) {
394 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'add_item_not_allowed' ) ) );
395 }
396
397 $object_data = map_deep( $_POST, 'wp_kses_post' );
398
399 unset( $object_data['ct-add'] );
400
401 // Insert a default object
402 $success = ct_insert_object( array() );
403
404 if( $success ) {
405 $object_data[$ct_table->db->primary_key] = $success;
406 $success = ct_update_object( $object_data );
407 }
408
409 $location = $this->get_link();
410
411 if( $success ) {
412 $location = add_query_arg( array( 'added' => 1 ), $location );
413 } else {
414 $location = add_query_arg( array( 'added' => 0 ), $location );
415 }
416
417 wp_redirect( $location );
418 exit;
419
420 }
421
422 /**
423 * View content.
424 *
425 * @since 1.0.0
426 */
427 public function render() {
428
429 global $ct_table, $ct_list_table;
430
431 $ct_list_table->prepare_items();
432
433 $bulk_counts = array(
434 'added' => isset( $_REQUEST['added'] ) ? absint( $_REQUEST['added'] ) : 0,
435 'updated' => isset( $_REQUEST['updated'] ) ? absint( $_REQUEST['updated'] ) : 0,
436 'locked' => isset( $_REQUEST['locked'] ) ? absint( $_REQUEST['locked'] ) : 0,
437 'deleted' => isset( $_REQUEST['deleted'] ) ? absint( $_REQUEST['deleted'] ) : 0,
438 'trashed' => isset( $_REQUEST['trashed'] ) ? absint( $_REQUEST['trashed'] ) : 0,
439 'untrashed' => isset( $_REQUEST['untrashed'] ) ? absint( $_REQUEST['untrashed'] ) : 0,
440 );
441
442 $bulk_messages = array(
443 // translators: %s: count
444 'added' => _n( '%s item added.', '%s items added.', $bulk_counts['added'], 'ct' ),
445 // translators: %s: count
446 'updated' => _n( '%s item updated.', '%s items updated.', $bulk_counts['updated'], 'ct' ),
447 'locked' => ( 1 == $bulk_counts['locked'] ) ? __( '1 item not updated, somebody is editing it.', 'ct' ) :
448 // translators: %s: count
449 _n( '%s item not updated, somebody is editing it.', '%s items not updated, somebody is editing them.', $bulk_counts['locked'], 'ct' ),
450 // translators: %s: count
451 'deleted' => _n( '%s item permanently deleted.', '%s items permanently deleted.', $bulk_counts['deleted'], 'ct' ),
452 // translators: %s: count
453 'trashed' => _n( '%s item moved to the Trash.', '%s items moved to the Trash.', $bulk_counts['trashed'], 'ct' ),
454 // translators: %s: count
455 'untrashed' => _n( '%s item restored from the Trash.', '%s items restored from the Trash.', $bulk_counts['untrashed'], 'ct' ),
456 );
457
458 /**
459 * Filters the bulk action updated messages.
460 *
461 * @since 1.0.0
462 *
463 * @param array $bulk_messages Arrays of messages. Messages are keyed with 'updated', 'locked', 'deleted', 'trashed', and 'untrashed'.
464 * @param array $bulk_counts Array of item counts for each message, used to build internationalized strings.
465 */
466 $bulk_messages = apply_filters( 'bulk_object_updated_messages', $bulk_messages, $bulk_counts );
467 $bulk_counts = array_filter( $bulk_counts );
468
469 ?>
470
471 <div class="wrap">
472
473 <h1 class="wp-heading-inline"><?php echo $ct_table->labels->plural_name; ?></h1>
474
475 <?php if ( property_exists( $ct_table->views, 'add' ) && $ct_table->views->add && current_user_can( $ct_table->cap->create_items ) ) :
476 echo ' <a href="' . esc_url( $ct_table->views->add->get_link() ) . '" class="page-title-action">' . esc_html( $ct_table->labels->add_new_item ) . '</a>';
477 endif; ?>
478
479 <hr class="wp-header-end">
480
481 <?php
482 // If we have a bulk message to issue:
483 $messages = array();
484 foreach ( $bulk_counts as $message => $count ) {
485 if ( isset( $bulk_messages[ $message ] ) )
486 $messages[] = sprintf( $bulk_messages[ $message ], number_format_i18n( $count ) );
487
488 //if ( $message == 'trashed' && isset( $_REQUEST['ids'] ) ) {
489 //$ids = preg_replace( '/[^0-9,]/', '', $_REQUEST['ids'] );
490 //$messages[] = '<a href="' . esc_url( wp_nonce_url( "edit.php?post_type=$post_type&doaction=undo&action=untrash&ids=$ids", "bulk-posts" ) ) . '">' . __('Undo') . '</a>';
491 //}
492 }
493
494 if ( $messages )
495 echo '<div id="message" class="updated notice is-dismissible"><p>' . join( ' ', $messages ) . '</p></div>';
496 unset( $messages );
497
498 $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'locked', 'skipped', 'updated', 'deleted', 'trashed', 'untrashed' ), sanitize_text_field( $_SERVER['REQUEST_URI'] ) );
499
500 if ( $this->add_form ) : ?>
501
502 <div id="col-container" class="wp-clearfix">
503 <div id="col-left">
504 <div id="col-wrap">
505 <?php $this->render_add_form(); ?>
506 </div>
507 </div>
508 <div id="col-right">
509 <div id="col-wrap">
510 <?php $this->render_table(); ?>
511 </div>
512 </div>
513 </div>
514
515 <?php else :
516
517 $this->render_table();
518
519 endif; ?>
520
521 </div>
522
523 <?php
524 }
525
526 function render_add_form() {
527
528 global $ct_table, $ct_list_table;
529
530 ?>
531
532 <div id="form-wrap">
533
534 <h2><?php echo $ct_table->labels->add_new_item; ?></h2>
535
536 <form name="ct_add_form" action="" method="post" id="ct_add_form">
537
538 <?php wp_nonce_field( 'ct_add_new' ); ?>
539
540 <?php do_action( "ct_render_{$ct_table->name}_edit_form", 0 ); ?>
541
542 <p class="submit">
543 <?php submit_button( $ct_table->labels->add_new_item, 'primary', 'ct-add', false ); ?>
544 <input type="button" name="" id="ct-add-disabled" class="button button-primary" value="<?php echo esc_attr( $ct_table->labels->add_new_item ); ?>" disabled="disabled" style="display: none;">
545 <span class="spinner"></span>
546 </p>
547
548 </form>
549
550 </div>
551
552 <?php
553 }
554
555 function render_table() {
556
557 global $ct_table, $ct_list_table;
558
559 ?>
560
561 <?php $ct_list_table->views(); ?>
562
563 <form id="ct-list-filter" method="get">
564
565 <input type="hidden" name="page" value="<?php echo esc_attr( $this->args['menu_slug'] ); ?>" />
566
567 <?php $ct_list_table->search_box( $ct_table->labels->search_items, $ct_table->name ); ?>
568
569 <?php $ct_list_table->display(); ?>
570
571 </form>
572
573 <div id="ajax-response"></div>
574 <br class="clear" />
575
576 <?php
577
578 }
579
580 }
581
582 endif;