0, 'meta_query' => array( 'relation' => 'AND', array( 'slug' => 'is_active', 'compare' => '=', 'val' => 1, ), array( 'slug' => 'is_agentgroup', 'compare' => '=', 'val' => 0, ), ), ); $agents = WPSC_Agent::find( $args )['results']; $roles = get_option( 'wpsc-agent-roles', array() ); $unique_id = uniqid( 'wpsc_' );?>

' . esc_attr__( 'Click here', 'supportcandy' ) . '' ); ?>
name ); ?> customer->email ); ?> role ]['label'] ); ?> |
$title, 'body' => $body, 'footer' => $footer, ); wp_send_json( $response ); } /** * Create an agent * * @return void */ public static function set_add_agent() { if ( check_ajax_referer( 'wpsc_set_add_agent', '_ajax_nonce', false ) != 1 ) { wp_send_json_error( 'Unauthorised request!', 401 ); } if ( ! WPSC_Functions::is_site_admin() ) { wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); } $users = isset( $_POST['users'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['users'] ) ) ) : array(); if ( ! $users ) { wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); } $roles = get_option( 'wpsc-agent-roles', array() ); $role = isset( $_POST['role'] ) ? intval( $_POST['role'] ) : 0; if ( ! $role || ! isset( $roles[ $role ] ) ) { wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); } foreach ( $users as $user_id ) { $user = new WP_User( $user_id ); if ( ! $user->ID ) { continue; } $agent = WPSC_Agent::get_by_user_id( $user->ID ); // skip if agent is already active. if ( $agent->id && $agent->is_active ) { continue; } if ( $agent->id ) { $customer = WPSC_Customer::get_by_user_id( $user->ID ); $agent->is_active = 1; $agent->role = $role; $agent->customer = $customer->id; $agent->save(); $agent->user->add_cap( 'wpsc_agent' ); } elseif ( ! $agent->id ) { $agent = self::insert_new_record( $user, $role ); } // reset agent counts. $agent->reset_workload(); $agent->reset_unresolved_count(); } do_action( 'after_set_add_agent', $agent ); wp_die(); } /** * Edit agent modal popup UI * * @return void */ public static function get_edit_agent() { if ( check_ajax_referer( 'wpsc_get_edit_agent', '_ajax_nonce', false ) != 1 ) { wp_send_json_error( 'Unauthorised request!', 401 ); } if ( ! WPSC_Functions::is_site_admin() ) { wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); } $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0; if ( ! $id ) { wp_send_json_error( __( 'Unauthorized access', 'supportcandy' ), 401 ); } $agent = new WPSC_Agent( $id ); if ( ! $agent->id ) { wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); } $title = $agent->name; $roles = get_option( 'wpsc-agent-roles', array() ); ob_start(); ?>
$title, 'body' => $body, 'footer' => $footer, ); wp_send_json( $response ); } /** * Update an agent */ public static function set_edit_agent() { if ( check_ajax_referer( 'wpsc_set_edit_agent', '_ajax_nonce', false ) != 1 ) { wp_send_json_error( 'Unauthorised request!', 401 ); } if ( ! WPSC_Functions::is_site_admin() ) { wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); } $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0; if ( ! $id ) { wp_send_json_error( __( 'Unauthorized access', 'supportcandy' ), 401 ); } $roles = get_option( 'wpsc-agent-roles', array() ); $role = isset( $_POST['role'] ) ? intval( $_POST['role'] ) : 0; if ( ! $role || ! isset( $roles[ $role ] ) ) { wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); } $agent = new WPSC_Agent( $id ); if ( ! $agent->id ) { wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); } if ( $agent->role != $role ) { $agent->role = $role; if ( $agent->has_cap( 'backend-access' ) ) { $agent->user->add_cap( 'wpsc_agent' ); } else { $agent->user->remove_cap( 'wpsc_agent' ); } } $agent = apply_filters( 'wpsc_set_edit_agent', $agent ); $agent->save(); wp_die(); } /** * Delete an agent * * @return void */ public static function delete_agent() { if ( check_ajax_referer( 'wpsc_get_delete_agent', '_ajax_nonce', false ) != 1 ) { wp_send_json_error( 'Unauthorised request!', 401 ); } if ( ! WPSC_Functions::is_site_admin() ) { wp_send_json_error( 'Unauthorized!', 401 ); } $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0; if ( ! $id ) { wp_send_json_error( 'Bad Request', 400 ); } $agent = new WPSC_Agent( $id ); if ( ! $agent->id ) { wp_send_json_error( 'Bad Request', 400 ); } $agent->is_active = 0; $success = $agent->save(); // Remove agent capability. if ( $agent->has_cap( 'backend-access' ) ) { $agent->user->remove_cap( 'wpsc_agent' ); } do_action( 'wpsc_delete_agent', $agent ); wp_die(); } /** * Search WordPress users for autocomplete purposes * * @return void */ public static function search_wp_users() { if ( check_ajax_referer( 'wpsc_search_wp_users', '_ajax_nonce', false ) != 1 ) { wp_send_json_error( 'Unauthorized request!', 400 ); } global $wpdb; if ( ! WPSC_Functions::is_site_admin() ) { wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); } $term = isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : ''; // search in wp user table. $users = ( new WP_User_Query( array( 'search' => '*' . esc_attr( $term ) . '*', 'search_columns' => array( 'user_login', 'user_nicename', 'user_email', 'display_name', ), 'number' => 10, ) ) )->get_results(); $response = array(); foreach ( $users as $user ) { $response[] = array( 'id' => $user->ID, 'title' => $user->display_name, 'email' => $user->user_email, ); } wp_send_json( $response ); } /** * Create new agent * * @param object $user - user name. * @param integer $role - role id. * @return WPSC_Agent */ private static function insert_new_record( $user, $role ) { $customer = WPSC_Customer::get_by_user_id( $user->ID ); // Create agent record. $data = array( 'user' => $user->ID, 'customer' => $customer->id, 'role' => $role, 'name' => $user->display_name, 'is_active' => 1, ); $agent = WPSC_Agent::insert( $data ); // Give agent capability to this WP user. if ( $agent->has_cap( 'backend-access' ) ) { $user->add_cap( 'wpsc_agent' ); } // working hrs. $company_whs = WPSC_Working_Hour::get(); for ( $i = 1; $i <= 7; $i++ ) { $data = array( 'agent' => $agent->id, 'day' => $i, 'start_time' => $company_whs[ $i ]->start_time, 'end_time' => $company_whs[ $i ]->end_time, ); WPSC_Working_Hour::insert( $data ); } return $agent; } /** * After user deleted * * @param integer $user_id - user id. * @param integer $reassign - reassign. * @param object $user -user info. * @return void */ public static function wp_user_delete( $user_id, $reassign, $user ) { global $wpdb; $agent = WPSC_Agent::get_by_user_id( $user_id ); if ( ! $agent->id || ! $agent->is_active ) { return; } $agent->is_active = 0; $agent->user = 0; $agent->save(); do_action( 'wpsc_delete_agent', $agent ); } /** * After new user added * * @param integer $user_id - user id. * @param array $user_data - user data. * @return void */ public static function wp_user_register( $user_id, $user_data ) { $customer = WPSC_Customer::get_by_email( $user_data['user_email'] ); if ( ! $customer->id ) { return; } $agent = WPSC_Agent::get_by_customer( $customer ); if ( ! $agent->id ) { return; } $name = $user_data['first_name'] . ' ' . $user_data['last_name']; $name = trim( $name ) ? $name : $user_data['user_login']; $agent->user = $user_id; $agent->name = $name; $agent->save(); } } endif; WPSC_Agent_Settings::init();