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