| 1 |
<?php |
| 2 |
|
| 3 |
use Sellkit\Database; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || die(); |
| 6 |
|
| 7 |
/** |
| 8 |
* Components class. |
| 9 |
* |
| 10 |
* @since 1.1.0 |
| 11 |
*/ |
| 12 |
class Sellkit_Custom_Tables { |
| 13 |
|
| 14 |
const CUSTOM_TABLE_PER_PAGE = 20; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class instance. |
| 18 |
* |
| 19 |
* @since 1.1.0 |
| 20 |
* @var Sellkit_Custom_Tables |
| 21 |
*/ |
| 22 |
private static $instance = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* Get a class instance. |
| 26 |
* |
| 27 |
* @since 1.1.0 |
| 28 |
* |
| 29 |
* @return Sellkit_Custom_Tables Class instance. |
| 30 |
*/ |
| 31 |
public static function get_instance() { |
| 32 |
if ( null === self::$instance ) { |
| 33 |
self::$instance = new self(); |
| 34 |
} |
| 35 |
|
| 36 |
return self::$instance; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Class constructor. |
| 41 |
* |
| 42 |
* @since 1.1.0 |
| 43 |
*/ |
| 44 |
public function __construct() { |
| 45 |
add_action( 'wp_ajax_sellkit_get_applied_discount', [ $this, 'get_applied_discounts' ] ); |
| 46 |
add_action( 'wp_ajax_sellkit_get_generated_coupons', [ $this, 'get_generated_coupons' ] ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Gets applied discounts. |
| 51 |
* |
| 52 |
* @since 1.1.0 |
| 53 |
*/ |
| 54 |
public function get_applied_discounts() { |
| 55 |
check_ajax_referer( 'sellkit', 'nonce' ); |
| 56 |
|
| 57 |
$page = sellkit_htmlspecialchars( INPUT_GET, 'page' ); |
| 58 |
$id = sellkit_htmlspecialchars( INPUT_GET, 'id' ); |
| 59 |
|
| 60 |
if ( empty( $page ) ) { |
| 61 |
$page = 1; |
| 62 |
} |
| 63 |
|
| 64 |
$page_index = ( $page - 1 ) * self::CUSTOM_TABLE_PER_PAGE; |
| 65 |
$limit = self::CUSTOM_TABLE_PER_PAGE; |
| 66 |
|
| 67 |
global $wpdb; |
| 68 |
|
| 69 |
$sellkit_prefix = Database::DATABASE_PREFIX; |
| 70 |
$discount_table = "{$wpdb->prefix}{$sellkit_prefix}applied_discount"; |
| 71 |
|
| 72 |
// phpcs:disable |
| 73 |
$prepared_query = $wpdb->prepare( "SELECT $discount_table.*, {$wpdb->prefix}users.ID as user_id FROM {$discount_table} |
| 74 |
LEFT JOIN {$wpdb->prefix}users on {$discount_table}.email = {$wpdb->prefix}users.user_email |
| 75 |
where {$discount_table}.discount_id = %d order by {$discount_table}.id desc limit %d , %d;", |
| 76 |
$id, |
| 77 |
$page_index, |
| 78 |
$limit |
| 79 |
); |
| 80 |
|
| 81 |
$prepared_total_query = $wpdb->prepare( "SELECT count(*) as total_discounts FROM {$wpdb->prefix}{$sellkit_prefix}applied_discount where discount_id = %d;", |
| 82 |
$id |
| 83 |
); |
| 84 |
|
| 85 |
$discounts = $wpdb->get_results( |
| 86 |
$prepared_query, |
| 87 |
ARRAY_A ); |
| 88 |
|
| 89 |
$total = $wpdb->get_results( |
| 90 |
$prepared_total_query, |
| 91 |
ARRAY_A ); |
| 92 |
|
| 93 |
$total_discount_num = ! empty( $total[0]['total_discounts'] ) ? $total[0]['total_discounts'] : ''; |
| 94 |
// phpcs:enable |
| 95 |
|
| 96 |
wp_send_json_success( [ |
| 97 |
'discounts' => $discounts, |
| 98 |
'total' => $total_discount_num, |
| 99 |
'max_item_num' => self::CUSTOM_TABLE_PER_PAGE, |
| 100 |
] ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Gets generated coupons. |
| 105 |
* |
| 106 |
* @since 1.1.0 |
| 107 |
*/ |
| 108 |
public function get_generated_coupons() { |
| 109 |
check_ajax_referer( 'sellkit', 'nonce' ); |
| 110 |
|
| 111 |
$page = sellkit_htmlspecialchars( INPUT_GET, 'page' ); |
| 112 |
$id = sellkit_htmlspecialchars( INPUT_GET, 'id' ); |
| 113 |
|
| 114 |
$args = [ |
| 115 |
'post_type' => 'shop_coupon', |
| 116 |
'post_status' => 'publish', |
| 117 |
'posts_per_page' => self::CUSTOM_TABLE_PER_PAGE, |
| 118 |
'paged' => sanitize_text_field( $page ), |
| 119 |
'orderby' => 'ID', |
| 120 |
'order' => 'DESC', |
| 121 |
'meta_key' => 'sellkit_personalised_coupon_rule', // phpcs:ignore |
| 122 |
'meta_value' => sanitize_text_field( $id ), // phpcs:ignore |
| 123 |
]; |
| 124 |
|
| 125 |
$query = new WP_Query( $args ); |
| 126 |
|
| 127 |
$coupons = []; |
| 128 |
|
| 129 |
foreach ( $query->posts as $post ) { |
| 130 |
$expiry_date = get_post_meta( $post->ID, 'date_expires', true ); |
| 131 |
$customer_email = get_post_meta( $post->ID, 'customer_email', true ); |
| 132 |
$usage_limit = get_post_meta( $post->ID, 'usage_limit', true ); |
| 133 |
$usage_count = get_post_meta( $post->ID, 'usage_count', true ); |
| 134 |
|
| 135 |
$post->customer = ! empty( $customer_email ) ? $customer_email : ''; |
| 136 |
$post->usage_limit = ! empty( $usage_limit ) ? $usage_limit : ''; |
| 137 |
$post->usage_count = ! empty( $usage_count ) ? $usage_count : ''; |
| 138 |
$post->created_at = date_i18n( 'Y/m/d h:i A', strtotime( $post->post_date ) ); |
| 139 |
|
| 140 |
if ( ! empty( $expiry_date ) ) { |
| 141 |
$post->expiry_date = round( ( get_post_meta( $post->ID, 'date_expires', true ) - time() ) / 86400 ); |
| 142 |
} |
| 143 |
|
| 144 |
$coupons[] = $post; |
| 145 |
} |
| 146 |
|
| 147 |
wp_send_json_success( [ |
| 148 |
'coupons' => $coupons, |
| 149 |
'max_item_num' => self::CUSTOM_TABLE_PER_PAGE, |
| 150 |
'total' => $query->found_posts, |
| 151 |
] ); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
Sellkit_Custom_Tables::get_instance(); |
| 156 |
|