Skip to content

Commit fb861ee

Browse files
committed
Merge pull request #98 from WP-API/renaissance
Renaissance (partial rewrite)
2 parents 04ae32d + 141f4f8 commit fb861ee

10 files changed

+954
-480
lines changed

admin.php

Lines changed: 26 additions & 283 deletions
Original file line numberDiff line numberDiff line change
@@ -3,276 +3,18 @@
33
* Administration UI and utilities
44
*/
55

6-
add_action( 'admin_menu', 'json_oauth_admin_register' );
7-
add_action( 'admin_init', 'json_oauth_admin_prerender' );
6+
require dirname( __FILE__ ) . '/lib/class-wp-rest-oauth1-admin.php';
87

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' );
8+
add_action( 'admin_menu', array( 'WP_REST_OAuth1_Admin', 'register' ) );
119

12-
add_action( 'personal_options', 'json_oauth_profile_section', 50 );
10+
add_action( 'personal_options', 'rest_oauth1_profile_section', 50 );
1311

14-
add_action( 'all_admin_notices', 'json_oauth_profile_messages' );
12+
add_action( 'all_admin_notices', 'rest_oauth1_profile_messages' );
1513

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 );
14+
add_action( 'personal_options_update', 'rest_oauth1_profile_save', 10, 1 );
15+
add_action( 'edit_user_profile_update', 'rest_oauth1_profile_save', 10, 1 );
1816

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 ) {
17+
function rest_oauth1_profile_section( $user ) {
27618
global $wpdb;
27719

27820
$results = $wpdb->get_col( "SELECT option_value FROM {$wpdb->options} WHERE option_name LIKE 'oauth1_access_%'", 0 );
@@ -281,36 +23,37 @@ function json_oauth_profile_section( $user ) {
28123
return $row['user'] === $user->ID;
28224
} );
28325

284-
$authenticator = new WP_JSON_Authentication_OAuth1();
26+
$authenticator = new WP_REST_OAuth1();
28527

28628
?>
28729
<table class="form-table">
28830
<tbody>
28931
<tr>
290-
<th scope="row"><?php _e( 'Authorized Applications', 'json_oauth' ) ?></th>
32+
<th scope="row"><?php _e( 'Authorized Applications', 'rest_oauth1' ) ?></th>
29133
<td>
29234
<?php if ( ! empty( $approved ) ): ?>
293-
<table class="widefat sessions-table">
35+
<table class="widefat">
29436
<thead>
29537
<tr>
296-
<th scope="col"><?php _e( 'Application Name', 'wpsm' ); ?></th>
38+
<th style="padding-left:10px;"><?php esc_html_e( 'Application Name', 'rest_oauth1' ); ?></th>
39+
<th></th>
29740
</tr>
29841
</thead>
29942
<tbody>
30043
<?php foreach ( $approved as $row ): ?>
30144
<?php
302-
$application = $authenticator->get_consumer( $row['consumer'] );
45+
$application = get_post($row['consumer']);
30346
?>
30447
<tr>
30548
<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>
49+
<td><button class="button" name="oauth_revoke" value="<?php echo esc_attr( $row['key'] ) ?>"><?php esc_html_e( 'Revoke', 'rest_oauth1' ) ?></button>
30750
</tr>
30851

30952
<?php endforeach ?>
31053
</tbody>
31154
</table>
31255
<?php else: ?>
313-
<p class="description"><?php esc_html_e( 'No applications authorized.' ) ?></p>
56+
<p class="description"><?php esc_html_e( 'No applications authorized.', 'rest_oauth1' ) ?></p>
31457
<?php endif ?>
31558
</td>
31659
</tr>
@@ -319,35 +62,35 @@ function json_oauth_profile_section( $user ) {
31962
<?php
32063
}
32164

322-
function json_oauth_profile_messages() {
65+
function rest_oauth1_profile_messages() {
32366
global $pagenow;
32467
if ( $pagenow !== 'profile.php' && $pagenow !== 'user-edit.php' ) {
32568
return;
32669
}
32770

328-
if ( ! empty( $_GET['oauth_revoked'] ) ) {
329-
echo '<div id="message" class="updated"><p>' . __( 'Token revoked.' ) . '</p></div>';
71+
if ( ! empty( $_GET['rest_oauth1_revoked'] ) ) {
72+
echo '<div id="message" class="updated"><p>' . __( 'Token revoked.', 'rest_oauth1' ) . '</p></div>';
33073
}
331-
if ( ! empty( $_GET['oauth_revocation_failed'] ) ) {
332-
echo '<div id="message" class="updated"><p>' . __( 'Unable to revoke token.' ) . '</p></div>';
74+
if ( ! empty( $_GET['rest_oauth1_revocation_failed'] ) ) {
75+
echo '<div id="message" class="updated"><p>' . __( 'Unable to revoke token.', 'rest_oauth1' ) . '</p></div>';
33376
}
33477
}
33578

336-
function json_oauth_profile_save( $user_id ) {
337-
if ( empty( $_POST['oauth_revoke'] ) ) {
79+
function rest_oauth1_profile_save( $user_id ) {
80+
if ( empty( $_POST['rest_oauth1_revoke'] ) ) {
33881
return;
33982
}
34083

341-
$key = wp_unslash( $_POST['oauth_revoke'] );
84+
$key = wp_unslash( $_POST['rest_oauth1_revoke'] );
34285

343-
$authenticator = new WP_JSON_Authentication_OAuth1();
86+
$authenticator = new WP_REST_OAuth1();
34487

34588
$result = $authenticator->revoke_access_token( $key );
34689
if ( is_wp_error( $result ) ) {
347-
$redirect = add_query_arg( 'oauth_revocation_failed', true, get_edit_user_link( $user_id ) );
90+
$redirect = add_query_arg( 'rest_oauth1_revocation_failed', true, get_edit_user_link( $user_id ) );
34891
}
34992
else {
350-
$redirect = add_query_arg( 'oauth_revoked', $key, get_edit_user_link( $user_id ) );
93+
$redirect = add_query_arg( 'rest_oauth1_revoked', $key, get_edit_user_link( $user_id ) );
35194
}
35295
wp_redirect($redirect);
35396
exit;

0 commit comments

Comments
 (0)