Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renaissance (partial rewrite) #98

Merged
merged 30 commits into from
Dec 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
fbdf01c
Rename plugin
rmccue Nov 22, 2015
1a910ae
Move filter registration out of constructor
rmccue Nov 22, 2015
d5e0928
Change function prefixes
rmccue Nov 22, 2015
11cb0aa
Rename UI class
rmccue Nov 22, 2015
f956ab4
Rename CLI class
rmccue Nov 22, 2015
8391515
Remove base class
rmccue Nov 22, 2015
a8952cc
Rename main class
rmccue Nov 22, 2015
0cf8504
Fix syntax error
rmccue Nov 22, 2015
6319e15
Call correct function for client
rmccue Nov 22, 2015
8b62719
Show key/secret on the client page
rmccue Nov 22, 2015
eeda304
Add the ability to delete clients
rmccue Nov 22, 2015
5b66d5b
Handle infinite recursion correctly
rmccue Nov 23, 2015
5f7f71c
Use correct names for meta querying
rmccue Nov 23, 2015
7b32d2e
Validate callback URLs using a stored version
rmccue Nov 23, 2015
e23fd7a
Allow OOB for out-of-band callbacks
rmccue Nov 23, 2015
bc2a797
Signed-off-by: swicks <swicks@devicesoftware.com>
swicks Nov 28, 2015
bc98845
Merge pull request #99 from swicks/renaissance
rmccue Nov 29, 2015
2a338bb
Fixed bug in Profile > Authorized Applications which failed to return…
kosso Dec 6, 2015
40a16ee
Slightly better fomratting on the table. The core css has .form-table…
kosso Dec 6, 2015
a54d1fd
Applications list styling tweak.
kosso Dec 6, 2015
ab42efc
Merge pull request #101 from kosso/renaissance
rmccue Dec 7, 2015
1e61a90
Start renaming prefixes to wombat_
rmccue Dec 7, 2015
d3869fd
Rework admin internals
rmccue Dec 7, 2015
50611d0
Display success message on creation or edit
rmccue Dec 7, 2015
fd031fa
Show message when application is deleted
rmccue Dec 7, 2015
9093493
Introduce the client object
rmccue Dec 7, 2015
2e8436b
Allow regenerating secret
rmccue Dec 7, 2015
67920e0
Add extra check for permissions in delete/regen
rmccue Dec 7, 2015
c94a814
Update plugin header
rmccue Dec 7, 2015
141f4f8
Use correct ::get function when we have a key
rmccue Dec 7, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
309 changes: 26 additions & 283 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,276 +3,18 @@
* Administration UI and utilities
*/

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

add_action( 'admin_action_json-oauth-add', 'json_oauth_admin_edit_page' );
add_action( 'admin_action_json-oauth-edit', 'json_oauth_admin_edit_page' );
add_action( 'admin_menu', array( 'WP_REST_OAuth1_Admin', 'register' ) );

add_action( 'personal_options', 'json_oauth_profile_section', 50 );
add_action( 'personal_options', 'rest_oauth1_profile_section', 50 );

add_action( 'all_admin_notices', 'json_oauth_profile_messages' );
add_action( 'all_admin_notices', 'rest_oauth1_profile_messages' );

add_action( 'personal_options_update', 'json_oauth_profile_save', 10, 1 );
add_action( 'edit_user_profile_update', 'json_oauth_profile_save', 10, 1 );
add_action( 'personal_options_update', 'rest_oauth1_profile_save', 10, 1 );
add_action( 'edit_user_profile_update', 'rest_oauth1_profile_save', 10, 1 );

/**
* Register the admin page
*/
function json_oauth_admin_register() {
/**
* Include anything we need that relies on admin classes/functions
*/
include_once dirname( __FILE__ ) . '/lib/class-wp-json-authentication-oauth1-listtable.php';

add_users_page(
// Page title
__( 'Registered OAuth Applications', 'json_oauth' ),

// Menu title
_x( 'Applications', 'menu title', 'json_oauth' ),

// Capability
'list_users',

// Menu slug
'json-oauth',

// Callback
'json_oauth_admin_render'
);
}

function json_oauth_admin_prerender() {
$hook = get_plugin_page_hook( 'json-oauth', 'users.php' );

add_action( 'load-' . $hook, 'json_oauth_admin_load' );
}

function json_oauth_admin_load() {
global $wp_list_table;

$wp_list_table = new WP_JSON_Authentication_OAuth1_ListTable();

$wp_list_table->prepare_items();
}

function json_oauth_admin_render() {
global $wp_list_table;

// ...
?>
<div class="wrap">
<h2>
<?php
esc_html_e( 'Registered OAuth Applications', 'json_oauth' );

if ( current_user_can( 'create_users' ) ): ?>
<a href="<?php echo admin_url( 'admin.php?action=json-oauth-add' ) ?>"
class="add-new-h2"><?php echo esc_html_x( 'Add New', 'application', 'json_oauth' ); ?></a>
<?php
endif;
?>
</h2>

<?php $wp_list_table->views(); ?>

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

<?php $wp_list_table->search_box( __( 'Search Applications', 'json_oauth' ), 'json_oauth' ); ?>

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

</form>

<br class="clear" />

</div>
<?php
}

function json_oauth_admin_validate_parameters( $params ) {
$valid = array();

if ( empty( $params['name'] ) ) {
return new WP_Error( 'json_oauth_missing_name', __( 'Consumer name is required' ) );
}
$valid['name'] = wp_filter_post_kses( $params['name'] );

if ( empty( $params['description'] ) ) {
return new WP_Error( 'json_oauth_missing_description', __( 'Consumer description is required' ) );
}
$valid['description'] = wp_filter_post_kses( $params['description'] );

return $valid;
}

/**
* Handle submission of the add page
*
* @return array|null List of errors. Issues a redirect and exits on success.
*/
function json_oauth_admin_handle_edit_submit( $consumer ) {
$messages = array();
if ( empty( $consumer ) ) {
$did_action = 'add';
check_admin_referer( 'json-oauth-add' );
}
else {
$did_action = 'edit';
check_admin_referer( 'json-oauth-edit-' . $consumer->ID );
}

// Check that the parameters are correct first
$params = json_oauth_admin_validate_parameters( wp_unslash( $_POST ) );
if ( is_wp_error( $params ) ) {
$messages[] = $params->get_error_message();
return $messages;
}

if ( empty( $consumer ) ) {
$authenticator = new WP_JSON_Authentication_OAuth1();

// Create the consumer
$data = array(
'name' => $params['name'],
'description' => $params['description'],
);
$consumer = $result = $authenticator->add_consumer( $data );
}
else {
// Update the existing consumer post
$data = array(
'ID' => $consumer->ID,
'post_title' => $params['name'],
'post_content' => $params['description'],
);
$result = wp_update_post( $data, true );
}

if ( is_wp_error( $result ) ) {
$messages[] = $result->get_error_message();

return $messages;
}

// Success, redirect to alias page
$location = add_query_arg(
array(
'action' => 'json-oauth-edit',
'id' => $consumer->ID,
'did_action' => $did_action,
'processed' => 1,
'_wpnonce' => wp_create_nonce( 'json-oauth-edit-' . $id ),
),
network_admin_url( 'admin.php' )
);
wp_safe_redirect( $location );
exit;
}

/**
* Output alias editing page
*/
function json_oauth_admin_edit_page() {
if ( ! current_user_can( 'edit_users' ) )
wp_die( __( 'You do not have permission to access this page.' ) );

// Are we editing?
$consumer = null;
$form_action = admin_url( 'admin.php?action=json-oauth-add' );
if ( ! empty( $_REQUEST['id'] ) ) {
$id = absint( $_REQUEST['id'] );
$consumer = get_post( $id );
if ( is_wp_error( $consumer ) || empty( $consumer ) ) {
wp_die( __( 'Invalid consumer ID.' ) );
}

$form_action = admin_url( 'admin.php?action=json-oauth-edit' );
}

// Handle form submission
$messages = array();
if ( ! empty( $_POST['submit'] ) ) {
$messages = json_oauth_admin_handle_edit_submit( $consumer );
}

$data = array();

if ( empty( $consumer ) || ! empty( $_POST['_wpnonce'] ) ) {
foreach ( array( 'name', 'description' ) as $key ) {
$data[ $key ] = empty( $_POST[ $key ] ) ? '' : wp_unslash( $_POST[ $key ] );
}
}
else {
$data['name'] = $consumer->post_title;
$data['description'] = $consumer->post_content;
}

// Header time!
global $title, $parent_file, $submenu_file;
$title = $consumer ? __( 'Edit Consumer' ) : __( 'Add Consumer' );
$parent_file = 'users.php';
$submenu_file = 'json-oauth';

include( ABSPATH . 'wp-admin/admin-header.php' );
?>

<div class="wrap">
<h2 id="edit-site"><?php echo esc_html( $title ) ?></h2>

<?php
if ( ! empty( $messages ) ) {
foreach ( $messages as $msg )
echo '<div id="message" class="updated"><p>' . $msg . '</p></div>';
}
?>

<form method="post" action="<?php echo esc_url( $form_action ) ?>">
<table class="form-table">
<tr>
<th scope="row">
<label for="oauth-name"><?php echo esc_html_x( 'Consumer Name', 'field name' ) ?></label>
</th>
<td>
<input type="text" class="regular-text"
name="name" id="oauth-name"
value="<?php echo esc_attr( $data['name'] ) ?>" />
</td>
</tr>
<tr>
<th scope="row">
<label for="oauth-description"><?php echo esc_html_x( 'Description', 'field name' ) ?></label>
</th>
<td>
<textarea class="regular-text" name="description" id="oauth-description"
cols="30" rows="5" style="width: 500px"><?php echo esc_textarea( $data['description'] ) ?></textarea>
</td>
</tr>
</table>

<?php

if ( empty( $consumer ) ) {
wp_nonce_field( 'json-oauth-add' );
submit_button( __( 'Add Consumer' ) );
}
else {
echo '<input type="hidden" name="id" value="' . esc_attr( $consumer->ID ) . '" />';
wp_nonce_field( 'json-oauth-edit-' . $consumer->ID );
submit_button( __( 'Save Consumer' ) );
}

?>
</form>
</div>

<?php

include(ABSPATH . 'wp-admin/admin-footer.php');
}

