From f16fc37a100434e98f8e3308fe38a8abb8687182 Mon Sep 17 00:00:00 2001 From: Erick Hitter <git-contrib@ethitter.com> Date: Sun, 18 Feb 2018 12:59:43 -0800 Subject: [PATCH] Finish options class --- inc/class-options.php | 34 ++++++++++++++++++++++++++-------- inc/functions.php | 2 +- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/inc/class-options.php b/inc/class-options.php index 084f38d..4927205 100644 --- a/inc/class-options.php +++ b/inc/class-options.php @@ -31,10 +31,14 @@ class Options { ]; /** - * Hooks and other preparations + * Retrieve full plugin options + * + * @return array */ - public function setup() { - // Hooks and such. + private function get_all() : array { + $options = get_option( $this->name, [] ); + $options = wp_parse_args( $options, $this->allowed_options ); + return $options; } /** @@ -48,9 +52,7 @@ class Options { return false; } - $options = get_option( $this->name, [] ); - $options = wp_parse_args( $options, $this->allowed_options ); - + $options = $this->get_all(); return $options[ $option ] ?? false; } @@ -61,7 +63,23 @@ class Options { * @param mixed $value Option value. * @return bool */ - public function update( string $option, $value ) : bool { - return false; + public function set( string $option, $value ) : bool { + switch ( $option ) { + case 'host': + $value = esc_url( $value ); + break; + + case 'key': + $value = sanitize_text_field( $value ); + break; + + default: + return false; + } + + $options = $this->get_all(); + $options[ $option ] = $value; + + return update_option( $this->name, $options ); } } diff --git a/inc/functions.php b/inc/functions.php index 1e11a12..7666210 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -2,7 +2,7 @@ /** * Assorted helpers * - * @pacakge Camo_Image_Proxy + * @package Camo_Image_Proxy */ namespace Camo_Image_Proxy; -- GitLab