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(); ?> status_display ); ?>
(stars ); ?>)
customer ) ) { return '-'; } $customer_name = $review->customer->name ?? $review->customer->email ?? '-'; return '' . esc_html( $customer_name ) . ''; } /** * Product column. * * @param Review $review Review model. * * @return string */ public function column_product( $review ) { if ( empty( $review->product ) ) { return '-'; } return '' . esc_html( $review->product->name ) . ''; } /** * Review column. * * @param Review $review Review model. * * @return string */ public function column_review( $review ) { ob_start(); ?>
title ); ?> verified ) ) : ?> body ) ) : ?>
body ); ?>
row_actions( $this->getRowActions( $review ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
$column_name ?? '' ); } /** * Get row actions. * * @param Review $review Review model. * * @return array */ protected function getRowActions( $review ) { $actions = [ 'edit' => '' . esc_html__( 'Edit', 'surecart' ) . '', ]; if ( in_array( $review->status, [ 'in_review', 'unpublished' ], true ) ) { $actions['publish'] = '' . esc_html__( 'Approve', 'surecart' ) . ''; } if ( in_array( $review->status, [ 'in_review', 'published' ], true ) ) { $actions['unpublish'] = '' . esc_html__( 'Reject', 'surecart' ) . ''; } $actions['delete'] = sprintf( /* translators: 1: Confirmation message, 2: URL, 3: aria-label, 4: link text. */ '%4$s', esc_attr__( 'Are you sure? This cannot be undone.', 'surecart' ), esc_url( add_query_arg( [ 'action' => 'delete', 'nonce' => wp_create_nonce( 'delete_review' ), 'id' => $review->id, ], admin_url( 'admin.php?page=sc-reviews' ) ) ), esc_attr__( 'Delete Review', 'surecart' ), esc_html__( 'Delete', 'surecart' ) ); return $actions; } /** * Displays extra table navigation. * * @param string $which Top or bottom placement. */ protected function extra_tablenav( $which ) { $status = ! empty( $_GET['status'] ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $order_by = ! empty( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $order = ! empty( $_GET['order'] ) ? sanitize_text_field( wp_unslash( $_GET['order'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?> __( 'All', 'surecart' ) ], ( new Review() )->getStatuses() ); } }