Last active
November 12, 2017 21:33
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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.