Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class TT_Example_List_Table extends WP_List_Table {
- var $example_data = array(
- array(
- 'ID' => 1,
- 'title' => '300',
- 'rating' => 'R',
- 'director' => 'Zach Snyder'
- ),
- array(
- 'ID' => 2,
- 'title' => 'Eyes Wide Shut',
- 'rating' => 'R',
- 'director' => 'Stanley Kubrick'
- ),
- array(
- 'ID' => 3,
- 'title' => 'Moulin Rouge!',
- 'rating' => 'PG-13',
- 'director' => 'Baz Luhrman'
- ),
- array(
- 'ID' => 4,
- 'title' => 'Snow White',
- 'rating' => 'G',
- 'director' => 'Walt Disney'
- ),
- array(
- 'ID' => 5,
- 'title' => 'Super 8',
- 'rating' => 'PG-13',
- 'director' => 'JJ Abrams'
- ),
- array(
- 'ID' => 6,
- 'title' => 'The Fountain',
- 'rating' => 'PG-13',
- 'director' => 'Darren Aronofsky'
- ),
- array(
- 'ID' => 7,
- 'title' => 'Watchmen',
- 'rating' => 'R',
- 'director' => 'Zach Snyder'
- )
- );
- /**
- * A set of conditional classes
- * you can add more stuff when you get the data in TT_Example_List_Table::prepare_elements()
- *
- * The data is stored as a (optionally)multi-dimensional key=>pair array.
- *
- * Only two top-level keys are reserved - "odd" and "even" - put your conditional classes for odd and even rows
- * The other top-level keys could be column id's(like "title" for instance) or item ID's
- * Each top-level key can hold either an array or a string
- * If the top-level key holds a string, then when this condition is met, that class is added
- * If the top-level key holds an array, then it's looped through.
- * The second-level array can have string values and key=>value pairs, where the key is the class and the value
- * is an array of conditions.
- *
- * I guess that's quite confusing, but the example bellow should give you an idea of how this works.
- */
- var $cond_classes = array(
- 'odd' => array(
- 'odd-class', // This class will always be given to odd rows and their columns
- 'special-odd-class' => array( // This class will only be given to odd rows and their columns if the rows is for an item with ID 1, 4 or 7
- 'ID' => array( 1, 4, 7 )
- )
- ),
- 'even' => array(
- 'even-class'
- ),
- 'title' => array(
- 'custom_title_class',
- 'special_title_class' => array(
- 'ID' => array( 3, 7 ), // This will only be given to the "title" column for an item with ID 3 or 7
- 'title' => 'The Fountain', // This will only be given to the "title" column for an item with title "The Fountain"
- ),
- ),
- 7 => 'id_7_class', // This will be given to a row and it's columns for item with ID 7
- );
- function __construct(){
- global $status, $page;
- //Set parent defaults
- parent::__construct( array(
- 'singular' => 'movie', //singular name of the listed records
- 'plural' => 'movies', //plural name of the listed records
- 'ajax' => false //does this table support ajax?
- ) );
- }
- function column_default($item, $column_name){
- switch($column_name){
- case 'rating':
- case 'director':
- return $item[$column_name] . 'hi';
- default:
- return print_r($item,true) . ' hi'; //Show the whole array for troubleshooting purposes
- }
- }
- function column_title($item){
- //Build row actions
- $actions = array(
- 'edit' => sprintf('<a href="?page=%s&action=%s&movie=%s">Edit</a>',$_REQUEST['page'],'edit',$item['ID']),
- 'delete' => sprintf('<a href="?page=%s&action=%s&movie=%s">Delete</a>',$_REQUEST['page'],'delete',$item['ID']),
- );
- //Return the title contents
- return sprintf('%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
- /*$1%s*/ $item['title'],
- /*$2%s*/ $item['ID'],
- /*$3%s*/ $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 ("movie")
- /*$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
- 'title' => 'Title',
- 'rating' => 'Rating',
- 'director' => 'Director'
- );
- return $columns;
- }
- function get_sortable_columns() {
- $sortable_columns = array(
- 'title' => array('title',true), //true means its already sorted
- 'rating' => array('rating',false),
- 'director' => array('director',false)
- );
- return $sortable_columns;
- }
- function get_bulk_actions() {
- $actions = array(
- 'delete' => 'Delete'
- );
- return $actions;
- }
- function process_bulk_action() {
- //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 = 10;
- $columns = $this->get_columns();
- $hidden = array();
- $sortable = $this->get_sortable_columns();
- $this->_column_headers = array($columns, $hidden, $sortable);
- $this->process_bulk_action();
- $data = $this->example_data;
- function usort_reorder($a,$b){
- $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'title'; //If no sort, default to title
- $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; //If no order, default to asc
- $result = strcmp($a[$orderby], $b[$orderby]); //Determine sort order
- return ($order==='asc') ? $result : -$result; //Send final sort direction to usort
- }
- usort($data, 'usort_reorder');
- $current_page = $this->get_pagenum();
- $total_items = count($data);
- $data = array_slice($data,(($current_page-1)*$per_page),$per_page);
- $this->items = $data;
- $this->set_pagination_args( array(
- 'total_items' => $total_items, //WE have to calculate the total number of items
- 'per_page' => $per_page, //WE have to determine how many items to show on a page
- 'total_pages' => ceil($total_items/$per_page) //WE have to calculate the total number of pages
- ) );
- }
- /**
- * Generate the table rows
- *
- * @since 3.1.0
- * @access protected
- */
- function display_rows() {
- foreach ( $this->items as $i => $item )
- $this->single_row( $item, ( ( $i + 1 ) % 2 == 0 ? true : false ) );
- }
- /**
- * Generates content for a single row of the table
- *
- * @since 3.1.0
- * @access protected
- *
- * @param object $item The current item
- */
- function single_row( $item, $even ) {
- $classes = $this->get_cond_classes( $item, $even );
- $row_class = ! empty( $classes ) ? ' class="' . implode( ' ', $classes ) . '"' : '';
- echo '<tr' . $row_class . '>';
- echo $this->single_row_columns( $item, $even );
- echo '</tr>';
- }
- /**
- * Generates the columns for a single row of the table
- *
- * @since 3.1.0
- * @access protected
- *
- * @param object $item The current item
- */
- function single_row_columns( $item, $even ) {
- list( $columns, $hidden ) = $this->get_column_info();
- foreach ( $columns as $column_name => $column_display_name ) {
- $classes = 'cb' == $column_name ? array( 'check-column' ) : array( $column_name, "column-$column_name" );
- $classes = array_merge( $classes, $this->get_cond_classes( $item, $even, $column_name ) );
- $class = ! empty( $classes ) ? ' class="' . implode( ' ', $classes ) . '"' : '';
- $style = '';
- if ( in_array( $column_name, $hidden ) )
- $style = ' style="display:none;"';
- $attributes = "$class$style";
- if ( 'cb' == $column_name ) {
- echo '<th scope="row"' . $attributes . '>';
- echo $this->column_cb( $item );
- echo '</th>';
- }
- elseif ( method_exists( $this, 'column_' . $column_name ) ) {
- echo "<td $attributes>";
- echo call_user_func( array( &$this, 'column_' . $column_name ), $item );
- echo "</td>";
- }
- else {
- echo "<td $attributes>";
- echo $this->column_default( $item, $column_name );
- echo "</td>";
- }
- }
- }
- // Gets conditional classes for $item in either a row/column
- function get_cond_classes( $item, $even, $column_name = false ) {
- $classes = array();
- if ( $even ) {
- // Check to see if there is a conditional class for even columns
- if ( isset( $this->cond_classes['even'] ) ) {
- $this->parse_cond_classes( $item, $this->cond_classes['even'], $classes );
- }
- } else {
- // Check to see if there is a conditional class for odd columns
- if ( isset( $this->cond_classes['odd'] ) ) {
- $this->parse_cond_classes( $item, $this->cond_classes['odd'], $classes );
- }
- // This class is applied by default
- $classes[] = 'alternate';
- }
- // Check to see if there is a conditional class for the current column
- if ( $column_name && isset( $this->cond_classes[ $column_name ] ) ) {
- $this->parse_cond_classes( $item, $this->cond_classes[ $column_name ], $classes );
- }
- // Check to see if there is a conditional class for the current element
- if ( isset( $this->cond_classes[ $item['ID'] ] ) ) {
- $this->parse_cond_classes( $item, $this->cond_classes[ $item['ID'] ], $classes );
- }
- return $classes;
- }
- // Parses the conditionals.
- // $classess is passed by reference, so we directly edit that
- function parse_cond_classes( $item, $to_parse, &$classes ) {
- // If this is an array, loop through it and check conditions
- if ( is_array( $to_parse ) ) {
- foreach ($to_parse as $key => $data) {
- // If this is an array as well - we have more conditions
- if ( is_array( $data ) ) {
- foreach ($data as $_key => $_value) {
- if ( isset( $item[ $_key ] ) ) {
- if ( ( is_array( $_value ) && in_array( $item[ $_key ], $_value ) ) || $item[ $_key ] == $value ) {
- $classes[] = $key;
- }
- }
- }
- } else {
- // Otherwise just add the class
- $classes[] = $data;
- }
- }
- } elseif ( $to_parse != '' ) {
- // In case we only have one class for this condition
- $classes[] = $to_parse;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement