Skip to content

Instantly share code, notes, and snippets.

@rheinardkorf
Last active November 12, 2017 21:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rheinardkorf/dab0f0f46b814e1a7d1d to your computer and use it in GitHub Desktop.
Save rheinardkorf/dab0f0f46b814e1a7d1d to your computer and use it in GitHub Desktop.
Enable the WP API OAuth Server consumer UI to create key pairs without using WP CLI.
<?php
/*
Plugin Name: Enable OAuth UI
Plugin URI: https://gist.github.com/rheinardkorf/dab0f0f46b814e1a7d1d
Description: Allows you to create consumer key pairs without WP CLI and the ability to name them for each APP. Must have WP API Oauth Server plugin active.
Version: 0.1
Author: Rheinard Korf
Author URI: https://gist.github.com/rheinardkorf/
License: GPL2
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
function rk20151105_enable_oauth_ui( $args, $post_type ) {
if( $post_type === 'json_consumer' ) {
$args['show_ui'] = true;
$args['supports'] = array('title');
$args['labels'] = array(
'name' => __( 'API Consumer' ),
'singular_name' => __( 'API Consumers' ),
);
}
return $args;
}
add_filter('register_post_type_args', 'rk20151105_enable_oauth_ui', 1, 2 );
function rk20151105_add_consumer_meta( $post, $args ) {
$key = get_post_meta( $post->ID, 'key', true );
$secret = get_post_meta( $post->ID, 'secret', true );
$type = get_post_meta( $post->ID, 'type', true );
if( empty( $key ) ) {
$consumer_meta = array(
'key' => wp_generate_password( WP_JSON_Authentication_OAuth1::CONSUMER_KEY_LENGTH, false ),
'secret' => wp_generate_password( WP_JSON_Authentication_OAuth1::CONSUMER_SECRET_LENGTH, false ),
);
$consumer_params = array( 'meta' => $consumer_meta, 'ID' => $post->ID );
rk20151105_update_consumer_meta( $consumer_params );
$key = get_post_meta( $post->ID, 'key', true );
$secret = get_post_meta( $post->ID, 'secret', true );
$type = get_post_meta( $post->ID, 'type', true );
}
echo '<div><strong>Object ID:</strong> ' . $post->ID . '</div>';
echo '<div><strong>Key:</strong> ' . $key . '</div>';
echo '<div><strong>Type:</strong> ' . $type . '</div>';
echo '<div><strong>Secret:</strong><span class="secret-to-hide" style="display:none;"> ' . $secret . '</span></div>';
echo '<div><span class="secret-toggle button button-primary">Show/Hide</span></div>';
echo '<script>jQuery(".secret-toggle").on("click", function() { jQuery(".secret-to-hide").toggle(); } )</script>';
}
function rk20151105_add_meta_boxes() {
add_meta_box(
'meta-box-one',
__( 'Consumer Meta' ),
'rk20151105_add_consumer_meta',
'json_consumer',
'normal',
'high',
array()
);
}
add_action( 'add_meta_boxes', 'rk20151105_add_meta_boxes' );
function rk20151105_update_consumer_meta( $params ) {
$default = array(
'name' => '',
'description' => '',
'meta' => array(),
);
$params = wp_parse_args( $params, $default );
$ID = $params['ID'];
unset( $params['ID'] );
$meta = $params['meta'];
$meta['type'] = 'oauth1';
$meta = apply_filters( 'json_consumer_meta', $meta, $ID, $params );
foreach ( $meta as $key => $value ) {
update_post_meta( $ID, $key, $value );
}
}
function rk20151105_admin_notice() {
if( ! class_exists( 'WP_JSON_Authentication_OAuth1' ) ) {
?>
<div class="updated">
<p>Please install the WP API OAuth Server Plugin ( <a href="https://github.com/WP-API/OAuth1">https://github.com/WP-API/OAuth1</a> ) for Enable OAuth UI to work.</p>
</div>
<?php
}
}
add_action( 'admin_notices', 'rk20151105_admin_notice' );
@harzer-knaller
Copy link

How should use this plugin? If I install this plugin in WordPress nothing happened ?! Or did I miss something?

@rheinardkorf
Copy link
Author

@harzer-knaller, I apologize for such an incredibly late reply.

This was proof of concept and will not work without WP-API/OAuth1 plugin installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment