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