'ap_moderate_list', // Singular name of the listed records
'plural' => 'ap_moderate_lists', // Plural name of the listed records
'ajax' => false // Does this table support ajax?
) );
$this->process_bulk_action();
$this->get_posts_counts();
$this->current_status = isset($_GET['status']) ? sanitize_text_field($_GET['status']) : 'publish' ;
}
/**
* @param string $input_id
*/
public function search_box( $text, $input_id ) {
if ( empty( $_REQUEST['s'] ) && !$this->has_items() )
return;
$input_id = $input_id . '-search-input';
if ( ! empty( $_REQUEST['orderby'] ) )
echo '';
if ( ! empty( $_REQUEST['order'] ) )
echo '';
?>
'search-submit') ); ?>
search_box( __( 'Search', 'ap' ), 'ap-moderates' ); ?>
(' . $this->moderate_count . ')';
$views = array(
'moderate' => sprintf( '%s', add_query_arg( array( 'status' => 'moderate', 'paged' => FALSE ) ), $this->current_status === 'moderate' ? ' class="current"' : '', __('Moderate', 'ap') . $moderate_count )
);
return apply_filters( 'ap_moderate_table_views', $views );
}
/**
* Retrieve the table columns
*/
public function get_columns() {
$columns = array(
'cb' => '', //Render a checkbox instead of text
'question_status' => __( 'Status', 'ap' ),
'post_title' => __( 'Title', 'ap' ),
'flag' => __( 'Flag', 'ap' ),
'category' => __( 'Category', 'ap' )
);
return apply_filters( 'ap_moderate_table_columns', $columns );
}
/**
* Retrieve the table's sortable columns
*/
public function get_sortable_columns() {
$columns = array(
'post_title' => array( 'post_title', false ),
);
return apply_filters( 'ap_moderate_table_sortable_columns', $columns );
}
/**
* This function renders most of the columns in the list table.
*/
public function column_default( $item, $column_name )
{
switch( $column_name ) {
case 'post_title':
return $item->$column_name;
default:
return print_r( $item, true ) ;
}
}
/**
* Render the checkbox column
*/
public function column_cb( $post ) {
return sprintf(
'',
'post',
$post->ID
);
}
public function column_question_status( $post ) {
if($post->post_type == 'question')
return $post->post_status;
}
public function column_post_title( $post ) {
if($post->post_type =='question')
$title =''.$post->post_title.'';
else
$title = ''. ap_truncate_chars(strip_tags($post->post_content), 80).'';
if('trash' == $this->current_status){
$actions = array(
'untrash' => sprintf('%s', ap_untrash_post($post->ID), __('Restore', 'ap')),
'delete' => sprintf('%s', get_delete_post_link($post->ID, null, true), __('Delete permanently', 'ap')),
);
}else{
$actions = array(
'edit' => sprintf('%s', get_edit_post_link($post->ID), __('Edit', 'ap')),
'trash' => sprintf('%s', get_delete_post_link($post->ID), __('Trash', 'ap')),
'view' => sprintf('%s', get_permalink($post->ID), __('View', 'ap'))
);
}
return sprintf('%1$s %2$s', $title, $this->row_actions($actions) );
}
public function column_flag( $post ) {
$flag_count = get_post_meta($post->ID, ANSPRESS_FLAG_META, true);
return '' . $flag_count . '';
}
public function column_category( $post ) {
/* Get the genres for the post. */
$category = get_the_terms($post->ID, ANSPRESS_CAT_TAX);
/* If terms were found. */
if (!empty($category)) {
$out = array();
/* Loop through each term, linking to the 'edit posts' page for the specific term. */
foreach ($category as $cat) {
$out[] = edit_term_link($cat->name, '', '', $cat, false);
}
/* Join the terms, separating them with a comma. */
return join(', ', $out);
}
/* If no terms were found, output a default message. */
else {
return __('--');
}
}
/**
* Retrieve the bulk actions
*/
public function get_bulk_actions() {
$status = isset($_GET['status']) ? sanitize_text_field($_GET['status']) : 'publish' ;
if('trash' == $status){
$actions = array(
'restore' => __( 'Restore', 'ap' ),
'delete' => __( 'Delete permanently', 'ap' ),
);
}else{
$actions = array(
'publish' => __( 'Published', 'ap' ),
'pending' => __( 'Pending', 'ap' ),
'trash' => __( 'Move to trash', 'ap' ),
);
}
return apply_filters( 'ap_moderate_table_bulk_actions', $actions );
}
/**
* Process the bulk actions
*/
public function process_bulk_action() {
$ids = isset( $_GET['post'] ) ? $_GET['post'] : false;
$action = $this->current_action();
if ( ! is_array( $ids ) )
$ids = array( $ids );
if( empty( $action ) )
return;
foreach ( $ids as $id ) {
// Detect when a bulk action is being triggered...
if ( 'trash' === $this->current_action() ) {
wp_trash_post( $id );
}
if ( 'publish' === $this->current_action() ) {
wp_publish_post( $id );
}
if ( 'pending' === $this->current_action() ) {
// Update post
$u_post = array();
$u_post['ID'] = $id;
$u_post['post_status'] = 'pending';
// Update the post into the database
wp_update_post( $u_post );
}
if ( 'delete' === $this->current_action() ) {
wp_delete_post( $id, true );
}
if ( 'restore' === $this->current_action() ) {
wp_untrash_post( $id);
}
do_action( 'ap_moderate_table_do_bulk_action', $id, $this->current_action() );
}
}
/**
* Retrieve the posts counts
*/
public function get_posts_counts() {
$num_posts = wp_count_posts( 'question', 'readable' );
$status = "moderate";
$mod_count = 0;
if ( !empty($num_posts->$status) )
$mod_count = $num_posts->$status;
$this->moderate_count = $mod_count;
$this->total_count = $mod_count;
}
public function posts_data() {
global $wpdb;
//$status = isset($_GET['status']) ? sanitize_text_field($_GET['status']) : 'publish' ;
$paged = isset( $_GET['paged'] ) ? sanitize_text_field($_GET['paged']) : 1;
// Preparing your query
$query = "SELECT * FROM $wpdb->posts WHERE (post_type = 'answer' OR post_type = 'question') AND post_status = 'moderate' ";
//adjust the query to take pagination
if(!empty($paged) && !empty($this->per_page)){
$offset=($paged-1)*$this->per_page;
$query.=' LIMIT '.(int)$offset.','.$this->per_page;
}
return $wpdb->get_results($query);
}
/**
* Setup the final data for the table
* @return void
*/
public function prepare_items() {
wp_reset_vars( array( 'action', 'post', 'orderby', 'order', 's' ) );
$columns = $this->get_columns();
$hidden = array(); // No hidden columns
$sortable = $this->get_sortable_columns();
$data = $this->posts_data();
$this->_column_headers = array( $columns, $hidden, $sortable );
$total_items = $this->moderate_count;
$this->items = $data;
$this->set_pagination_args( array(
'total_items' => $total_items,
'per_page' => $this->per_page,
'total_pages' => ceil( $total_items / $this->per_page )
)
);
}
}