| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_REST_Current_User' ) ) : |
| 7 |
|
| 8 |
final class WPSC_REST_Current_User { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize this class |
| 12 |
* |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
public static function init() { |
| 16 |
|
| 17 |
add_action( 'wpsc_rest_register_routes', array( __CLASS__, 'register_routes' ) ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Register routes |
| 22 |
* |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public static function register_routes() { |
| 26 |
|
| 27 |
register_rest_route( |
| 28 |
'supportcandy/v2', |
| 29 |
'/current-user', |
| 30 |
array( |
| 31 |
'methods' => 'GET', |
| 32 |
'callback' => array( __CLASS__, 'get_current_user' ), |
| 33 |
'permission_callback' => 'is_user_logged_in', |
| 34 |
), |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Current user information |
| 40 |
* |
| 41 |
* @param WP_REST_Request $request - request object. |
| 42 |
* @return WP_Error|WP_REST_Response |
| 43 |
*/ |
| 44 |
public static function get_current_user( $request ) { |
| 45 |
|
| 46 |
$current_user = WPSC_Current_User::$current_user; |
| 47 |
|
| 48 |
$data = array( |
| 49 |
'name' => $current_user->customer->name, |
| 50 |
'email' => $current_user->customer->email, |
| 51 |
'is_agent' => $current_user->is_agent, |
| 52 |
); |
| 53 |
|
| 54 |
if ( $current_user->is_agent ) { |
| 55 |
|
| 56 |
$agent_roles = get_option( 'wpsc-agent-roles', array() ); |
| 57 |
$data['agent_caps'] = $agent_roles[ $current_user->agent->role ]['caps']; |
| 58 |
} |
| 59 |
|
| 60 |
return new WP_REST_Response( $data, 200 ); |
| 61 |
} |
| 62 |
} |
| 63 |
endif; |
| 64 |
|
| 65 |
WPSC_REST_Current_User::init(); |
| 66 |
|