Skip to content

Commit 04ae32d

Browse files
committed
Merge pull request #55 from WP-API/admin-ui
Add admin UI
2 parents f8fa82d + 25ce7c3 commit 04ae32d

File tree

3 files changed

+431
-0
lines changed

3 files changed

+431
-0
lines changed

admin.php

Lines changed: 354 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,354 @@
1+
<?php
2+
/**
3+
* Administration UI and utilities
4+
*/
5+
6+
add_action( 'admin_menu', 'json_oauth_admin_register' );
7+
add_action( 'admin_init', 'json_oauth_admin_prerender' );
8+
9+
add_action( 'admin_action_json-oauth-add', 'json_oauth_admin_edit_page' );
10+
add_action( 'admin_action_json-oauth-edit', 'json_oauth_admin_edit_page' );
11+
12+
add_action( 'personal_options', 'json_oauth_profile_section', 50 );
13+
14+
add_action( 'all_admin_notices', 'json_oauth_profile_messages' );
15+
16+
add_action( 'personal_options_update', 'json_oauth_profile_save', 10, 1 );
17+
add_action( 'edit_user_profile_update', 'json_oauth_profile_save', 10, 1 );
18+
19+
/**
20+
* Register the admin page
21+
*/
22+
function json_oauth_admin_register() {
23+
/**
24+
* Include anything we need that relies on admin classes/functions
25+
*/
26+
include_once dirname( __FILE__ ) . '/lib/class-wp-json-authentication-oauth1-listtable.php';
27+
28+
add_users_page(
29+
// Page title
30+
__( 'Registered OAuth Applications', 'json_oauth' ),
31+
32+
// Menu title
33+
_x( 'Applications', 'menu title', 'json_oauth' ),
34+
35+
// Capability
36+
'list_users',
37+
38+
// Menu slug
39+
'json-oauth',
40+
41+
// Callback
42+
'json_oauth_admin_render'
43+
);
44+
}
45+
46+
function json_oauth_admin_prerender() {
47+
$hook = get_plugin_page_hook( 'json-oauth', 'users.php' );
48+
49+
add_action( 'load-' . $hook, 'json_oauth_admin_load' );
50+
}
51+
52+
function json_oauth_admin_load() {
53+
global $wp_list_table;
54+
55+
$wp_list_table = new WP_JSON_Authentication_OAuth1_ListTable();
56+
57+
$wp_list_table->prepare_items();
58+
}
59+
60+
function json_oauth_admin_render() {
61+
global $wp_list_table;
62+
63+
// ...
64+
?>
65+
<div class="wrap">
66+
<h2>
67+
<?php
68+
esc_html_e( 'Registered OAuth Applications', 'json_oauth' );
69+
70+
if ( current_user_can( 'create_users' ) ): ?>
71+
<a href="<?php echo admin_url( 'admin.php?action=json-oauth-add' ) ?>"
72+
class="add-new-h2"><?php echo esc_html_x( 'Add New', 'application', 'json_oauth' ); ?></a>
73+
<?php
74+
endif;
75+
?>
76+
</h2>
77+
78+
<?php $wp_list_table->views(); ?>
79+
80+
<form action="" method="get">
81+
82+
<?php $wp_list_table->search_box( __( 'Search Applications', 'json_oauth' ), 'json_oauth' ); ?>
83+
84+
<?php $wp_list_table->display(); ?>
85+
86+
</form>
87+
88+
<br class="clear" />
89+
90+
</div>
91+
<?php
92+
}
93+
94+
function json_oauth_admin_validate_parameters( $params ) {
95+
$valid = array();
96+
97+
if ( empty( $params['name'] ) ) {
98+
return new WP_Error( 'json_oauth_missing_name', __( 'Consumer name is required' ) );
99+
}
100+
$valid['name'] = wp_filter_post_kses( $params['name'] );
101+
102+
if ( empty( $params['description'] ) ) {
103+
return new WP_Error( 'json_oauth_missing_description', __( 'Consumer description is required' ) );
104+
}
105+
$valid['description'] = wp_filter_post_kses( $params['description'] );
106+
107+
return $valid;
108+
}
109+
110+
/**
111+
* Handle submission of the add page
112+
*
113+
* @return array|null List of errors. Issues a redirect and exits on success.
114+
*/
115+
function json_oauth_admin_handle_edit_submit( $consumer ) {
116+
$messages = array();
117+
if ( empty( $consumer ) ) {
118+
$did_action = 'add';
119+
check_admin_referer( 'json-oauth-add' );
120+
}
121+
else {
122+
$did_action = 'edit';
123+
check_admin_referer( 'json-oauth-edit-' . $consumer->ID );
124+
}
125+
126+
// Check that the parameters are correct first
127+
$params = json_oauth_admin_validate_parameters( wp_unslash( $_POST ) );
128+
if ( is_wp_error( $params ) ) {
129+
$messages[] = $params->get_error_message();
130+
return $messages;
131+
}
132+
133+
if ( empty( $consumer ) ) {
134+
$authenticator = new WP_JSON_Authentication_OAuth1();
135+
136+
// Create the consumer
137+
$data = array(
138+
'name' => $params['name'],
139+
'description' => $params['description'],
140+
);
141+
$consumer = $result = $authenticator->add_consumer( $data );
142+
}
143+
else {
144+
// Update the existing consumer post
145+
$data = array(
146+
'ID' => $consumer->ID,
147+
'post_title' => $params['name'],
148+
'post_content' => $params['description'],
149+
);
150+
$result = wp_update_post( $data, true );
151+
}
152+
153+
if ( is_wp_error( $result ) ) {
154+
$messages[] = $result->get_error_message();
155+
156+
return $messages;
157+
}
158+
159+
// Success, redirect to alias page
160+
$location = add_query_arg(
161+
array(
162+
'action' => 'json-oauth-edit',
163+
'id' => $consumer->ID,
164+
'did_action' => $did_action,
165+
'processed' => 1,
166+
'_wpnonce' => wp_create_nonce( 'json-oauth-edit-' . $id ),
167+
),
168+
network_admin_url( 'admin.php' )
169+
);
170+
wp_safe_redirect( $location );
171+
exit;
172+
}
173+
174+
/**
175+
* Output alias editing page
176+
*/
177+
function json_oauth_admin_edit_page() {
178+
if ( ! current_user_can( 'edit_users' ) )
179+
wp_die( __( 'You do not have permission to access this page.' ) );
180+
181+
// Are we editing?
182+
$consumer = null;
183+
$form_action = admin_url( 'admin.php?action=json-oauth-add' );
184+
if ( ! empty( $_REQUEST['id'] ) ) {
185+
$id = absint( $_REQUEST['id'] );
186+
$consumer = get_post( $id );
187+
if ( is_wp_error( $consumer ) || empty( $consumer ) ) {
188+
wp_die( __( 'Invalid consumer ID.' ) );
189+
}
190+
191+
$form_action = admin_url( 'admin.php?action=json-oauth-edit' );
192+
}
193+
194+
// Handle form submission
195+
$messages = array();
196+
if ( ! empty( $_POST['submit'] ) ) {
197+
$messages = json_oauth_admin_handle_edit_submit( $consumer );
198+
}
199+
200+
$data = array();
201+
202+
if ( empty( $consumer ) || ! empty( $_POST['_wpnonce'] ) ) {
203+
foreach ( array( 'name', 'description' ) as $key ) {
204+
$data[ $key ] = empty( $_POST[ $key ] ) ? '' : wp_unslash( $_POST[ $key ] );
205+
}
206+
}
207+
else {
208+
$data['name'] = $consumer->post_title;
209+
$data['description'] = $consumer->post_content;
210+
}
211+
212+
// Header time!
213+
global $title, $parent_file, $submenu_file;
214+
$title = $consumer ? __( 'Edit Consumer' ) : __( 'Add Consumer' );
215+
$parent_file = 'users.php';
216+
$submenu_file = 'json-oauth';
217+
218+
include( ABSPATH . 'wp-admin/admin-header.php' );
219+
?>
220+
221+
<div class="wrap">
222+
<h2 id="edit-site"><?php echo esc_html( $title ) ?></h2>
223+
224+
<?php
225+
if ( ! empty( $messages ) ) {
226+
foreach ( $messages as $msg )
227+
echo '<div id="message" class="updated"><p>' . $msg . '</p></div>';
228+
}
229+
?>
230+
231+
<form method="post" action="<?php echo esc_url( $form_action ) ?>">
232+
<table class="form-table">
233+
<tr>
234+
<th scope="row">
235+
<label for="oauth-name"><?php echo esc_html_x( 'Consumer Name', 'field name' ) ?></label>
236+
</th>
237+
<td>
238+
<input type="text" class="regular-text"
239+
name="name" id="oauth-name"
240+
value="<?php echo esc_attr( $data['name'] ) ?>" />
241+
</td>
242+
</tr>
243+
<tr>
244+
<th scope="row">
245+
<label for="oauth-description"><?php echo esc_html_x( 'Description', 'field name' ) ?></label>
246+
</th>
247+
<td>
248+
<textarea class="regular-text" name="description" id="oauth-description"
249+
cols="30" rows="5" style="width: 500px"><?php echo esc_textarea( $data['description'] ) ?></textarea>
250+
</td>
251+
</tr>
252+
</table>
253+
254+
<?php
255+
256+
if ( empty( $consumer ) ) {
257+
wp_nonce_field( 'json-oauth-add' );
258+
submit_button( __( 'Add Consumer' ) );
259+
}
260+
else {
261+
echo '<input type="hidden" name="id" value="' . esc_attr( $consumer->ID ) . '" />';
262+
wp_nonce_field( 'json-oauth-edit-' . $consumer->ID );
263+
submit_button( __( 'Save Consumer' ) );
264+
}
265+
266+
?>
267+
</form>
268+
</div>
269+
270+
<?php
271+
272+
include(ABSPATH . 'wp-admin/admin-footer.php');
273+
}
274+
275+
function json_oauth_profile_section( $user ) {
276+
global $wpdb;
277+
278+
$results = $wpdb->get_col( "SELECT option_value FROM {$wpdb->options} WHERE option_name LIKE 'oauth1_access_%'", 0 );
279+
$results = array_map( 'unserialize', $results );
280+
$approved = array_filter( $results, function ( $row ) use ( $user ) {
281+
return $row['user'] === $user->ID;
282+
} );
283+
284+
$authenticator = new WP_JSON_Authentication_OAuth1();
285+
286+
?>
287+
<table class="form-table">
288+
<tbody>
289+
<tr>
290+
<th scope="row"><?php _e( 'Authorized Applications', 'json_oauth' ) ?></th>
291+
<td>
292+
<?php if ( ! empty( $approved ) ): ?>
293+
<table class="widefat sessions-table">
294+
<thead>
295+
<tr>
296+
<th scope="col"><?php _e( 'Application Name', 'wpsm' ); ?></th>
297+
</tr>
298+
</thead>
299+
<tbody>
300+
<?php foreach ( $approved as $row ): ?>
301+
<?php
302+
$application = $authenticator->get_consumer( $row['consumer'] );
303+
?>
304+
<tr>
305+
<td><?php echo esc_html( $application->post_title ) ?></td>
306+
<td><button class="button" name="oauth_revoke" value="<?php echo esc_attr( $row['key'] ) ?>"><?php esc_html_e( 'Revoke', 'json_oauth' ) ?></button>
307+
</tr>
308+
309+
<?php endforeach ?>
310+
</tbody>
311+
</table>
312+
<?php else: ?>
313+
<p class="description"><?php esc_html_e( 'No applications authorized.' ) ?></p>
314+
<?php endif ?>
315+
</td>
316+
</tr>
317+
</tbody>
318+
</table>
319+
<?php
320+
}
321+
322+
function json_oauth_profile_messages() {
323+
global $pagenow;
324+
if ( $pagenow !== 'profile.php' && $pagenow !== 'user-edit.php' ) {
325+
return;
326+
}
327+
328+
if ( ! empty( $_GET['oauth_revoked'] ) ) {
329+
echo '<div id="message" class="updated"><p>' . __( 'Token revoked.' ) . '</p></div>';
330+
}
331+
if ( ! empty( $_GET['oauth_revocation_failed'] ) ) {
332+
echo '<div id="message" class="updated"><p>' . __( 'Unable to revoke token.' ) . '</p></div>';
333+
}
334+
}
335+
336+
function json_oauth_profile_save( $user_id ) {
337+
if ( empty( $_POST['oauth_revoke'] ) ) {
338+
return;
339+
}
340+
341+
$key = wp_unslash( $_POST['oauth_revoke'] );
342+
343+
$authenticator = new WP_JSON_Authentication_OAuth1();
344+
345+
$result = $authenticator->revoke_access_token( $key );
346+
if ( is_wp_error( $result ) ) {
347+
$redirect = add_query_arg( 'oauth_revocation_failed', true, get_edit_user_link( $user_id ) );
348+
}
349+
else {
350+
$redirect = add_query_arg( 'oauth_revoked', $key, get_edit_user_link( $user_id ) );
351+
}
352+
wp_redirect($redirect);
353+
exit;
354+
}

0 commit comments

Comments
 (0)