| Server IP : 185.143.233.238 / Your IP : 185.215.232.195 Web Server : LiteSpeed System : Linux saman.shetabanhost.com 4.18.0-553.121.1.lve.el8.x86_64 #1 SMP Thu Apr 30 16:40:41 UTC 2026 x86_64 User : behrakir ( 1361) PHP Version : 8.1.34 Disable Function : symlink,shell_exec,show_source,exec,popen,system,passthru,proc_open,proc_close,pcntl_exec,mail MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/behrakir/khanomam.com/wp-content/plugins/wp-score-gamification/app/classes/parts/ |
Upload File : |
<?php namespace Avans\parts;
class coupons
{
private $data;
public function __construct(array $data)
{
$this->data = $data;
}
public function create($data)
{
$status = false;
$code = '';
do {
$code = $this->generate_code((int)$data['char'],$data['prefix'], $data['suffix']);
if ($this->is_valid_coupon($code)) {
$status = true;
};
} while (!$status);
$this->insert_coupon($code);
return $code;
}
private function generate_code(int $char, string $prefix = null, string $suffix = null)
{
$coupon_code = wp_generate_password($char, false);
if (isset($prefix)) {
$coupon_code = $prefix.$coupon_code;
}
if (isset($suffix)) {
$coupon_code = $coupon_code.$suffix;
}
return $coupon_code;
}
private function is_valid_coupon($coupon_code)
{
global $wpdb;
$sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1;", $coupon_code);
if (empty($wpdb->get_var($sql))) {
return true;
}
return false;
}
private function insert_coupon($code)
{
$arg = array(
'post_title' => $code,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
);
$coupon_id = wp_insert_post($arg);
$this->set_meta_coupon($coupon_id);
}
private function set_meta_coupon($coupon_id)
{
if (!empty($this->data['include_product']) && isset($this->data['include_product'])) {
$product_ids = implode(',', $this->data['include_product']);
update_post_meta($coupon_id, 'product_ids', $product_ids);
}
if (!empty($this->data['exclude_product']) && isset($this->data['exclude_product'])) {
$product_ids = implode(',', $this->data['exclude_product']);
update_post_meta($coupon_id, 'exclude_product_ids', $product_ids);
}
if (!empty($this->data['include_category']) && isset($this->data['include_category'])) {
update_post_meta($coupon_id, 'product_categories', $this->data['include_category']);
}
if (!empty($this->data['exclude_category']) && isset($this->data['exclude_category'])) {
update_post_meta($coupon_id, 'exclude_product_categories', $this->data['exclude_category']);
}
if (!empty($this->data['email']) && isset($this->data['email'])) {
update_post_meta($coupon_id, 'customer_email', $this->data['email']);
}
if (!empty($this->data['type']) && isset($this->data['type'])) {
update_post_meta($coupon_id, 'discount_type', $this->data['type']);
}
if ( !empty($this->data['min_price']) && isset($this->data['min_price']) ) {
update_post_meta($coupon_id, 'minimum_amount', $this->data['min_price']);
}
if ( !empty($this->data['max_price']) && isset($this->data['max_price']) ) {
update_post_meta($coupon_id, 'maximum_amount', $this->data['max_price']);
}
if ( !empty($this->data['max_discount']) && isset($this->data['max_discount']) ) {
update_post_meta($coupon_id, 'max_discount', $this->data['max_discount']);
}
if(!empty($this->data['exclude_sale_product']) && isset($this->data['exclude_sale_product']) && $this->data['exclude_sale_product']){
update_post_meta($coupon_id, 'exclude_sale_items', 'yes');
}
update_post_meta($coupon_id, 'free_shipping', 'no');
update_post_meta($coupon_id, 'coupon_amount', $this->data['amount']);
update_post_meta($coupon_id, 'individual_use', 'yes');
update_post_meta($coupon_id, 'expiry_date', $this->get_expire_date());
update_post_meta($coupon_id, 'usage_limit', '1');
update_post_meta($coupon_id, 'apply_before_tax', 'yes');
update_post_meta($coupon_id, 'admin_coupons_enabled_for_vendor', 'yes');
update_post_meta($coupon_id, 'coupon_commissions_type', 'default');
update_post_meta($coupon_id, 'admin_shared_coupon_type', 'percentage');
}
private function get_expire_date()
{
return date('Y-m-d', strtotime('+'.$this->data['expire_date'].' days'));
}
}