# sellkit/1.3.2/includes/admin/class-custom-tables.php

SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster, version 1.3.2. 156 lines.

- Page: https://pluginprobe.com/plugins/sellkit/1.3.2/code/includes/admin/class-custom-tables.php
- Raw: https://pluginprobe.com/plugins/sellkit/1.3.2/raw/includes/admin/class-custom-tables.php
- Modified: 2022-10-31T08:54:32+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/sellkit/1.3.2/code/includes/admin/class-custom-tables.php#L10-L20`.

```php
<?php

use Sellkit\Database;

defined( 'ABSPATH' ) || die();

/**
 * Components class.
 *
 * @since 1.1.0
 */
class Sellkit_Custom_Tables {

	const CUSTOM_TABLE_PER_PAGE = 20;

	/**
	 * Class instance.
	 *
	 * @since 1.1.0
	 * @var Sellkit_Custom_Tables
	 */
	private static $instance = null;

	/**
	 * Get a class instance.
	 *
	 * @since 1.1.0
	 *
	 * @return Sellkit_Custom_Tables Class instance.
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Class constructor.
	 *
	 * @since 1.1.0
	 */
	public function __construct() {
		add_action( 'wp_ajax_sellkit_get_applied_discount', [ $this, 'get_applied_discounts' ] );
		add_action( 'wp_ajax_sellkit_get_generated_coupons', [ $this, 'get_generated_coupons' ] );
	}

	/**
	 * Gets applied discounts.
	 *
	 * @since 1.1.0
	 */
	public function get_applied_discounts() {
		check_ajax_referer( 'sellkit', 'nonce' );

		$page = sellkit_htmlspecialchars( INPUT_GET, 'page' );
		$id   = sellkit_htmlspecialchars( INPUT_GET, 'id' );

		if ( empty( $page ) ) {
			$page = 1;
		}

		$page_index = ( $page - 1 ) * self::CUSTOM_TABLE_PER_PAGE;
		$limit      = self::CUSTOM_TABLE_PER_PAGE;

		global $wpdb;

		$sellkit_prefix = Database::DATABASE_PREFIX;
		$discount_table = "{$wpdb->prefix}{$sellkit_prefix}applied_discount";

		// phpcs:disable
		$prepared_query = $wpdb->prepare( "SELECT $discount_table.*, {$wpdb->prefix}users.ID as user_id  FROM {$discount_table}
		LEFT JOIN {$wpdb->prefix}users on {$discount_table}.email = {$wpdb->prefix}users.user_email
		where {$discount_table}.discount_id = %d order by {$discount_table}.id desc limit %d , %d;",
			$id,
			$page_index,
			$limit
		);

		$prepared_total_query = $wpdb->prepare( "SELECT count(*) as total_discounts FROM {$wpdb->prefix}{$sellkit_prefix}applied_discount where discount_id = %d;",
			$id
		);

		$discounts = $wpdb->get_results(
			$prepared_query,
			ARRAY_A );

		$total = $wpdb->get_results(
			$prepared_total_query,
			ARRAY_A );

		$total_discount_num = ! empty( $total[0]['total_discounts'] ) ? $total[0]['total_discounts'] : '';
		// phpcs:enable

		wp_send_json_success( [
			'discounts' => $discounts,
			'total' => $total_discount_num,
			'max_item_num' => self::CUSTOM_TABLE_PER_PAGE,
		] );
	}

	/**
	 * Gets generated coupons.
	 *
	 * @since 1.1.0
	 */
	public function get_generated_coupons() {
		check_ajax_referer( 'sellkit', 'nonce' );

		$page = sellkit_htmlspecialchars( INPUT_GET, 'page' );
		$id   = sellkit_htmlspecialchars( INPUT_GET, 'id' );

		$args = [
			'post_type' => 'shop_coupon',
			'post_status' => 'publish',
			'posts_per_page' => self::CUSTOM_TABLE_PER_PAGE,
			'paged' => sanitize_text_field( $page ),
			'orderby' => 'ID',
			'order' => 'DESC',
			'meta_key'   => 'sellkit_personalised_coupon_rule', // phpcs:ignore
			'meta_value' => sanitize_text_field( $id ), // phpcs:ignore
		];

		$query = new WP_Query( $args );

		$coupons = [];

		foreach ( $query->posts as $post ) {
			$expiry_date    = get_post_meta( $post->ID, 'date_expires', true );
			$customer_email = get_post_meta( $post->ID, 'customer_email', true );
			$usage_limit    = get_post_meta( $post->ID, 'usage_limit', true );
			$usage_count    = get_post_meta( $post->ID, 'usage_count', true );

			$post->customer    = ! empty( $customer_email ) ? $customer_email : '';
			$post->usage_limit = ! empty( $usage_limit ) ? $usage_limit : '';
			$post->usage_count = ! empty( $usage_count ) ? $usage_count : '';
			$post->created_at  = date_i18n( 'Y/m/d h:i A', strtotime( $post->post_date ) );

			if ( ! empty( $expiry_date ) ) {
				$post->expiry_date = round( ( get_post_meta( $post->ID, 'date_expires', true ) - time() ) / 86400 );
			}

			$coupons[] = $post;
		}

		wp_send_json_success( [
			'coupons' => $coupons,
			'max_item_num' => self::CUSTOM_TABLE_PER_PAGE,
			'total' => $query->found_posts,
		] );
	}
}

Sellkit_Custom_Tables::get_instance();

```
