Skip to content

Commit 8391515

Browse files
committed
Remove base class
1 parent f956ab4 commit 8391515

File tree

4 files changed

+82
-95
lines changed

4 files changed

+82
-95
lines changed

client.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/**
4+
* Get a client by key.
5+
*
6+
* @param string $type Client type.
7+
* @param string $key Client key.
8+
* @return WP_Post|WP_Error
9+
*/
10+
function rest_get_client( $type, $key ) {
11+
// $this->should_attempt = false;
12+
13+
$query = new WP_Query();
14+
$consumers = $query->query( array(
15+
'post_type' => 'json_consumer',
16+
'post_status' => 'any',
17+
'meta_query' => array(
18+
array(
19+
'meta_key' => 'key',
20+
'meta_value' => $key,
21+
),
22+
array(
23+
'meta_key' => 'type',
24+
'meta_value' => $type,
25+
),
26+
),
27+
) );
28+
29+
// $this->should_attempt = true;
30+
31+
if ( empty( $consumers ) || empty( $consumers[0] ) ) {
32+
return new WP_Error( 'json_consumer_notfound', __( 'Consumer Key is invalid' ), array( 'status' => 401 ) );
33+
}
34+
35+
return new $consumers[0];
36+
}
37+
38+
/**
39+
* Create a new client.
40+
*
41+
* @param string $type Client type.
42+
* @param array $params {
43+
* @type string $name Client name
44+
* @type string $description Client description
45+
* @type array $meta Metadata for the client (map of key => value)
46+
* }
47+
* @return WP_Post|WP_Error
48+
*/
49+
function rest_create_client( $type, $params ) {
50+
$default = array(
51+
'name' => '',
52+
'description' => '',
53+
'meta' => array(),
54+
);
55+
$params = wp_parse_args( $params, $default );
56+
57+
$data = array();
58+
$data['post_title'] = $params['name'];
59+
$data['post_content'] = $params['description'];
60+
$data['post_type'] = 'json_consumer';
61+
62+
$ID = wp_insert_post( $data );
63+
if ( is_wp_error( $ID ) ) {
64+
return $ID;
65+
}
66+
67+
$meta = $params['meta'];
68+
$meta['type'] = $type;
69+
$meta = apply_filters( 'json_consumer_meta', $meta, $ID, $params );
70+
71+
foreach ( $meta as $key => $value ) {
72+
update_post_meta( $ID, $key, $value );
73+
}
74+
75+
return get_post( $ID );
76+
}

lib/class-wp-json-authentication-oauth1.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @subpackage JSON API
77
*/
88

9-
class WP_JSON_Authentication_OAuth1 extends WP_JSON_Authentication {
9+
class WP_JSON_Authentication_OAuth1 {
1010
const CONSUMER_KEY_LENGTH = 12;
1111
const CONSUMER_SECRET_LENGTH = 48;
1212
const TOKEN_KEY_LENGTH = 24;
@@ -300,7 +300,7 @@ public function add_consumer( $params ) {
300300
}
301301
$params['meta'] = array_merge( $params['meta'], $meta );
302302

303-
return parent::add_consumer( $params );
303+
return rest_create_client( $this->type, $params );
304304
}
305305

306306
/**
@@ -311,7 +311,7 @@ public function add_consumer( $params ) {
311311
* @return array Array of consumer object, user object
312312
*/
313313
public function check_token( $token, $consumer_key ) {
314-
$consumer = $this->get_consumer( $consumer_key );
314+
$consumer = rest_get_client( $this->type, $consumer_key );
315315
if ( is_wp_error( $consumer ) ) {
316316
return $consumer;
317317
}
@@ -352,7 +352,7 @@ public function get_request_token( $key ) {
352352
* @return array|WP_Error Array of token data on success, error otherwise
353353
*/
354354
public function generate_request_token( $params ) {
355-
$consumer = $this->get_consumer( $params['oauth_consumer_key'] );
355+
$consumer = rest_get_client( $this->type, $params['oauth_consumer_key'] );
356356
if ( is_wp_error( $consumer ) ) {
357357
return $consumer;
358358
}
@@ -479,7 +479,7 @@ public function generate_access_token( $oauth_consumer_key, $oauth_token, $oauth
479479
return new WP_Error( 'json_oauth1_invalid_verifier', __( 'OAuth verifier does not match' ), array( 'status' => 400 ) );
480480
}
481481

482-
$consumer = $this->get_consumer( $oauth_consumer_key );
482+
$consumer = rest_get_client( $this->type, $oauth_consumer_key );
483483
if ( is_wp_error( $consumer ) ) {
484484
return $consumer;
485485
}

lib/class-wp-json-authentication.php

Lines changed: 0 additions & 89 deletions
This file was deleted.

oauth-server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
* Version 0.1
66
*/
77

8-
include_once( dirname( __FILE__ ) . '/lib/class-wp-json-authentication.php' );
98
include_once( dirname( __FILE__ ) . '/lib/class-wp-json-authentication-oauth1.php' );
109
include_once( dirname( __FILE__ ) . '/lib/class-wp-rest-oauth1-ui.php' );
1110

1211
include_once( dirname( __FILE__ ) . '/admin.php' );
12+
include_once( dirname( __FILE__ ) . '/client.php' );
1313

1414
if ( defined( 'WP_CLI' ) && WP_CLI ) {
1515
include_once( dirname( __FILE__ ) . '/lib/class-wp-rest-oauth1-cli.php' );

0 commit comments

Comments
 (0)