سیستم تیکت پشتیبانی وردپرس و ووکامرس
این کد یک سیستم تیکت پشتیبانی اختصاصی برای وردپرس و ووکامرس ایجاد میکند و نیازی به افزونه جداگانه ندارد.
با استفاده از شورتکد [support_tickets] کاربران میتوانند تیکت جدید ایجاد کنند، تیکتهای قبلی را مشاهده کنند و در صورت باز بودن تیکت، به آن پاسخ دهند.
امکان ارتباط تیکت با سفارش ووکامرس نیز وجود دارد و در صورت مرتبط بودن سفارش با فروشنده، مدیر میتواند فروشنده مربوطه را به تیکت دعوت کند. پیامهای کاربر، مدیر و فروشنده نیز با ظاهر متفاوت نمایش داده میشوند.
در بخش مدیریت وردپرس، مدیر میتواند مکالمات تیکت، وضعیت باز یا بسته بودن، کاربران دعوتشده و پاسخها را مدیریت کند. همچنین تیکتهایی که نیاز به پاسخ دارند در لیست مدیریت اولویت بیشتری میگیرند و تعداد تیکتهای منتظر پاسخ بهصورت اعلان نمایش داده میشود.
<?php
add_action('init','ticket_system_post_type');
function ticket_system_post_type(){
register_post_type('support_ticket',array(
'labels' => array(
'name' => 'تیکتها',
'singular_name' => 'تیکت',
'menu_name' => 'تیکتها',
),
'public'=>false,
'show_ui'=>true,
'menu_icon'=>'dashicons-sos',
'supports'=>array('title','author')
));
}
function get_ticket_user_display_name($user_id_or_role){
if($user_id_or_role == 'admin'){
$admin_user = get_user_by('email', get_option('admin_email'));
return $admin_user ? $admin_user->display_name : 'مدیر';
}
$user = get_userdata($user_id_or_role);
return $user ? $user->display_name : 'کاربر ناشناس';
}
function user_can_access_ticket($ticket_id, $user_id) {
$author_id = get_post_field('post_author', $ticket_id);
if ($author_id == $user_id) return true;
$assigned = get_post_meta($ticket_id, 'ticket_assigned_users', true);
if (is_array($assigned) && in_array($user_id, $assigned)) return true;
return current_user_can('manage_options');
}
function ticket_last_sender_is_user($ticket_id){
$messages = get_post_meta($ticket_id,'ticket_messages');
if(empty($messages)) return false;
$last = end($messages);
if(isset($last['user']) && $last['user'] != 'admin'){
return true;
}
return false;
}
add_action('template_redirect','ticket_process_forms');
function ticket_process_forms(){
if(!is_user_logged_in()) return;
$user_id = get_current_user_id();
if(isset($_POST['new_ticket'])){
if(!isset($_POST['new_ticket_nonce']) ||
!wp_verify_nonce($_POST['new_ticket_nonce'],'new_ticket_action')){
wp_die('درخواست نامعتبر است.');
}
$order = isset($_POST['ticket_order_id']) ? intval($_POST['ticket_order_id']) : 0;
$ticket_id = wp_insert_post(array(
'post_type'=>'support_ticket',
'post_title'=>sanitize_text_field($_POST['ticket_title']) . ($order ? " - سفارش #{$order}" : ''),
'post_status'=>'publish',
'post_author'=>$user_id
));
update_post_meta($ticket_id,'ticket_status','open');
add_post_meta($ticket_id,'ticket_messages',array(
'user'=>$user_id,
'message'=>sanitize_textarea_field($_POST['ticket_message']),
'date'=>current_time('mysql')
));
update_post_meta($ticket_id, 'ticket_assigned_users', array($user_id));
wp_redirect( add_query_arg('sent','1') );
exit;
}
if(isset($_POST['reply_ticket'])){
if(!isset($_POST['reply_ticket_nonce']) ||
!wp_verify_nonce($_POST['reply_ticket_nonce'],'reply_ticket_action')){
wp_die('درخواست نامعتبر است.');
}
$ticket_id = intval($_POST['ticket_id']);
if(!user_can_access_ticket($ticket_id, $user_id)){
wp_die('شما دسترسی به این تیکت ندارید.');
}
add_post_meta($ticket_id,'ticket_messages',array(
'user'=>$user_id,
'message'=>sanitize_textarea_field($_POST['reply']),
'date'=>current_time('mysql')
));
wp_redirect( add_query_arg('reply_sent','1') );
exit;
}
if(isset($_POST['close_ticket'])){
if(!isset($_POST['close_ticket_nonce']) ||
!wp_verify_nonce($_POST['close_ticket_nonce'],'close_ticket_action')){
wp_die('درخواست نامعتبر است.');
}
$ticket_id = intval($_POST['ticket_id']);
if(!user_can_access_ticket($ticket_id, $user_id)){
wp_die('شما دسترسی به این تیکت ندارید.');
}
update_post_meta($ticket_id,'ticket_status','closed');
wp_redirect( add_query_arg('closed','1') );
exit;
}
}
add_action('add_meta_boxes','ticket_admin_metabox');
function ticket_admin_metabox(){
add_meta_box(
'ticket_messages_box',
'مکالمه تیکت',
'ticket_messages_box_callback',
'support_ticket',
'normal',
'high'
);
}
function ticket_messages_box_callback($post){
$messages = get_post_meta($post->ID,'ticket_messages');
$status = get_post_meta($post->ID,'ticket_status',true);
$assigned = get_post_meta($post->ID, 'ticket_assigned_users', true) ?: array();
echo '<div style="max-height:300px;overflow:auto;background:#fafafa;padding:10px;border:1px solid #ddd;margin-bottom:15px;">';
foreach($messages as $msg){
$user_name = get_ticket_user_display_name($msg['user']);
$cls = $msg['user']=='admin' ? 'ticket-msg-admin' : (is_numeric($msg['user']) && $msg['user'] != get_post_field('post_author', $post->ID) ? 'ticket-msg-vendor' : 'ticket-msg-user');
echo "<p class='{$cls}' style='margin:8px 0;'><strong>{$user_name}</strong><br>".nl2br($msg['message'])."</p>";
}
echo '</div>';
if(!empty($assigned)){
echo '<div style="margin-bottom:10px;padding:8px;background:#e8f5e9;border-radius:5px;">';
echo '<strong>کاربران دعوتشده:</strong> ';
$names = array();
foreach($assigned as $uid){
$u = get_userdata($uid);
if($u) $names[] = $u->display_name;
}
echo implode('، ', $names);
echo '</div>';
}
$order_id = 0;
if(preg_match('/سفارش #(\d+)/', $post->post_title, $matches)){
$order_id = intval($matches[1]);
}
if($order_id){
$order = wc_get_order($order_id);
if($order){
$vendor_ids = array();
foreach($order->get_items() as $item){
$product = $item->get_product();
if($product){
$author_id = get_post_field('post_author', $product->get_id());
if($author_id) $vendor_ids[] = $author_id;
}
}
$vendor_ids = array_unique($vendor_ids);
foreach($vendor_ids as $vid){
if(!in_array($vid, $assigned)){
$v = get_userdata($vid);
if($v){
echo '<div style="padding:10px;background:#fff3e0;border:1px solid #ffe0b2;border-radius:4px;margin-bottom:10px;display:flex;justify-content:space-between;align-items:center;">';
echo '<span><strong>فروشنده سفارش:</strong> '.$v->display_name.'</span>';
echo '<button type="submit" name="invite_vendor" value="'.$vid.'" style="background:#ff9800;color:#fff;border:none;padding:6px 15px;border-radius:4px;cursor:pointer;">دعوت به تیکت</button>';
echo '</div>';
}
}
}
}
}
if($status!='closed'){ ?>
<textarea style="width:100%;height:100px" name="admin_reply"></textarea>
<p style="margin-top:10px;">
<label><input type="checkbox" name="close_ticket_admin"> بستن تیکت</label>
</p>
<?php } else { ?>
<p style="margin-top:10px;">
<label><input type="checkbox" name="reopen_ticket_admin"> باز کردن مجدد تیکت</label>
</p>
<?php }
wp_nonce_field('admin_ticket_reply','admin_ticket_nonce');
}
add_action('save_post_support_ticket','save_admin_ticket_reply');
function save_admin_ticket_reply($post_id){
if(!isset($_POST['admin_ticket_nonce'])) return;
if(!wp_verify_nonce($_POST['admin_ticket_nonce'],'admin_ticket_reply')) return;
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if(!current_user_can('edit_post',$post_id)) return;
if(isset($_POST['invite_vendor'])){
$assigned = get_post_meta($post_id, 'ticket_assigned_users', true) ?: array();
$vendor_id = intval($_POST['invite_vendor']);
if(!in_array($vendor_id, $assigned)){
$assigned[] = $vendor_id;
update_post_meta($post_id, 'ticket_assigned_users', $assigned);
}
}
if(!empty($_POST['admin_reply'])){
add_post_meta($post_id,'ticket_messages',array(
'user'=>'admin',
'message'=>sanitize_textarea_field($_POST['admin_reply']),
'date'=>current_time('mysql')
));
}
if(isset($_POST['close_ticket_admin'])){
update_post_meta($post_id,'ticket_status','closed');
}
if(isset($_POST['reopen_ticket_admin'])){
update_post_meta($post_id,'ticket_status','open');
}
}
add_action('admin_bar_menu', 'ticket_new_notification_admin_bar', 100);
function ticket_new_notification_admin_bar($wp_admin_bar) {
$user_id = get_current_user_id();
if(!$user_id) return;
$is_admin = current_user_can('manage_options');
$tickets = get_posts(array(
'post_type' => 'support_ticket',
'numberposts' => -1
));
$new_tickets = 0;
foreach($tickets as $ticket){
$status = get_post_meta($ticket->ID, 'ticket_status', true);
if($status == 'closed') continue;
$assigned = get_post_meta($ticket->ID, 'ticket_assigned_users', true) ?: array();
$can_see = $is_admin || in_array($user_id, $assigned);
if($can_see && ticket_last_sender_is_user($ticket->ID)){
$new_tickets++;
}
}
if($new_tickets > 0){
$wp_admin_bar->add_node(array(
'id' => 'new-ticket-notification',
'title' => '<span style="background:#d63638;color:#fff;padding:2px 8px;border-radius:4px;font-size:12px;font-weight:bold;">تیکت جدید دارید</span>',
'href' => $is_admin ? admin_url('edit.php?post_type=support_ticket') : home_url('/my-account'),
'meta' => array(
'title' => '',
'class' => 'new-ticket-notification-item',
'target' => '_blank'
)
));
}
}
add_action('wp_head', 'ticket_notification_styles');
add_action('admin_head', 'ticket_notification_styles');
function ticket_notification_styles(){ ?>
<style>
#wp-admin-bar-new-ticket-notification .ab-item {
background: transparent !important;
}
#wp-admin-bar-new-ticket-notification .ab-item:hover {
background: transparent !important;
}
</style>
<?php }
add_action('wp_head','ticket_system_styles');
function ticket_system_styles(){ ?>
<style>
.support-wrapper{font-family:iranyekan,sans-serif; background:#fafafa; padding: 5px; border-radius:5px;}
.dk-btn{display:inline-block !important;}
.dk-btn:hover{background:#c62828;}
.dk-input, textarea.dk-input{width:100%;padding:10px;border:1px solid #ccc;border-radius:5px;background:#fafafa;}
.dk-input::placeholder {color: #ccc;opacity: 1;}
.ticket-accordion{border:1px solid #e0e0e0;border-radius:5px;margin-top:5px;overflow:hidden;background:#fff;}
.ticket-header{padding:10px;cursor:pointer;background:#f5f5f5;font-size:15px;display:flex;justify-content:space-between;align-items:center;}
.ticket-content{display:none; padding: 10px; background:#fcfcfc;}
.ticket-msg{padding-bottom: 10px; margin-bottom:8px;}
.ticket-msg-admin{color:#DDA63A; padding: 10px; background:#fff; border-radius:10px; }
.ticket-msg-user{color:#808080; padding: 10px; background:#fff; border-radius:10px;}
.ticket-msg-vendor{color:#6FA8DC; padding: 10px; background:#fff; border-radius:10px;}
.status-open{color:#1565c0;font-size:12px;}
.status-closed{color:#c62828;font-size:12px;}
#new-ticket-box{display:none;}
</style>
<script>
document.addEventListener("DOMContentLoaded", function(){
document.getElementById("new-ticket-btn")?.addEventListener("click",function(){
let box = document.getElementById("new-ticket-box");
box.style.display = box.style.display=="block" ? "none" : "block";
});
document.querySelectorAll(".ticket-header").forEach(function(h){
h.addEventListener("click", function(){
let content = this.nextElementSibling;
let isOpen = content.style.display == "block";
document.querySelectorAll(".ticket-content").forEach(function(c){
c.style.display = "none";
});
if(!isOpen){
content.style.display = "block";
}
});
});
});
</script>
<?php }
add_shortcode('support_tickets','support_ticket_panel');
function support_ticket_panel(){
if(!is_user_logged_in()) return "برای ارسال تیکت ابتدا وارد شوید.";
$user_id=get_current_user_id();
$orders = wc_get_orders(array('customer_id'=>$user_id,'limit'=>-1));
$tickets = get_posts(array(
'post_type'=>'support_ticket',
'numberposts'=>-1
));
$accessible_tickets = array();
foreach($tickets as $ticket){
if(user_can_access_ticket($ticket->ID, $user_id)){
$accessible_tickets[] = $ticket;
}
}
usort($accessible_tickets,function($a,$b){
$a_user = ticket_last_sender_is_user($a->ID);
$b_user = ticket_last_sender_is_user($b->ID);
if($a_user == $b_user) return 0;
return $a_user ? -1 : 1;
});
ob_start(); ?>
<div class="support-wrapper">
<div style="display:flex;justify-content:space-between;align-items:center;border:1px solid #D9D9D9;border-radius:5px; background:#eeeeee; padding: 8px; ">
پشتیبانی / تیکت های من
<button class="dk-btn" id="new-ticket-btn">تیکت جدید</button>
</div>
<div id="new-ticket-box" class="ticket-content" style="margin: 5px 0; border:1px solid #eee;border-radius:5px;">
<form method="post">
<?php wp_nonce_field('new_ticket_action','new_ticket_nonce'); ?>
<input type="text" name="ticket_title" class="dk-input" placeholder="موضوع تیکت" required>
<select name="ticket_order_id" class="dk-input" style="margin-top:10px; border-radius:5px;">
<option value=""> مربوط به کدام سفارش؟ (اختیاری) </option>
<?php foreach($orders as $o){ echo '<option value="'.$o->get_id().'">سفارش #'.$o->get_id().'</option>'; } ?>
</select>
<textarea name="ticket_message" class="dk-input" style="height:120px;margin-top:10px;" placeholder="متن پیام" required></textarea>
<button name="new_ticket" class="dk-btn" style="margin-top:0px;">ارسال تیکت</button>
</form>
</div>
<?php foreach($accessible_tickets as $ticket){
$status=get_post_meta($ticket->ID,'ticket_status',true);
$status_text = $status=='closed' ? 'بسته' : 'باز';
$badge = $status=='closed'?'status-closed':'status-open';
?>
<div class="ticket-accordion">
<div class="ticket-header">
<span>
<?php
echo $ticket->post_title;
$messages = get_post_meta($ticket->ID,'ticket_messages');
$last = end($messages);
if($last){
$last_user_name = get_ticket_user_display_name($last['user']);
if($last['user'] == 'admin'){
echo " <span style='background:#4caf50;color:#fff;padding:2px 6px;border-radius:4px;font-size:11px;margin-right:6px;'>{$last_user_name} پاسخ داد</span>";
}else{
echo " <span style='background:#ef394e;color:#fff;padding:2px 6px;border-radius:4px;font-size:11px;margin-right:6px;'>{$last_user_name}</span>";
}
}
?>
</span>
<span class="<?php echo $badge; ?>"><?php echo $status_text; ?></span>
</div>
<div class="ticket-content">
<?php
$messages=get_post_meta($ticket->ID,'ticket_messages');
foreach($messages as $msg){
$user_name = get_ticket_user_display_name($msg['user']);
// تشخیص نوع فرستنده
if($msg['user'] == 'admin'){
$cls = 'ticket-msg-admin';
} elseif(is_numeric($msg['user']) && $msg['user'] != get_post_field('post_author', $ticket->ID)){
// اگه کاربر غیر از سازنده تیکت باشه => فروشنده
$cls = 'ticket-msg-vendor';
} else {
$cls = 'ticket-msg-user';
}
echo "<div class='ticket-msg {$cls}'><strong>{$user_name}</strong><br>".nl2br($msg['message'])."</div>";
}
if($status!='closed'){ ?>
<form method="post" style="margin-top:15px;">
<?php wp_nonce_field('reply_ticket_action','reply_ticket_nonce'); ?>
<input type="hidden" name="ticket_id" value="<?php echo $ticket->ID; ?>">
<textarea name="reply" class="dk-input" placeholder="پاسخ شما" style="margin-bottom:5px; height:100px;"></textarea>
<div style="display:flex; gap:5px; align-items:center;">
<button name="reply_ticket" class="dk-btn">ارسال پاسخ</button>
</div>
</form>
<?php } ?>
</div>
</div>
<?php } ?>
</div>
<?php return ob_get_clean();
}
add_action('admin_menu', 'add_ticket_notification_badge');
function add_ticket_notification_badge() {
global $menu;
$tickets = get_posts(array(
'post_type'=>'support_ticket',
'numberposts'=>-1
));
$need_reply = 0;
foreach($tickets as $ticket){
$status = get_post_meta($ticket->ID,'ticket_status',true);
if($status == 'closed') continue;
if(ticket_last_sender_is_user($ticket->ID)){
$need_reply++;
}
}
if ($need_reply > 0) {
foreach ($menu as $key => $value) {
if ($value[2] == 'edit.php?post_type=support_ticket') {
$menu[$key][0] .= " <span class='update-plugins count-$need_reply'><span class='plugin-count'>" . number_format_i18n($need_reply) . "</span></span>";
break;
}
}
}
}
function ticket_conversation_status($ticket_id){
$messages = get_post_meta($ticket_id,'ticket_messages');
if(empty($messages)) return 'unknown';
$last = end($messages);
if($last['user'] == 'admin'){
return 'answered';
}
return 'waiting';
}
add_action('pre_get_posts','ticket_admin_sorting');
function ticket_admin_sorting($query){
if(!is_admin()) return;
if(!$query->is_main_query()) return;
if($query->get('post_type') != 'support_ticket') return;
add_filter('posts_results','ticket_sort_results');
}
function ticket_sort_results($posts){
usort($posts,function($a,$b){
$a_status = ticket_conversation_status($a->ID);
$b_status = ticket_conversation_status($b->ID);
if($a_status == $b_status) return 0;
return $a_status == 'waiting' ? -1 : 1;
});
return $posts;
}
add_filter('manage_support_ticket_posts_columns','ticket_columns');
function ticket_columns($columns){
$new = array();
foreach($columns as $key=>$value){
$new[$key] = $value;
if($key == 'title'){
$new['ticket_status_conv'] = 'وضعیت مکالمه';
$new['ticket_assigned'] = 'کاربران دعوتشده';
}
}
return $new;
}
add_action('manage_support_ticket_posts_custom_column','ticket_column_content',10,2);
function ticket_column_content($column,$post_id){
if($column == 'ticket_status_conv'){
$status = ticket_conversation_status($post_id);
if($status == 'waiting'){
echo '<span style="color:#d63638;font-weight:bold;">در انتظار پاسخ</span>';
}else{
echo '<span style="color:#2e7d32;font-weight:bold;">پاسخ داده شده</span>';
}
}
if($column == 'ticket_assigned'){
$assigned = get_post_meta($post_id, 'ticket_assigned_users', true) ?: array();
$names = array();
foreach($assigned as $uid){
$u = get_userdata($uid);
if($u) $names[] = $u->display_name;
}
echo !empty($names) ? implode('، ', $names) : '—';
}
}