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->items = []; return; } $this->set_pagination_args( [ 'total_items' => $query->pagination->count, 'per_page' => $this->get_items_per_page( 'coupons' ), ] ); $this->items = $query->data; } /** * Search form. * * @return void */ public function search() { ?>
search_box( __( 'Search Coupons', 'surecart' ), 'coupon' ); ?>
'', 'name' => __( 'Name', 'surecart' ), 'promotion_code' => __( 'Code', 'surecart' ), 'price' => __( 'Price', 'surecart' ), 'usage' => __( 'Usage', 'surecart' ), 'date' => __( 'Date', 'surecart' ), ], parent::get_columns() ); } /** * Displays the checkbox column. * * @param Product $product The product model. */ public function column_cb( $product ) { ?> array( 'title', false ) ); } /** * Get the table data * * @return Object */ private function table_data() { return Coupon::where( [ 'archived' => $this->getArchiveStatus(), 'query' => $this->get_search_query(), ] )->with( [ 'promotions' ] ) ->paginate( [ 'per_page' => $this->get_items_per_page( 'coupons' ), 'page' => $this->get_pagenum(), ] ); } /** * Handle the price column. * * @param \SureCart\Models\Coupon $coupon Coupon model. * * @return string */ public function column_price( $coupon ) { ob_start(); // phpcs:ignore echo $this->get_price_string( $coupon ?? false ); // this is already escaped. ?>
get_duration_string( $coupon ?? false ) ); ?>
max_redemptions ?? '∞'; ob_start(); echo \esc_html( "$coupon->times_redeemed / $max" ); ?>
get_expiration_string( $coupon ) ); ?>
redeem_by ) { return ''; } // translators: coupon expiration date. return sprintf( __( 'Valid until %s', 'surecart' ), $coupon->redeem_by_date ); } /** * Get the price string for the coupon. * * @param \SureCart\Models\Coupon|null $coupon Coupon model. * @return string */ public function get_price_string( $coupon = null ) { if ( ! $coupon || empty( $coupon->duration ) ) { return; } if ( ! empty( $coupon->percent_off ) ) { $percent = sprintf( '%d%%', (int) $coupon->percent_off ); // translators: %s is the discount percentage (e.g. "10%"). return esc_html( sprintf( __( '%s off', 'surecart' ), $percent ) ); } if ( ! empty( $coupon->amount_off ) ) { return $coupon->human_discount; } return esc_html__( 'No discount.', 'surecart' ); } /** * Get the duration string * * @param Coupon|boolean $coupon Coupon object. * @return string|void; */ public function get_duration_string( $coupon = '' ) { if ( ! $coupon || empty( $coupon->duration ) ) { return; } if ( 'forever' === $coupon->duration ) { return __( 'Forever', 'surecart' ); } if ( 'repeating' === $coupon->duration ) { // translators: number of months. return sprintf( __( 'For %d months', 'surecart' ), $coupon->duration_in_months ?? 1 ); } return __( 'Once', 'surecart' ); } /** * Handle the status * * @param \SureCart\Models\Price $product Product model. * * @return string */ public function column_status( $coupon ) { // TODO: Add Badge. return $coupon->expired ? __( 'Expired', 'surecart' ) : __( 'Active', 'surecart' ); } protected function extra_tablenav( $which ) { if ( 'top' === $which ) { return $this->views(); } } /** * Name of the coupon * * @param \SureCart\Models\Promotion $promotion Promotion model. * * @return string */ public function column_name( $coupon ) { ob_start(); ?> name ); ?> row_actions( [ 'edit' => '' . __( 'Edit', 'surecart' ) . '', ], ); return ob_get_clean(); } /** * Name of the coupon * * @param \SureCart\Models\Promotion $promotion Promotion model. * * @return string */ public function column_promotion_code( $coupon ) { if ( empty( $coupon->promotions->data[0]->code ) ) { return __( 'No code specified', 'surecart' ); } $and = ''; if ( $coupon->promotions->pagination->count > 1 ) { $coupon_count = $coupon->promotions->pagination->count - 1; // translators: %d is the number of additional coupons. $and = sprintf( __( '+ %d more', 'surecart' ), number_format_i18n( $coupon_count ) ); } return '' . sanitize_text_field( $coupon->promotions->data[0]->code ) . ' ' . $and; } /** * Toggle archive action link and text. * * @param \SureCart\Models\Product $product Product model. * @return string */ public function action_toggle_archive( $coupon ) { $text = $coupon->archived ? __( 'Un-Archive', 'surecart' ) : __( 'Archive', 'surecart' ); $confirm_message = $coupon->archived ? __( 'Are you sure you want to restore this coupon? This will be be available to purchase.', 'surecart' ) : __( 'Are you sure you want to archive this coupon? This will be unavailable for purchase.', 'surecart' ); $link = \SureCart::getUrl()->toggleArchive( 'coupon', $coupon->id ); return sprintf( '%4s', esc_attr( $confirm_message ), esc_url( $link ), esc_attr__( 'Toggle Coupon Archive', 'surecart' ), esc_html( $text ) ); } /** * Allows you to sort the data by the variables set in the $_GET. * * @return Mixed */ private function sort_data( $a, $b ) { // Set defaults. $orderby = 'title'; $order = 'asc'; // If orderby is set, use this as the sort column. if ( ! empty( $_GET['orderby'] ) ) { $orderby = sanitize_text_field( wp_unslash( $_GET['orderby'] ) ); } // If order is set use this as the order. if ( ! empty( $_GET['order'] ) ) { $order = sanitize_text_field( wp_unslash( $_GET['order'] ) ); } $result = strcmp( $a[ $orderby ], $b[ $orderby ] ); if ( 'asc' === $order ) { return $result; } return -$result; } /** * @global string $comment_status * * @return array */ protected function get_bulk_actions() { return false; global $comment_status; $actions = array(); if ( in_array( $comment_status, array( 'all', 'approved' ), true ) ) { $actions['unapprove'] = __( 'Unapprove', 'surecart' ); } if ( in_array( $comment_status, array( 'all', 'moderated' ), true ) ) { $actions['approve'] = __( 'Approve', 'surecart' ); } if ( in_array( $comment_status, array( 'all', 'moderated', 'approved', 'trash' ), true ) ) { $actions['spam'] = _x( 'Mark as spam', 'comment', 'surecart' ); } if ( 'trash' === $comment_status ) { $actions['untrash'] = __( 'Restore', 'surecart' ); } elseif ( 'spam' === $comment_status ) { $actions['unspam'] = _x( 'Not spam', 'comment', 'surecart' ); } if ( in_array( $comment_status, array( 'trash', 'spam' ), true ) || ! EMPTY_TRASH_DAYS ) { $actions['delete'] = __( 'Delete permanently', 'surecart' ); } else { $actions['trash'] = __( 'Move to Trash', 'surecart' ); } return $actions; } }