InstructorsList
Source
File: classes/InstructorsList.php
class InstructorsList extends \WP_List_Table { function __construct() { global $status, $page; //Set parent defaults parent::__construct( array( 'singular' => 'instructor', //singular name of the listed records 'plural' => 'instructors', //plural name of the listed records 'ajax' => false //does this table support ajax? ) ); $this->process_bulk_action(); } function column_default( $item, $column_name ) { switch ( $column_name ) { case 'user_email': case 'display_name': return $item->$column_name; default: return print_r( $item, true ); //Show the whole array for troubleshooting purposes } } function column_total_course( $item ) { global $wpdb; $total_course = (int) $wpdb->get_var( "select count(ID) from {$wpdb->posts} WHERE post_author={$item->ID} AND post_type = 'dozent_course' " ); echo $total_course; } /** * @param $item * * Completed Course by User */ function column_status( $item ) { $status = dozent_instructor_status( $item->ID ); echo "<span class='dozent-pill dozent-pill-{$status->status}'> {$status->status_name} </span>"; } function column_display_name( $item ) { //Build row actions $actions = []; $status = dozent_instructor_status( $item->ID ); switch ( $status->status ) { case 'pending': case 'blocked': $actions['approved'] = sprintf( '<a class="dozent-pill dozent-pill-success" data-action="approve" data-instructor-id="' . $item->ID . '" href="?page=%s&action=%s&instructor=%s"> ' . __( 'Approve', 'dozent' ) . ' </a>', $_REQUEST['page'], 'approve', $item->ID ); break; case 'approved': $actions['blocked'] = sprintf( '<a class="dozent-pill dozent-pill-danger" data-action="blocked" data-instructor-id="' . $item->ID . '" href="?page=%s&action=%s&instructor=%s"> <i class="dicon-trash-line"></i> ' . __( 'Block', 'dozent' ) . ' </a>', $_REQUEST['page'], 'blocked', $item->ID ); break; } //Return the title contents return sprintf( '%1$s <span style="color:silver">(id:%2$s)</span>%3$s', $item->display_name, $item->ID, $this->row_actions( $actions ) ); } function column_cb( $item ) { return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', /*$1%s*/ $this->_args['singular'], //Let's simply repurpose the table's singular label ("instructor") /*$2%s*/ $item->ID //The value of the checkbox should be the record's id ); } function get_columns() { $columns = array( //'cb' => '<input type="checkbox" />', //Render a checkbox instead of text 'display_name' => __( 'Name', 'dozent' ), 'user_email' => __( 'E-Mail', 'dozent' ), 'total_course' => __( 'Total Course', 'dozent' ), 'status' => __( 'Status', 'dozent' ), ); return $columns; } function get_sortable_columns() { $sortable_columns = array(//'display_name' => array('title',false), //true means it's already sorted ); return $sortable_columns; } function get_bulk_actions() { $actions = array(//'delete' => 'Delete' ); return $actions; } function process_bulk_action() { if ( 'approve' === $this->current_action() ) { $instructor_id = (int) dozent_input_text( 'instructor' ); dozent_instructor_approve( $instructor_id ); } if ( 'blocked' === $this->current_action() ) { $instructor_id = (int) sanitize_text_field( $_GET['instructor'] ); dozent_instructor_block( $instructor_id ); } //Detect when a bulk action is being triggered... if ( 'delete' === $this->current_action() ) { wp_die( 'Items deleted (or they would be if we had items to delete)!' ); } } function prepare_items() { $per_page = 20; $search_term = ''; if ( isset( $_REQUEST['s'] ) ) { $search_term = sanitize_text_field( $_REQUEST['s'] ); } $columns = $this->get_columns(); $hidden = array(); $sortable = $this->get_sortable_columns(); $this->_column_headers = array( $columns, $hidden, $sortable ); $current_page = $this->get_pagenum(); $args = [ 'search_term' => $search_term, 'start' => ( $current_page - 1 ) * $per_page, 'per_page' => $per_page, ]; $instructors = dozent_get_instructors( $args ); $this->items = $instructors->results; $this->set_pagination_args( array( 'total_items' => $instructors->count, 'per_page' => $per_page, 'total_pages' => ceil( $instructors->count / $per_page ) ) ); } }