Keyring) which includes:
* - managing Service credentials
* - creating connections
* - deleting connections
* - (coming soon) managing active/inactive Services
*
* Run Keyring with KEYRING__HEADLESS_MODE defined as true to disable all UI.
*
* @package Keyring
*/
class Keyring_Admin_UI {
var $keyring = false;
function __construct() {
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
}
static function &init() {
static $instance = false;
if ( !$instance ) {
$instance = new Keyring_Admin_UI;
}
return $instance;
}
function inline_css() {
?>keyring = Keyring::init();
add_action( 'admin_head', array( $this, 'inline_css' ) );
}
function admin_page_header( $screen = false ) {
// Output the actual heading + icon for the page
echo '
';
screen_icon( 'ms-admin' );
switch ( $screen ) {
case 'tokens' :
echo '
';
break;
case 'services' :
echo '
' . __( 'Add New Connection', 'keyring' ) . '
';
echo '
' . __( '← Back', 'keyring' ) . '
';
break;
case 'error' :
echo '
' . __( 'Keyring Error!', 'keyring' ) . '
';
break;
default :
echo '
' . __( 'Keyring', 'keyring' ) . '
';
}
// Output any errors if we have them, then stop, and link back to home.
if ( $this->keyring->has_errors() ) {
echo '
';
foreach ( $this->keyring->get_errors() as $error ) {
echo "- " . esc_html( $error ) . "
";
}
echo '
';
echo '
' . __( 'Start Again', 'keyring' ) . '
';
return;
}
// Output any messages as part of the UI (don't abort).
if ( $this->keyring->has_messages() ) {
echo '
';
foreach ( $this->keyring->get_messages() as $message ) {
echo "- " . esc_html( $message ) . "
";
}
echo '
';
}
}
static function admin_page_footer() {
echo '
'; // class="wrap"
}
function admin_page() {
// Handle delete request. Will default back to "tokens" later
if ( isset( $_REQUEST['action'] ) && 'delete' == $_REQUEST['action'] ) {
if ( !isset( $_REQUEST['nonce'] ) || !wp_verify_nonce( $_REQUEST['nonce'], 'keyring-delete-' . $_REQUEST['service'] . '-' . $_REQUEST['token'] ) ) {
Keyring::error( __( 'Invalid/missing delete nonce.', 'keyring' ) );
exit;
}
if ( $this->keyring->get_token_store()->delete( array( 'id' => (int) $_REQUEST['token'], 'type' => 'access' ) ) )
Keyring::message( __( 'That connection has been deleted.', 'keyring' ) );
else
Keyring::message( __( 'Could not delete that connection!', 'keyring' ) );
}
// Set up our defaults
$service = '';
if ( !empty( $_REQUEST['service'] ) )
$service = $_REQUEST['service'];
$action = 'tokens';
if ( isset( $_REQUEST['action'] ) && in_array( $_REQUEST['action'], array( 'tokens', 'services', 'request', 'verify', 'manage' ) ) )
$action = $_REQUEST['action'];
// Custom UI optionally hooked in to handle this service/action. Trigger action
// and assume it handles everything, so bail out after that.
if ( Keyring_Util::has_custom_ui( $service, $action ) ) {
do_action( "keyring_{$service}_{$action}_ui" );
return;
}
// Nothing else has bailed, so it must be one of our default/core screens.
switch ( $action ) {
case 'tokens' :
$this->admin_page_header( 'tokens' );
$list_table = new Keyring_Connections_List_Table();
$list_table->display();
$this->admin_page_footer();
break;
case 'services' :
$this->admin_page_header( 'services' );
$services = $this->keyring->get_registered_services();
if ( count( $services ) ) {
$configured = $not_configured = array();
foreach ( $services as $service ) {
if ( $service->is_configured() )
$configured[] = $service;
else
$not_configured[] = $service;
}
if ( count( $configured ) ) {
echo '' . __( 'Click a service to create a new connection:', 'keyring' ) . '
';
echo '';
foreach ( $configured as $service ) {
$request_kr_nonce = wp_create_nonce( 'keyring-request' );
$request_nonce = wp_create_nonce( 'keyring-request-' . $service->get_name() );
echo '- ' . esc_html( $service->get_label() ) . '';
if ( has_action( 'keyring_' . $service->get_name() . '_manage_ui' ) ) {
$manage_kr_nonce = wp_create_nonce( 'keyring-manage' );
$manage_nonce = wp_create_nonce( 'keyring-manage-' . $service->get_name() );
echo ' (' . esc_html( __( 'Manage', 'keyring' ) ) . ')';
}
echo '
';
}
echo '
';
} else {
echo '' . __( 'There are no fully-configured services available to connect to.', 'keyring' ) . '
';
}
if ( count( $not_configured ) ) {
echo '' . __( 'The following services need to be configured correctly before you can connect to them.', 'keyring' ) . '
';
echo '';
foreach ( $not_configured as $service ) {
if ( !has_action( 'keyring_' . $service->get_name() . '_manage_ui' ) )
continue;
$manage_kr_nonce = wp_create_nonce( 'keyring-manage' );
$manage_nonce = wp_create_nonce( 'keyring-manage-' . $service->get_name() );
echo '- ' . esc_html( $service->get_label() ) . '
';
}
echo '
';
}
}
$this->admin_page_footer();
break;
}
}
}
/** WordPress List Table Administration API and base class */
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
class Keyring_Connections_List_Table extends WP_List_Table {
var $keyring = false;
function __construct() {
$this->keyring = Keyring::init();
parent::__construct( array(
'singular' => 'connection',
'plural' => 'connections',
'screen' => $this->keyring->admin_page,
) );
$this->items = Keyring::get_token_store()->get_tokens();
}
function no_items() {
echo '' . sprintf( __( 'You haven\'t added any connections yet. Add a New Connection.', 'keyring' ), esc_url( Keyring_Util::admin_url( false, array( 'action' => 'services' ) ) ) ) . '
';
}
function get_columns() {
return array(
'service' => __( 'Service', 'keyring' ),
'avatar' => __( 'Avatar', 'keyring' ),
'id' => __( 'External ID', 'keyring' ),
'name' => __( 'Name', 'keyring' ),
'actions' => ' '
);
}
function column_service( $row ) {
echo $row->get_service()->get_label();
}
function column_avatar( $row ) {
$picture = $row->get_meta( 'picture' );
if ( $picture ) {
echo '';
} else {
echo '-';
}
}
function column_id( $row ) {
echo $row->get_meta( 'user_id' );
}
function column_name( $row ) {
// Make a few attempts to get something to display here
$name = $row->get_meta( 'name' );
if ( !$name )
$name = $row->get_meta( 'username' );
if ( !$name )
$name = trim( $row->get_meta( 'first_name' ) . ' ' . $row->get_meta( 'last_name' ) );
if ( $name )
echo $name;
else
echo '-';
}
function column_actions( $row ) {
$kr_nonce = wp_create_nonce( 'keyring-delete' );
$delete_nonce = wp_create_nonce( 'keyring-delete-' . $row->get_service()->get_name() . '-' . $row->get_uniq_id() );
echo 'Delete';
}
}