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