| @@ -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 | - } | |
| 14 | + public static function source( $source = 'settings' ) { | |
| 15 | + if ( ! self::$instance ) { | |
| 16 | + self::$instance = new self(); | |
| 17 | + } | |
| 17 | 18 | |
| 18 | - public static function source($source = 'settings') { | |
| 19 | - if (!isset(self::$instances[$source])) { | |
| 20 | - self::$instances[$source] = new self($source); | |
| 21 | - } | |
| 19 | + self::$instance->set_db_key( $source ); | |
| 22 | 20 | |
| 23 | - return self::$instances[$source]; | |
| 21 | + return self::$instance; | |
| 24 | 22 | } |
| 25 | 23 | |
| 26 | 24 | private function set_db_key( $source ) { |
| 27 | 25 | $this->db_key = 'rtsb_' . $source; |
| @@ -27,25 +25,16 @@ | ||
| 27 | 25 | $this->db_key = 'rtsb_' . $source; |
| 28 | 26 | } |
| 29 | 27 | |
| 30 | 28 | public function get_option( $key, $default = null ) { |
| 31 | - if( isset( $GLOBALS[$this->db_key] )){ | |
| 32 | - $db = $GLOBALS[$this->db_key]; | |
| 33 | - } else { | |
| 34 | - $GLOBALS[$this->db_key] = get_option( $this->db_key, [] ); | |
| 35 | - $db = $GLOBALS[$this->db_key]; | |
| 36 | - } | |
| 37 | 29 | |
| 38 | - return $db[ $key ] ?? $default; | |
| 30 | + $db = get_option( $this->db_key, [] ); | |
| 31 | + | |
| 32 | + return isset( $db[ $key ] ) ? $db[ $key ] : $default; | |
| 39 | 33 | } |
| 40 | 34 | |
| 41 | 35 | public function set_option( $key, $value ) { |
| 42 | - //if( isset( $GLOBALS[$this->db_key] ) ){ | |
| 43 | - // $db = $GLOBALS[$this->db_key]; | |
| 44 | - //} else { | |
| 45 | - // $db = get_option( $this->db_key, [] ); | |
| 46 | - // $GLOBALS[$this->db_key] = $db; | |
| 47 | - //} | |
| 36 | + | |
| 48 | 37 | $db = get_option( $this->db_key, [] ); |
| 49 | 38 | |
| 50 | 39 | if ( is_object( $db ) ) { |
| 51 | 40 | $db = (array) $db; |
| @@ -60,17 +49,14 @@ | ||
| 60 | 49 | return update_option( $this->db_key, $db ); |
| 61 | 50 | } |
| 62 | 51 | |
| 63 | 52 | public function delete_option( $key ) { |
| 64 | - //if( isset( $GLOBALS[$this->db_key] ) ){ | |
| 65 | - // $db = $GLOBALS[$this->db_key]; | |
| 66 | - //} else { | |
| 67 | - // $db = get_option( $this->db_key, [] ); | |
| 68 | - // $GLOBALS[$this->db_key] = $db; | |
| 69 | - //} | |
| 53 | + | |
| 70 | 54 | $db = get_option( $this->db_key, [] ); |
| 55 | + | |
| 71 | 56 | if ( isset( $db[ $key ] ) ) { |
| 72 | 57 | unset( $db[ $key ] ); |
| 73 | 58 | } |
| 74 | - return update_option( $this->db_key, $db ); | |
| 59 | + | |
| 60 | + return true; | |
| 75 | 61 | } |
| 76 | 62 | } |