Skip to content

Commit 9a72656

Browse files
authored
Merge pull request #42 from WP-API/improve-storage
Improve storage mechanisms
2 parents b34b101 + 7438697 commit 9a72656

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

inc/admin/profile/namespace.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ function bootstrap() {
2626
*/
2727
function render_profile_section( WP_User $user ) {
2828
$tokens = Access_Token::get_for_user( $user );
29+
$tokens = array_filter( $tokens, function ( Access_Token $token ) {
30+
return (bool) $token->get_client();
31+
});
32+
2933
?>
3034
<h2><?php _e( 'Authorized Applications', 'oauth2' ) ?></h2>
3135
<?php if ( ! empty( $tokens ) ) : ?>

inc/authentication/namespace.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@ function attempt_authentication( $user = null ) {
117117
// Attempt to find the token.
118118
$is_querying_token = true;
119119
$token = Tokens\get_by_id( $token_value );
120+
$client = $token->get_client();
120121
$is_querying_token = false;
121122

122-
if ( empty( $token ) ) {
123+
if ( empty( $token ) || empty( $client ) ) {
123124
$oauth2_error = create_invalid_token_error( $token_value );
124125
return $user;
125126
}

inc/class-client.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
class Client {
1313
const POST_TYPE = 'oauth2_client';
14-
const CLIENT_ID_KEY = '_oauth2_client_id';
1514
const CLIENT_SECRET_KEY = '_oauth2_client_secret';
1615
const TYPE_KEY = '_oauth2_client_type';
1716
const REDIRECT_URI_KEY = '_oauth2_redirect_uri';
@@ -41,12 +40,7 @@ protected function __construct( WP_Post $post ) {
4140
* @return string Client ID.
4241
*/
4342
public function get_id() {
44-
$result = get_post_meta( $this->get_post_id(), static::CLIENT_ID_KEY, false );
45-
if ( empty( $result ) ) {
46-
return null;
47-
}
48-
49-
return $result[0];
43+
return $this->post->post_name;
5044
}
5145

5246
/**
@@ -284,12 +278,9 @@ public static function get_by_id( $id ) {
284278
'post_status' => 'publish',
285279
'posts_per_page' => 1,
286280
'no_found_rows' => true,
287-
'meta_query' => [
288-
[
289-
'key' => static::CLIENT_ID_KEY,
290-
'value' => $id,
291-
],
292-
],
281+
282+
// Query by slug.
283+
'name' => $id,
293284
];
294285
$query = new WP_Query( $args );
295286
if ( empty( $query->posts ) ) {
@@ -322,10 +313,12 @@ public static function get_by_post_id( $id ) {
322313
* @return WP_Error|Client Client instance on success, error otherwise.
323314
*/
324315
public static function create( $data ) {
316+
$client_id = wp_generate_password( static::CLIENT_ID_LENGTH, false );
325317
$post_data = [
326318
'post_type' => static::POST_TYPE,
327319
'post_title' => $data['name'],
328320
'post_content' => $data['description'],
321+
'post_name' => $client_id,
329322
'post_status' => 'draft',
330323
];
331324

@@ -338,7 +331,6 @@ public static function create( $data ) {
338331
$meta = [
339332
static::REDIRECT_URI_KEY => $data['meta']['callback'],
340333
static::TYPE_KEY => $data['meta']['type'],
341-
static::CLIENT_ID_KEY => wp_generate_password( static::CLIENT_ID_LENGTH, false ),
342334
static::CLIENT_SECRET_KEY => wp_generate_password( static::CLIENT_SECRET_LENGTH, false ),
343335
];
344336

inc/tokens/class-access-token.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ public static function get_by_id( $id ) {
6666
$args = [
6767
'number' => 1,
6868
'count_total' => false,
69-
'meta_query' => [
69+
70+
// We use an EXISTS query here, limited by 1, so we can ignore
71+
// the performance warning.
72+
'meta_query' => [ // WPCS: tax_query OK
7073
[
7174
'key' => $key,
7275
'compare' => 'EXISTS',

0 commit comments

Comments
 (0)