From 33bba8d055ec56c141b35d0044dcd20846dbfc1a Mon Sep 17 00:00:00 2001
From: Erick Hitter <git-contrib@ethitter.com>
Date: Sun, 18 Feb 2018 14:45:43 -0800
Subject: [PATCH] Settings page

---
 camo-image-proxy.php       | 16 ++++++++--
 inc/class-options-page.php | 60 ++++++++++++++++++++++++++++++++++++--
 inc/class-options.php      | 32 ++++++++++++++++++--
 inc/functions.php          |  2 +-
 4 files changed, 100 insertions(+), 10 deletions(-)

diff --git a/camo-image-proxy.php b/camo-image-proxy.php
index 5306018..f431994 100755
--- a/camo-image-proxy.php
+++ b/camo-image-proxy.php
@@ -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' );
diff --git a/inc/class-options-page.php b/inc/class-options-page.php
index 7fbb526..0ed5cdb 100644
--- a/inc/class-options-page.php
+++ b/inc/class-options-page.php
@@ -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
 	}
 }
diff --git a/inc/class-options.php b/inc/class-options.php
index 67ff87b..b2addfd 100644
--- a/inc/class-options.php
+++ b/inc/class-options.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;
 	}
 }
diff --git a/inc/functions.php b/inc/functions.php
index 7666210..6179bab 100644
--- a/inc/functions.php
+++ b/inc/functions.php
@@ -12,6 +12,6 @@ namespace Camo_Image_Proxy;
  *
  * @return object
  */
-function options() {
+function Options() {
 	return Options::instance();
 }
-- 
GitLab