PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.3.9
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.3.9
3.5.4 3.5.3 3.5.2 3.5.1 3.4.9 3.5.0 3.4.8 3.4.7 trunk 2.3.1 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6
supportcandy / includes / admin / recent-activities / class-wpsc-ra-logs.php

class-wpsc-ra-logs.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.3.9, at includes/admin/recent-activities/class-wpsc-ra-logs.php

424 lines 13.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly!
5 }
6
7 if ( ! class_exists( 'WPSC_RA_Logs' ) ) :
8
9 final class WPSC_RA_Logs {
10
11 /**
12 * Initialize this class
13 */
14 public static function init() {
15
16 add_action( 'wp_ajax_wpsc_get_recent_activities', array( __CLASS__, 'get_recent_activities' ) );
17
18 add_action( 'wp_ajax_wpsc_ra_agents_autocomplete', array( __CLASS__, 'ra_agents_autocomplete' ) );
19 add_action( 'wp_ajax_nopriv_wpsc_ra_agents_autocomplete', array( __CLASS__, 'ra_agents_autocomplete' ) );
20 }
21
22 /**
23 * Admin submenu layout
24 *
25 * @return void
26 */
27 public static function layout() {
28
29 $customer_id = isset( $_REQUEST['cid'] ) ? intval( $_REQUEST['cid'] ) : 0; // phpcs:ignore
30 if ( $customer_id ) {
31 $customer = new WPSC_Customer( $customer_id );
32 }
33 ?>
34 <div class="wrap">
35 <hr class="wp-header-end">
36 <div id="wpsc-container">
37 <div class="wpsc-setting-header">
38 <h2><?php esc_attr_e( 'Recent Activities', 'supportcandy' ); ?></h2>
39 </div>
40 <form action="#" onsubmit="return false;" class="wpsc-ra-custom-filter">
41 <div class="wpsc-setting-section-body">
42
43 <div class="wpsc-ra-filter-container">
44 <div class="wpsc-filter-container">
45 <div class="wpsc-filter-item" style="min-width: 200px;">
46 <select name="wpsc-customer" id="wpsc-customer" class="wpsc-input-filter">
47 <?php
48 if ( isset( $customer ) && $customer->id ) {
49 ?>
50 <option value="<?php echo intval( $customer->id ); ?>"><?php echo esc_attr( $customer->name ); ?></option>
51 <?php
52 }
53 ?>
54 </select>
55 </div>
56 <div class="wpsc-filter-item" style="min-width: 250px;">
57 <div class="wpsc-input-close-group">
58 <input
59 type="text"
60 id="wpsc-ra-date-picker"
61 name="wpsc-ra-date-picker"
62 placeholder="<?php esc_attr_e( 'Start To End', 'supportcandy' ); ?>"
63 value=""
64 autocomplete="off"/>
65 <span onclick="wpsc_clear_date(this);"><?php WPSC_Icons::get( 'times' ); ?></span>
66 </div>
67 </div>
68 <div class="wpsc-filter-submit">
69 <button type="button" class="wpsc-button normal primary" onclick="wpsc_get_activity_logs()"><?php esc_attr_e( 'Apply', 'supportcandy' ); ?></button>
70 <div class="wpsc-filter-actions" style="margin-left: 5px;">
71 <span class="wpsc-link">
72 <a href="<?php echo esc_url( admin_url( 'admin.php?page=wpsc-recent-activities' ) ); ?>"><?php echo esc_attr__( 'Reset', 'supportcandy' ); ?></a>
73 </span>
74 </div>
75 </div>
76 </div>
77 </div>
78
79 <table class="wpsc_recent_activities">
80 <thead class="wpsc-ra-logs-thead">
81 <tr class="wpsc-ra-logs-tr">
82 <th class="wpsc-ra-logs-th"></th>
83 </tr>
84 </thead>
85 </table>
86
87 <script>
88 function wpsc_get_activity_logs(){
89
90 date_range = jQuery('#wpsc-ra-date-picker').val();
91 customer = jQuery('#wpsc-customer').val();
92
93 jQuery('.wpsc_recent_activities').dataTable({
94 processing: true,
95 serverSide: true,
96 serverMethod: 'post',
97 ajax: {
98 url: supportcandy.ajax_url,
99 data: {
100 'action': 'wpsc_get_recent_activities',
101 date_range,
102 customer,
103 '_ajax_nonce': '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_recent_activities' ) ); ?>'
104 }
105 },
106 'columns': [
107 { data: 'log' },
108 ],
109 'bDestroy': true,
110 'searching': false,
111 'ordering': false,
112 'bLengthChange': false,
113 pageLength: 20,
114 columnDefs: [
115 { targets: '_all', className: 'dt-left' },
116 ],
117 language: supportcandy.translations.datatables
118 });
119 }
120 jQuery(document).ready(function() {
121
122 wpsc_get_activity_logs();
123
124 jQuery('#wpsc-ra-date-picker').flatpickr({
125 mode: "range",
126 maxDate: "today",
127 dateFormat: "Y-m-d",
128 })
129
130 jQuery('select#wpsc-customer').selectWoo({
131 ajax: {
132 url: supportcandy.ajax_url,
133 dataType: 'json',
134 delay: 250,
135 data: function (params) {
136 return {
137 q: params.term, // search term
138 action: 'wpsc_ra_agents_autocomplete',
139 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_ra_agents_autocomplete' ) ); ?>'
140 };
141 },
142 processResults: function (data, params) {
143 var terms = [];
144 if ( data ) {
145 jQuery.each( data, function( id, customer ) {
146 terms.push({
147 id: customer.id,
148 text: customer.text,
149 name: customer.name
150 });
151 });
152 }
153 return {
154 results: terms
155 };
156 },
157 cache: true
158 },
159 escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
160 minimumInputLength: 1,
161 placeholder:'<?php esc_attr_e( 'Select customers or agents', 'supportcandy' ); ?>'
162 });
163 });
164 </script>
165 </div>
166 </form>
167 </div>
168 </div>
169 <?php
170 }
171
172 /**
173 * Get all recent activities
174 *
175 * @return void
176 */
177 public static function get_recent_activities() {
178
179 if ( check_ajax_referer( 'wpsc_get_recent_activities', '_ajax_nonce', false ) != 1 ) {
180 wp_send_json_error( 'Unauthorized request!', 401 );
181 }
182
183 if ( ! WPSC_Functions::is_site_admin() ) {
184 wp_send_json_error( 'Unauthorized request!', 401 );
185 }
186
187 $search = isset( $_POST['search'] ) && isset( $_POST['search']['value'] ) ? sanitize_text_field( wp_unslash( $_POST['search']['value'] ) ) : '';
188 $draw = isset( $_POST['draw'] ) ? intval( $_POST['draw'] ) : 1;
189 $start = isset( $_POST['start'] ) ? intval( $_POST['start'] ) : 1;
190 $rowperpage = isset( $_POST['length'] ) ? intval( $_POST['length'] ) : 20;
191 $page_no = ( $start / $rowperpage ) + 1;
192
193 $date_range = isset( $_POST['date_range'] ) ? sanitize_text_field( wp_unslash( $_POST['date_range'] ) ) : array();
194 $date_range = ! empty( $date_range ) ? explode( 'to', $date_range ) : array();
195
196 $customer_id = isset( $_POST['customer'] ) && intval( $_POST['customer'] ) ? intval( $_POST['customer'] ) : 'all';
197
198 $now = new DateTime();
199 $data = array();
200 $log_str = '';
201 $logs = self::get_activity_logs( $rowperpage, $page_no, $customer_id, $date_range );
202
203 foreach ( $logs['results'] as $log ) {
204 $log_str = '';
205 $time_ago = WPSC_Functions::date_interval_highest_unit_ago( $log->date_created->diff( $now ) );
206 if ( $log->type == 'report' ) {
207 $log_str = '<span class="wpsc-ra-log-desc">' . $log->customer->name . ' created a ticket <a href="' . admin_url( 'admin.php?page=wpsc-tickets&section=ticket-list&id=' . esc_attr( $log->ticket->id ) ) . '">#' . $log->ticket->id . ' ' . esc_attr( $log->ticket->subject ) . '</a></span>
208 <span class="wpsc-ra-log-time">' . $time_ago . '</span>';
209 } elseif ( $log->type == 'reply' ) {
210 $log_str = '<span class="wpsc-ra-log-desc">' . $log->customer->name . ' replied to ticket <a href="' . admin_url( 'admin.php?page=wpsc-tickets&section=ticket-list&id=' . esc_attr( $log->ticket->id ) ) . '">#' . $log->ticket->id . ' ' . esc_attr( $log->ticket->subject ) . '</a></span>
211 <span class="wpsc-ra-log-time">' . $time_ago . '</span>';
212 } elseif ( $log->type == 'note' ) {
213 $log_str = '<span class="wpsc-ra-log-desc">' . $log->customer->name . ' added a note to ticket <a href="' . admin_url( 'admin.php?page=wpsc-tickets&section=ticket-list&id=' . esc_attr( $log->ticket->id ) ) . '">#' . $log->ticket->id . ' ' . esc_attr( $log->ticket->subject ) . '</a></span>
214 <span class="wpsc-ra-log-time">' . $time_ago . '</span>';
215 } elseif ( $log->type == 'log' ) {
216 if ( ! $log->customer->id ) {
217 continue;
218 }
219 $body = json_decode( $log->body );
220 $is_json = ( json_last_error() == JSON_ERROR_NONE ) ? true : false;
221 if ( $is_json ) {
222 $cf = WPSC_Custom_Field::get_cf_by_slug( $body->slug );
223 if ( ! $cf ) {
224 continue;
225 }
226 $log_str = '<span class="wpsc-ra-log-desc">' . $cf->type::print_activity( $cf, $log, $body, '0' ) . '</span>
227 <span class="wpsc-ra-log-time">' . $time_ago . '</span>';
228 }
229 }
230 $data[] = array(
231 'log' => '<div class="wpsc-ra-user-details">
232 <div class="wpsc-ra-avatar">' . get_avatar( $log->customer->email, 35 ) . '</div>
233 <div class="wpsc-ra-log-details">' . $log_str . '</div>
234 </div>',
235 );
236 }
237
238 $response = array(
239 'draw' => intval( $draw ),
240 'iTotalRecords' => $logs['total_items'],
241 'iTotalDisplayRecords' => $logs['total_items'],
242 'data' => $data,
243 );
244
245 wp_send_json( $response );
246 }
247
248 /**
249 * Get activity logs
250 *
251 * @param int $rowperpage - items per page count.
252 * @param int $page_no - page number.
253 * @param string $customer_id - customer id.
254 * @param array $date_range - date range.
255 * @return array
256 */
257 public static function get_activity_logs( $rowperpage, $page_no, $customer_id = 'all', $date_range = array() ) {
258
259 $gs = get_option( 'wpsc-db-gs-settings' );
260 $current_user = WPSC_Current_User::$current_user;
261
262 $type = array();
263 if ( in_array( 'report', $gs['allowed-recent-activity-logs'] ) ) {
264 $type[] = 'report';
265 }
266 if ( in_array( 'reply', $gs['allowed-recent-activity-logs'] ) ) {
267 $type[] = 'reply';
268 }
269 if ( in_array( 'note', $gs['allowed-recent-activity-logs'] ) ) {
270 $type[] = 'note';
271 }
272
273 $meta_query = array(
274 'relation' => 'OR',
275 array(
276 'relation' => 'AND',
277 array(
278 'slug' => 'is_active',
279 'compare' => '=',
280 'val' => '1',
281 ),
282 array(
283 'slug' => 'type',
284 'compare' => 'IN',
285 'val' => $type,
286 ),
287 ),
288 );
289 if ( $customer_id === 'all' ) {
290 $meta_query[0][] = array(
291 'slug' => 'customer',
292 'compare' => 'NOT IN',
293 'val' => array( '0' ),
294 );
295 } else {
296
297 $meta_query[0][] = array(
298 'slug' => 'customer',
299 'compare' => 'IN',
300 'val' => array( $customer_id ),
301 );
302 }
303
304 if ( $date_range ) {
305 $from_date = trim( $date_range[0] );
306 $to_date = isset( $date_range[1] ) ? trim( $date_range[1] ) : $from_date;
307 $meta_query[0][] = array(
308 'slug' => 'date_created',
309 'compare' => 'BETWEEN',
310 'val' => array(
311 $from_date . ' 00:00:00',
312 $to_date . ' 23:59:59',
313 ),
314 );
315 }
316
317 $log_meta = array();
318 $cust_fields = array_diff( $gs['allowed-recent-activity-logs'], array( 'report', 'reply', 'note' ) );
319 if ( $cust_fields ) {
320 $cust_fields = "'" . implode( "','", $cust_fields ) . "'";
321 $log_meta = array(
322 array(
323 'relation' => 'AND',
324 array(
325 'slug' => 'type',
326 'compare' => '=',
327 'val' => 'log',
328 ),
329 array(
330 'slug' => 'custom_query',
331 'compare' => '=',
332 'val' => 'JSON_VALID(body)',
333 ),
334 array(
335 'slug' => 'custom_query',
336 'compare' => '=',
337 'val' => "JSON_EXTRACT(body, '$.slug') IN (" . $cust_fields . ')',
338 ),
339 ),
340 );
341 if ( $customer_id === 'all' ) {
342 $log_meta[0][] = array(
343 'slug' => 'customer',
344 'compare' => 'NOT IN',
345 'val' => array( '0' ),
346 );
347 } else {
348
349 $log_meta[0][] = array(
350 'slug' => 'customer',
351 'compare' => 'IN',
352 'val' => array( $customer_id ),
353 );
354 }
355
356 if ( $date_range ) {
357 $from_date = trim( $date_range[0] );
358 $to_date = isset( $date_range[1] ) ? trim( $date_range[1] ) : $from_date;
359 $log_meta[0][] = array(
360 'slug' => 'date_created',
361 'compare' => 'BETWEEN',
362 'val' => array(
363 $from_date . ' 00:00:00',
364 $to_date . ' 23:59:59',
365 ),
366 );
367 }
368 }
369 $meta_query = array_merge( $meta_query, $log_meta );
370
371 $logs = WPSC_Thread::find(
372 array(
373 'items_per_page' => $rowperpage,
374 'page_no' => $page_no,
375 'order' => 'DESC',
376 'order_by' => 'date_created',
377 'meta_query' => $meta_query,
378 )
379 );
380
381 return $logs;
382 }
383
384 /**
385 * Create as auto-complete
386 *
387 * @return void
388 */
389 public static function ra_agents_autocomplete() {
390
391 if ( check_ajax_referer( 'wpsc_ra_agents_autocomplete', '_ajax_nonce', false ) != 1 ) {
392 wp_send_json_error( 'Unauthorised request!', 401 );
393 }
394
395 $current_user = WPSC_Current_User::$current_user;
396
397 if ( ! $current_user->is_agent ) {
398 wp_send_json_error( new WP_Error( '001', 'Unauthorized access!' ), 401 );
399 }
400
401 $term = isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : '';
402 if ( ! $term ) {
403 wp_send_json_error( new WP_Error( '002', 'Search term should have at least one character!' ), 400 );
404 }
405
406 wp_send_json(
407 array_map(
408 fn( $customer ) => array(
409 'id' => $customer->id,
410 'text' => sprintf(
411 /* translators: %1$s: Name */
412 esc_attr__( '%1$s', 'supportcandy' ), //phpcs:ignore
413 $customer->name,
414 ),
415 'name' => $customer->name,
416 ),
417 WPSC_Customer::customer_search( $term )
418 )
419 );
420 }
421 }
422 endif;
423 WPSC_RA_Logs::init();
424