get_columns();
$hidden = $this->get_hidden_columns();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array( $columns, $hidden, $sortable );
$query = $this->table_data();
if ( is_wp_error( $query ) ) {
$this->error = $query->get_error_message();
$this->items = array();
return;
}
$this->set_pagination_args(
array(
'total_items' => $query->pagination->count,
'per_page' => $this->get_items_per_page( 'reviews' ),
)
);
$this->items = $query->data;
}
/**
* Get views for the list table status links.
*
* @global int $post_id
* @global string $comment_status
* @global string $comment_type
*/
protected function get_views() {
$allowed_statuses = array( 'all', 'published', 'in_review', 'unpublished' );
$requested_status = ! empty( $_GET['status'] ) && in_array( $_GET['status'], $allowed_statuses, true ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : 'all'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
foreach ( $this->getStatuses() as $status => $label ) {
$link = admin_url( 'admin.php?page=sc-reviews' );
$current_link_attributes = '';
if ( ! empty( $requested_status ) ) {
if ( $status === $requested_status ) {
$current_link_attributes = ' class="current" aria-current="page"';
}
} elseif ( 'all' === $status ) {
$current_link_attributes = ' class="current" aria-current="page"';
}
$link = add_query_arg( 'status', $status, $link );
$link = esc_url( $link ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
// translators: %s: The number of reviews for a specific status.
$status_links[ $status ] = sprintf( '%s', esc_url( $link ), $current_link_attributes, esc_html( $label ) );
}
/**
* Filters the comment status links.
*
* @since 2.5.0
* @since 5.1.0 The 'Mine' link was added.
*
* @param string[] $status_links An associative array of fully-formed comment status links. Includes 'All', 'Mine',
* 'Pending', 'Approved', 'Spam', and 'Trash'.
*/
return apply_filters( 'comment_status_links', $status_links );
}
/**
* Override the parent columns method. Defines the columns to use in your listing table.
*
* @return array
*/
public function get_columns() {
return array_merge(
[
'review' => __( 'Review', 'surecart' ),
'stars' => __( 'Rating', 'surecart' ),
'customer' => __( 'Customer', 'surecart' ),
'product' => __( 'Product', 'surecart' ),
'status' => __( 'Status', 'surecart' ),
'date' => __( 'Submitted on', 'surecart' ),
],
parent::get_columns()
);
}
/**
* Define which columns are sortable.
*
* @return array
*/
public function get_sortable_columns() {
return array(
'stars' => array( 'stars', false ),
'date' => array( 'created_at', false ),
);
}
/**
* Define which columns are hidden.
*
* @return array
*/
public function get_hidden_columns() {
return array();
}
/**
* Get the table data.
*
* @return object|WP_Error
*/
private function table_data() {
$args = array(
'status[]' => $this->getFilteredStatus(),
'query' => $this->get_search_query(),
);
// Add sorting.
$orderby = $this->get_orderby();
$order = $this->get_order();
if ( $orderby && in_array( $orderby, [ 'stars', 'created_at', 'updated_at' ], true ) ) {
$args['sort'] = $orderby . ':' . $order;
}
$review_query = Review::where( $args )->with( [ 'customer', 'product' ] );
// Check if there is any sc_product.
if ( ! empty( $_GET['sc_product'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$review_query->where(
array(
'product_ids' => array( sanitize_text_field( wp_unslash( $_GET['sc_product'] ) ) ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended
)
);
}
return $review_query->paginate(
array(
'per_page' => $this->get_items_per_page( 'reviews' ),
'page' => $this->get_pagenum(),
)
);
}
/**
* Nothing found.
*
* @return void
*/
public function no_items() {
if ( $this->error ) {
echo esc_html( $this->error );
return;
}
echo esc_html_e( 'No reviews found.', 'surecart' );
}
/**
* Status column.
*
* @param Review $review Review model.
*
* @return string
*/
public function column_status( $review ) {
ob_start();
?>