PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 14.2
Jetpack – WP Security, Backup, Speed, & Growth v14.2
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / sal / class.json-api-token.php

class.json-api-token.php in Jetpack – WP Security, Backup, Speed, & Growth 14.2, at sal/class.json-api-token.php

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