forked from brainstormforce/wp-freshdesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-freshdesk.php
593 lines (510 loc) · 20.6 KB
/
wp-freshdesk.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
<?php
/**
* Plugin Name: WP Freshdesk
* Plugin URI:
* Description: With this plugin, your users will be able to see their tickets on your Freshdesk support portal. Other features include - SSO, ticket filtering, sorting & search options. Admins have an options to display only certain status tickets with shortcodes.
* Version: 1.0.1
* Author: Brainstorm Force
* Author URI: https://www.brainstormforce.com/
* License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
//Block direct access to plugin files
defined( 'ABSPATH' ) or die();
if(!class_exists("FreshDeskAPI")){
class FreshDeskAPI{
//Class Variables
private $freshdeskUrl;
private $opt;
private $options;
private $display_option;
/*
* Function Name: __construct
* Function Description: Constructor
*/
function __construct() {
add_action( 'init', array( $this, 'init' ) );
add_action( 'plugins_loaded', array( $this, 'fd_load_textdomain' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_shortcode( "fd_fetch_tickets", array($this, "fetch_tickets"));
add_shortcode( "fd_new_ticket", array($this, "new_ticket"));
include_once( 'admin-settings.php' );
$this->options = get_option( 'fd_url' );
$this->opt = get_option( 'fd_apikey' );
$this->display_option = get_option( 'fd_display' );
if( isset( $this->opt['freshdesk_url'] ) ) {
if ( preg_match( "/^[A-Za-z\d\s]+$/", $this->opt['freshdesk_url'] ) ) {
$this->freshdeskUrl = 'https://' . $this->opt['freshdesk_url'] . '.freshdesk.com/';
} else {
$this->freshdeskUrl = '';
}
} else {
$this->freshdeskUrl = '';
}
}
/**
* Load plugin textdomain.
*
* @since 1.0.0
*/
function fd_load_textdomain() {
load_plugin_textdomain( 'wp-freshdesk', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
}
/*
* Function Name: enqueue_scripts
* Function Description: Adds scripts to wp pages
*/
function enqueue_scripts() {
wp_register_style( 'fd-style', plugins_url('css/fd-style.css', __FILE__) );
wp_enqueue_style( 'fd-style' );
wp_register_script( 'fd-script-frontend', plugins_url('js/fd-script-frontend.js', __FILE__), array('jquery'), '1.1', true );
wp_enqueue_script( 'fd-script-frontend' );
}
/*
* Function Name: init
* Function Description: Initialization
*/
public function init(){
if ( is_user_logged_in() ) {
// This is a login request.
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'fd-remote-login' ) {
// Don't waste time if remote auth is turned off.
if ( !isset( $this->options['freshdesk_enable'] ) && $this->options['freshdesk_enable'] != 'on' && !isset( $this->options['freshdesk_sharedkey'] ) && $this->options['freshdesk_sharedkey'] != '' ) {
__( 'Remote authentication is not configured yet.', 'wp-freshdesk' );
die();
}
// Filter freshdesk_return_to
$return_to = apply_filters( 'freshdesk_return_to', $_REQUEST['host_url'] ) ;
global $current_user;
wp_get_current_user();
// If the current user is logged in
if ( 0 != $current_user->ID ) {
// Pick the most appropriate name for the current user.
if ( $current_user->user_firstname != '' && $current_user->user_lastname != '' )
$name = $current_user->user_firstname . ' ' . $current_user->user_lastname;
else
$name = $current_user->display_name;
// Gather more info from the user, incl. external ID
$email = $current_user->user_email;
// The token is the remote "Shared Secret" under Admin - Security - Enable Single Sign On
$token = $this->options['freshdesk_sharedkey'];
// Generate the hash as per http://www.freshdesk.com/api/remote-authentication
$hash = md5( $name . $email . $token );
// Create the SSO redirect URL and fire the redirect.
$sso_url = trailingslashit( $this->freshdeskUrl ) . 'login/sso/?action=fd-remote-login&return_to=' . urlencode( 'https://' . $return_to . '/' ) . '&name=' . urlencode( $name ) . '&email=' . urlencode( $email ) . '&hash=' . urlencode( $hash );
//Hook before redirecting logged in user.
do_action( 'freshdesk_logged_in_redirect_before' );
wp_redirect( $sso_url );
// No further output.
die();
} else {
//Hook before redirecting user to login form
do_action( 'freshdesk_logged_in_redirect_before' );
// If the current user is not logged in we ask him to visit the login form
// first, authenticate and specify the current URL again as the return
// to address. Hopefully WordPress will understand this.
wp_redirect( wp_login_url( wp_login_url() . '?action=fd-remote-login&&return_to=' . urlencode( $return_to ) ) );
die();
}
}
// Is this a logout request? Errors from Freshdesk are handled here too.
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'fd-remote-logout' ) {
// Error processing and info messages are done here.
$kind = isset( $_REQUEST['kind'] ) ? $_REQUEST['kind'] : 'info';
$message = isset( $_REQUEST['message'] ) ? $_REQUEST['message'] : 'nothing';
// Depending on the message kind
if ( $kind == 'info' ) {
// When the kind is an info, it probably means that the logout
// was successful, thus, logout of WordPress too.
wp_redirect( htmlspecialchars_decode( wp_logout_url() ) );
die();
} elseif ( $kind == 'error' ) {
// If there was an error...
?>
<p><?php __( 'Remote authentication failed: ', 'wp-freshdesk' ); ?><?php echo $message; ?>.</p>
<ul>
<li><a href="<?php echo $this->freshdeskUrl; ?>"><?php __( 'Try again', 'wp-freshdesk' ); ?></a></li>
<li><a href="<?php echo wp_logout_url(); ?>"><?php printf( __( 'Log out of %s', 'wp-freshdesk' ), get_bloginfo( 'name' ) ); ?></a></li>
<li><a href="<?php echo admin_url(); ?>"><?php printf( __( 'Return to %s dashboard', 'wp-freshdesk' ), get_bloginfo( 'name' ) ); ?></a></li>
</ul>
<?php
}
// No further output.
die();
}
}
}
/*
* Function Name: fetch_tickets
* Function Description: Fetched all tickets from Freshdesk for current logged in user.
*/
public function fetch_tickets( $atts ){
$result = '
<div class="fd-tickets-outter">
<ul>';
if ( is_user_logged_in() ) {
global $current_user;
$fd_filter_dropdown = ( isset( $_GET["fd-filter_dropdown"] ) ) ? esc_attr( $_GET["fd-filter_dropdown"] ) : '';
if( ( isset( $this->opt['freshdesk_apikey'] ) && $this->opt['freshdesk_apikey'] != '' ) || !isset( $this->opt['use_apikey'] ) ) {
if( isset( $atts['filter'] ) && trim( $atts['filter'] ) != '' ) {
switch( trim( ucwords( strtolower( $atts['filter'] ) ) ) ) {
case 'Open':
$fd_filter_dropdown = 'Open';
break;
case 'Closed':
$fd_filter_dropdown = 'Closed';
break;
case 'Resolved':
$fd_filter_dropdown = 'Resolved';
break;
case 'Waiting On Third Party':
$fd_filter_dropdown = 'Waiting on Third Party';
break;
case 'Waiting On Customer':
$fd_filter_dropdown = 'Waiting on Customer';
break;
case 'Pending':
$fd_filter_dropdown = 'Pending';
break;
default:
break;
}
}
if( !isset( $fd_filter_dropdown ) || $fd_filter_dropdown == '' ){
$fd_filter_dropdown = 'Open';
}
$tickets = $this->get_tickets( $current_user->data->user_email, $current_user->roles );
$filteredTickets = false;
$search_txt = ( isset( $_GET['search_txt'] ) ) ? esc_attr( $_GET['search_txt'] ) : '';
if( isset( $tickets ) ) {
$tickets = json_decode( json_encode( $tickets ), true );
if( isset( $fd_filter_dropdown ) && $fd_filter_dropdown != '' ) {
$filteredTickets = ( $fd_filter_dropdown != 'all_tickets' ) ? $this->filter_tickets( $tickets, $fd_filter_dropdown ) : $tickets ;
}
if( isset( $search_txt ) && $search_txt != '' ) {
$filteredTickets = ( trim( $search_txt ) != '' ) ? $this->search_tickets( $filteredTickets, $search_txt ) : $tickets ;
}
} else {
$filteredTickets = false;
}
$result .= '
<li class="fd-filter-tickets">
<form method="get" action="" id="fd-filter_form" name="fd-filter_form">
<div class="fd-filter-dropdown fd-filter">
<select id="fd-filter_dropdown" name="fd-filter_dropdown">
<option value="all_tickets" ';
if( isset( $fd_filter_dropdown ) ) {
$result .= ( $fd_filter_dropdown == "all_tickets" ) ? 'selected="selected"' : '';
}
$result .= '>' . __( 'All Tickets', 'wp-freshdesk' ) . '</option>
<option value="Open" ';
if( isset( $fd_filter_dropdown ) ) {
$result .= ( $fd_filter_dropdown == "Open" ) ? 'selected="selected"' : '';
}
$result .= '>' . __( 'Open', 'wp-freshdesk' ) . '</option>
<option value="Pending" ';
if( isset( $fd_filter_dropdown ) ) {
$result .= ( $fd_filter_dropdown == "Pending" ) ? 'selected="selected"' : '';
}
$result .= '>' . __( 'Pending', 'wp-freshdesk' ) . '</option>
<option value="Resolved" ';
if( isset( $fd_filter_dropdown ) ) {
$result .= ( $fd_filter_dropdown == "Resolved" ) ? 'selected="selected"' : '';
}
$result .= '>' . __( 'Resolved', 'wp-freshdesk' ) . '</option>
<option value="Closed" ';
if( isset( $fd_filter_dropdown ) ) {
$result .= ( $fd_filter_dropdown == "Closed" ) ? 'selected="selected"' : '';
}
$result .= '>' . __( 'Closed', 'wp-freshdesk' ) . '</option>
<option value="Waiting on Customer" ';
if( isset( $fd_filter_dropdown ) ) {
$result .= ( $fd_filter_dropdown == "Waiting on Customer" ) ? 'selected="selected"' : '';
}
$result .= '>' . __( 'Waiting on Customer', 'wp-freshdesk' ) . '</option>
<option value="Waiting on Third Party" ';
if( isset( $fd_filter_dropdown ) ) {
$result .= ( $fd_filter_dropdown == "Waiting on Third Party" ) ? 'selected="selected"' : '';
}
$txt = ( isset( $search_txt ) ) ? $search_txt : '';
$result .= '>' . __( 'Waiting on Third Party', 'wp-freshdesk' ) . '</option>
</select>
</div>
<div class="fd-search-box fd-filter">
<input type="text" value="' . $txt . '" id="search_txt" name="search_txt" placeholder="' . __( 'Search...', 'wp-freshdesk' ) . '"/>
</div>
<div class="fd-filter">
<input type="submit" value="Search" id="filter_tickets"/>
</div>
<div class="fd-filter">
<input type="button" value="Reset" id="reset_filter">
</div>
<div class="clear"></div>
</form>
</li>';
if( !isset( $tickets->require_login ) && $tickets != '' && !isset( $tickets->errors ) && !empty( $tickets ) ){
if( isset( $search_txt ) || isset( $fd_filter_dropdown ) ) {
if( !isset( $filteredTickets->require_login ) && $filteredTickets != '' && !isset( $filteredTickets->errors ) && !empty( $filteredTickets ) ) {
$result .= $this->get_html( $filteredTickets );
} else {
if( isset( $filteredTickets->require_login ) ) {
$msg = __( 'Invalid Credentials', 'wp-freshdesk' );
} else if( isset( $filteredTickets->errors ) ) {
if( isset( $filteredTickets->errors->no_email ) ){
$msg = ( isset( $this->display_option['invalid_user_msg'] ) && $this->display_option['invalid_user_msg'] != '' ) ? $this->display_option['invalid_user_msg'] : __( 'Invalid User', 'wp-freshdesk' );
} else {
$msg = __( 'Invalid Freshdesk URL' , 'wp-freshdesk');
}
} else if( empty( $filteredTickets ) ) {
$keyword = ( isset( $search_txt ) && $search_txt != '' ) ? 'keyword <strong>"' . $search_txt . '"</strong>.' : '';
$dropdown = ( isset( $fd_filter_dropdown ) && $fd_filter_dropdown != '' ) ? 'No tickets for <strong>"' . strtoupper( str_replace( '_', ' ', $fd_filter_dropdown ) ) . '"</strong> category' : '';
$str = $dropdown;
$str .= ( $keyword != '' ) ? ' & ' . $keyword : '';
$msg = '<p> ' . $str . '</p><div class="fd-more-ticket">Could not find what you are searching for? Click <a href="' . $this->freshdeskUrl . 'support/tickets" target="_blank">here</a> to check all your old tickets.</div>';
} else {
$msg = __( 'Error!', 'wp-freshdesk' );
}
$result .= '<li>
<div class="fd-message">' . $msg . '</div>
</li>';
}
} else {
$result .= $this->get_html( $tickets );
}
} else {
if( isset( $tickets->require_login ) ) {
$msg = __( 'Invalid Credentials', 'wp-freshdesk' );
} else if( isset( $tickets->errors ) ) {
if( isset( $tickets->errors->no_email ) ){
$msg = ( isset( $this->display_option['invalid_user_msg'] ) && $this->display_option['invalid_user_msg'] != '' ) ? $this->display_option['invalid_user_msg'] : __( 'Invalid User', 'wp-freshdesk' );
} else {
$msg = __( 'Invalid Freshdesk URL' , 'wp-freshdesk');
}
} else if( empty( $tickets ) ) {
$msg = ( isset( $this->display_option['no_tickets_msg'] ) && $this->display_option['no_tickets_msg'] != '' ) ? $this->display_option['no_tickets_msg'] : __( 'No tickets', 'wp-freshdesk' );
} else {
$msg = __( 'Error!', 'wp-freshdesk' );
}
$result .= '<li>
<div class="fd-message">' . $msg . '</div>
</li>';
}
} else {
$result .= '
<li>
<div class="fd-message">Please configure settings for <strong>Freshdesk API</strong> from <a href="' . admin_url( '/options-general.php?page=wp-freshdesk' ) . '" target="_blank">admin panel</a></div>
</li>
';
}
} else{
$result .= '
<li>
<div class="fd-message"><a href="' . wp_login_url() . '" title="Login">Login</a> to view your tickets!</div>
</li>
';
}
$result .=
'</ul>
</div>';
return $result;
}
/*
* Function Name: new_ticket
* Function Description: Create a new ticket button html
*/
function new_ticket(){
echo '<form action="' . $this->freshdeskUrl . 'support/tickets/new/" target="_blank"><input type="submit" value="New Ticket" id="new_ticket"></form>';
}
/*
* Function Name: get_tickets
* Function Description: API call to Freshdesk to get all tickets of the user(email)
*/
public function get_tickets( $uemail = '', $roles = array() ){
if( !empty( $uemail ) ){
$filterName = 'all_tickets';
if( isset( $this->opt['use_apikey'] ) ){
$apikey = ( $this->opt['freshdesk_apikey'] != '' ) ? $this->opt['freshdesk_apikey'] : '';
$password = "";
} else {
$apikey = ( $this->opt['api_username'] != '' ) ? $this->opt['api_username'] : '';
$password = ( $this->opt['api_pwd'] != '' ) ? $this->opt['api_pwd'] : '';
}
$filter = ( !in_array( 'administrator', $roles ) ) ? '&email=' . $uemail : '';
$url = $this->freshdeskUrl . 'helpdesk/tickets.json?filter_name=' . $filterName . $filter;
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_USERPWD, "$apikey:$password");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$server_output = curl_exec ($ch);
curl_close ($ch);
$tickets = json_decode( $server_output );
return $tickets;
} else{
return false;
}
}
/*
* Function Name: get_html
* Function Description: Returns HTML string of the tickets
*/
public function get_html( $tickets = '' ){
$html = '';
$tickets = json_decode( json_encode( $tickets ), FALSE );
$append = ( count( $tickets ) > 1 ) ? 's' : '';
$html .=
'<li>
<div class="fd-message">' . count( $tickets ) . ' ticket' . $append . ' found!</div>
</li>';
foreach( $tickets as $d ) {
$class = ( $d->status_name == "Closed" ) ? 'status-closed' : '';
$diff = ( strtotime( date_i18n('Y-m-d H:i:s') ) - strtotime( date_i18n( 'Y-m-d H:i:s', false, 'gmt' ) ) );
$date = date_i18n( 'j M\' Y, g:i A', strtotime( $d->updated_at ) + $diff );
$description = ( strlen( $d->description ) > 125 ) ? strip_tags( substr( $d->description, 0, 125 ) ) . '...' : strip_tags( $d->description );
$time_elapsed = $this->timeAgo( date_i18n( 'Y-m-d H:i:s', strtotime( $d->updated_at ) + $diff ) );
$html .= '
<li class="group ' . $class . '">
<a href="' . $this->freshdeskUrl . 'helpdesk/tickets/' . $d->display_id . '" target="_blank">
<span class="ticket-data">
<span class="ticket-title">' . strip_tags( $d->subject ) . ' <span class="ticket-id">#' . $d->display_id . '</span></span>
<span class="ticket-excerpt">' . $description . '</span>
</span>
<span class="ticket-meta">
<span class="ticket-status ' . $class . '">' . strip_tags( $d->status_name ) . '</span>
<span class="ticket-time"><abbr title="Last Updated on - ' . $date . '" class="timeago comment-time ticket-updated-at">' . $time_elapsed . '</abbr></span>
</span>
</a>
</li>';
}
return $html;
}
/*
* Function Name: filter_tickets
* Function Description: Filters the tickets according to ticket_status
*/
public function filter_tickets( $tickets = '', $status = '' ){
$filtered_tickets = array();
if( $status != 'all_tickets' ) {
foreach( $tickets as $t ){
if( $t['status_name'] == $status ) {
$filtered_tickets[] = $t;
}
}
return $filtered_tickets;
} else {
return $tickets;
}
}
/*
* Function Name: search_tickets
* Function Description: Searches the tickets according to input text
*/
public function search_tickets( $tickets, $txt = '' ){
$filtered_tickets = array();
foreach( $tickets as $t ){
if( stristr( $t['subject'], trim( $txt ) ) || stristr( $t['description'], trim( $txt ) ) || stristr( $t['id'], trim( $txt ) ) ) {
$filtered_tickets[] = $t;
}
}
return $filtered_tickets;
}
/*
* Function Name: timeAgo
* Function Description: returns input php time to "mins/hours/months/weeks/years ago" format.
*/
function timeAgo( $time_ago ) {
$time_ago = strtotime( $time_ago );
$cur_time = time();
$time_elapsed = $cur_time - $time_ago;
$seconds = $time_elapsed ;
$minutes = round( $time_elapsed / 60 );
$hours = round( $time_elapsed / 3600 );
$days = round( $time_elapsed / 86400 );
$weeks = round( $time_elapsed / 604800 );
$months = round( $time_elapsed / 2600640 );
$years = round( $time_elapsed / 31207680 );
// Seconds
if( $seconds <= 60 ) {
return "just now";
}
//Minutes
else if( $minutes <= 60 ){
if( $minutes == 1 ) {
return "one minute ago";
} else {
return "$minutes minutes ago";
}
}
//Hours
else if( $hours <= 24 ) {
if( $hours == 1 ) {
return "an hour ago";
} else {
return "$hours hrs ago";
}
}
//Days
else if( $days <= 7 ) {
if( $days == 1 ) {
return "yesterday";
} else {
return "$days days ago";
}
}
//Weeks
else if( $weeks <= 4.3 ){
if( $weeks == 1 ) {
return "a week ago";
} else {
return "$weeks weeks ago";
}
}
//Months
else if( $months <= 12 ) {
if( $months == 1 ) {
return "a month ago";
} else {
return "$months months ago";
}
}
//Years
else {
if( $years == 1 ) {
return "one year ago";
} else {
return "$years years ago";
}
}
}
}
} //end of class
/* Register the activation function and redirect to Setting page. */
register_activation_hook(__FILE__, 'fd_plugin_activate');
add_action('admin_init', 'fd_plugin_redirect' );
/*
* Function Name: fd_plugin_redirect
* Function Description:
*/
function fd_plugin_redirect() {
if ( get_option( 'fd_do_activation_redirect', false ) ) {
delete_option( 'fd_do_activation_redirect' );
$activate_multi = esc_attr( $_GET['activate-multi'] );
if( !isset( $activate_multi ) ) {
wp_redirect( 'options-general.php?page=wp-freshdesk' );
}
}
}
/*
* Function Name: fd_plugin_activate
* Function Description:
*/
function fd_plugin_activate() {
add_option('fd_do_activation_redirect', true);
$activate_multi = esc_attr( $_GET['activate-multi'] );
if( !isset( $activate_multi ) ) {
wp_redirect( 'options-general.php?page=wp-freshdesk' );
}
}
new FreshDeskAPI();
?>