Commit 33bba8d0 authored by Erick Hitter's avatar Erick Hitter
Browse files

Settings page

parent d4f73c32
......@@ -29,11 +29,21 @@ require_once PLUGIN_PATH . '/inc/class-options.php';
/**
* Options page
*/
if ( is_admin() ) {
require_once PLUGIN_PATH . '/inc/class-options-page.php';
}
require_once PLUGIN_PATH . '/inc/class-options-page.php';
/**
* Assorted functions
*/
require_once PLUGIN_PATH . '/inc/functions.php';
/**
* Load plugin singletons
*/
function init() {
Options::instance();
if ( is_admin() ) {
Options_Page::instance();
}
}
add_action( 'init', __NAMESPACE__ . '\init' );
......@@ -13,10 +13,36 @@ namespace Camo_Image_Proxy;
class Options_Page {
use Singleton;
/**
* Settings screen section
*
* @var string
*/
private $section = 'camp-image-proxy';
/**
* Field labels
*
* @var array
*/
private $labels = [];
/**
* Option name
*
* @var string
*/
private $name;
/**
* Hooks
*/
public function setup() {
$this->name = Options::instance()->name;
$this->labels['host'] = __( 'Host', 'camo-image-proxy' );
$this->labels['key'] = __( 'Shared Key', 'camo-image-proxy' );
add_action( 'admin_init', [ $this, 'action_admin_init' ] );
}
......@@ -24,9 +50,37 @@ class Options_Page {
* Add fields to Media settings page
*/
public function action_admin_init() {
//register_setting();
register_setting( 'media', $this->name, [ Options::instance(), 'sanitize_all' ] );
add_settings_section( $this->section, __( 'Camo Image Proxy', 'camo-image-proxy' ), '__return_false', 'media' );
foreach ( $this->labels as $key => $label ) {
$args = [
'option' => $key,
'label' => $label,
];
add_settings_field( $key, $label, [ $this, 'screen' ], 'media', $this->section, $args );
}
}
/**
* Render options field
*
* @param array $args Field arguments.
*/
public function screen( $args ) {
$value = Options::instance()->get( $args['option'] );
$input_type = 'host' === $args['option'] ? 'url' : 'text';
$name = sprintf( '%1$s[%2$s]', $this->name, $args['option'] );
$html_id = sprintf( '%1$s-%2$s', str_replace( '_', '-', $this->name ), $args['option'] );
//add_settings_section();
//add_settings_field();
?>
<input
type="<?php echo esc_attr( $input_type ); ?>"
name="<?php echo esc_html( $name ); ?>"
class="regular-text"
id="<?php echo esc_attr( $html_id ); ?>"
value="<?php echo esc_attr( $value ); ?>"
/>
<?php
}
}
......@@ -80,6 +80,21 @@ class Options {
* @return bool
*/
public function set( string $option, $value ) : bool {
$value = $this->sanitize( $option, $value );
$options = $this->get_all();
$options[ $option ] = $value;
return update_option( $this->name, $options );
}
/**
* Sanitize option
*
* @param string $option Plugin option.
* @param mixed $value Option value to sanitize.
* @return mixed
*/
public function sanitize( string $option, $value ) {
switch ( $option ) {
case 'host':
$value = esc_url( $value );
......@@ -93,9 +108,20 @@ class Options {
return false;
}
$options = $this->get_all();
$options[ $option ] = $value;
return $value;
}
return update_option( $this->name, $options );
/**
* Sanitize array of options
*
* @param array $options Options to sanitize.
* @return array
*/
public function sanitize_all( array $options ) : array {
foreach ( $options as $option => $value ) {
$options[ $option ] = $this->sanitize( $option, $value );
}
return $options;
}
}
......@@ -12,6 +12,6 @@ namespace Camo_Image_Proxy;
*
* @return object
*/
function options() {
function Options() {
return Options::instance();
}
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