@@ -149,8 +149,13 @@ protected static function validate_parameters( $params ) {
149149 }
150150 $ valid ['description ' ] = wp_filter_post_kses ( $ params ['description ' ] );
151151
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+
152157 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 ' ) );
154159 }
155160 if ( ! empty ( $ params ['callback ' ] ) ) {
156161 $ valid ['callback ' ] = $ params ['callback ' ];
@@ -162,6 +167,8 @@ protected static function validate_parameters( $params ) {
162167 /**
163168 * Handle submission of the add page
164169 *
170+ * @param $consumer
171+ *
165172 * @return array|null List of errors. Issues a redirect and exits on success.
166173 */
167174 protected static function handle_edit_submit ( $ consumer ) {
@@ -171,39 +178,41 @@ protected static function handle_edit_submit( $consumer ) {
171178 check_admin_referer ( 'rest-oauth2-add ' );
172179 } else {
173180 $ did_action = 'edit ' ;
174- check_admin_referer ( 'rest-oauth2-edit- ' . $ consumer ->ID );
181+ check_admin_referer ( 'rest-oauth2-edit- ' . $ consumer ->get_post_id () );
175182 }
176183
177184 // Check that the parameters are correct first
178185 $ params = self ::validate_parameters ( wp_unslash ( $ _POST ) );
186+
179187 if ( is_wp_error ( $ params ) ) {
180188 $ messages [] = $ params ->get_error_message ();
181189
182190 return $ messages ;
183191 }
184192
185193 if ( empty ( $ consumer ) ) {
186- /** @todo Implement this! */
187- // $authenticator = new WP_REST_OAuth1();
188-
189194 // Create the consumer
190195 $ data = [
191196 'name ' => $ params ['name ' ],
192197 'description ' => $ params ['description ' ],
193198 'meta ' => [
199+ 'type ' => $ params ['type ' ],
194200 'callback ' => $ params ['callback ' ],
195201 ],
196202 ];
203+
197204 $ consumer = $ result = Client::create ( $ data );
198205 } else {
199206 // Update the existing consumer post
200207 $ data = [
201208 'name ' => $ params ['name ' ],
202209 'description ' => $ params ['description ' ],
203210 'meta ' => [
211+ 'type ' => $ params ['type ' ],
204212 'callback ' => $ params ['callback ' ],
205213 ],
206214 ];
215+
207216 $ result = $ consumer ->update ( $ data );
208217 }
209218
@@ -217,7 +226,7 @@ protected static function handle_edit_submit( $consumer ) {
217226 $ location = self ::get_url (
218227 [
219228 'action ' => 'edit ' ,
220- 'id ' => $ consumer ->ID ,
229+ 'id ' => $ consumer ->get_post_id () ,
221230 'did_action ' => $ did_action ,
222231 ]
223232 );
@@ -238,7 +247,7 @@ public static function render_edit_page() {
238247 $ form_action = self ::get_url ( 'action=add ' );
239248 if ( ! empty ( $ _REQUEST ['id ' ] ) ) {
240249 $ id = absint ( $ _REQUEST ['id ' ] );
241- $ consumer = Client::get ( $ id );
250+ $ consumer = Client::get_by_id ( $ id );
242251 if ( is_wp_error ( $ consumer ) || empty ( $ consumer ) ) {
243252 wp_die ( __ ( 'Invalid consumer ID. ' , 'rest_oauth2 ' ) );
244253 }
@@ -271,13 +280,18 @@ public static function render_edit_page() {
271280 $ data = [];
272281
273282 if ( empty ( $ consumer ) || ! empty ( $ _POST ['_wpnonce ' ] ) ) {
274- foreach ( [ 'name ' , 'description ' , 'callback ' ] as $ key ) {
283+ foreach ( [ 'name ' , 'description ' , 'callback ' , ' type ' ] as $ key ) {
275284 $ data [ $ key ] = empty ( $ _POST [ $ key ] ) ? '' : wp_unslash ( $ _POST [ $ key ] );
276285 }
277286 } 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+ }
281295 }
282296
283297 // Header time!
@@ -307,9 +321,7 @@ public static function render_edit_page() {
307321 <label for="oauth-name"><?php echo esc_html_x ( 'Consumer Name ' , 'field name ' , 'rest_oauth2 ' ) ?> </label>
308322 </th>
309323 <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 ' ] ) ?> "/>
313325 <p class="description"><?php esc_html_e ( 'This is shown to users during authorization and in their profile. ' , 'rest_oauth2 ' ) ?> </p>
314326 </td>
315327 </tr>
@@ -318,20 +330,27 @@ public static function render_edit_page() {
318330 <label for="oauth-description"><?php echo esc_html_x ( 'Description ' , 'field name ' , 'rest_oauth2 ' ) ?> </label>
319331 </th>
320332 <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>
324345 </td>
325346 </tr>
326347 <tr>
327348 <th scope="row">
328349 <label for="oauth-callback"><?php echo esc_html_x ( 'Callback ' , 'field name ' , 'rest_oauth2 ' ) ?> </label>
329350 </th>
330351 <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>
335354 </td>
336355 </tr>
337356 </table>
@@ -342,15 +361,15 @@ public static function render_edit_page() {
342361 wp_nonce_field ( 'rest-oauth2-add ' );
343362 submit_button ( __ ( 'Add Consumer ' , 'rest_oauth2 ' ) );
344363 } 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 () );
347366 submit_button ( __ ( 'Save Consumer ' , 'rest_oauth2 ' ) );
348367 }
349368
350369 ?>
351370 </form>
352371
353- <?php if ( ! empty ( $ consumer ) ): ?>
372+ <?php if ( ! empty ( $ consumer ) ) : ?>
354373 <form method="post" action="<?php echo esc_url ( $ regenerate_action ) ?> ">
355374 <h3><?php esc_html_e ( 'OAuth Credentials ' , 'rest_oauth2 ' ) ?> </h3>
356375
@@ -360,21 +379,21 @@ public static function render_edit_page() {
360379 <?php esc_html_e ( 'Client Key ' , 'rest_oauth2 ' ) ?>
361380 </th>
362381 <td>
363- <code><?php echo esc_html ( $ consumer ->key ) ?> </code>
382+ <code><?php echo esc_html ( $ consumer ->get_id () ) ?> </code>
364383 </td>
365384 </tr>
366385 <tr>
367386 <th scope="row">
368387 <?php esc_html_e ( 'Client Secret ' , 'rest_oauth2 ' ) ?>
369388 </th>
370389 <td>
371- <code><?php echo esc_html ( $ consumer ->secret ) ?> </code>
390+ <code><?php echo esc_html ( $ consumer ->get_secret () ) ?> </code>
372391 </td>
373392 </tr>
374393 </table>
375394
376395 <?php
377- wp_nonce_field ( 'rest-oauth2-regenerate: ' . $ consumer ->ID );
396+ wp_nonce_field ( 'rest-oauth2-regenerate: ' . $ consumer ->get_post_id () );
378397 submit_button ( __ ( 'Regenerate Secret ' , 'rest_oauth2 ' ), 'delete ' );
379398 ?>
380399 </form>
@@ -384,6 +403,9 @@ public static function render_edit_page() {
384403 <?php
385404 }
386405
406+ /**
407+ * Delete the client.
408+ */
387409 public static function handle_delete () {
388410 if ( empty ( $ _GET ['id ' ] ) ) {
389411 return ;
@@ -418,12 +440,15 @@ public static function handle_delete() {
418440 exit ;
419441 }
420442
443+ /**
444+ * Regenerate the client secret.
445+ */
421446 public static function handle_regenerate () {
422447 if ( empty ( $ _GET ['id ' ] ) ) {
423448 return ;
424449 }
425450
426- $ id = $ _GET ['id ' ];
451+ $ id = absint ( $ _GET ['id ' ] ) ;
427452 check_admin_referer ( 'rest-oauth2-regenerate: ' . $ id );
428453
429454 if ( ! current_user_can ( 'edit_post ' , $ id ) ) {
@@ -435,7 +460,10 @@ public static function handle_regenerate() {
435460 }
436461
437462 $ 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+ }
439467
440468 wp_safe_redirect ( self ::get_url ( [ 'action ' => 'edit ' , 'id ' => $ id , 'did_action ' => 'regenerate ' ] ) );
441469 exit ;
0 commit comments