Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adds a shortcode to display the support tickets list and single ticket in front end
<?php
/*
Plugin Name: Front end upfront support tickets
Plugin URI: https://premium.wpmudev.org/
Description: Adds a shortcode to display the support tickets list and single ticket in front end
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/
if( ! class_exists( 'WPMUDEV_Inc_Support_Upfront_Front' ) ){
class WPMUDEV_Inc_Support_Upfront_Front{
private static $_instance = null;
private static $_tickets = null;
private static $_total_pages = null;
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new WPMUDEV_Inc_Support_Upfront_Front();
}
return self::$_instance;
}
private function __construct(){
add_shortcode( 'support_system_upfront_tickets_front', array( $this, 'get_upfront_tickets' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'set_front_stylesheet' ), 10 );
add_filter( 'query_vars', array( $this, 'set_query_vars' ), 10 );
}
public function get_upfront_tickets( $atts, $content ){
if( ! is_user_logged_in() ){
return;
}
global $wpdb, $current_site;
$current_site_id = ! empty ( $current_site ) ? $current_site->id : 1;
$user = wp_get_current_user();
$allowed_roles = array('editor', 'administrator', 'author');
if( ! array_intersect($allowed_roles, $user->roles ) ){
$this->error_message();
}
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$atts = shortcode_atts( array(
'per_page' => get_option( 'posts_per_page' ),
'page' => $paged,
'status' => 'all',
'view_by_superadmin' => null,
'blog_id' => false,
'user_in' => false,
'category' => false,
'priority' => false,
'site_id' => $current_site_id,
'count' => false,
'orderby' => 'ticket_updated',
'order' => 'desc',
's' => false
), $atts, 'get_upfront_tickets' );
self::$_tickets = incsub_support_get_tickets( $atts );
$found_tickets = incsub_support_get_tickets_count( $atts );
self::$_total_pages = ceil( $found_tickets / $atts[ 'per_page' ] );
if( get_query_var( 'tid' ) == '' ){
return $this->ticket_list_template();
}
$ticket_id = (int) get_query_var( 'tid' );
return $this->ticket_single_template( $ticket_id );
}
public function ticket_single_template( $ticket_id ){
$ticket = incsub_support_get_ticket( $ticket_id );
ob_start();
?>
<div id="support-system">
<div id="support-system-single-ticket">
<?php if ( ! empty($ticket ) ): ?>
<div class="support-system-ticket row <?php echo esc_attr( $this->ticket_classes( $ticket ) ); ?>" id="support-system-ticket-<?php echo $ticket->ticket_id; ?>">
<div class="large-12 columns">
<h1 class="text-center support-system-ticket-title"><?php echo esc_attr( $ticket->title ); ?></h1>
<?php if ( incsub_support_is_ticket_closed( $ticket->ticket_id ) ): ?>
<div data-alert class="alert-box alert">
<?php _e( 'This ticket is closed', INCSUB_SUPPORT_LANG_DOMAIN ); ?>
</div>
<?php endif; ?>
<ul class="row">
<li class="small-3 large-2 columns">
<?php echo get_avatar( $ticket->user_id, 96 ); ?><br/>
</li>
<li class="small-9 large-10 columns">
<ul class="row inline-list support-system-ticket-meta">
<li class="first"><?php echo $this->ticket_author( $ticket ); ?></li>
<li><?php echo $this->ticket_updated_date( $ticket ); ?></li>
</ul>
<div class="row support-system-ticket-message">
<?php echo $ticket->message; ?>
</div>
<?php $attachments = $this->get_the_ticket_attachments( $ticket ); ?>
<?php if ( ! empty( $attachments ) ): ?>
<div class="row support-system-ticket-attachments">
<h5><?php _e( 'Attachments', INCSUB_SUPPORT_LANG_DOMAIN ); ?></h5>
<ul>
<?php foreach ( $attachments as $attachment ): ?>
<li><a href="<?php echo esc_url( $attachment ); ?>" title="<?php printf( esc_attr__( 'Download %s attachment', INCSUB_SUPPORT_LANG_DOMAIN ), basename( $attachment ) ); ?>"><?php echo basename( $attachment ); ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
</li>
</ul>
</div>
</div>
<hr>
<div class="row">
<div class="support-system-ticket-replies large-8 columns">
<?php $this->ticket_replies( $ticket ); ?>
</div>
<div class="large-4 columns">
<div class="row">
<?php if ( ! incsub_support_is_staff() ): ?>
<?php
echo incsub_support_widget(
array( 'class' => 'panel support-system-ticket-details large-12 columns', 'title' => __( 'Ticket Details', INCSUB_SUPPORT_LANG_DOMAIN ) ),
'incsub_support_the_ticket_details_box'
);
?>
<?php else: ?>
<?php
echo incsub_support_widget(
array( 'class' => 'panel support-system-ticket-details support-system-staff-box large-12 columns', 'title' => __( 'Edit Ticket Details', INCSUB_SUPPORT_LANG_DOMAIN ) ),
'incsub_support_the_staff_box',
array( 'submit_class' => 'button expand' )
);
?>
<?php endif; ?>
<?php if ( incsub_support_current_user_can( 'close_ticket', incsub_support_get_the_ticket_id() ) ): ?>
<?php
echo incsub_support_widget(
array( 'class' => 'panel support-system-close-ticket large-12 columns' ),
'incsub_support_the_open_close_box',
array( 'submit_class' => 'button tiny' )
);
?>
<?php endif; ?>
</div>
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php
return ob_get_clean();
}
public function ticket_list_template(){
$tickets = self::$_tickets;
ob_start();
?>
<div style="clear:both; float: none;"></div>
<div id="support-system">
<?php if ( ! empty( $tickets ) ): ?>
<ul class="support-system-tickets-list">
<?php foreach ( $tickets as $ticket ): ?>
<ul class="support-system-ticket row <?php echo esc_attr( $this->ticket_classes( $ticket ) ); ?>" id="support-ticket-<?php echo $ticket->ticket_id; ?>">
<li class="small-2 large-1 columns support-system-ticket-author-data">
<?php echo get_avatar( $ticket->user_id , 48 ); ?>
</li>
<li class="small-10 large-11 columns support-system-ticket-content">
<h2 class="support-system-ticket-title">
<a href="<?php echo esc_url( incsub_support_get_the_ticket_permalink( $ticket->ticket_id ) ); ?>" title="<?php echo esc_attr( $ticket->title ); ?>">
<?php echo $ticket->title; ?>
</a>
<?php
$this->ticket_badges( $ticket,
array(
'badge_base_class' => 'label',
'replies_badge_class' => 'secondary',
'status_badge_class' => 'success'
)
);
?>
</h2>
<div class="support-system-ticket-message">
<?php echo $this->trim( $ticket->message ); ?>
</div>
<ul class="inline-list support-system-ticket-meta">
<li class="support-system-byauthor">
<span>
<?php echo sprintf( __( 'by <strong>%s</strong>', INCSUB_SUPPORT_LANG_DOMAIN ), $this->ticket_author( $ticket ) ); ?>
</span>
</li>
<li class="support-system-lastreply">
<span>
<?php echo $this->ticket_updated_date( $ticket ); ?>
</span>
</li>
<li class="support-system-category">
<span class="support-system-tag">
<?php echo $this->ticket_category_link( $ticket ); ?>
</span>
</li>
</ul>
</li>
</ul>
<?php endforeach; ?>
</ul>
<div class="row">
<div class="large-12 columns">
<?php
$big = 999999999; // need an unlikely integer
$translated = __( 'Page', 'plugin-textdomain' ); // Supply translatable string
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => self::$_total_pages,
'before_page_number' => '<span class="screen-reader-text">'.$translated.' </span>'
) );
?>
</div>
</div>
<?php else: ?>
<h2 class="support-system-no-tickets-upfront">
<?php _e( 'No tickets!!' ); ?>
</h2>
<?php endif; ?>
</div>
<?php
return ob_get_clean();
}
function ticket_classes( $ticket ) {
$class = array();
$class[] = "support-system-ticket-priority-" . $ticket->ticket_priority;
$class[] = "support-system-ticket-category-" . $ticket->cat_id;
$class[] = "support-system-ticket-staff-" . $ticket->admin_id;
$class[] = "support-system-ticket-status-" . $ticket->ticket_status;
/**
* Filters the HTML ticket class in the frontend
*
* @param String $classes Ticket HTML classes
*/
return apply_filters( 'support_system_the_ticket_class', implode( ' ', $class ) );
}
public function ticket_badges( $ticket, $args = array() ) {
$defaults = array(
'badge_base_class' => 'support-system-badge',
'replies_badge_class' => 'support-system-replies-badge',
'status_badge_class' => 'support-system-closed-badge'
);
$args = wp_parse_args( $args, $defaults );
extract( $args );
$badges = array();
// Ticket status
$badges[] = '<span class="' . esc_attr( $badge_base_class . ' ' . $status_badge_class ) . '">' . incsub_support_get_ticket_status_name( $ticket->ticket_status ) . '</span>';
// Replies number
$num_replies = number_format_i18n( $ticket->num_replies, 0 );
$badges[] = '<span class="' . esc_attr( $badge_base_class . ' ' . $replies_badge_class ) . '">' . esc_html( sprintf( _n( '1 reply', '%s replies', $num_replies , INCSUB_SUPPORT_LANG_DOMAIN ), $num_replies ) ) . '</span>';
$badges = implode( ' ', $badges );
echo $badges;
}
public function ticket_author( $ticket ){
$user = get_userdata( $ticket->user_id );
if ( ! $user )
return __( 'Unknown user', INCSUB_SUPPORT_LANG_DOMAIN );
return $user->data->display_name;
}
public function ticket_last_reply_url( $ticket ) {
$url = incsub_support_get_the_ticket_permalink();
$replies = $ticket->get_replies();
$last_reply = end( $replies );
$last_reply_id = $last_reply->message_id;
reset( $replies );
$url .= '#support-system-reply-' . $last_reply_id;
return $url;
}
public function ticket_updated_date( $ticket ) {
return incsub_support_get_translated_date( $ticket->ticket_updated, true );
}
public function ticket_category_link( $ticket ) {
$page = incsub_support_get_user_ticket_url( $ticket->ticket_id );
$cat = incsub_support_get_ticket_category( $ticket->cat_id );
$url = add_query_arg( 'ticket-cat-id', $cat->cat_id, $page );
$url = remove_query_arg( 'tid', $url );
return '<a href="' . esc_url( $url ) . '">' . $cat->cat_name . '</a>';
}
public function get_the_ticket_attachments( $ticket ) {
$ticket_id = $ticket->ticket_id;
$ticket = incsub_support_get_ticket( $ticket_id );
if ( ! $ticket )
return array();
$replies = $ticket->get_replies();
$main_reply = wp_list_filter( $replies, array( 'is_main_reply' => true ) );
return $main_reply[0]->attachments;
}
public function ticket_replies( $ticket ) {
$ticket_id = $ticket->ticket_id;
incsub_support_get_template( 'ticket-replies', $ticket_id );
}
public function trim( $message, $length = 40, $sep = ' ...' ){
return wp_trim_words( $message, $length, $sep );
}
public function error_message(){
?>
<div>
<?php _e( 'Nothing here!' ); ?>
</div>
<?php
}
public function set_front_stylesheet(){
global $post;
if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'support_system_upfront_tickets_front') ) {
wp_enqueue_style( 'suport-upfront2-tickets-style', INCSUB_SUPPORT_ASSETS_URL . 'css/incsub-support.css' );
}
}
public function set_query_vars( $query_vars ){
$query_vars[] = 'tid';
return $query_vars;
}
}
add_action( 'plugins_loaded', function(){
$GLOBALS['WPMUDEV_Inc_Support_Upfront_Front'] = WPMUDEV_Inc_Support_Upfront_Front::get_instance();
}, 10 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.