Нет описания

REST.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace NSL;
  3. use Exception;
  4. use NextendSocialLogin;
  5. use WP_Error;
  6. use WP_REST_Request;
  7. use WP_REST_Response;
  8. use function add_action;
  9. use function register_rest_route;
  10. class REST {
  11. public function __construct() {
  12. add_action('rest_api_init', array(
  13. $this,
  14. 'rest_api_init'
  15. ));
  16. }
  17. public function rest_api_init() {
  18. register_rest_route('nextend-social-login/v1', '/(?P<provider>\w[\w\s\-]*)/get_user', array(
  19. 'args' => array(
  20. 'provider' => array(
  21. 'required' => true,
  22. 'validate_callback' => array(
  23. $this,
  24. 'validate_provider'
  25. )
  26. ),
  27. 'access_token' => array(
  28. 'required' => true,
  29. ),
  30. ),
  31. array(
  32. 'methods' => 'POST',
  33. 'callback' => array(
  34. $this,
  35. 'get_user'
  36. ),
  37. 'permission_callback' => '__return_true'
  38. ),
  39. ));
  40. }
  41. public function validate_provider($providerID) {
  42. return NextendSocialLogin::isProviderEnabled($providerID);
  43. }
  44. /**
  45. * @param WP_REST_Request $request Full details about the request.
  46. *
  47. * @return WP_Error|WP_REST_Response
  48. */
  49. public function get_user($request) {
  50. $provider = NextendSocialLogin::$enabledProviders[$request['provider']];
  51. try {
  52. $user = $provider->findUserByAccessToken($request['access_token']);
  53. } catch (Exception $e) {
  54. return new WP_Error('error', $e->getMessage());
  55. }
  56. return $user;
  57. }
  58. }
  59. new REST();