Commit 5da94812 authored by Erick Hitter's avatar Erick Hitter
Browse files

Functional, but unfinished, options page.

Still need to provide switch for environment.

No user-level work yet.
parent c1ee5d28
......@@ -30,12 +30,29 @@ class Authy_WP {
// Oh look, a singleton
private static $__instance = null;
private $settings = null;
// Authy API
protected $api_key = null;
protected $api_endpoint = null;
// Commong plugin elements
protected $settings_page = 'authy-for-wp';
protected $users_page = 'authy-for-wp-user';
protected $settings_key = 'authy_for_wp';
protected $users_key = 'authy_for_wp_user';
protected $settings_fields = array();
protected $settings_field_defaults = array(
'label' => null,
'type' => 'text',
'sanitizer' => 'sanitize_text_field',
'section' => 'default',
'class' => null
);
/**
* Singleton implementation
*
......@@ -60,11 +77,53 @@ class Authy_WP {
*
*/
private function setup() {
$this->register_settings_fields();
$this->prepare_api();
// Commong plugin elements
add_action( 'admin_init', array( $this, 'action_admin_init' ) );
add_action( 'admin_menu', array( $this, 'action_admin_menu' ) );
}
/**
*
*/
protected function register_settings_fields() {
$this->settings_fields = array(
array(
'name' => 'api_key_production',
'label' => __( 'Production API Key', 'authy_wp' ),
'type' => 'text',
'sanitizer' => 'alphanumeric'
),
array(
'name' => 'api_key_development',
'label' => __( 'Development API Key', 'authy_wp' ),
'type' => 'text',
'sanitizer' => 'alphanumeric'
)
);
}
/**
*
*/
protected function prepare_api() {
$endpoints = array(
'production' => 'https://api.authy.com',
'development' => 'http://sandbox-api.authy.com'
);
$environment = $this->get_setting( 'environment' );
$api_key = $this->get_setting( 'api_key_' . $environment );
if ( $api_key && isset( $endpoints[ $environment ] ) ) {
$this->api_key = $api_key;
$this->api_endpoint = $endpoints[ $environment ];
}
}
/**
* COMMON PLUGIN ELEMENTS
*/
......@@ -73,7 +132,8 @@ class Authy_WP {
*
*/
public function action_admin_init() {
register_setting( $this->settings_key, $this->settings_key, array( $this, 'validate_plugin_settings' ) );
register_setting( $this->settings_page, $this->settings_key, array( $this, 'validate_plugin_settings' ) );
add_settings_section( 'default', '', array( $this, 'register_settings_page_sections' ), $this->settings_page );
}
/**
......@@ -83,22 +143,125 @@ class Authy_WP {
add_options_page( 'Authy for WP', 'Authy for WP', 'manage_options', $this->settings_page, array( $this, 'plugin_settings_page' ) );
}
/**
*
*/
public function get_setting( $key ) {
$value = false;
if ( is_null( $this->settings ) || ! is_array( $this->settings ) ) {
$this->settings = get_option( $this->settings_key );
$this->settings = wp_parse_args( $this->settings, array(
'api_key_production' => '',
'api_key_development' => '',
'environment' => 'development'
) );
}
if ( isset( $this->settings[ $key ] ) )
$value = $this->settings[ $key ];
return $value;
}
/**
* GENERAL OPTIONS PAGE
*/
/**
*
*/
public function register_settings_page_sections() {
foreach ( $this->settings_fields as $args ) {
$args = wp_parse_args( $args, $this->settings_field_defaults );
add_settings_field( $args['name'], $args['label'], array( $this, 'form_field_' . $args['type'] ), $this->settings_page, $args['section'], $args );
}
}
/**
*
*/
public function form_field_text( $args ) {
$args = wp_parse_args( $args, $this->settings_field_defaults );
$name = esc_attr( $args['name'] );
if ( empty( $name ) )
return;
if ( is_null( $args['class'] ) )
$args['class'] = 'regular-text';
$value = $this->get_setting( $args['name'] );
?><input type="text" name="<?php echo esc_attr( $this->settings_key ); ?>[<?php echo $name; ?>]" class="<?php echo esc_attr( $args['class'] ); ?>" id="field-<?php echo $name; ?>" value="<?php echo esc_attr( $value ); ?>" /><?php
}
/**
*
*/
public function plugin_settings_page() {
//
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
<form action="options.php" method="post">
<?php settings_fields( $this->settings_page ); ?>
<?php do_settings_sections( $this->settings_page ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
*
*/
public function validate_plugin_settings( $settings ) {
check_admin_referer( $this->settings_page . '-options' );
$settings_validated = array();
foreach ( $this->settings_fields as $field ) {
$field = wp_parse_args( $field, $this->settings_field_defaults );
if ( ! isset( $settings[ $field['name'] ] ) )
continue;
switch ( $field['type'] ) {
case 'text' :
switch ( $field['sanitizer'] ) {
case 'alphanumeric' :
$value = preg_replace( '#[^a-z0-9]#i', '', $settings[ $field['name' ] ] );
break;
default:
case 'sanitize_text_field' :
$value = sanitize_text_field( $settings[ $field['name'] ] );
break;
}
break;
default:
$value = sanitize_text_field( $settings[ $field['name'] ] );
break;
}
if ( isset( $value ) && ! empty( $value ) )
$settings_validated[ $field['name'] ] = $value;
}
return $settings_validated;
}
/**
* USER SETTINGS PAGE
*/
/**
*
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment