Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
65.71% covered (warning)
65.71%
23 / 35
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SAL_Token
69.70% covered (warning)
69.70%
23 / 33
50.00% covered (danger)
50.00%
2 / 4
14.37
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 is_global
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 for_anonymous_user
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 from_rest_token
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
8
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * SAL_Token class
4 *
5 * @package automattic/jetpack
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12/**
13 * Base class for Jetpack_Site, so that we have a real class instead of just passing around an array.
14 */
15class SAL_Token {
16
17    /**
18     * The Jetpack blog ID for the site.
19     *
20     * @var int
21     */
22    public $blog_id;
23
24    /**
25     * The Jetpack user's user ID.
26     *
27     * @var int
28     */
29    public $user_id;
30
31    /**
32     * The scope for the token, for example global or auth.
33     *
34     * @var string
35     */
36    public $scope;
37
38    /**
39     * The Client ID (or WordPress.com Blog ID of this site.
40     *
41     * @var int
42     */
43    public $client_id;
44
45    /**
46     * The user ID on the local site.
47     *
48     * @var int
49     */
50    public $external_user_id;
51
52    /**
53     * Used for tokens created by Oauth clients.
54     *
55     * @var string
56     */
57    public $external_user_code;
58
59    /**
60     * The type of authorization based on where the Jetpack connection is made - eg 'calypso', 'jetpack', 'client'.
61     *
62     * @var string
63     */
64    public $auth_type;
65
66    /**
67     * Contructs the SAL_Token instance.
68     *
69     * @param int    $blog_id The Jetpack blog ID for the site.
70     * @param int    $user_id The Jetpack user's user ID.
71     * @param string $scope The scope for the token, for example global or auth.
72     * @param int    $client_id The Client ID (or WordPress.com Blog ID of this site.
73     * @param int    $external_user_id The user ID on the local site.
74     * @param string $external_user_code Used for tokens created by Oauth clients.
75     * @param string $auth_type The type of authorization based on where the Jetpack connection is made (eg. calypso).
76     */
77    public function __construct( $blog_id, $user_id, $scope, $client_id, $external_user_id, $external_user_code, $auth_type ) {
78        $this->blog_id            = $blog_id; // if blog_id is set and scope is not global, limit to that blog.
79        $this->user_id            = $user_id;
80        $this->client_id          = $client_id;
81        $this->scope              = $scope;
82        $this->external_user_id   = $external_user_id;
83        $this->external_user_code = $external_user_code;
84        $this->auth_type          = $auth_type;
85    }
86
87    /**
88     * Checks if the scope is 'global'.
89     *
90     * @return bool
91     */
92    public function is_global() {
93        return $this->scope === 'global';
94    }
95
96    /**
97     * This function is used to create a SAL_Token instance with only a user id, if a token doesn't already exist.
98     *
99     * @return SAL_Token
100     */
101    public static function for_anonymous_user() {
102        return new SAL_Token(
103            null,
104            get_current_user_id(),
105            null, // there's only ever one scope in our current API implementation, auth or global.
106            null,
107            null,
108            null,
109            null
110        );
111    }
112
113    /**
114     * If a user token exists, the information is used to construct a SAL_Token with the correct parameters.
115     *
116     * @param array $token An array of details relevant to the connected user (may be empty).
117     *
118     * @return SAL_Token
119     */
120    public static function from_rest_token( $token ) {
121        $user_id            = isset( $token['user_id'] ) ? $token['user_id'] : get_current_user_id();
122        $scope              = isset( $token['scope'][0] ) ? $token['scope'][0] : null;
123        $client_id          = isset( $token['client_id'] ) ? $token['client_id'] : null;
124        $external_user_id   = isset( $token['external_user_id'] ) ? $token['external_user_id'] : null;
125        $external_user_code = isset( $token['external_user_code'] ) ? $token['external_user_code'] : null;
126        $auth               = isset( $token['auth'] ) ? $token['auth'] : null;
127        $blog_id            = isset( $token['blog_id'] ) ? $token['blog_id'] : null;
128
129        return new SAL_Token(
130            $blog_id,
131            $user_id,
132            $scope, // there's only ever one scope in our current API implementation, auth or global.
133            $client_id,
134            $external_user_id,
135            $external_user_code,
136            $auth
137        );
138    }
139}