Skip to content

Commit 9093493

Browse files
committed
Introduce the client object
1 parent fd031fa commit 9093493

7 files changed

+297
-127
lines changed

client.php

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

lib/class-wp-rest-client.php

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
<?php
2+
3+
abstract class WP_REST_Client {
4+
/**
5+
* Get the client type.
6+
*
7+
* Must be overridden in subclass.
8+
*
9+
* @return string
10+
*/
11+
protected static function get_type() {
12+
return new WP_Error( 'rest_client_missing_type', __( 'Overridden class must implement get_type', 'rest_oauth1' ) );
13+
}
14+
15+
/**
16+
* Constructor.
17+
*
18+
* @param WP_Post $post Underlying post data.
19+
*/
20+
protected function __construct( WP_Post $post ) {
21+
$this->post = $post;
22+
}
23+
24+
/**
25+
* Getter.
26+
*
27+
* Passes through to post object.
28+
*
29+
* @param string $key Key to get.
30+
* @return mixed
31+
*/
32+
public function __get( $key ) {
33+
return $this->post->$key;
34+
}
35+
36+
/**
37+
* Isset-er.
38+
*
39+
* Passes through to post object.
40+
*
41+
* @param string $key Property to check if set.
42+
* @return bool
43+
*/
44+
public function __isset( $key ) {
45+
return isset( $this->post->$key );
46+
}
47+
48+
/**
49+
* Update the client's post.
50+
*
51+
* @param array $params Parameters to update.
52+
* @return bool|WP_Error True on success, error object otherwise.
53+
*/
54+
public function update( $params ) {
55+
$data = array();
56+
if ( isset( $params['name'] ) ) {
57+
$data['post_title'] = $params['name'];
58+
}
59+
if ( isset( $params['description'] ) ) {
60+
$data['post_content'] = $params['description'];
61+
}
62+
63+
// Are we updating the post itself?
64+
if ( ! empty( $data ) ) {
65+
$data['ID'] = $this->post->ID;
66+
67+
$result = wp_update_post( wp_slash( $data ), true );
68+
if ( is_wp_error( $result ) ) {
69+
return $result;
70+
}
71+
72+
// Reload the post property
73+
$this->post = get_post( $this->post->ID );
74+
}
75+
76+
// Are we updating any meta?
77+
if ( ! empty( $params['meta'] ) ) {
78+
$meta = $params['meta'];
79+
80+
foreach ( $meta as $key => $value ) {
81+
$existing = get_post_meta( $this->post->ID, $key, true );
82+
if ( $existing === $value ) {
83+
continue;
84+
}
85+
86+
$did_update = update_post_meta( $this->post->ID, $key, wp_slash( $value ) );
87+
if ( ! $did_update ) {
88+
return new WP_Error(
89+
'rest_client_update_meta_failed',
90+
__( 'Could not update client metadata.', 'rest_oauth' )
91+
);
92+
}
93+
}
94+
}
95+
96+
return true;
97+
}
98+
99+
/**
100+
* Delete a client.
101+
*
102+
* @param string $type Client type.
103+
* @param int $id Client post ID.
104+
* @return bool True if delete, false otherwise.
105+
*/
106+
public function delete() {
107+
return (bool) wp_delete_post( $this->post->ID, true );
108+
}
109+
110+
/**
111+
* Get a client by ID.
112+
*
113+
* @param int $id Client post ID.
114+
* @return self|WP_Error
115+
*/
116+
public static function get( $id ) {
117+
$post = get_post( $id );
118+
if ( empty( $id ) || empty( $post ) || $post->post_type !== 'json_consumer' ) {
119+
return new WP_Error( 'rest_oauth1_invalid_id', __( 'Client ID is not valid.', 'rest_oauth1' ), array( 'status' => 404 ) );
120+
}
121+
122+
$class = function_exists( 'get_called_class' ) ? get_called_class() : self::get_called_class();
123+
return new $class( $post );
124+
}
125+
126+
/**
127+
* Get a client by key.
128+
*
129+
* @param string $type Client type.
130+
* @param string $key Client key.
131+
* @return WP_Post|WP_Error
132+
*/
133+
public static function get_by_key( $key ) {
134+
$class = function_exists( 'get_called_class' ) ? get_called_class() : self::get_called_class();
135+
$type = call_user_func( array( $class, 'get_type' ) );
136+
137+
$query = new WP_Query();
138+
$consumers = $query->query( array(
139+
'post_type' => 'json_consumer',
140+
'post_status' => 'any',
141+
'meta_query' => array(
142+
array(
143+
'key' => 'key',
144+
'value' => $key,
145+
),
146+
array(
147+
'key' => 'type',
148+
'value' => $type,
149+
),
150+
),
151+
) );
152+
153+
if ( empty( $consumers ) || empty( $consumers[0] ) ) {
154+
return new WP_Error( 'json_consumer_notfound', __( 'Consumer Key is invalid' ), array( 'status' => 401 ) );
155+
}
156+
157+
return $consumers[0];
158+
}
159+
160+
/**
161+
* Create a new client.
162+
*
163+
* @param string $type Client type.
164+
* @param array $params {
165+
* @type string $name Client name
166+
* @type string $description Client description
167+
* @type array $meta Metadata for the client (map of key => value)
168+
* }
169+
* @return WP_Post|WP_Error
170+
*/
171+
public static function create( $params ) {
172+
$default = array(
173+
'name' => '',
174+
'description' => '',
175+
'meta' => array(),
176+
);
177+
$params = wp_parse_args( $params, $default );
178+
179+
$data = array();
180+
$data['post_title'] = $params['name'];
181+
$data['post_content'] = $params['description'];
182+
$data['post_type'] = 'json_consumer';
183+
184+
$ID = wp_insert_post( $data );
185+
if ( is_wp_error( $ID ) ) {
186+
return $ID;
187+
}
188+
189+
$class = function_exists( 'get_called_class' ) ? get_called_class() : self::get_called_class();
190+
$meta = $params['meta'];
191+
$meta['type'] = call_user_func( array( $class, 'get_type' ) );
192+
193+
// Allow types to add their own meta too
194+
$meta = $class::add_extra_meta( $meta, $params );
195+
196+
/**
197+
* Add extra meta to the consumer on creation.
198+
*
199+
* @param array $meta Metadata map of key => value
200+
* @param int $ID Post ID we created.
201+
* @param array $params Parameters passed to create.
202+
*/
203+
$meta = apply_filters( 'json_consumer_meta', $meta, $ID, $params );
204+
205+
foreach ( $meta as $key => $value ) {
206+
update_post_meta( $ID, $key, $value );
207+
}
208+
209+
$post = get_post( $ID );
210+
return new $class( $post );
211+
}
212+
213+
/**
214+
* Add extra meta to a post.
215+
*
216+
* If you'd like to add extra meta on client creation, add it here. This
217+
* works the same as a filter; make sure you return the original array!
218+
*
219+
* @param array $meta Metadata for the post.
220+
* @param array $params Parameters used to create the post.
221+
* @return array Metadata to actually save.
222+
*/
223+
protected static function add_extra_meta( $meta, $params ) {
224+
return $meta;
225+
}
226+
227+
/**
228+
* Shim for get_called_class() for PHP 5.2
229+
*
230+
* @return string Class name.
231+
*/
232+
protected static function get_called_class() {
233+
// PHP 5.2 only
234+
$backtrace = debug_backtrace();
235+
// [0] WP_REST_Client::get_called_class()
236+
// [1] WP_REST_Client::function()
237+
if ( 'call_user_func' === $backtrace[2]['function'] ) {
238+
return $backtrace[2]['args'][0][0];
239+
}
240+
return $backtrace[2]['class'];
241+
}
242+
}

lib/class-wp-rest-oauth1-admin.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,18 @@ protected static function handle_edit_submit( $consumer ) {
187187
'callback' => $params['callback'],
188188
),
189189
);
190-
$consumer = $result = $authenticator->add_consumer( $data );
190+
$consumer = $result = WP_REST_OAuth1_Client::create( $data );
191191
}
192192
else {
193193
// Update the existing consumer post
194194
$data = array(
195-
'ID' => $consumer->ID,
196-
'post_title' => $params['name'],
197-
'post_content' => $params['description'],
195+
'name' => $params['name'],
196+
'description' => $params['description'],
197+
'meta' => array(
198+
'callback' => $params['callback'],
199+
),
198200
);
199-
$result = wp_update_post( $data, true );
200-
update_post_meta( $consumer->ID, 'callback', wp_slash( $params['callback'] ) );
201+
$result = $consumer->update( $data );
201202
}
202203

203204
if ( is_wp_error( $result ) ) {
@@ -231,7 +232,7 @@ public static function render_edit_page() {
231232
$form_action = self::get_url('action=add');
232233
if ( ! empty( $_REQUEST['id'] ) ) {
233234
$id = absint( $_REQUEST['id'] );
234-
$consumer = get_post( $id );
235+
$consumer = WP_REST_OAuth1_Client::get( $id );
235236
if ( is_wp_error( $consumer ) || empty( $consumer ) ) {
236237
wp_die( __( 'Invalid consumer ID.' ) );
237238
}
@@ -367,7 +368,13 @@ public static function handle_delete() {
367368
$id = $_GET['id'];
368369
check_admin_referer( 'rest-oauth1-delete:' . $id );
369370

370-
if ( ! rest_delete_client( $id ) ) {
371+
$client = WP_REST_OAuth1_Client::get( $id );
372+
if ( is_wp_error( $client ) ) {
373+
wp_die( $client );
374+
return;
375+
}
376+
377+
if ( ! $client->delete() ) {
371378
$message = 'Invalid consumer ID';
372379
wp_die( $message );
373380
return;

lib/class-wp-rest-oauth1-cli.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ class WP_REST_OAuth1_CLI extends WP_CLI_Command {
1212
* : Consumer description
1313
*/
1414
public function add( $_, $args ) {
15-
$authenticator = new WP_REST_OAuth1();
16-
$consumer = $authenticator->add_consumer( $args );
15+
$consumer = WP_REST_OAuth1_Client::create( $args );
1716
WP_CLI::line( sprintf( 'ID: %d', $consumer->ID ) );
1817
WP_CLI::line( sprintf( 'Key: %s', $consumer->key ) );
1918
WP_CLI::line( sprintf( 'Secret: %s', $consumer->secret ) );

0 commit comments

Comments
 (0)