function json_oauth_profile_section( $user ) {
function rest_oauth1_profile_section( $user ) {
global $wpdb;

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

$authenticator = new WP_JSON_Authentication_OAuth1();
$authenticator = new WP_REST_OAuth1();

?>
<table class="form-table">
<tbody>
<tr>
<th scope="row"><?php _e( 'Authorized Applications', 'json_oauth' ) ?></th>
<th scope="row"><?php _e( 'Authorized Applications', 'rest_oauth1' ) ?></th>
<td>
<?php if ( ! empty( $approved ) ): ?>
<table class="widefat sessions-table">
<table class="widefat">
<thead>
<tr>
<th scope="col"><?php _e( 'Application Name', 'wpsm' ); ?></th>
<th style="padding-left:10px;"><?php esc_html_e( 'Application Name', 'rest_oauth1' ); ?></th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach ( $approved as $row ): ?>
<?php
$application = $authenticator->get_consumer( $row['consumer'] );
$application = get_post($row['consumer']);
?>
<tr>
<td><?php echo esc_html( $application->post_title ) ?></td>
<td><button class="button" name="oauth_revoke" value="<?php echo esc_attr( $row['key'] ) ?>"><?php esc_html_e( 'Revoke', 'json_oauth' ) ?></button>
<td><button class="button" name="oauth_revoke" value="<?php echo esc_attr( $row['key'] ) ?>"><?php esc_html_e( 'Revoke', 'rest_oauth1' ) ?></button>
</tr>

<?php endforeach ?>
</tbody>
</table>
<?php else: ?>
<p class="description"><?php esc_html_e( 'No applications authorized.' ) ?></p>
<p class="description"><?php esc_html_e( 'No applications authorized.', 'rest_oauth1' ) ?></p>
<?php endif ?>
</td>
</tr>
Expand All @@ -319,35 +62,35 @@ function json_oauth_profile_section( $user ) {
<?php
}

function json_oauth_profile_messages() {
function rest_oauth1_profile_messages() {
global $pagenow;
if ( $pagenow !== 'profile.php' && $pagenow !== 'user-edit.php' ) {
return;
}

if ( ! empty( $_GET['oauth_revoked'] ) ) {
echo '<div id="message" class="updated"><p>' . __( 'Token revoked.' ) . '</p></div>';
if ( ! empty( $_GET['rest_oauth1_revoked'] ) ) {
echo '<div id="message" class="updated"><p>' . __( 'Token revoked.', 'rest_oauth1' ) . '</p></div>';
}
if ( ! empty( $_GET['oauth_revocation_failed'] ) ) {
echo '<div id="message" class="updated"><p>' . __( 'Unable to revoke token.' ) . '</p></div>';
if ( ! empty( $_GET['rest_oauth1_revocation_failed'] ) ) {
echo '<div id="message" class="updated"><p>' . __( 'Unable to revoke token.', 'rest_oauth1' ) . '</p></div>';
}
}

function json_oauth_profile_save( $user_id ) {
if ( empty( $_POST['oauth_revoke'] ) ) {
function rest_oauth1_profile_save( $user_id ) {
if ( empty( $_POST['rest_oauth1_revoke'] ) ) {
return;
}

$key = wp_unslash( $_POST['oauth_revoke'] );
$key = wp_unslash( $_POST['rest_oauth1_revoke'] );

$authenticator = new WP_JSON_Authentication_OAuth1();
$authenticator = new WP_REST_OAuth1();

$result = $authenticator->revoke_access_token( $key );
if ( is_wp_error( $result ) ) {
$redirect = add_query_arg( 'oauth_revocation_failed', true, get_edit_user_link( $user_id ) );
$redirect = add_query_arg( 'rest_oauth1_revocation_failed', true, get_edit_user_link( $user_id ) );
}
else {
$redirect = add_query_arg( 'oauth_revoked', $key, get_edit_user_link( $user_id ) );
$redirect = add_query_arg( 'rest_oauth1_revoked', $key, get_edit_user_link( $user_id ) );
}
wp_redirect($redirect);
exit;
Expand Down
Loading