Skip to content

Commit 0ed043b

Browse files
authored
Merge pull request #15 from WP-API/approval-process
Add approval process
2 parents 8b4b28c + bbe4ec2 commit 0ed043b

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

inc/admin/class-admin.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public static function load() {
6969
self::handle_regenerate();
7070
break;
7171

72+
case 'approve':
73+
self::handle_approve();
74+
break;
75+
7276
default:
7377
global $wp_list_table;
7478

@@ -86,6 +90,7 @@ public static function dispatch() {
8690
case 'add':
8791
case 'edit':
8892
case 'delete':
93+
case 'approve':
8994
break;
9095

9196
default:
@@ -116,6 +121,8 @@ class="add-new-h2"><?php echo esc_html_x( 'Add New', 'application', 'rest_oauth2
116121
<?php
117122
if ( ! empty( $_GET['deleted'] ) ) {
118123
echo '<div id="message" class="updated"><p>' . esc_html__( 'Deleted application.', 'rest_oauth2' ) . '</p></div>';
124+
} elseif ( ! empty( $_GET['approved'] ) ) {
125+
echo '<div id="message" class="updated"><p>' . esc_html__( 'Approved application.', 'rest_oauth2' ) . '</p></div>';
119126
}
120127
?>
121128

@@ -480,6 +487,39 @@ public static function handle_delete() {
480487
exit;
481488
}
482489

490+
/**
491+
* Approve the client.
492+
*/
493+
public static function handle_approve() {
494+
if ( empty( $_GET['id'] ) ) {
495+
return;
496+
}
497+
498+
$id = absint( $_GET['id'] );
499+
check_admin_referer( 'rest-oauth2-approve:' . $id );
500+
501+
if ( ! current_user_can( 'publish_post', $id ) ) {
502+
wp_die(
503+
'<h1>' . __( 'Cheatin&#8217; uh?', 'rest_oauth2' ) . '</h1>' .
504+
'<p>' . __( 'You are not allowed to approve this application.', 'rest_oauth2' ) . '</p>',
505+
403
506+
);
507+
}
508+
509+
$client = Client::get_by_post_id( $id );
510+
if ( is_wp_error( $client ) ) {
511+
wp_die( $client );
512+
}
513+
514+
$did_approve = $client->approve();
515+
if ( is_wp_error( $did_approve ) ) {
516+
wp_die( $did_approve );
517+
}
518+
519+
wp_safe_redirect( self::get_urL( 'approved=1' ) );
520+
exit;
521+
}
522+
483523
/**
484524
* Regenerate the client secret.
485525
*/

inc/admin/class-listtable.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,37 @@ protected function column_name( $item ) {
102102
'edit' => sprintf( '<a href="%s">%s</a>', esc_url( $edit_link ), esc_html__( 'Edit', 'rest_oauth2' ) ),
103103
'delete' => sprintf( '<a href="%s">%s</a>', esc_url( $delete_link ), esc_html__( 'Delete', 'rest_oauth2' ) ),
104104
];
105+
106+
$post_type_object = get_post_type_object( $item->post_type );
107+
if ( current_user_can( $post_type_object->cap->publish_posts ) && $item->post_status !== 'publish' ) {
108+
$publish_link = add_query_arg(
109+
[
110+
'page' => 'rest-oauth2-apps',
111+
'action' => 'approve',
112+
'id' => $item->ID,
113+
],
114+
admin_url( 'users.php' )
115+
);
116+
$publish_link = wp_nonce_url( $publish_link, 'rest-oauth2-approve:' . $item->ID );
117+
$actions['app-approve'] = sprintf(
118+
'<a href="%s">%s</a>',
119+
esc_url( $publish_link ),
120+
esc_html__( 'Approve', 'rest_oauth2' )
121+
);
122+
}
123+
105124
$action_html = $this->row_actions( $actions );
106125

126+
// Get suffixes for draft, etc
127+
ob_start();
128+
_post_states( $item );
129+
$title = sprintf(
130+
'<strong><a href="%s">%s</a>%s</strong>',
131+
$edit_link,
132+
$title,
133+
ob_get_clean()
134+
);
135+
107136
return $title . ' ' . $action_html;
108137
}
109138

inc/class-client.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,20 @@ public function delete() {
423423
return (bool) wp_delete_post( $this->get_post_id(), true );
424424
}
425425

426+
/**
427+
* Approve a client.
428+
*
429+
* @return bool|WP_Error True if client was updated, error otherwise.
430+
*/
431+
public function approve() {
432+
$data = array(
433+
'ID' => $this->get_post_id(),
434+
'post_status' => 'publish',
435+
);
436+
$result = wp_update_post( wp_slash( $data ), true );
437+
return is_wp_error( $result ) ? $result : true;
438+
}
439+
426440
/**
427441
* Register the underlying post type.
428442
*/
@@ -431,8 +445,13 @@ public static function register_type() {
431445
'public' => false,
432446
'hierarchical' => true,
433447
'capability_type' => array(
434-
'client',
435-
'clients',
448+
'oauth2_client',
449+
'oauth2_clients',
450+
),
451+
'capabilities' => array(
452+
'edit_posts' => 'edit_users',
453+
'edit_others_posts' => 'edit_users',
454+
'publish_posts' => 'edit_users',
436455
),
437456
'supports' => array(
438457
'title',

plugin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function bootstrap() {
1717
load();
1818

1919
// Core authentication hooks.
20+
add_action( 'init', __NAMESPACE__ . '\\Client::register_type' );
2021
add_filter( 'determine_current_user', __NAMESPACE__ . '\\Authentication\\attempt_authentication', 11 );
2122

2223
// REST API integration.

0 commit comments

Comments
 (0)