Skip to content

Commit d3869fd

Browse files
committed
Rework admin internals
1 parent 1e61a90 commit d3869fd

File tree

3 files changed

+52
-55
lines changed

3 files changed

+52
-55
lines changed

admin.php

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

6-
require dirname( __FILE__ ) . '/lib/class-wombat-admin.php';
6+
require dirname( __FILE__ ) . '/lib/class-wp-rest-oauth1-admin.php';
77

8-
add_action( 'admin_menu', array( 'Wombat_Admin', 'register' ) );
8+
add_action( 'admin_menu', array( 'WP_REST_OAuth1_Admin', 'register' ) );
99

10-
add_action( 'personal_options', 'wombat_profile_section', 50 );
10+
add_action( 'personal_options', 'rest_oauth1_profile_section', 50 );
1111

12-
add_action( 'all_admin_notices', 'wombat_profile_messages' );
12+
add_action( 'all_admin_notices', 'rest_oauth1_profile_messages' );
1313

14-
add_action( 'personal_options_update', 'wombat_profile_save', 10, 1 );
15-
add_action( 'edit_user_profile_update', 'wombat_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 );
1616

17-
function wombat_profile_section( $user ) {
17+
function rest_oauth1_profile_section( $user ) {
1818
global $wpdb;
1919

2020
$results = $wpdb->get_col( "SELECT option_value FROM {$wpdb->options} WHERE option_name LIKE 'oauth1_access_%'", 0 );
@@ -29,13 +29,13 @@ function wombat_profile_section( $user ) {
2929
<table class="form-table">
3030
<tbody>
3131
<tr>
32-
<th scope="row"><?php _e( 'Authorized Applications', 'wombat' ) ?></th>
32+
<th scope="row"><?php _e( 'Authorized Applications', 'rest_oauth1' ) ?></th>
3333
<td>
3434
<?php if ( ! empty( $approved ) ): ?>
3535
<table class="widefat">
3636
<thead>
3737
<tr>
38-
<th style="padding-left:10px;"><?php esc_html_e( 'Application Name', 'wombat' ); ?></th>
38+
<th style="padding-left:10px;"><?php esc_html_e( 'Application Name', 'rest_oauth1' ); ?></th>
3939
<th></th>
4040
</tr>
4141
</thead>
@@ -46,14 +46,14 @@ function wombat_profile_section( $user ) {
4646
?>
4747
<tr>
4848
<td><?php echo esc_html( $application->post_title ) ?></td>
49-
<td><button class="button" name="oauth_revoke" value="<?php echo esc_attr( $row['key'] ) ?>"><?php esc_html_e( 'Revoke', 'wombat' ) ?></button>
49+
<td><button class="button" name="oauth_revoke" value="<?php echo esc_attr( $row['key'] ) ?>"><?php esc_html_e( 'Revoke', 'rest_oauth1' ) ?></button>
5050
</tr>
5151

5252
<?php endforeach ?>
5353
</tbody>
5454
</table>
5555
<?php else: ?>
56-
<p class="description"><?php esc_html_e( 'No applications authorized.', 'wombat' ) ?></p>
56+
<p class="description"><?php esc_html_e( 'No applications authorized.', 'rest_oauth1' ) ?></p>
5757
<?php endif ?>
5858
</td>
5959
</tr>
@@ -62,35 +62,35 @@ function wombat_profile_section( $user ) {
6262
<?php
6363
}
6464

65-
function wombat_profile_messages() {
65+
function rest_oauth1_profile_messages() {
6666
global $pagenow;
6767
if ( $pagenow !== 'profile.php' && $pagenow !== 'user-edit.php' ) {
6868
return;
6969
}
7070

71-
if ( ! empty( $_GET['wombat_revoked'] ) ) {
72-
echo '<div id="message" class="updated"><p>' . __( 'Token revoked.', 'wombat' ) . '</p></div>';
71+
if ( ! empty( $_GET['rest_oauth1_revoked'] ) ) {
72+
echo '<div id="message" class="updated"><p>' . __( 'Token revoked.', 'rest_oauth1' ) . '</p></div>';
7373
}
74-
if ( ! empty( $_GET['wombat_revocation_failed'] ) ) {
75-
echo '<div id="message" class="updated"><p>' . __( 'Unable to revoke token.', 'wombat' ) . '</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>';
7676
}
7777
}
7878

79-
function wombat_profile_save( $user_id ) {
80-
if ( empty( $_POST['wombat_revoke'] ) ) {
79+
function rest_oauth1_profile_save( $user_id ) {
80+
if ( empty( $_POST['rest_oauth1_revoke'] ) ) {
8181
return;
8282
}
8383

84-
$key = wp_unslash( $_POST['wombat_revoke'] );
84+
$key = wp_unslash( $_POST['rest_oauth1_revoke'] );
8585

8686
$authenticator = new WP_REST_OAuth1();
8787

8888
$result = $authenticator->revoke_access_token( $key );
8989
if ( is_wp_error( $result ) ) {
90-
$redirect = add_query_arg( 'wombat_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 ) );
9191
}
9292
else {
93-
$redirect = add_query_arg( 'wombat_revoked', $key, get_edit_user_link( $user_id ) );
93+
$redirect = add_query_arg( 'rest_oauth1_revoked', $key, get_edit_user_link( $user_id ) );
9494
}
9595
wp_redirect($redirect);
9696
exit;

lib/class-wombat-admin.php renamed to lib/class-wp-rest-oauth1-admin.php

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

3-
class Wombat_Admin {
4-
const BASE_SLUG = 'wombat-apps';
3+
class WP_REST_OAuth1_Admin {
4+
const BASE_SLUG = 'rest-oauth1-apps';
55

66
/**
77
* Register the admin page
@@ -10,14 +10,14 @@ public static function register() {
1010
/**
1111
* Include anything we need that relies on admin classes/functions
1212
*/
13-
include_once dirname( __FILE__ ) . '/class-wombat-listtable.php';
13+
include_once dirname( __FILE__ ) . '/class-wp-rest-oauth1-listtable.php';
1414

1515
$hook = add_users_page(
1616
// Page title
17-
__( 'Registered OAuth Applications', 'wombat' ),
17+
__( 'Registered OAuth Applications', 'rest_oauth1' ),
1818

1919
// Menu title
20-
_x( 'Applications', 'menu title', 'wombat' ),
20+
_x( 'Applications', 'menu title', 'rest_oauth1' ),
2121

2222
// Capability
2323
'list_users',
@@ -68,7 +68,7 @@ public static function load() {
6868
default:
6969
global $wp_list_table;
7070

71-
$wp_list_table = new Wombat_ListTable();
71+
$wp_list_table = new WP_REST_OAuth1_ListTable();
7272

7373
$wp_list_table->prepare_items();
7474

@@ -99,11 +99,11 @@ public static function render() {
9999
<div class="wrap">
100100
<h2>
101101
<?php
102-
esc_html_e( 'Registered Applications', 'wombat' );
102+
esc_html_e( 'Registered Applications', 'rest_oauth1' );
103103

104104
if ( current_user_can( 'create_users' ) ): ?>
105105
<a href="<?php echo esc_url( self::get_url( 'action=add' ) ) ?>"
106-
class="add-new-h2"><?php echo esc_html_x( 'Add New', 'application', 'wombat' ); ?></a>
106+
class="add-new-h2"><?php echo esc_html_x( 'Add New', 'application', 'rest_oauth1' ); ?></a>
107107
<?php
108108
endif;
109109
?>
@@ -113,7 +113,7 @@ class="add-new-h2"><?php echo esc_html_x( 'Add New', 'application', 'wombat' );
113113

114114
<form action="" method="get">
115115

116-
<?php $wp_list_table->search_box( __( 'Search Applications', 'wombat' ), 'wombat' ); ?>
116+
<?php $wp_list_table->search_box( __( 'Search Applications', 'rest_oauth1' ), 'rest_oauth1' ); ?>
117117

118118
<?php $wp_list_table->display(); ?>
119119

@@ -129,17 +129,17 @@ protected static function validate_parameters( $params ) {
129129
$valid = array();
130130

131131
if ( empty( $params['name'] ) ) {
132-
return new WP_Error( 'wombat_missing_name', __( 'Consumer name is required', 'wombat' ) );
132+
return new WP_Error( 'rest_oauth1_missing_name', __( 'Consumer name is required', 'rest_oauth1' ) );
133133
}
134134
$valid['name'] = wp_filter_post_kses( $params['name'] );
135135

136136
if ( empty( $params['description'] ) ) {
137-
return new WP_Error( 'wombat_missing_description', __( 'Consumer description is required', 'wombat' ) );
137+
return new WP_Error( 'rest_oauth1_missing_description', __( 'Consumer description is required', 'rest_oauth1' ) );
138138
}
139139
$valid['description'] = wp_filter_post_kses( $params['description'] );
140140

141141
if ( empty( $params['callback'] ) ) {
142-
return new WP_Error( 'wombat_missing_description', __( 'Consumer callback is required and must be a valid URL.', 'wombat' ) );
142+
return new WP_Error( 'rest_oauth1_missing_description', __( 'Consumer callback is required and must be a valid URL.', 'rest_oauth1' ) );
143143
}
144144
if ( ! empty( $params['callback'] ) ) {
145145
$valid['callback'] = $params['callback'];
@@ -157,11 +157,11 @@ protected static function handle_edit_submit( $consumer ) {
157157
$messages = array();
158158
if ( empty( $consumer ) ) {
159159
$did_action = 'add';
160-
check_admin_referer( 'wombat-add' );
160+
check_admin_referer( 'rest-oauth1-add' );
161161
}
162162
else {
163163
$did_action = 'edit';
164-
check_admin_referer( 'wombat-edit-' . $consumer->ID );
164+
check_admin_referer( 'rest-oauth1-edit-' . $consumer->ID );
165165
}
166166

167167
// Check that the parameters are correct first
@@ -202,15 +202,12 @@ protected static function handle_edit_submit( $consumer ) {
202202
}
203203

204204
// Success, redirect to alias page
205-
$location = add_query_arg(
205+
$location = self::get_url(
206206
array(
207-
'action' => 'wombat-edit',
207+
'action' => 'edit',
208208
'id' => $consumer->ID,
209209
'did_action' => $did_action,
210-
'processed' => 1,
211-
'_wpnonce' => wp_create_nonce( 'wombat-edit-' . $id ),
212-
),
213-
network_admin_url( 'admin.php' )
210+
)
214211
);
215212
wp_safe_redirect( $location );
216213
exit;
@@ -226,15 +223,15 @@ public static function render_edit_page() {
226223

227224
// Are we editing?
228225
$consumer = null;
229-
$form_action = admin_url( 'admin.php?action=wombat-add' );
226+
$form_action = self::get_url('action=add');
230227
if ( ! empty( $_REQUEST['id'] ) ) {
231228
$id = absint( $_REQUEST['id'] );
232229
$consumer = get_post( $id );
233230
if ( is_wp_error( $consumer ) || empty( $consumer ) ) {
234231
wp_die( __( 'Invalid consumer ID.' ) );
235232
}
236233

237-
$form_action = admin_url( 'admin.php?action=wombat-edit' );
234+
$form_action = self::get_url( array( 'action' => 'edit', 'id' => $id ) );
238235
}
239236

240237
// Handle form submission
@@ -258,7 +255,7 @@ public static function render_edit_page() {
258255

259256
// Header time!
260257
global $title, $parent_file, $submenu_file;
261-
$title = $consumer ? __( 'Edit Application', 'wombat' ) : __( 'Add Application', 'wombat' );
258+
$title = $consumer ? __( 'Edit Application', 'rest_oauth1' ) : __( 'Add Application', 'rest_oauth1' );
262259
$parent_file = 'users.php';
263260
$submenu_file = self::BASE_SLUG;
264261

@@ -332,12 +329,12 @@ public static function render_edit_page() {
332329
<?php
333330

334331
if ( empty( $consumer ) ) {
335-
wp_nonce_field( 'wombat-add' );
332+
wp_nonce_field( 'rest-oauth1-add' );
336333
submit_button( __( 'Add Consumer' ) );
337334
}
338335
else {
339336
echo '<input type="hidden" name="id" value="' . esc_attr( $consumer->ID ) . '" />';
340-
wp_nonce_field( 'wombat-edit-' . $consumer->ID );
337+
wp_nonce_field( 'rest-oauth1-edit-' . $consumer->ID );
341338
submit_button( __( 'Save Consumer' ) );
342339
}
343340

@@ -356,7 +353,7 @@ public static function handle_delete() {
356353
}
357354

358355
$id = $_GET['id'];
359-
check_admin_referer( 'wombat-delete:' . $id );
356+
check_admin_referer( 'rest-oauth1-delete:' . $id );
360357

361358
if ( ! rest_delete_client( $id ) ) {
362359
$message = 'Invalid consumer ID';

lib/class-wombat-listtable.php renamed to lib/class-wp-rest-oauth1-listtable.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
class Wombat_ListTable extends WP_List_Table {
3+
class WP_REST_OAuth1_ListTable extends WP_List_Table {
44
public function prepare_items() {
55
$paged = $this->get_pagenum();
66

@@ -43,7 +43,7 @@ public function get_columns() {
4343
public function column_cb( $item ) {
4444
?>
4545
<label class="screen-reader-text"
46-
for="cb-select-<?php echo esc_attr( $item->ID ) ?>"><?php esc_html_e( 'Select consumer', 'wombat' ); ?></label>
46+
for="cb-select-<?php echo esc_attr( $item->ID ) ?>"><?php esc_html_e( 'Select consumer', 'rest_oauth1' ); ?></label>
4747

4848
<input id="cb-select-<?php echo esc_attr( $item->ID ) ?>" type="checkbox"
4949
name="consumers[]" value="<?php echo $item->ID ?>" />
@@ -54,30 +54,30 @@ public function column_cb( $item ) {
5454
protected function column_name( $item ) {
5555
$title = get_the_title( $item->ID );
5656
if ( empty( $title ) ) {
57-
$title = '<em>' . esc_html__( 'Untitled', 'wombat' ) . '</em>';
57+
$title = '<em>' . esc_html__( 'Untitled', 'rest_oauth1' ) . '</em>';
5858
}
5959

6060
$edit_link = add_query_arg(
6161
array(
62-
'page' => 'wombat-apps',
62+
'page' => 'rest-oauth1-apps',
6363
'action' => 'edit',
6464
'id' => $item->ID,
6565
),
6666
admin_url( 'users.php' )
6767
);
6868
$delete_link = add_query_arg(
6969
array(
70-
'page' => 'wombat-apps',
70+
'page' => 'rest-oauth1-apps',
7171
'action' => 'delete',
7272
'id' => $item->ID,
7373
),
7474
admin_url( 'users.php' )
7575
);
76-
$delete_link = wp_nonce_url( $delete_link, 'wombat-delete:' . $item->ID );
76+
$delete_link = wp_nonce_url( $delete_link, 'rest-oauth1-delete:' . $item->ID );
7777

7878
$actions = array(
79-
'edit' => sprintf( '<a href="%s">%s</a>', $edit_link, esc_html__( 'Edit', 'wombat' ) ),
80-
'delete' => sprintf( '<a href="%s">%s</a>', $delete_link, esc_html__( 'Delete', 'wombat' ) ),
79+
'edit' => sprintf( '<a href="%s">%s</a>', $edit_link, esc_html__( 'Edit', 'rest_oauth1' ) ),
80+
'delete' => sprintf( '<a href="%s">%s</a>', $delete_link, esc_html__( 'Delete', 'rest_oauth1' ) ),
8181
);
8282
$action_html = $this->row_actions( $actions );
8383

0 commit comments

Comments
 (0)