$code, 'event_id' => absint(wp_unslash($_POST['event_id'] ?? 0)), 'type' => sanitize_key(wp_unslash($_POST['type'] ?? ''))=='fixed'?'fixed':'percent', 'amount' => (float)sanitize_text_field(wp_unslash($_POST['amount'] ?? 0)), 'max_uses' => absint(wp_unslash($_POST['max_uses'] ?? 0)), 'expires' => trim(sanitize_text_field(wp_unslash($_POST['expires'] ?? '')))!=''?gmdate("Y-m-d 23:59:59",strtotime(sanitize_text_field(wp_unslash($_POST['expires'])))):null, 'active' => isset($_POST['active'])?1:0, ]; $edit_id = absint(wp_unslash($_POST['id'] ?? 0)); if ($code=='' || $data['amount']<=0) $message = __('Please provide a code and an amount greater than zero.','ticketdrum'); elseif ($edit_id>0) { $wpdb->update(TICKETDRUM_PROMOCODES_TABLE,$data,['id'=>$edit_id]); $message = __('Promo code updated.','ticketdrum'); } else { $data['added'] = gmdate("Y-m-d H:i:s"); if ($wpdb->insert(TICKETDRUM_PROMOCODES_TABLE,$data)===false) $message = __('Could not save - is that code already in use?','ticketdrum'); else $message = __('Promo code added.','ticketdrum'); } } elseif (sanitize_key(wp_unslash($_GET['action'] ?? ''))=='delete' && absint(wp_unslash($_GET['id'] ?? 0))>0 && check_admin_referer('ticketdrum_promocode_delete')) { $wpdb->delete(TICKETDRUM_PROMOCODES_TABLE,['id'=>(int)$_GET['id']]); $message = __('Promo code deleted.','ticketdrum'); } $editing = false; if (sanitize_key(wp_unslash($_GET['action'] ?? ''))=='edit' && absint(wp_unslash($_GET['id'] ?? 0))>0) $editing = $wpdb->get_row($wpdb->prepare("SELECT * FROM %i WHERE id=%d",TICKETDRUM_PROMOCODES_TABLE,(int)$_GET['id']),ARRAY_A); $codes = $wpdb->get_results($wpdb->prepare("SELECT * FROM %i ORDER BY added DESC",TICKETDRUM_PROMOCODES_TABLE),ARRAY_A); $events = $wpdb->get_results($wpdb->prepare("SELECT id,title FROM %i WHERE status!='archived' ORDER BY added DESC",TICKETDRUM_EVENTS_TABLE),ARRAY_A); ?>


'.esc_html($message).'

':'';?>
0?' / '.(int)$c['max_uses']:'';?> '.esc_html__('off','ticketdrum').'':($expired?''.esc_html__('expired','ticketdrum').'':''.esc_html__('active','ticketdrum').'');?>

false,'error'=>__('Please enter a promo code','ticketdrum')]; $promo = $wpdb->get_row($wpdb->prepare("SELECT * FROM %i WHERE code=%s AND active=1 LIMIT 1",TICKETDRUM_PROMOCODES_TABLE,$code),ARRAY_A); if (!$promo) return ['result'=>false,'error'=>__('That promo code isn\'t valid','ticketdrum')]; if ($promo['expires'] && strtotime($promo['expires'])false,'error'=>__('That promo code has expired','ticketdrum')]; if ($promo['max_uses']>0 && $promo['used_count']>=$promo['max_uses']) return ['result'=>false,'error'=>__('That promo code has been fully redeemed','ticketdrum')]; if ($promo['event_id']>0 && count($event_ids)>0 && !in_array((int)$promo['event_id'],array_map('intval',$event_ids))) return ['result'=>false,'error'=>__('That code doesn\'t apply to the tickets in your basket','ticketdrum')]; return ['result'=>true,'promo'=>$promo]; } //Increment used_count once a purchase using a code is actually paid function ticketdrum_promocode_redeem($purchase_id) { global $wpdb; $ticket_data = json_decode($wpdb->get_var($wpdb->prepare("SELECT ticket_data FROM %i WHERE id=%d",TICKETDRUM_PURCHASES_TABLE,$purchase_id)),true); if (!isset($ticket_data['promo_code']) || $ticket_data['promo_code']=='') return; $wpdb->query($wpdb->prepare("UPDATE %i SET used_count=used_count+1 WHERE code=%s",TICKETDRUM_PROMOCODES_TABLE,$ticket_data['promo_code'])); }