PluginProbe
Awesome Support – WordPress HelpDesk & Support Plugin / trunk
Awesome Support – WordPress HelpDesk & Support Plugin vtrunk
6.4.0 trunk 3.0.0 3.0.1 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 All 95 releases
awesome-support / includes / admin / class-admin-user.php

class-admin-user.php in Awesome Support – WordPress HelpDesk & Support Plugin trunk, at includes/admin/class-admin-user.php

500 lines 15.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * User.
4 *
5 * @package Admin/User
6 * @author Julien Liabeuf <julien@liabeuf.fr>
7 * @license GPL-2.0+
8 * @link https://getawesomesupport.com
9 * @copyright 2014-2017 AwesomeSupport
10 */
11
12 class WPAS_User {
13
14 /**
15 * Instance of this class.
16 *
17 * @since 1.0.0
18 * @var object
19 */
20 protected static $instance = null;
21
22 public function __construct() {
23 add_action( 'edit_user_profile', array( $this, 'user_profile_custom_fields' ) ); // Add user preferences
24 add_action( 'show_user_profile', array( $this, 'user_profile_custom_fields' ) ); // Add user preferences
25 add_action( 'personal_options_update', array( $this, 'save_user_custom_fields' ) ); // Save the user preferences
26 add_action( 'edit_user_profile_update', array( $this, 'save_user_custom_fields' ) ); // Save the user preferences when modified by admins
27 add_action( 'user_register', array( $this, 'enable_assignment' ), 10, 1 ); // Enable auto-assignment for new users
28 add_filter( 'manage_users_columns', array( $this, 'auto_assignment_user_column' ) );
29 add_filter( 'manage_users_custom_column', array( $this, 'auto_assignment_user_column_content' ), 10, 3 );
30
31 /**
32 * Custom profile fields
33 */
34 add_action( 'wpas_user_profile_fields', array( $this, 'profile_field_user_can_be_assigned' ), 10, 1 );
35 add_action( 'wpas_user_profile_fields', array( $this, 'profile_field_smart_tickets_order' ), 10, 1 );
36 add_action( 'wpas_user_profile_fields', array( $this, 'profile_field_after_reply' ), 10, 1 );
37 add_action( 'wpas_user_profile_fields', array( $this, 'profile_field_user_view_all_tickets' ), 10, 1 );
38 add_action( 'wpas_user_profile_fields', array( $this, 'profile_field_allow_assignment_to' ), 11, 1 );
39 add_action( 'wpas_all_user_profile_fields', array( $this, 'profile_phone_fields' ), 10, 1 );
40 }
41
42 /**
43 * Return an instance of this class.
44 *
45 * @since 3.0.0
46 * @return object A single instance of this class.
47 */
48 public static function get_instance() {
49
50 // If the single instance hasn't been set, set it now.
51 if ( null == self::$instance ) {
52 self::$instance = new self;
53 }
54
55 return self::$instance;
56 }
57
58 /**
59 * Add user phone fields to the profile page.
60 *
61 * @param WP_User $user
62 */
63 public function profile_phone_fields( $user ) {
64
65 $mobile_phone = esc_attr( get_user_option( 'wpas_mobile_phone', $user->ID ) );
66 $office_phone = esc_attr( get_user_option( 'wpas_office_phone', $user->ID ) );
67 $home_phone = esc_attr( get_user_option( 'wpas_home_phone', $user->ID ) );
68 $other_phone = esc_attr( get_user_option( 'wpas_other_phone', $user->ID ) );
69 ?>
70
71 <div id="wpas_user_profile_segment">
72 <h3><?php esc_html_e( 'Awesome Support: Additional User Data', 'awesome-support') ?></h3>
73
74
75 <table class="form-table">
76
77 <tbody>
78 <tr>
79 <th><label><?php esc_html_e( 'Mobile Phone', 'awesome-support' ); ?></label></th>
80 <td><input type="text" name="wpas_mobile_phone" id="wpas_mobile_phone" value="<?php echo esc_attr( $mobile_phone ); ?>" class="regular-text code"></td>
81 </tr>
82 <tr>
83 <th><label><?php esc_html_e( 'Office Phone', 'awesome-support' ); ?></label></th>
84 <td><input type="text" name="wpas_office_phone" id="wpas_office_phone" value="<?php echo esc_attr( $office_phone ); ?>" class="regular-text code"></td>
85 </tr>
86 <tr>
87 <th><label><?php esc_html_e( 'Home Phone', 'awesome-support' ); ?></label></th>
88 <td><input type="text" name="wpas_home_phone" id="wpas_home_phone" value="<?php echo esc_attr( $home_phone ); ?>" class="regular-text code"></td>
89 </tr>
90 <tr>
91 <th><label><?php esc_html_e( 'Other Phone', 'awesome-support' ); ?></label></th>
92 <td><input type="text" name="wpas_other_phone" id="wpas_other_phone" value="<?php echo esc_attr( $other_phone ); ?>" class="regular-text code"></td>
93 </tr>
94 </tbody>
95
96 </table>
97 </div>
98
99 <?php
100 }
101
102 /**
103 * Add user preferences to the profile page.
104 *
105 * @since 3.0.0
106 *
107 * @param WP_User $user
108 *
109 * @return bool|void
110 */
111 public function user_profile_custom_fields( $user ) {
112
113 do_action( 'wpas_all_user_profile_fields', $user );
114
115 if ( ! user_can( $user->ID, 'edit_ticket' ) ) {
116 return false;
117 } ?>
118
119 <div id="wpas_user_profile_segment">
120 <h3><?php esc_html_e( 'Awesome Support: Preferences', 'awesome-support' ); ?></h3>
121
122 <table class="form-table">
123 <tbody>
124 <?php do_action( 'wpas_user_profile_fields', $user ); ?>
125 </tbody>
126 </table>
127 </div>
128 <?php }
129
130 /**
131 * User profile field "tickets order"
132 *
133 * Let the user selects the order in which his tickets appear in the tickets list screen.
134 *
135 * @since 3.3.2
136 *
137 * @param WP_User $user
138 *
139 * @return void
140 */
141 public function profile_field_smart_tickets_order( $user ) {
142
143 /* If this user is not an agent, then don't allow this field to be set/shown */
144 if ( ! wpas_is_agent( $user->ID ) ) {
145 return ;
146 }
147
148 ?>
149
150 <tr class="wpas-after-reply-wrap">
151 <th><label><?php esc_attr_e( 'Smart Tickets Order', 'awesome-support' ); ?></label></th>
152 <td>
153 <?php $smart = esc_attr( get_user_option( 'wpas_smart_tickets_order', $user->ID ) ); ?>
154 <label for="wpas_smart_tickets_order"><input type="checkbox" name="wpas_smart_tickets_order" id="wpas_smart_tickets_order" value="yes" <?php if ( ! empty( $smart ) ) { echo 'checked'; } ?>> <?php esc_html_e( 'Enable', 'awesome-support' ); ?></label>
155 <p class="description"><?php esc_attr_e( 'If Smart Tickets Order is enabled, Awesome Support will display tickets that need immediate attention at the top.', 'awesome-support' ); ?></p>
156 </td>
157 </tr>
158
159 <?php }
160
161 /**
162 * User profile field "after reply"
163 *
164 * @since 3.1.5
165 *
166 * @param WP_User $user
167 *
168 * @return void
169 */
170 public function profile_field_after_reply( $user ) {
171
172 /* If this user is not an agent, then don't allow this field to be set/shown */
173 if ( ! wpas_is_agent( $user->ID ) ) {
174 return ;
175 }
176
177 ?>
178
179 <tr class="wpas-after-reply-wrap">
180 <th><label for="wpas_after_reply"><?php echo esc_html_x( 'After Reply', 'Action after replying to a ticket', 'awesome-support' ); ?></label></th>
181 <td>
182 <?php $after_reply = esc_attr( get_user_option( 'wpas_after_reply', $user->ID ) ); ?>
183 <select name="wpas_after_reply" id="wpas_after_reply">
184 <option value=""><?php esc_html_e( 'Default', 'awesome-support' ); ?></option>
185 <option value="stay" <?php if ( $after_reply === 'stay' ): ?>selected="selected"<?php endif; ?>><?php esc_html_e( 'Stay on screen', 'awesome-support' ); ?></option>
186 <option value="back" <?php if ( $after_reply === 'back' ): ?>selected="selected"<?php endif; ?>><?php esc_html_e( 'Back to list', 'awesome-support' ); ?></option>
187 <option value="ask" <?php if ( $after_reply === 'ask' ): ?>selected="selected"<?php endif; ?>><?php esc_html_e( 'Always ask', 'awesome-support' ); ?></option>
188 </select>
189 <p class="description"><?php esc_html_e( 'Where do you want to go after replying to a ticket?', 'awesome-support' ); ?></p>
190 </td>
191 </tr>
192
193 <?php }
194
195 /**
196 * User profile field "can be assigned"
197 *
198 * @since 3.1.5
199 *
200 * @param WP_User $user
201 *
202 * @return void
203 */
204 public function profile_field_user_can_be_assigned( $user ) {
205
206 /* Only admins can set this field for an agent */
207 if ( ! wpas_is_asadmin() ) {
208 return;
209 }
210
211 /* If this user is not an agent, then don't allow this field to be set/shown */
212 if ( ! wpas_is_agent( $user->ID ) ) {
213 return ;
214 }
215
216 ?>
217
218 <tr class="wpas-after-reply-wrap">
219 <th><label><?php esc_html_e( 'Can Be Assigned', 'awesome-support' ); ?></label></th>
220 <td>
221 <?php $can_assign = esc_attr( get_user_option( 'wpas_can_be_assigned', $user->ID ) ); ?>
222 <label for="wpas_can_be_assigned"><input type="checkbox" name="wpas_can_be_assigned" id="wpas_can_be_assigned" value="yes" <?php if ( ! empty( $can_assign ) ) { echo 'checked'; } ?>> <?php esc_html_e( 'Yes', 'awesome-support' ); ?></label>
223 <p class="description"><?php esc_html_e( 'Can the system assign new tickets to this user?', 'awesome-support' ); ?></p>
224 </td>
225 </tr>
226
227 <?php
228 }
229
230
231 /**
232 * User profile field "View All Tickets"
233 *
234 * @since 3.3.5
235 *
236 * @param WP_User $user
237 *
238 * @return void
239 */
240 public function profile_field_user_view_all_tickets( $user ) {
241
242 if ( ! user_can( $user->ID, 'view_all_tickets' ) ) {
243 return;
244 } ?>
245
246 <tr class="wpas-after-reply-wrap">
247 <th><label><?php esc_html_e( 'View All Tickets', 'awesome-support' ); ?></label></th>
248 <td>
249 <?php $view_all_tickets = esc_attr( get_user_option( 'wpas_view_all_tickets', $user->ID ) ); ?>
250 <label for="wpas_view_all_tickets"><input type="checkbox" name="wpas_view_all_tickets" id="wpas_view_all_tickets" value="yes" <?php if ( ! empty( $view_all_tickets ) ) { echo 'checked'; } ?>> <?php esc_html_e( 'Yes', 'awesome-support' ); ?></label>
251 <p class="description"><?php esc_html_e( 'If agents role is allowed to view all tickets, turn on the option to do so?', 'awesome-support' ); ?></p>
252 </td>
253 </tr>
254
255 <?php }
256
257
258 /**
259 * User profile field "departments"
260 *
261 * @since 3.3
262 *
263 * @param WP_User $user
264 *
265 * @return void
266 */
267 public function profile_field_agent_department( $user ) {
268
269 /* Only admins can set the dept field for an agent */
270 if ( ! wpas_is_asadmin() ) {
271 return;
272 }
273
274 /* If this user is not an agent, then don't allow this field to be set/shown */
275 if ( ! wpas_is_agent( $user->ID ) ) {
276 return ;
277 }
278
279 if ( false == wpas_get_option( 'departments', false ) ) {
280 return;
281 }
282
283 $departments = get_terms( array(
284 'taxonomy' => 'department',
285 'hide_empty' => false,
286 ) );
287
288 if ( empty( $departments ) ) {
289 return;
290 }
291
292 $current = get_user_option( 'wpas_department', $user->ID );
293
294 $current = is_array( $current ) ? $current : array();
295
296 ?>
297
298 <tr class="wpas-after-reply-wrap">
299 <th><label><?php esc_html_e( 'Department(s)', 'awesome-support' ); ?></label></th>
300 <td>
301 <?php
302 foreach ( $departments as $department ) {
303 $checked = in_array( $department->term_id, $current ) ? 'checked="checked"' : '';
304 printf( '<label for="wpas_department_%1$s"><input type="checkbox" name="%3$s" id="wpas_department_%1$s" value="%2$d" %5$s> %4$s</label><br>', esc_attr( $department->slug ), esc_attr( $department->term_id ), 'wpas_department[]', esc_attr( $department->name ), esc_attr( $checked ) );
305 }
306 ?>
307 <p class="description"><?php esc_html_e( 'Which department(s) does this agent belong to?', 'awesome-support' ); ?></p>
308 </td>
309 </tr>
310
311 <?php }
312
313
314 /**
315 * User profile field "allow assignment to"
316 *
317 * @since 3.3
318 *
319 * @param WP_User $user
320 *
321 * @return void
322 */
323 public function profile_field_allow_assignment_to( $user ) {
324
325 /* Only admins can set the dept field for an agent */
326 if ( ! wpas_is_asadmin() ) {
327 return;
328 }
329
330 if ( false == wpas_get_option( 'departments', false ) ) {
331 return;
332 }
333
334 $departments = get_terms( array(
335 'taxonomy' => 'department',
336 'hide_empty' => false,
337 ) );
338
339 if ( empty( $departments ) || is_wp_error( $departments ) ) {
340 return;
341 }
342 if( !class_exists( 'Smart_Agent_Assignment' ) ) {
343 return;
344 }
345 $current = get_user_option( 'wpas_department_assignment', $user->ID );
346 $current = is_array( $current ) ? $current : array();
347
348 ?>
349
350 <tr class="wpas-after-reply-wrap">
351 <th><label><?php esc_html_e( 'Allow assignment to', 'awesome-support' ); ?></label></th>
352 <td>
353 <?php
354 $checked_all = in_array( 0, $current ) ? 'checked="checked"' : '';
355 printf( '<label for="wpas_department_assignment_%1$s"><input type="checkbox" name="%3$s" id="wpas_department_assignment_%1$s" value="%2$d" %5$s> %4$s</label><br>', 'all', 0, 'wpas_department_assignment[]', 'Users from all departments', wp_kses_post($checked_all) );
356 ?>
357 <?php
358 foreach ( $departments as $department ) {
359 $checked = in_array( $department->term_id, $current ) ? 'checked="checked"' : '';
360 printf( '<label for="wpas_department_assignment_%1$s"><input type="checkbox" name="%3$s" id="wpas_department_assignment_%1$s" value="%2$d" %5$s> %4$s</label><br>', wp_kses_post($department->slug), wp_kses_post($department->term_id), 'wpas_department_assignment[]', wp_kses_post($department->name), wp_kses_post($checked) );
361 }
362 ?>
363 <p class="description"><?php esc_html_e( 'To agents from which departments is the user allowed to assign tickets', 'awesome-support' ); ?></p>
364 </td>
365 </tr>
366
367 <?php
368 }
369
370
371 /**
372 * Save the user preferences.
373 *
374 * @since 3.0.0
375 *
376 * @param integer $user_id ID of the user to modify
377 *
378 * @return void
379 */
380 public function save_user_custom_fields( $user_id ) {
381
382 if ( ! current_user_can( 'edit_user', $user_id ) ) {
383 return;
384 }
385
386 $wpas_after_reply = filter_input( INPUT_POST, 'wpas_after_reply' );
387 $can_assign = filter_input( INPUT_POST, 'wpas_can_be_assigned' );
388 $smart = filter_input( INPUT_POST, 'wpas_smart_tickets_order' );
389 $view_all_tickets = filter_input( INPUT_POST, 'wpas_view_all_tickets' );
390 $department = isset( $_POST['wpas_department'] ) ? array_map( 'intval', $_POST['wpas_department'] ) : array();
391 $department_assignment = isset( $_POST['wpas_department_assignment'] ) ? array_map( 'intval', $_POST['wpas_department_assignment'] ) : array();
392
393 $mobile_phone = filter_input( INPUT_POST, 'wpas_mobile_phone' );
394 $office_phone = filter_input( INPUT_POST, 'wpas_office_phone' );
395 $home_phone = filter_input( INPUT_POST, 'wpas_home_phone' );
396 $other_phone = filter_input( INPUT_POST, 'wpas_other_phone' );
397
398
399 if ( $wpas_after_reply ) {
400 update_user_option( $user_id, 'wpas_after_reply', $wpas_after_reply );
401 }
402
403 update_user_option( $user_id, 'wpas_can_be_assigned', $can_assign );
404 update_user_option( $user_id, 'wpas_smart_tickets_order', $smart );
405 update_user_option( $user_id, 'wpas_department', $department );
406 update_user_option( $user_id, 'wpas_department_assignment', $department_assignment );
407 update_user_option( $user_id, 'wpas_view_all_tickets', $view_all_tickets );
408
409 update_user_option( $user_id, 'wpas_mobile_phone', $mobile_phone );
410 update_user_option( $user_id, 'wpas_office_phone', $office_phone );
411 update_user_option( $user_id, 'wpas_home_phone', $home_phone );
412 update_user_option( $user_id, 'wpas_other_phone', $other_phone );
413
414 }
415
416 /**
417 * Enable auto-assignment for new agents
418 *
419 * @since 3.2
420 *
421 * @param int $user_id
422 *
423 * @return void
424 */
425 public function enable_assignment( $user_id ) {
426 if ( user_can( $user_id, 'edit_ticket' ) && ! user_can( $user_id, 'administrator' ) ) {
427 update_user_option( $user_id, 'wpas_can_be_assigned', 'yes' );
428 }
429 }
430
431 /**
432 * Maybe enable auto assignment for this user
433 *
434 * Unfortunately there is no way to know what were the previous user capabilities
435 * which makes it impossible to safely enable auto-assignment.
436 * We are not able to differentiate a user being upgraded to support agent from a user
437 * who already was an agent but deactivated auto assignment and updated his profile.
438 *
439 * @since 3.2
440 *
441 * @param int $user_id
442 * @param array $old_data
443 *
444 * @return void
445 */
446 public function maybe_enable_assignment( $user_id, $old_data ) {
447 if ( user_can( $user_id, 'edit_ticket' ) ) {
448 $this->enable_assignment( $user_id );
449 }
450 }
451
452 /**
453 * Add auto-assignment column in users table
454 *
455 * @since 3.2
456 *
457 * @param array $columns
458 *
459 * @return mixed
460 */
461 public function auto_assignment_user_column( $columns ) {
462
463 $columns['wpas_auto_assignment'] = __( 'Auto-Assign', 'awesome-support' );
464
465 return $columns;
466 }
467
468 /**
469 * Add auto-assignment user column content
470 *
471 * @since 3.2
472 *
473 * @param mixed $value Column value
474 * @param string $column_name Column name
475 * @param int $user_id Current user ID
476 *
477 * @return string
478 */
479 public function auto_assignment_user_column_content( $value, $column_name, $user_id ) {
480
481 if ( 'wpas_auto_assignment' !== $column_name ) {
482 return $value;
483 }
484
485 $agent = new WPAS_Member_Agent( $user_id );
486
487 if ( true !== $agent->is_agent() ) {
488 return 'N/A';
489 }
490
491 if ( false === $agent->can_be_assigned() ) {
492 return '&#10005;';
493 }
494
495 return '&#10003;';
496
497 }
498
499 }
500