| @@ -8,20 +8,18 @@ | ||
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | class DataModel { |
| 11 | 11 | private $db_key; |
| 12 | - private static $instances = []; | |
| 12 | + private static $instance; | |
| 13 | 13 | |
| 14 | - private function __construct( $source ) { | |
| 15 | - $this->set_db_key( $source ); | |
| 16 | - } | |
| 17 | - | |
| 18 | 14 | public static function source( $source = 'settings' ) { |
| 19 | - if ( ! isset( self::$instances[ $source ] ) ) { | |
| 20 | - self::$instances[ $source ] = new self( $source ); | |
| 15 | + if ( ! self::$instance ) { | |
| 16 | + self::$instance = new self(); | |
| 21 | 17 | } |
| 22 | 18 | |
| 23 | - return self::$instances[ $source ]; | |
| 19 | + self::$instance->set_db_key( $source ); | |
| 20 | + | |
| 21 | + return self::$instance; | |
| 24 | 22 | } |
| 25 | 23 | |
| 26 | 24 | private function set_db_key( $source ) { |
| 27 | 25 | $this->db_key = 'rtsb_' . $source; |
| @@ -26,19 +24,17 @@ | ||
| 26 | 24 | private function set_db_key( $source ) { |
| 27 | 25 | $this->db_key = 'rtsb_' . $source; |
| 28 | 26 | } |
| 29 | 27 | |
| 30 | - public function get_option( $key, $default = null, $cache = true ) { | |
| 31 | - if ( $cache && isset( $GLOBALS[ $this->db_key ] ) ) { | |
| 32 | - $db = $GLOBALS[ $this->db_key ]; | |
| 33 | - } else { | |
| 34 | - $db = get_option( $this->db_key, [] ); | |
| 35 | - $GLOBALS[ $this->db_key ] = $db; | |
| 36 | - } | |
| 37 | - return $db[ $key ] ?? $default; | |
| 28 | + public function get_option( $key, $default = null ) { | |
| 29 | + | |
| 30 | + $db = get_option( $this->db_key, [] ); | |
| 31 | + | |
| 32 | + return isset( $db[ $key ] ) ? $db[ $key ] : $default; | |
| 38 | 33 | } |
| 39 | 34 | |
| 40 | 35 | public function set_option( $key, $value ) { |
| 36 | + | |
| 41 | 37 | $db = get_option( $this->db_key, [] ); |
| 42 | 38 | |
| 43 | 39 | if ( is_object( $db ) ) { |
| 44 | 40 | $db = (array) $db; |
| @@ -53,11 +49,14 @@ | ||
| 53 | 49 | return update_option( $this->db_key, $db ); |
| 54 | 50 | } |
| 55 | 51 | |
| 56 | 52 | public function delete_option( $key ) { |
| 53 | + | |
| 57 | 54 | $db = get_option( $this->db_key, [] ); |
| 55 | + | |
| 58 | 56 | if ( isset( $db[ $key ] ) ) { |
| 59 | 57 | unset( $db[ $key ] ); |
| 60 | 58 | } |
| 61 | - return update_option( $this->db_key, $db ); | |
| 59 | + | |
| 60 | + return true; | |
| 62 | 61 | } |
| 63 | -} | |
| 62 | +} | |