forked from designwall/dw-question-answer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-answers-list-table.php
118 lines (103 loc) · 3.24 KB
/
class-answers-list-table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
class DWQA_answer_list_table extends WP_List_Table {
function __construct() {
parent::__construct();
add_filter( 'wp_trim_excerpt', array( $this, 'trim_exceprt_more' ) );
}
/**
* Prepares the list of items for displaying.
* @uses WP_List_Table::set_pagination_args()
*
* @since 3.1.0
* @access public
* @abstract
*/
function prepare_items() {
global $avail_post_stati, $wp_query, $per_page, $mode, $post;
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
/**
* REQUIRED. Finally, we build an array to be used by the class for column
* headers. The $this->_column_headers property takes an array which contains
* 3 other arrays. One for all columns, one for hidden columns, and one
* for sortable columns.
*/
$this->_column_headers = array( $columns, $hidden, $sortable );
$post_type = 'dwqa-answer';
$per_page = $this->get_items_per_page( 'edit_' . $post_type . '_per_page' );
/** This filter is documented in wp-admin/includes/post.php */
$per_page = apply_filters( 'edit_posts_per_page', $per_page, $post_type );
$question_id = $post->ID;
$args = array(
'post_type' => 'dwqa-answer',
'posts_per_page' => $per_page,
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_question',
'value' => array( $question_id ),
'compare' => 'IN',
),
),
'post_status' => 'publish',
);
$data = get_posts( $args );
$this->items = $data;
}
/**
* Display the table
*
* @since 3.1.0
* @access public
*/
function display() {
extract( wp_parse_args( $this->_args, array( 'dev' => false ) ) );
$this->prepare_items();
?>
<table class="wp-list-table dwqa-answer-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>" cellspacing="0">
<tbody id="the-list"<?php if ( $singular ) echo " data-wp-lists='list:$singular'"; ?>>
<?php $this->display_rows_or_placeholder(); ?>
</tbody>
</table>
<?php
}
function get_columns() {
$columns = array(
'author' => __( 'Author', 'dwqa' ),
'detail' => __( 'Detail', 'dwqa' ),
);
return $columns;
}
function column_default( $item, $column_name ) {
switch ( $column_name ) {
case 'author':
$user_info = get_userdata( $item->post_author );
if ( ! $user_info ) {
echo '<strong>'.__( 'Anonymous','dwqa' ).'</strong>';
} else {
echo '<strong>'.get_avatar( $item->post_author, $size = '32' ) . ' ' .$user_info->display_name . '</strong>';
}
break;
case 'detail':
global $post;
setup_postdata( $item );
?>
<div class="submitted-on"><?php _e( 'Answered on ', 'dwqa' ) ?><a href="<?php echo get_permalink( $item->ID ) ?>"><?php echo $item->post_date ?></a></div>
<?php the_excerpt(); ?>
<?php
break;
default:
return print_r( $item,true );
}
}
function trim_exceprt_more( $excerpt ) {
if ( $excerpt ) {
return str_replace( '[...]', '<a href="'.get_permalink().'" title="'.__( 'Read more', 'dwqa' ).'" >...</a>', $excerpt );
}
}
}
?>