| 1 |
<?php |
| 2 |
|
| 3 |
class LSD_API_Controller extends LSD_API |
| 4 |
{ |
| 5 |
protected LSD_API_Validation $validate; |
| 6 |
|
| 7 |
public function __construct() |
| 8 |
{ |
| 9 |
parent::__construct(); |
| 10 |
|
| 11 |
// Validation |
| 12 |
$this->validate = new LSD_API_Validation(); |
| 13 |
} |
| 14 |
|
| 15 |
public function getUserToken($user_id): string |
| 16 |
{ |
| 17 |
$token = $this->str_random(40); |
| 18 |
update_user_meta($user_id, 'lsd_token', $token); |
| 19 |
|
| 20 |
return $token; |
| 21 |
} |
| 22 |
|
| 23 |
public function revokeUserToken($user_id) |
| 24 |
{ |
| 25 |
delete_user_meta($user_id, 'lsd_token'); |
| 26 |
} |
| 27 |
|
| 28 |
public function getLoginKey($user_id): string |
| 29 |
{ |
| 30 |
$token = $this->str_random(40); |
| 31 |
update_user_meta($user_id, 'lsd_login', $token); |
| 32 |
|
| 33 |
return $token; |
| 34 |
} |
| 35 |
|
| 36 |
public function revokeLoginKey($user_id) |
| 37 |
{ |
| 38 |
delete_user_meta($user_id, 'lsd_login'); |
| 39 |
} |
| 40 |
|
| 41 |
public function perform(WP_REST_Request $request): WP_REST_Response |
| 42 |
{ |
| 43 |
$response = new WP_REST_Response(['success' => 1]); |
| 44 |
$response->set_status(200); |
| 45 |
|
| 46 |
return $response; |
| 47 |
} |
| 48 |
|
| 49 |
public function permission(WP_REST_Request $request) |
| 50 |
{ |
| 51 |
// Validate API Token |
| 52 |
if (!$this->validate->APIToken($request, $request->get_header('lsd-token'))) return new WP_Error('invalid_api_token', esc_html__('Invalid API Token!', 'listdom')); |
| 53 |
|
| 54 |
// Validate User Token |
| 55 |
if (!$this->validate->UserToken($request, $request->get_header('lsd-user'))) return new WP_Error('invalid_user_token', esc_html__('Invalid User Token!', 'listdom')); |
| 56 |
|
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
public function guest(WP_REST_Request $request) |
| 61 |
{ |
| 62 |
// Validate API Token |
| 63 |
if (!$this->validate->APIToken($request, $request->get_header('lsd-token'))) return new WP_Error('invalid_api_token', esc_html__('Invalid API Token!', 'listdom')); |
| 64 |
|
| 65 |
// Set Current User if Token Provided |
| 66 |
$this->validate->UserToken($request, $request->get_header('lsd-user')); |
| 67 |
|
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
public function response(array $response): WP_REST_Response |
| 72 |
{ |
| 73 |
$data = $response['data'] ?? []; |
| 74 |
$status = $response['status'] ?? 200; |
| 75 |
|
| 76 |
$wp = new WP_REST_Response($data); |
| 77 |
$wp->set_status($status); |
| 78 |
|
| 79 |
return $wp; |
| 80 |
} |
| 81 |
} |
| 82 |
|