From 26dc85d6144cf9ea4c689647b48cd84539d7e4d2 Mon Sep 17 00:00:00 2001
From: Erick Hitter <git-contrib@ethitter.com>
Date: Sun, 18 Feb 2018 12:06:54 -0800
Subject: [PATCH] Basic options class

---
 camo-image-proxy.php  | 10 +++++++
 inc/class-options.php | 67 +++++++++++++++++++++++++++++++++++++++++++
 inc/functions.php     | 17 +++++++++++
 3 files changed, 94 insertions(+)
 create mode 100644 inc/class-options.php
 create mode 100644 inc/functions.php

diff --git a/camo-image-proxy.php b/camo-image-proxy.php
index d9f8865..3833b1d 100755
--- a/camo-image-proxy.php
+++ b/camo-image-proxy.php
@@ -20,3 +20,13 @@ define( __NAMESPACE__ . '\PLUGIN_PATH', dirname( __FILE__ ) );
  * Trait for singletons
  */
 require_once PLUGIN_PATH . '/inc/trait-singleton.php';
+
+/**
+ * Plugin options
+ */
+require_once PLUGIN_PATH . '/inc/class-options.php';
+
+/**
+ * Assorted functions
+ */
+require_once PLUGIN_PATH . '/inc/functions.php';
diff --git a/inc/class-options.php b/inc/class-options.php
new file mode 100644
index 0000000..084f38d
--- /dev/null
+++ b/inc/class-options.php
@@ -0,0 +1,67 @@
+<?php
+/**
+ * Plugin options
+ *
+ * @package Camo_Image_Proxy
+ */
+
+namespace Camo_Image_Proxy;
+
+/**
+ * Class Options
+ */
+class Options {
+	use Singleton;
+
+	/**
+	 * Option name
+	 *
+	 * @var string
+	 */
+	private $name = 'camo_image_proxy_opts';
+
+	/**
+	 * Allowed options
+	 *
+	 * @var array
+	 */
+	private $allowed_options = [
+		'host' => '',
+		'key'  => '',
+	];
+
+	/**
+	 * Hooks and other preparations
+	 */
+	public function setup() {
+		// Hooks and such.
+	}
+
+	/**
+	 * Get plugin option
+	 *
+	 * @param string $option Plugin option to retrieve.
+	 * @return mixed
+	 */
+	public function get( string $option ) {
+		if ( ! array_key_exists( $option, $this->allowed_options ) ) {
+			return false;
+		}
+
+		$options = get_option( $this->name, [] );
+		$options = wp_parse_args( $options, $this->allowed_options );
+
+		return $options[ $option ] ?? false;
+	}
+
+	/**
+	 * Set plugin option
+	 *
+	 * @param string $option Plugin option to set.
+	 * @param mixed  $value Option value.
+	 * @return bool
+	 */
+	public function update( string $option, $value ) : bool {
+		return false;
+	}
+}
diff --git a/inc/functions.php b/inc/functions.php
new file mode 100644
index 0000000..1e11a12
--- /dev/null
+++ b/inc/functions.php
@@ -0,0 +1,17 @@
+<?php
+/**
+ * Assorted helpers
+ *
+ * @pacakge Camo_Image_Proxy
+ */
+
+namespace Camo_Image_Proxy;
+
+/**
+ * Access plugin options
+ *
+ * @return object
+ */
+function options() {
+	return Options::instance();
+}
-- 
GitLab