4
4
5
5
use WP_Error ;
6
6
use WP \OAuth2 \Client ;
7
- use WP_Query ;
8
7
use WP_User ;
8
+ use WP_User_Query ;
9
9
10
10
class Access_Token extends Token {
11
11
const META_PREFIX = '_oauth2_access_ ' ;
@@ -19,21 +19,12 @@ protected function get_meta_prefix() {
19
19
}
20
20
21
21
/**
22
- * Get the ID for the user that the token represents .
22
+ * Get client for the token.
23
23
*
24
- * @return int
24
+ * @return Client|null
25
25
*/
26
- public function get_user_id () {
27
- return (int ) $ this ->value ['user ' ];
28
- }
29
-
30
- /**
31
- * Get the user that the token represents.
32
- *
33
- * @return WP_User|null
34
- */
35
- public function get_user () {
36
- return get_user_by ( 'id ' , $ this ->get_user_id () );
26
+ public function get_client () {
27
+ return Client::get_by_id ( $ this ->value ['client ' ] );
37
28
}
38
29
39
30
/**
@@ -45,28 +36,28 @@ public function get_user() {
45
36
public static function get_by_id ( $ id ) {
46
37
$ key = static ::META_PREFIX . $ id ;
47
38
$ args = array (
48
- 'post_type ' => Client::POST_TYPE ,
49
- 'post_status ' => 'publish ' ,
50
- 'posts_per_page ' => 1 ,
51
- 'no_found_rows ' => true ,
52
- 'meta_query ' => array (
39
+ 'number ' => 1 ,
40
+ 'count_total ' => false ,
41
+ 'meta_query ' => array (
53
42
array (
54
43
'key ' => $ key ,
55
44
'compare ' => 'EXISTS ' ,
56
45
),
57
46
),
58
47
);
59
- $ query = new WP_Query ( $ args );
60
- if ( empty ( $ query ->posts ) ) {
48
+ $ query = new WP_User_Query ( $ args );
49
+ $ results = $ query ->get_results ();
50
+ if ( empty ( $ results ) ) {
61
51
return null ;
62
52
}
63
53
64
- $ value = get_post_meta ( $ query ->posts [0 ]->ID , wp_slash ( $ key ), false );
54
+ $ user = $ results [0 ];
55
+ $ value = get_user_meta ( $ user ->ID , wp_slash ( $ key ), false );
65
56
if ( empty ( $ value ) ) {
66
57
return null ;
67
58
}
68
59
69
- return new static ( $ key , $ value [0 ] );
60
+ return new static ( $ user , $ key , $ value [0 ] );
70
61
}
71
62
72
63
/**
@@ -86,20 +77,20 @@ public static function create( Client $client, WP_User $user ) {
86
77
}
87
78
88
79
$ data = array (
89
- 'user ' => ( int ) $ user -> ID ,
80
+ 'client ' => $ client -> get_id () ,
90
81
);
91
82
$ key = wp_generate_password ( static ::KEY_LENGTH , false );
92
83
$ meta_key = static ::META_PREFIX . $ key ;
93
84
94
- $ result = add_post_meta ( $ client -> get_post_id () , wp_slash ( $ meta_key ), wp_slash ( $ data ), true );
85
+ $ result = add_user_meta ( $ user -> ID , wp_slash ( $ meta_key ), wp_slash ( $ data ), true );
95
86
if ( ! $ result ) {
96
87
return new WP_Error (
97
88
'oauth2.tokens.access_token.create.could_not_create ' ,
98
89
__ ( 'Unable to create token. ' , 'oauth2 ' )
99
90
);
100
91
}
101
92
102
- return new static ( $ key , $ data );
93
+ return new static ( $ user , $ key , $ data );
103
94
}
104
95
105
96
/**
0 commit comments