Created
May 28, 2017 17:38
-
-
Save panoslyrakis/8edc021bb0544598e540b037bed26e52 to your computer and use it in GitHub Desktop.
Front end assigned support tickets - Requires incsub-support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Front end assigned support tickets | |
Plugin URI: https://premium.wpmudev.org/ | |
Description: Adds a shortcode to display the assigned tickets for each user in front end | |
Author: Panos Lyrakis @ WPMUDEV | |
Author URI: https://premium.wpmudev.org/ | |
License: GPLv2 or later | |
*/ | |
if( ! class_exists( 'WPMUDEV_Inc_Support_Assigned_Front' ) ){ | |
class WPMUDEV_Inc_Support_Assigned_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_Assigned_Front(); | |
} | |
return self::$_instance; | |
} | |
private function __construct(){ | |
add_shortcode( 'support_system_assigned_tickets_front', array( $this, 'get_assigned_tickets' ) ); | |
add_action( 'wp_enqueue_scripts', array( $this, 'set_front_stylesheet' ), 10 ); | |
} | |
public function get_assigned_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_assigned_tickets' ); | |
//We are adding the admin_id using the "support_system_get_tickets_where" filter | |
add_filter( 'support_system_get_tickets_where', array( $this, 'support_system_get_tickets_where' ), 10, 3 ); | |
self::$_tickets = incsub_support_get_tickets( $atts ); | |
$found_tickets = incsub_support_get_tickets_count( $atts ); | |
self::$_total_pages = ceil( $found_tickets / $atts[ 'per_page' ] ); | |
return $this->ticket_list_template(); | |
} | |
public function support_system_get_tickets_where( $where, $count, $args ){ | |
$where .= ' AND admin_id=' . get_current_user_id(); | |
return $where; | |
} | |
public function ticket_list_template(){ | |
$tickets = self::$_tickets; | |
?> | |
<div id="support-system"> | |
<hr/> | |
<?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-assigned"> | |
<?php _e( 'Hooray, no tickets assigned!!' ); ?> | |
</h2> | |
<?php endif; ?> | |
</div> | |
<?php | |
} | |
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 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_assigned_tickets_front') ) { | |
wp_enqueue_style( 'suport-assigned-tickets-style', INCSUB_SUPPORT_ASSETS_URL . 'css/incsub-support.css' ); | |
} | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
$GLOBALS['WPMUDEV_Inc_Support_Assigned_Front'] = WPMUDEV_Inc_Support_Assigned_Front::get_instance(); | |
}, 10 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment