In order to actually get access to the ticket, I had to use the wpsc_has_permission
filter.
I think this filter should control any access to the tickets, including listing them, without having to use the wpsc_tl_customer_restrict_rules
filter. It would make the developer’s life so much easier.
To wrap up, here’s my solution to give other team members (based on my customer’s plugin logic, which is not important for the broad scenario – all the remaining code was removed ):
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Webdados_Clients class
*/
class Webdados_Clients {
/* Constructor */
public function __construct() {
//Hooks
add_action( 'plugins_loaded', array( $this, 'call_global_hooks' ) );
}
/* Global Hooks */
public function call_global_hooks() {
//SupportCandy integration
add_filter( 'wpsc_tl_customer_restrict_rules', array( $this, 'wpsc_tl_customer_restrict_rules' ) );
add_filter( 'wpsc_has_permission', array( $this, 'wpsc_has_permission' ), 10, 3 );
}
/* Allow team members to access main account tickets and vice-versa */
public function wpsc_tl_customer_restrict_rules( $restrict_rules ) {
//Use your own logic to get other users that should have access to the tickets list
$users = get_users( array( ... ) );
//Add emails to the restric_rules array
foreach( $users as $user ) {
$restrict_rules[$i] = array(
'key' => 'customer_email',
'value' => $user->data->user_email,
'compare' => '=',
);
$i++;
}
return $restrict_rules;
}
private function get_team_members_emails_for_support( $client_id ) {
$users_email = array();
$users = get_users( array(
'meta_key' => 'webdados_clients_client_id',
'meta_value' => $client_id
) );
foreach( $users as $user ) {
$users_email[] = $user->data->user_email;
}
return $users_email;
}
public function wpsc_has_permission( $response, $ticket_id, $permission ) {
//Only if the current user doesn't have access yet
if ( ! $response ) {
//Use your own logic to get other users that should have access to the tickets list (in this case, that might have created it in the first place)
$users = get_users( array( ... ) );
foreach( $users as $user ) {
$users_email[] = $user->data->user_email;
}
//Get the ticket and check if it was created by one of the team members
$ticket = $wpscfunction->get_ticket( $ticket_id );
if ( in_array( $ticket['customer_email'], $users_email ) ) {
switch( $permission ) {
//You could give other permissions - My goal is to let the current user interact with the ticket belonging to a felow team member, but not delete it, for example
case 'view_ticket':
case 'reply_ticket':
case 'change_status':
return true;
break;
}
}
}
return $response;
}
}