| 1 |
<?php |
| 2 |
|
| 3 |
namespace RadiusTheme\SB\Models; |
| 4 |
|
| 5 |
// Do not allow directly accessing this file. |
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit( 'This script cannot be accessed directly.' ); |
| 8 |
} |
| 9 |
|
| 10 |
class DataModel { |
| 11 |
private $db_key; |
| 12 |
private static $instances = []; |
| 13 |
|
| 14 |
private function __construct($source) { |
| 15 |
$this->set_db_key($source); |
| 16 |
} |
| 17 |
|
| 18 |
public static function source($source = 'settings') { |
| 19 |
if (!isset(self::$instances[$source])) { |
| 20 |
self::$instances[$source] = new self($source); |
| 21 |
} |
| 22 |
|
| 23 |
return self::$instances[$source]; |
| 24 |
} |
| 25 |
|
| 26 |
private function set_db_key( $source ) { |
| 27 |
$this->db_key = 'rtsb_' . $source; |
| 28 |
} |
| 29 |
|
| 30 |
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 |
|
| 38 |
return $db[ $key ] ?? $default; |
| 39 |
} |
| 40 |
|
| 41 |
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 |
//} |
| 48 |
$db = get_option( $this->db_key, [] ); |
| 49 |
|
| 50 |
if ( is_object( $db ) ) { |
| 51 |
$db = (array) $db; |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! is_array( $db ) ) { |
| 55 |
$db = []; |
| 56 |
} |
| 57 |
|
| 58 |
$db[ $key ] = $value; |
| 59 |
|
| 60 |
return update_option( $this->db_key, $db ); |
| 61 |
} |
| 62 |
|
| 63 |
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 |
//} |
| 70 |
$db = get_option( $this->db_key, [] ); |
| 71 |
if ( isset( $db[ $key ] ) ) { |
| 72 |
unset( $db[ $key ] ); |
| 73 |
} |
| 74 |
return update_option( $this->db_key, $db ); |
| 75 |
} |
| 76 |
} |