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 / settings / dashboard-settings / class-wpsc-dashboard-widgets-setting.php

class-wpsc-dashboard-widgets-setting.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.3.9, at includes/admin/settings/dashboard-settings/class-wpsc-dashboard-widgets-setting.php

346 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly!
4 }
5
6 if ( ! class_exists( 'WPSC_Dashboard_Widgets_Setting' ) ) :
7
8 final class WPSC_Dashboard_Widgets_Setting {
9
10 /**
11 * List of allowed custom field types in dashboard widgets
12 *
13 * @var array
14 */
15 public static $allowed_cft = array();
16
17 /**
18 * Initialize this class
19 */
20 public static function init() {
21
22 add_action( 'init', array( __CLASS__, 'allowed_cft' ) );
23
24 // Ticket widget.
25 add_action( 'wp_ajax_wpsc_get_dashboard_widgets_settings', array( __CLASS__, 'get_dashboard_widgets' ) );
26
27 // Add new dashboard widget.
28 add_action( 'wp_ajax_wpsc_get_new_dashboard_widget', array( __CLASS__, 'get_new_dashboard_widget' ) );
29 add_action( 'wp_ajax_wpsc_set_new_dashboard_widget', array( __CLASS__, 'set_new_dashboard_widget' ) );
30
31 // Delete dashboard widget.
32 add_action( 'wp_ajax_wpsc_delete_dashboard_widget', array( __CLASS__, 'delete_dashboard_widget' ) );
33
34 // Set load order.
35 add_action( 'wp_ajax_wpsc_set_dashboard_widget_load_order', array( __CLASS__, 'set_dashboard_widget_load_order' ) );
36
37 // allow access to new agent role.
38 add_action( 'wpsc_after_add_agent_role', array( __CLASS__, 'after_add_agent_role' ) );
39 }
40
41 /**
42 * List of allowed custom field types in reports
43 *
44 * @return void
45 */
46 public static function allowed_cft() {
47
48 self::$allowed_cft = apply_filters(
49 'allowed_cft_dashboard_widgets',
50 array(
51 'cf_single_select',
52 'cf_multi_select',
53 'cf_checkbox',
54 'cf_radio_button',
55 )
56 );
57 }
58
59 /**
60 * Load ticket widgets
61 */
62 public static function get_dashboard_widgets() {
63
64 if ( ! WPSC_Functions::is_site_admin() ) {
65 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
66 }
67
68 $dashboard_widgets = get_option( 'wpsc-dashboard-widgets', array() );
69 ob_start(); ?>
70
71 <div class="wpsc-dock-container">
72 <?php
73 printf(
74 /* translators: Click here to see the documentation */
75 esc_attr__( '%s to see the documentation!', 'supportcandy' ),
76 '<a href="https://supportcandy.net/docs/dashboard-widgets/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>'
77 );
78 ?>
79 </div>
80 <div class="wpsc-setting-cards-container ui-sortable">
81 <div class="wpsc-actions-btn-setting">
82 <button class="wpsc-button normal primary margin-right" onclick="wpsc_get_new_dashboard_widget('<?php echo esc_attr( wp_create_nonce( 'wpsc_get_new_dashboard_widget' ) ); ?>');"><?php esc_attr_e( 'Add widget', 'supportcandy' ); ?></button>
83 </div>
84 <?php
85 foreach ( $dashboard_widgets as $key => $widget ) {
86 if ( ! class_exists( $widget['class'] ) ) {
87 continue;
88 }
89 $style = ! $widget['is_enable'] ? 'background-color:#eec7ca;color:#dc2222' : '';
90 ?>
91 <div class="wpsc-setting-card" data-id="<?php echo esc_attr( $key ); ?>" style="<?php echo esc_attr( $style ); ?>">
92 <span class="wpsc-sort-handle action-btn"><?php WPSC_Icons::get( 'sort' ); ?></span>
93 <span class="title">
94 <?php
95 $widget_title = $widget['title'] ? WPSC_Translations::get( 'wpsc-dashboard-widget-' . $key, stripslashes( htmlspecialchars( $widget['title'] ) ) ) : stripslashes( htmlspecialchars( $widget['title'] ) );
96 echo esc_attr( $widget_title );
97 ?>
98 </span>
99 <div class="actions">
100 <span class="action-btn" onclick="wpsc_get_edit_dashboard_card_widget( 'wpsc-dashboard-widgets', '<?php echo esc_attr( $key ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_edit_dashboard_card_widget' ) ); ?>' );"><?php WPSC_Icons::get( 'edit' ); ?></span>
101 <?php if ( $widget['type'] != 'default' ) { ?>
102 <span class="action-btn" onclick="wpsc_delete_dashboard_widget( '<?php echo esc_attr( $key ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_delete_dashboard_widget' ) ); ?>' );"><?php WPSC_Icons::get( 'trash-alt' ); ?></span>
103 <?php } ?>
104 </div>
105 </div>
106 <?php
107 }
108 ?>
109 </div>
110 <div class="setting-footer-actions">
111 <button class="wpsc-button normal secondary wpsc-save-sort-order"><?php esc_attr_e( 'Save Order', 'supportcandy' ); ?></button>
112 </div>
113 <script>
114 var items = jQuery( ".wpsc-setting-cards-container" ).sortable({ handle: '.wpsc-sort-handle' });
115 jQuery(".wpsc-save-sort-order").click(function(){
116 var slugs = items.sortable( "toArray", {attribute: 'data-id'} );
117 wpsc_set_dashboard_widget_load_order(slugs, '<?php echo esc_attr( wp_create_nonce( 'wpsc_set_dashboard_widget_load_order' ) ); ?>');
118 });
119 </script>
120 <?php
121 wp_die();
122 }
123
124 /**
125 * Get add dashboard widgets
126 *
127 * @return void
128 */
129 public static function get_new_dashboard_widget() {
130
131 if ( check_ajax_referer( 'wpsc_get_new_dashboard_widget', '_ajax_nonce', false ) != 1 ) {
132 wp_send_json_error( 'Unauthorized request!', 400 );
133 }
134
135 if ( ! WPSC_Functions::is_site_admin() ) {
136 wp_send_json_error( 'Unauthorized!', 401 );
137 }
138
139 $custom_fields = WPSC_Custom_Field::$custom_fields;
140 $dbw = get_option( 'wpsc-dashboard-widgets', array() );
141
142 $title = esc_attr__( 'Add new widget', 'supportcandy' );
143 ob_start();
144 ?>
145 <form action="#" onsubmit="return false;" class="wpsc-add-new-dashboard-widget">
146 <div class="wpsc-input-group">
147 <div class="label-container">
148 <label for=""><?php esc_attr_e( 'Select custom fields', 'supportcandy' ); ?></label>
149 </div>
150 <select multiple id="wpsc-select-card-items" name="cf_id[]">
151 <?php
152 foreach ( $custom_fields as $cf ) {
153 if ( key_exists( $cf->slug, $dbw ) ) {
154 continue;
155 }
156 if (
157 in_array( $cf->type::$slug, self::$allowed_cft ) &&
158 in_array( $cf->field, array( 'ticket', 'agentonly' ) )
159 ) {
160 ?>
161 <option value="<?php echo esc_attr( $cf->id ); ?>"><?php echo esc_attr( $cf->name ); ?></option>
162 <?php
163 }
164 }
165 ?>
166 </select>
167 <script>
168 jQuery('#wpsc-select-card-items').selectWoo({
169 allowClear: false,
170 placeholder: ""
171 });
172 </script>
173 </div>
174 <input type="hidden" name="action" value="wpsc_set_new_dashboard_widget">
175 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_new_dashboard_widget' ) ); ?>">
176 </form>
177 <?php
178 $body = ob_get_clean();
179
180 ob_start();
181 ?>
182 <button class="wpsc-button small primary" onclick="wpsc_set_new_dashboard_widget(this);">
183 <?php echo esc_attr( wpsc__( 'Submit', 'supportcandy' ) ); ?>
184 </button>
185 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
186 <?php echo esc_attr( wpsc__( 'Cancel', 'supportcandy' ) ); ?>
187 </button>
188 <?php
189 do_action( 'wpsc_get_edit_agent_footer' );
190 $footer = ob_get_clean();
191
192 $response = array(
193 'title' => $title,
194 'body' => $body,
195 'footer' => $footer,
196 );
197
198 wp_send_json( $response, 200 );
199 }
200
201 /**
202 * Set new dashboard widgets
203 *
204 * @return void
205 */
206 public static function set_new_dashboard_widget() {
207
208 if ( check_ajax_referer( 'wpsc_set_new_dashboard_widget', '_ajax_nonce', false ) != 1 ) {
209 wp_send_json_error( 'Unauthorized request!', 400 );
210 }
211
212 if ( ! WPSC_Functions::is_site_admin() ) {
213 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
214 }
215
216 $cf_ids = isset( $_POST['cf_id'] ) ? array_filter( array_map( 'intval', $_POST['cf_id'] ) ) : array();
217
218 $dbw = get_option( 'wpsc-dashboard-widgets', array() );
219 $cf_dashboard_widget = array();
220 foreach ( $cf_ids as $id ) {
221
222 $cf = new WPSC_Custom_Field( $id );
223 if ( ! $cf->id ) {
224 continue;
225 }
226
227 if ( $cf->type::$slug == 'cf_single_select' ) {
228 $cf_class = 'WPSC_RP_Single_Select';
229 } elseif ( $cf->type::$slug == 'cf_multi_select' ) {
230 $cf_class = 'WPSC_RP_Multi_Select';
231 } elseif ( $cf->type::$slug == 'cf_checkbox' ) {
232 $cf_class = 'WPSC_RP_Checkbox';
233 } elseif ( $cf->type::$slug == 'cf_radio_button' ) {
234 $cf_class = 'WPSC_RP_Radio_Button';
235 }
236
237 $cf_dashboard_widget[ $cf->slug ] = array(
238 'title' => $cf->name,
239 'is_enable' => 1,
240 'allowed-agent-roles' => array( 1 ),
241 'callback' => 'wpsc_dbw_cf_widgets()',
242 'class' => $cf_class,
243 'type' => 'custom_field',
244 'chart-type' => 'doughnut',
245 );
246
247 // add string translations.
248 WPSC_Translations::add( 'wpsc-dashboard-widget-' . $cf->slug, stripslashes( $cf->name ) );
249 }
250 update_option( 'wpsc-dashboard-widgets', array_filter( array_merge( $dbw, $cf_dashboard_widget ) ) );
251
252 wp_die();
253 }
254
255 /**
256 * Delete dashboard card
257 *
258 * @return void
259 */
260 public static function delete_dashboard_widget() {
261
262 if ( check_ajax_referer( 'wpsc_delete_dashboard_widget', '_ajax_nonce', false ) != 1 ) {
263 wp_send_json_error( 'Unauthorised request!', 401 );
264 }
265
266 if ( ! WPSC_Functions::is_site_admin() ) {
267 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 400 );
268 }
269
270 $db_widget = get_option( 'wpsc-dashboard-widgets' );
271 $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : 0;
272 if ( ! $slug || ! isset( $db_widget[ $slug ] ) ) {
273 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
274 }
275
276 if ( in_array( $slug, array( 'ticket-statistics', 'todays-trends', 'recent-activities', 'category-report', 'recent-tickets', 'agent-list', 'unresolved-statuses', 'unresolved-priorities' ) ) ) {
277 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
278 }
279
280 unset( $db_widget[ $slug ] );
281 update_option( 'wpsc-dashboard-widgets', $db_widget );
282
283 // remove string translations.
284 WPSC_Translations::remove( 'wpsc-dashboard-widget-' . $slug );
285 wp_die();
286 }
287
288 /**
289 * Set dashboard cards order
290 *
291 * @return void
292 */
293 public static function set_dashboard_widget_load_order() {
294
295 if ( check_ajax_referer( 'wpsc_set_dashboard_widget_load_order', '_ajax_nonce', false ) != 1 ) {
296 wp_send_json_error( 'Unauthorised request!', 401 );
297 }
298
299 if ( ! WPSC_Functions::is_site_admin() ) {
300 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
301 }
302
303 $slugs = isset( $_POST['slugs'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['slugs'] ) ) ) : array();
304 if ( ! $slugs ) {
305 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
306 }
307 $sorted_widgets = array();
308
309 $dashboard_widgets = get_option( 'wpsc-dashboard-widgets', array() );
310 $dashboard_widgets_keys = array_keys( $dashboard_widgets );
311 // Verifying if slug is present in list item.
312 foreach ( $slugs as $slug ) {
313 if ( ! in_array( $slug, $dashboard_widgets_keys ) ) {
314 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
315 }
316 }
317
318 foreach ( $slugs as $slug ) :
319 $sorted_widgets[ $slug ] = $dashboard_widgets[ $slug ];
320 endforeach;
321 update_option( 'wpsc-dashboard-widgets', $sorted_widgets );
322 wp_die();
323 }
324
325 /**
326 * After new agent role added add that role in ticket widgets
327 *
328 * @param integer $role_id - agent role id.
329 * @return void
330 */
331 public static function after_add_agent_role( $role_id ) {
332
333 $dashboard_widgets = get_option( 'wpsc-dashboard-widgets', array() );
334 foreach ( $dashboard_widgets as $key => $widget ) {
335
336 $widget['allowed-agent-roles'][] = $role_id;
337 $dashboard_widgets[ $key ] = $widget;
338 }
339 update_option( 'wpsc-dashboard-widgets', $dashboard_widgets );
340 }
341 }
342
343 endif;
344
345 WPSC_Dashboard_Widgets_Setting::init();
346