Skip to content
Snippets Groups Projects
Commit 33bba8d0 authored by Erick Hitter's avatar Erick Hitter
Browse files

Settings page

parent d4f73c32
Branches
No related tags found
3 merge requests!6add linting,!5WIP: Add JUnit reporting,!4WIP: Initial release
...@@ -29,11 +29,21 @@ require_once PLUGIN_PATH . '/inc/class-options.php'; ...@@ -29,11 +29,21 @@ require_once PLUGIN_PATH . '/inc/class-options.php';
/** /**
* Options page * 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 * Assorted functions
*/ */
require_once PLUGIN_PATH . '/inc/functions.php'; 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; ...@@ -13,10 +13,36 @@ namespace Camo_Image_Proxy;
class Options_Page { class Options_Page {
use Singleton; 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 * Hooks
*/ */
public function setup() { 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' ] ); add_action( 'admin_init', [ $this, 'action_admin_init' ] );
} }
...@@ -24,9 +50,37 @@ class Options_Page { ...@@ -24,9 +50,37 @@ class Options_Page {
* Add fields to Media settings page * Add fields to Media settings page
*/ */
public function action_admin_init() { 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 { ...@@ -80,6 +80,21 @@ class Options {
* @return bool * @return bool
*/ */
public function set( string $option, $value ) : 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 ) { switch ( $option ) {
case 'host': case 'host':
$value = esc_url( $value ); $value = esc_url( $value );
...@@ -93,9 +108,20 @@ class Options { ...@@ -93,9 +108,20 @@ class Options {
return false; return false;
} }
$options = $this->get_all(); return $value;
$options[ $option ] = $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; ...@@ -12,6 +12,6 @@ namespace Camo_Image_Proxy;
* *
* @return object * @return object
*/ */
function options() { function Options() {
return Options::instance(); return Options::instance();
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment