@@ -149,8 +149,13 @@ protected static function validate_parameters( $params ) {
149
149
}
150
150
$ valid ['description ' ] = wp_filter_post_kses ( $ params ['description ' ] );
151
151
152
+ if ( empty ( $ params ['type ' ] ) ) {
153
+ return new WP_Error ( 'rest_oauth2_missing_type ' , __ ( 'Type is required. ' , 'rest_oauth2 ' ) );
154
+ }
155
+ $ valid ['type ' ] = wp_filter_post_kses ( $ params ['type ' ] );
156
+
152
157
if ( empty ( $ params ['callback ' ] ) ) {
153
- return new WP_Error ( 'rest_oauth2_missing_description ' , __ ( 'Consumer callback is required and must be a valid URL. ' , 'rest_oauth2 ' ) );
158
+ return new WP_Error ( 'rest_oauth2_missing_callback ' , __ ( 'Consumer callback is required and must be a valid URL. ' , 'rest_oauth2 ' ) );
154
159
}
155
160
if ( ! empty ( $ params ['callback ' ] ) ) {
156
161
$ valid ['callback ' ] = $ params ['callback ' ];
@@ -162,6 +167,8 @@ protected static function validate_parameters( $params ) {
162
167
/**
163
168
* Handle submission of the add page
164
169
*
170
+ * @param $consumer
171
+ *
165
172
* @return array|null List of errors. Issues a redirect and exits on success.
166
173
*/
167
174
protected static function handle_edit_submit ( $ consumer ) {
@@ -171,39 +178,41 @@ protected static function handle_edit_submit( $consumer ) {
171
178
check_admin_referer ( 'rest-oauth2-add ' );
172
179
} else {
173
180
$ did_action = 'edit ' ;
174
- check_admin_referer ( 'rest-oauth2-edit- ' . $ consumer ->ID );
181
+ check_admin_referer ( 'rest-oauth2-edit- ' . $ consumer ->get_post_id () );
175
182
}
176
183
177
184
// Check that the parameters are correct first
178
185
$ params = self ::validate_parameters ( wp_unslash ( $ _POST ) );
186
+
179
187
if ( is_wp_error ( $ params ) ) {
180
188
$ messages [] = $ params ->get_error_message ();
181
189
182
190
return $ messages ;
183
191
}
184
192
185
193
if ( empty ( $ consumer ) ) {
186
- /** @todo Implement this! */
187
- // $authenticator = new WP_REST_OAuth1();
188
-
189
194
// Create the consumer
190
195
$ data = [
191
196
'name ' => $ params ['name ' ],
192
197
'description ' => $ params ['description ' ],
193
198
'meta ' => [
199
+ 'type ' => $ params ['type ' ],
194
200
'callback ' => $ params ['callback ' ],
195
201
],
196
202
];
203
+
197
204
$ consumer = $ result = Client::create ( $ data );
198
205
} else {
199
206
// Update the existing consumer post
200
207
$ data = [
201
208
'name ' => $ params ['name ' ],
202
209
'description ' => $ params ['description ' ],
203
210
'meta ' => [
211
+ 'type ' => $ params ['type ' ],
204
212
'callback ' => $ params ['callback ' ],
205
213
],
206
214
];
215
+
207
216
$ result = $ consumer ->update ( $ data );
208
217
}
209
218
@@ -217,7 +226,7 @@ protected static function handle_edit_submit( $consumer ) {
217
226
$ location = self ::get_url (
218
227
[
219
228
'action ' => 'edit ' ,
220
- 'id ' => $ consumer ->ID ,
229
+ 'id ' => $ consumer ->get_post_id () ,
221
230
'did_action ' => $ did_action ,
222
231
]
223
232
);
@@ -238,7 +247,7 @@ public static function render_edit_page() {
238
247
$ form_action = self ::get_url ( 'action=add ' );
239
248
if ( ! empty ( $ _REQUEST ['id ' ] ) ) {
240
249
$ id = absint ( $ _REQUEST ['id ' ] );
241
- $ consumer = Client::get ( $ id );
250
+ $ consumer = Client::get_by_id ( $ id );
242
251
if ( is_wp_error ( $ consumer ) || empty ( $ consumer ) ) {
243
252
wp_die ( __ ( 'Invalid consumer ID. ' , 'rest_oauth2 ' ) );
244
253
}
@@ -271,13 +280,18 @@ public static function render_edit_page() {
271
280
$ data = [];
272
281
273
282
if ( empty ( $ consumer ) || ! empty ( $ _POST ['_wpnonce ' ] ) ) {
274
- foreach ( [ 'name ' , 'description ' , 'callback ' ] as $ key ) {
283
+ foreach ( [ 'name ' , 'description ' , 'callback ' , ' type ' ] as $ key ) {
275
284
$ data [ $ key ] = empty ( $ _POST [ $ key ] ) ? '' : wp_unslash ( $ _POST [ $ key ] );
276
285
}
277
286
} else {
278
- $ data ['name ' ] = $ consumer ->post_title ;
279
- $ data ['description ' ] = $ consumer ->post_content ;
280
- $ data ['callback ' ] = $ consumer ->callback ;
287
+ $ data ['name ' ] = $ consumer ->get_name ();
288
+ $ data ['description ' ] = $ consumer ->get_description ();
289
+ $ data ['type ' ] = $ consumer ->get_type ();
290
+ $ data ['callback ' ] = $ consumer ->get_redirect_uris ();
291
+
292
+ if ( is_array ( $ data ['callback ' ] ) ) {
293
+ $ data ['callback ' ] = implode ( ', ' , $ data ['callback ' ] );
294
+ }
281
295
}
282
296
283
297
// Header time!
@@ -307,9 +321,7 @@ public static function render_edit_page() {
307
321
<label for="oauth-name"><?php echo esc_html_x ( 'Consumer Name ' , 'field name ' , 'rest_oauth2 ' ) ?> </label>
308
322
</th>
309
323
<td>
310
- <input type="text" class="regular-text"
311
- name="name" id="oauth-name"
312
- value="<?php echo esc_attr ( $ data ['name ' ] ) ?> "/>
324
+ <input type="text" class="regular-text" name="name" id="oauth-name" value="<?php echo esc_attr ( $ data ['name ' ] ) ?> "/>
313
325
<p class="description"><?php esc_html_e ( 'This is shown to users during authorization and in their profile. ' , 'rest_oauth2 ' ) ?> </p>
314
326
</td>
315
327
</tr>
@@ -318,20 +330,27 @@ public static function render_edit_page() {
318
330
<label for="oauth-description"><?php echo esc_html_x ( 'Description ' , 'field name ' , 'rest_oauth2 ' ) ?> </label>
319
331
</th>
320
332
<td>
321
- <textarea class="regular-text" name="description" id="oauth-description"
322
- cols="30" rows="5"
323
- style="width: 500px"><?php echo esc_textarea ( $ data ['description ' ] ) ?> </textarea>
333
+ <textarea class="regular-text" name="description" id="oauth-description" cols="30" rows="5" style="width: 500px"><?php echo esc_textarea ( $ data ['description ' ] ) ?> </textarea>
334
+ </td>
335
+ </tr>
336
+ <tr>
337
+ <th scope="row">
338
+ <label for="oauth-type"><?php echo esc_html_x ( 'Type ' , 'field name ' , 'rest_oauth2 ' ) ?> </label>
339
+ </th>
340
+ <td>
341
+ <select name="type" id="oauth-type">
342
+ <option <?php selected ( 'public ' , $ data ['type ' ] ); ?> value="public"><?php echo esc_html_x ( 'Public ' , 'Client type select option ' , 'rest_oauth2 ' ); ?> </option>
343
+ <option <?php selected ( 'private ' , $ data ['type ' ] ); ?> value="private"><?php echo esc_html_x ( 'Private ' , 'Client type select option ' , 'rest_oauth2 ' ); ?> </option>
344
+ </select>
324
345
</td>
325
346
</tr>
326
347
<tr>
327
348
<th scope="row">
328
349
<label for="oauth-callback"><?php echo esc_html_x ( 'Callback ' , 'field name ' , 'rest_oauth2 ' ) ?> </label>
329
350
</th>
330
351
<td>
331
- <input type="text" class="regular-text"
332
- name="callback" id="oauth-callback"
333
- value="<?php echo esc_attr ( $ data ['callback ' ] ) ?> "/>
334
- <p class="description"><?php esc_html_e ( "Your application's callback URL. The callback passed with the request token must match the scheme, host, port, and path of this URL. " , 'rest_oauth2 ' ) ?> </p>
352
+ <input type="text" class="regular-text" name="callback" id="oauth-callback" value="<?php echo esc_attr ( $ data ['callback ' ] ) ?> "/>
353
+ <p class="description"><?php esc_html_e ( "Your application's callback URI or a list of comma separated URIs. The callback passed with the request token must match the scheme, host, port, and path of this URL. " , 'rest_oauth2 ' ) ?> </p>
335
354
</td>
336
355
</tr>
337
356
</table>
@@ -342,15 +361,15 @@ public static function render_edit_page() {
342
361
wp_nonce_field ( 'rest-oauth2-add ' );
343
362
submit_button ( __ ( 'Add Consumer ' , 'rest_oauth2 ' ) );
344
363
} else {
345
- echo '<input type="hidden" name="id" value=" ' . esc_attr ( $ consumer ->ID ) . '" /> ' ;
346
- wp_nonce_field ( 'rest-oauth2-edit- ' . $ consumer ->ID );
364
+ echo '<input type="hidden" name="id" value=" ' . esc_attr ( $ consumer ->get_post_id () ) . '" /> ' ;
365
+ wp_nonce_field ( 'rest-oauth2-edit- ' . $ consumer ->get_post_id () );
347
366
submit_button ( __ ( 'Save Consumer ' , 'rest_oauth2 ' ) );
348
367
}
349
368
350
369
?>
351
370
</form>
352
371
353
- <?php if ( ! empty ( $ consumer ) ): ?>
372
+ <?php if ( ! empty ( $ consumer ) ) : ?>
354
373
<form method="post" action="<?php echo esc_url ( $ regenerate_action ) ?> ">
355
374
<h3><?php esc_html_e ( 'OAuth Credentials ' , 'rest_oauth2 ' ) ?> </h3>
356
375
@@ -360,21 +379,21 @@ public static function render_edit_page() {
360
379
<?php esc_html_e ( 'Client Key ' , 'rest_oauth2 ' ) ?>
361
380
</th>
362
381
<td>
363
- <code><?php echo esc_html ( $ consumer ->key ) ?> </code>
382
+ <code><?php echo esc_html ( $ consumer ->get_id () ) ?> </code>
364
383
</td>
365
384
</tr>
366
385
<tr>
367
386
<th scope="row">
368
387
<?php esc_html_e ( 'Client Secret ' , 'rest_oauth2 ' ) ?>
369
388
</th>
370
389
<td>
371
- <code><?php echo esc_html ( $ consumer ->secret ) ?> </code>
390
+ <code><?php echo esc_html ( $ consumer ->get_secret () ) ?> </code>
372
391
</td>
373
392
</tr>
374
393
</table>
375
394
376
395
<?php
377
- wp_nonce_field ( 'rest-oauth2-regenerate: ' . $ consumer ->ID );
396
+ wp_nonce_field ( 'rest-oauth2-regenerate: ' . $ consumer ->get_post_id () );
378
397
submit_button ( __ ( 'Regenerate Secret ' , 'rest_oauth2 ' ), 'delete ' );
379
398
?>
380
399
</form>
@@ -384,6 +403,9 @@ public static function render_edit_page() {
384
403
<?php
385
404
}
386
405
406
+ /**
407
+ * Delete the client.
408
+ */
387
409
public static function handle_delete () {
388
410
if ( empty ( $ _GET ['id ' ] ) ) {
389
411
return ;
@@ -418,12 +440,15 @@ public static function handle_delete() {
418
440
exit ;
419
441
}
420
442
443
+ /**
444
+ * Regenerate the client secret.
445
+ */
421
446
public static function handle_regenerate () {
422
447
if ( empty ( $ _GET ['id ' ] ) ) {
423
448
return ;
424
449
}
425
450
426
- $ id = $ _GET ['id ' ];
451
+ $ id = absint ( $ _GET ['id ' ] ) ;
427
452
check_admin_referer ( 'rest-oauth2-regenerate: ' . $ id );
428
453
429
454
if ( ! current_user_can ( 'edit_post ' , $ id ) ) {
@@ -435,7 +460,10 @@ public static function handle_regenerate() {
435
460
}
436
461
437
462
$ client = Client::get_by_id ( $ id );
438
- $ client ->regenerate_secret ();
463
+ $ result = $ client ->regenerate_secret ();
464
+ if ( is_wp_error ( $ result ) ) {
465
+ wp_die ( $ result ->get_error_message () );
466
+ }
439
467
440
468
wp_safe_redirect ( self ::get_url ( [ 'action ' => 'edit ' , 'id ' => $ id , 'did_action ' => 'regenerate ' ] ) );
441
469
exit ;
0 commit comments