| @@ -1,129 +1,62 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * DataModel class file. | |
| 4 | - * | |
| 5 | - * Provides a simple option storage abstraction with | |
| 6 | - * source-based singleton instances and optional caching. | |
| 7 | - * | |
| 8 | - * @package RadiusTheme\SB\Models | |
| 9 | - */ | |
| 10 | - | |
| 11 | -namespace RadiusTheme\SB\Models; | |
| 12 | - | |
| 13 | -// Do not allow directly accessing this file. | |
| 14 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | - exit( 'This script cannot be accessed directly.' ); | |
| 16 | -} | |
| 17 | - | |
| 18 | -/** | |
| 19 | - * Class DataModel | |
| 20 | - * | |
| 21 | - * Handles grouped WordPress options using a prefixed database key. | |
| 22 | - * Supports multiple data sources via singleton instances. | |
| 23 | - */ | |
| 24 | -class DataModel { | |
| 25 | - | |
| 26 | - /** | |
| 27 | - * Option key used in the WordPress options table. | |
| 28 | - * | |
| 29 | - * @var string | |
| 30 | - */ | |
| 31 | - private $db_key; | |
| 32 | - | |
| 33 | - /** | |
| 34 | - * Cached instances per data source. | |
| 35 | - * | |
| 36 | - * @var array<string, self> | |
| 37 | - */ | |
| 38 | - private static $instances = []; | |
| 39 | - | |
| 40 | - /** | |
| 41 | - * DataModel constructor. | |
| 42 | - * | |
| 43 | - * Private to enforce singleton usage via source(). | |
| 44 | - * | |
| 45 | - * @param string $source Data source identifier. | |
| 46 | - */ | |
| 47 | - private function __construct( $source ) { | |
| 48 | - $this->set_db_key( $source ); | |
| 49 | - } | |
| 50 | - | |
| 51 | - /** | |
| 52 | - * Get DataModel instance for a specific source. | |
| 53 | - * | |
| 54 | - * @param string $source Data source name. | |
| 55 | - * @return self | |
| 56 | - */ | |
| 57 | - public static function source( $source = 'settings' ) { | |
| 58 | - if ( ! isset( self::$instances[ $source ] ) ) { | |
| 59 | - self::$instances[ $source ] = new self( $source ); | |
| 60 | - } | |
| 61 | - | |
| 62 | - return self::$instances[ $source ]; | |
| 63 | - } | |
| 64 | - | |
| 65 | - /** | |
| 66 | - * Build and set the database option key. | |
| 67 | - * | |
| 68 | - * @param string $source Data source identifier. | |
| 69 | - * @return void | |
| 70 | - */ | |
| 71 | - private function set_db_key( $source ) { | |
| 72 | - $this->db_key = 'rtsb_' . $source; | |
| 73 | - } | |
| 74 | - | |
| 75 | - /** | |
| 76 | - * Retrieve an option value by key. | |
| 77 | - * | |
| 78 | - * @param string $key Option key. | |
| 79 | - * @param mixed $default Default value if option does not exist. | |
| 80 | - * @param bool $cache Whether to use global cache. | |
| 81 | - * @return mixed | |
| 82 | - */ | |
| 83 | - public function get_option( $key, $default = null, $cache = true ) { | |
| 84 | - if ( $cache && isset( $GLOBALS[ $this->db_key ] ) ) { | |
| 85 | - $db = $GLOBALS[ $this->db_key ]; | |
| 86 | - } else { | |
| 87 | - $db = get_option( $this->db_key, [] ); | |
| 88 | - $GLOBALS[ $this->db_key ] = $db; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound | |
| 89 | - } | |
| 90 | - return $db[ $key ] ?? $default; | |
| 91 | - } | |
| 92 | - | |
| 93 | - /** | |
| 94 | - * Update or create an option value. | |
| 95 | - * | |
| 96 | - * @param string $key Option key. | |
| 97 | - * @param mixed $value Option value. | |
| 98 | - * @return bool True on success, false on failure. | |
| 99 | - */ | |
| 100 | - public function set_option( $key, $value ) { | |
| 101 | - $db = get_option( $this->db_key, [] ); | |
| 102 | - | |
| 103 | - if ( is_object( $db ) ) { | |
| 104 | - $db = (array) $db; | |
| 105 | - } | |
| 106 | - | |
| 107 | - if ( ! is_array( $db ) ) { | |
| 108 | - $db = []; | |
| 109 | - } | |
| 110 | - | |
| 111 | - $db[ $key ] = $value; | |
| 112 | - | |
| 113 | - return update_option( $this->db_key, $db ); | |
| 114 | - } | |
| 115 | - | |
| 116 | - /** | |
| 117 | - * Delete an option value by key. | |
| 118 | - * | |
| 119 | - * @param string $key Option key. | |
| 120 | - * @return bool True on success, false on failure. | |
| 121 | - */ | |
| 122 | - public function delete_option( $key ) { | |
| 123 | - $db = get_option( $this->db_key, [] ); | |
| 124 | - if ( isset( $db[ $key ] ) ) { | |
| 125 | - unset( $db[ $key ] ); | |
| 126 | - } | |
| 127 | - return update_option( $this->db_key, $db ); | |
| 128 | - } | |
| 129 | -} | |
| 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 | +} | |