class.json-api-token.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * So that we have a real class instead of just passing around an array
  4. */
  5. class SAL_Token {
  6. public $blog_id;
  7. public $user_id;
  8. public $scope;
  9. public $client_id;
  10. public $external_user_id;
  11. public $external_user_code;
  12. public $auth_type;
  13. function __construct( $blog_id, $user_id, $scope, $client_id, $external_user_id, $external_user_code, $auth_type ) {
  14. $this->blog_id = $blog_id; // if blog_id is set and scope is not global, limit to that blog
  15. $this->user_id = $user_id;
  16. $this->client_id = $client_id;
  17. $this->scope = $scope;
  18. $this->external_user_id = $external_user_id;
  19. $this->external_user_code = $external_user_code;
  20. $this->auth_type = $auth_type;
  21. }
  22. public function is_global() {
  23. return $scope === 'global';
  24. }
  25. static function for_anonymous_user() {
  26. return new SAL_Token(
  27. null,
  28. get_current_user_id(),
  29. null, // there's only ever one scope in our current API implementation, auth or global
  30. null,
  31. null,
  32. null,
  33. null
  34. );
  35. }
  36. static function from_rest_token( $token ) {
  37. $user_id = isset( $token['user_id'] ) ? $token['user_id'] : get_current_user_id();
  38. $scope = isset( $token['scope'] ) ? $token['scope'][0] : null;
  39. $client_id = isset( $token['client_id'] ) ? $token['client_id'] : null;
  40. $external_user_id = isset( $token['external_user_id'] ) ? $token['external_user_id'] : null;
  41. $external_user_code = isset( $token['external_user_code'] ) ? $token['external_user_code'] : null;
  42. $auth = isset( $token['auth'] ) ? $token['auth'] : null;
  43. return new SAL_Token(
  44. $token['blog_id'],
  45. $user_id,
  46. $scope, // there's only ever one scope in our current API implementation, auth or global
  47. $client_id,
  48. $external_user_id,
  49. $external_user_code,
  50. $auth
  51. );
  52. }
  53. }