diff --git a/class-Options.php b/class-Options.php index b354a10..4e234e5 100644 --- a/class-Options.php +++ b/class-Options.php @@ -1,160 +1,156 @@ . /** * Site associative options handled by a specific database table (if you use it) */ class Options { /** * Cached options * @var array */ private $cache = []; /** * List of formally registered options. * @var array */ - private $optionsRegistered = []; + private $opts = []; /** * Get the singleton instance * * @return self */ public static function instance() { static $me = false; if( ! $me ) { $me = new self(); } return $me; } /** * Load options with autoload in cache */ public function autoload() { static $todo = true; if( $todo ) { $options = Query::factory() ->select( [ 'option_name', 'option_value', ] ) ->from( 'option' ) ->whereInt( 'option_autoload', 1 ) ->queryResults(); foreach( $options as $option ) { $this->override( $option->option_name, $option->option_value ); } $todo = false; } } /** * Formally register an option and know if it's already registered * * @param string $name Option name * @return bool Successfully or not */ public function register( $name ) { - if( in_array( $name, $this->optionsRegistered, true) ) { - DEBUG and error( sprintf( - 'error registering the option "%s" because it is already registered', - $name - ) ); - return false; + if( in_array( $name, $this->opts, true) ) { + return error( "error registering the option $name because it is already registered" ); } - $this->optionsRegistered[] = $name; + $this->opts[] = $name; return true; } /** * Get all formally registered options * * @return array */ public function getRegistereds() { - return $this->optionsRegistered; + return $this->opts; } /** * Get the value of an option * * @param string $name Option name * @param string $defalut Default option value */ public function get( $name, $default = '' ) { $value = ''; $this->autoload(); if( array_key_exists( $name, $this->cache ) ) { $value = $this->cache[ $name ]; } else { $value = Query::factory() ->from( 'option' ) ->whereStr( 'option_name', $name ) ->queryValue( 'option_value' ); $this->override( $name, $value ); } return empty( $value ) ? $default : $value; } /** * Set an option in the cache in order to override the database value * * @param string $name Option name * @param string $value Option value */ public function override( $name, $value ) { $this->cache[ $name ] = $value; } /** * Set the value of an option into the database (updating or inserting) and cache * * @param string $name Option name * @param string $value Option value * @param true $autoload If the option it's automatically requested on every page-request */ public function set( $name, $value, $autoload = true ) { $this->override( $name, $value ); $autoload = $autoload ? 1 : 0; insert_row( 'option', [ new DBCol( 'option_name', $name, 's' ), new DBCol( 'option_value', $value, 's' ), new DBCol( 'option_autoload', $autoload, 'd' ), ], [ 'replace-into' => true ] ); } /** * Permanently remove an option from the database * * @param string $name Option name */ public function remove( $name ) { query( sprintf( "DELETE FROM %s WHERE option_name LIKE '%s' LIMIT 1", T( 'option' ), esc_sql( $name ) ) ); $this->override( $name, null ); } }